From 30ac5c76bee3e55a3e095b59b30866081bf86152 Mon Sep 17 00:00:00 2001 From: JuLuogo <1576586736@qq.com> Date: Wed, 8 Oct 2025 16:51:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(proxy):=20=E6=B7=BB=E5=8A=A0=E4=BB=A3?= =?UTF-8?q?=E7=90=86IP=E6=94=AF=E6=8C=81=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=A4=B4=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在wrangler.toml中新增PROXY_IP配置项用于代理IP设置 - 重构[[path]].ts中的请求处理逻辑,支持通过代理IP转发请求 - 优化请求头处理,仅保留必要头部并添加浏览器模拟头 --- functions/[[path]].ts | 43 +++++++++++++++++++++++++++++++++---------- wrangler.toml | 4 +++- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/functions/[[path]].ts b/functions/[[path]].ts index a5a1b5b..81ba0d8 100644 --- a/functions/[[path]].ts +++ b/functions/[[path]].ts @@ -261,20 +261,43 @@ async function handleGenericProxy(request: Request, env: any) { const url = new URL(request.url) url.hostname = 'www.pixiv.net' - const headers = new Headers(request.headers) + const headers = new Headers() + + // 复制原始请求中的重要头部(如 Cookie) + const importantHeaders = ['cookie', 'authorization', 'x-csrf-token'] + importantHeaders.forEach(headerName => { + const value = request.headers.get(headerName) + if (value) { + headers.set(headerName, value) + } + }) + + // 设置必要的请求头来模拟真实浏览器访问 + headers.set('host', 'www.pixiv.net') headers.set('origin', 'https://www.pixiv.net') headers.set('referer', 'https://www.pixiv.net/') - // 使用默认的 User-Agent,或者环境变量中的自定义 User-Agent headers.set('user-agent', env.USER_AGENT || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0') + headers.set('accept-language', 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6') - const newReq = new Request(url.toString(), { - method: request.method, - headers, - body: request.body, - }) - - const response = await fetch(newReq) - return corsResponse(response) + // 如果配置了代理IP,则通过代理发送请求 + if (env.PROXY_IP) { + const proxyUrl = `https://${env.PROXY_IP}/${url.toString()}` + const newReq = new Request(proxyUrl, { + method: request.method, + headers, + body: request.body, + }) + const response = await fetch(newReq) + return corsResponse(response) + } else { + const newReq = new Request(url.toString(), { + method: request.method, + headers, + body: request.body, + }) + const response = await fetch(newReq) + return corsResponse(response) + } } // 图片代理处理器 diff --git a/wrangler.toml b/wrangler.toml index a11b537..7799596 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -12,7 +12,9 @@ bucket = "./dist" # User-Agent 黑名单(JSON 数组格式) UA_BLACKLIST = "[]" # 自定义 User-Agent(可选,有默认值) -# USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" +USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0" +# 代理IP配置(可选,用于绕过Pixiv WAF) +PROXY_IP = "ProxyIP.US.CMLiussss.net" # 自定义图片代理 URL(可选,默认使用 Pixiv 原始 URL) # 示例配置: # VITE_PXIMG_BASEURL_I = "https://i.pixiv.re/"