Skip to content

Commit

Permalink
小优化
Browse files Browse the repository at this point in the history
  • Loading branch information
jianjianai committed Jun 18, 2024
1 parent f3779ba commit 641bff5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
28 changes: 14 additions & 14 deletions src/proxy/bingPorxyWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,17 @@ const bingProxyLink = newProxyLinkHttp<Env>({
return null;
},
async reqTranslator(config, req, env) {//修改请求
const url = new URL(config.url);
config.url = url;
{ //基础转换
url.port = ""
url.protocol = 'https:';
config.url.port = ""
config.url.protocol = 'https:';
config.init.headers = new Headers(config.init.headers);
}

{//重定向请求
const p = url.pathname;
const p = config.url.pathname;
//sydney的请求
if (p.startsWith("/sydney/")) {
url.hostname = "sydney.bing.com";//设置链接
config.url.hostname = "sydney.bing.com";//设置链接
}
//copilot的请求
if (
Expand All @@ -116,11 +114,11 @@ const bingProxyLink = newProxyLinkHttp<Env>({
p.startsWith("/pwa/") ||
p.startsWith("/videos/")
) {
url.hostname = "copilot.microsoft.com"
config.url.hostname = "copilot.microsoft.com"
}
// bing请求
if (p.startsWith("/opaluqu/")) {
url.hostname = "www.bing.com"
config.url.hostname = "www.bing.com"
}
// login请求
if (
Expand All @@ -132,23 +130,23 @@ const bingProxyLink = newProxyLinkHttp<Env>({
p == "/GetExperimentAssignments.srf" ||
p == "/logout.srf"
) {
url.hostname = "login.live.com"
config.url.hostname = "login.live.com"
}
// login account请求
if(
p.startsWith("/proofs/") ||
p=="/SummaryPage.aspx"
){
url.hostname = "account.live.com"
config.url.hostname = "account.live.com"
}
//storage请求
if (p.startsWith("/users/") ) {
url.hostname = "storage.live.com"
config.url.hostname = "storage.live.com"
}
//bing验证请求
if(p=='/turing/captcha/challenge'){
url.hostname = "www.bing.com";
url.searchParams.delete('h');
config.url.hostname = "www.bing.com";
config.url.searchParams.delete('h');
}
}

Expand All @@ -161,7 +159,7 @@ const bingProxyLink = newProxyLinkHttp<Env>({
const resHeaders = config.init.headers as Headers;
const origin = resHeaders.get('Origin');
if (origin) {
const url = config.url as URL;
const url = config.url;
const originUrl = new URL(origin);
originUrl.protocol = "https:";
originUrl.port = '';
Expand Down Expand Up @@ -200,6 +198,7 @@ const bingProxyLink = newProxyLinkHttp<Env>({
}

{//修改登录请求 /secure/Passport.aspx
const url = config.url;
const p = url.pathname;
const porxyOrigin = new URL(req.url).origin;
if (p == "/secure/Passport.aspx" || p == "/passport.aspx") {
Expand Down Expand Up @@ -244,6 +243,7 @@ const bingProxyLink = newProxyLinkHttp<Env>({
}

{//不同域重定向转换
const url = config.url;
if (url.searchParams.has("cprt")) { //cprt -> hostname
url.hostname = url.searchParams.get("cprt") as string;
url.searchParams.delete("cprt");
Expand Down
9 changes: 5 additions & 4 deletions src/proxy/proxyLinkHttp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type ReqHttpConfig = { url: string | URL, init: RequestInit };
export type ReqHttpConfig = { url: URL, init: RequestInit };
export type ReqHttpTranslator<ENV> = (config: ReqHttpConfig, req: Request,env:ENV) => Promise<ReqHttpConfig>;
export type ResHttpConfig = { body: BodyInit | null, init: ResponseInit };
export type ReshttpTranslator<ENV> = (config: ResHttpConfig, res: Response , req: Request,env:ENV) => Promise<ResHttpConfig>;
Expand Down Expand Up @@ -26,16 +26,17 @@ export function newProxyLinkHttp<ENV>({intercept,reqTranslator,resTranslator}:Ne
return interceptRes;
}
const reqConfig = await reqTranslator({
url: req.url,
url: new URL(req.url),
init: {
method: req.method,
headers: req.headers,
body: req.body,
redirect: "manual"
}
}, req,env);
if (req.url === reqConfig.url) {
return new Response("not curl", { status: 400 });

if (reqConfig.url.hostname == new URL(req.url).hostname) {
return new Response("Same hostname", { status: 404 });
}
const res = await fetch(reqConfig.url, reqConfig.init);
const resConfig = await resTranslator({
Expand Down

0 comments on commit 641bff5

Please sign in to comment.