feat: Add https-proxy-agent dependency and conditionally use it for fetch requests in the proxy route.

This commit is contained in:
kuekhaoyang
2025-11-22 21:15:49 +08:00
parent 5655d1032b
commit 1fdeffe57d
3 changed files with 31 additions and 2 deletions
+7
View File
@@ -1,4 +1,5 @@
import { NextRequest, NextResponse } from 'next/server';
import { HttpsProxyAgent } from 'https-proxy-agent';
export async function GET(request: NextRequest) {
const url = request.nextUrl.searchParams.get('url');
@@ -11,6 +12,10 @@ export async function GET(request: NextRequest) {
// Beijing IP address to simulate request from China
const chinaIP = '202.108.22.5';
// Configure proxy agent if environment variable is set
const proxyUrl = process.env.CHINA_PROXY_URL;
const agent = proxyUrl ? new HttpsProxyAgent(proxyUrl) : undefined;
const response = await fetch(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
@@ -18,6 +23,8 @@ export async function GET(request: NextRequest) {
'Client-IP': chinaIP,
'Referer': new URL(url).origin,
},
// @ts-ignore - node-fetch types don't perfectly match Next.js fetch extension
agent: agent,
});
const contentType = response.headers.get('Content-Type');
+23 -2
View File
@@ -9,6 +9,7 @@
"version": "0.1.0",
"dependencies": {
"@vercel/analytics": "^1.5.0",
"https-proxy-agent": "^7.0.6",
"next": "16.0.3",
"react": "19.2.0",
"react-dom": "19.2.0",
@@ -2209,6 +2210,15 @@
"acorn": "^6.0.0 || ^7.0.0 || ^8.0.0"
}
},
"node_modules/agent-base": {
"version": "7.1.4",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.4.tgz",
"integrity": "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ==",
"license": "MIT",
"engines": {
"node": ">= 14"
}
},
"node_modules/ajv": {
"version": "6.12.6",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
@@ -2771,7 +2781,6 @@
"version": "4.4.3",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
"integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==",
"dev": true,
"license": "MIT",
"dependencies": {
"ms": "^2.1.3"
@@ -3971,6 +3980,19 @@
"hermes-estree": "0.25.1"
}
},
"node_modules/https-proxy-agent": {
"version": "7.0.6",
"resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz",
"integrity": "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==",
"license": "MIT",
"dependencies": {
"agent-base": "^7.1.2",
"debug": "4"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/ignore": {
"version": "5.3.2",
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz",
@@ -4985,7 +5007,6 @@
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"dev": true,
"license": "MIT"
},
"node_modules/nanoid": {
+1
View File
@@ -10,6 +10,7 @@
},
"dependencies": {
"@vercel/analytics": "^1.5.0",
"https-proxy-agent": "^7.0.6",
"next": "16.0.3",
"react": "19.2.0",
"react-dom": "19.2.0",