feat(proxy): 添加代理IP支持并优化请求头处理

- 在wrangler.toml中新增PROXY_IP配置项用于代理IP设置
- 重构[[path]].ts中的请求处理逻辑,支持通过代理IP转发请求
- 优化请求头处理,仅保留必要头部并添加浏览器模拟头
This commit is contained in:
2025-10-08 16:51:01 +08:00
parent 6a401aea98
commit 30ac5c76be
2 changed files with 36 additions and 11 deletions

View File

@@ -261,20 +261,43 @@ async function handleGenericProxy(request: Request, env: any) {
const url = new URL(request.url) const url = new URL(request.url)
url.hostname = 'www.pixiv.net' 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('origin', 'https://www.pixiv.net')
headers.set('referer', '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('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(), { // 如果配置了代理IP则通过代理发送请求
method: request.method, if (env.PROXY_IP) {
headers, const proxyUrl = `https://${env.PROXY_IP}/${url.toString()}`
body: request.body, const newReq = new Request(proxyUrl, {
}) method: request.method,
headers,
const response = await fetch(newReq) body: request.body,
return corsResponse(response) })
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)
}
} }
// 图片代理处理器 // 图片代理处理器

View File

@@ -12,7 +12,9 @@ bucket = "./dist"
# User-Agent 黑名单JSON 数组格式) # User-Agent 黑名单JSON 数组格式)
UA_BLACKLIST = "[]" UA_BLACKLIST = "[]"
# 自定义 User-Agent可选有默认值 # 自定义 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 # 自定义图片代理 URL可选默认使用 Pixiv 原始 URL
# 示例配置: # 示例配置:
# VITE_PXIMG_BASEURL_I = "https://i.pixiv.re/" # VITE_PXIMG_BASEURL_I = "https://i.pixiv.re/"