Skip to content

Commit 7a9dd78

Browse files
committed
fix: 优化反代请求头处理,增强响应内容类型检查
1 parent 0a258d8 commit 7a9dd78

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

_worker.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,17 @@ export default {
348348
try {
349349
const 反代URL = new URL(伪装页URL), 新请求头 = new Headers(request.headers);
350350
新请求头.set('Host', 反代URL.host);
351-
if (新请求头.has('Referer')) { const u = new URL(新请求头.get('Referer')); 新请求头.set('Referer', 反代URL.protocol + '//' + 反代URL.host + u.pathname + u.search); }
352-
if (新请求头.has('Origin')) 新请求头.set('Origin', 反代URL.protocol + '//' + 反代URL.host);
351+
新请求头.set('Referer', 反代URL.origin);
352+
新请求头.set('Origin', 反代URL.origin);
353353
if (!新请求头.has('User-Agent') && UA && UA !== 'null') 新请求头.set('User-Agent', UA);
354-
return fetch(new Request(反代URL.protocol + 反代URL.host + url.pathname + url.search, { method: request.method, headers: 新请求头, body: request.body, cf: request.cf }));
354+
const 反代响应 = await fetch(反代URL.origin + url.pathname + url.search, { method: request.method, headers: 新请求头, body: request.body, cf: request.cf });
355+
const 内容类型 = 反代响应.headers.get('content-type') || '';
356+
// 只处理文本类型的响应
357+
if (/text|javascript|json|xml/.test(内容类型)) {
358+
const 响应内容 = (await 反代响应.text()).replaceAll(反代URL.host, url.host);
359+
return new Response(响应内容, { status: 反代响应.status, headers: { ...Object.fromEntries(反代响应.headers), 'Cache-Control': 'no-store' } });
360+
}
361+
return 反代响应;
355362
} catch (error) { }
356363
return new Response(await nginx(), { status: 200, headers: { 'Content-Type': 'text/html; charset=UTF-8' } });
357364
}

0 commit comments

Comments
 (0)