diff --git a/src/utils/Fetcher.test.ts b/src/utils/Fetcher.test.ts index 82bc593..80c7a77 100644 --- a/src/utils/Fetcher.test.ts +++ b/src/utils/Fetcher.test.ts @@ -31,8 +31,12 @@ describe('fetch', () => { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en', 'Authorization': 'Basic dXNlcjpwYXNz', + 'Forwarded': 'for=0.0.0.0', 'Priority': 'u=0', 'User-Agent': 'node', + 'X-Forwarded-For': '0.0.0.0', + 'X-Forwarded-Proto': 'https', + 'X-Real-IP': '0.0.0.0', }); }); diff --git a/src/utils/Fetcher.ts b/src/utils/Fetcher.ts index 3b6c150..f5abaa7 100644 --- a/src/utils/Fetcher.ts +++ b/src/utils/Fetcher.ts @@ -47,6 +47,7 @@ export type CustomRequestInit = RequestInit & { timeoutsCountThrow?: number; noCache?: boolean; noFlareSolverr?: boolean; + noProxyHeaders?: boolean; queueLimit?: number; queueTimeout?: number; timeout?: number; @@ -87,8 +88,9 @@ export class Fetcher { return (await this.cachedFetch(ctx, url, { ...init, method: 'HEAD' })).headers; }; - private getInit(url: URL, init?: CustomRequestInit): CustomRequestInit { + private getInit(ctx: Context, url: URL, init?: CustomRequestInit): CustomRequestInit { const cookieString = this.cookieJar.getCookieStringSync(url.href); + const noProxyHeaders = init?.noProxyHeaders ?? false; return { ...init, @@ -99,6 +101,12 @@ export class Fetcher { 'Priority': 'u=0', 'User-Agent': this.hostUserAgentMap.get(url.host) ?? 'node', ...(cookieString && { Cookie: cookieString }), + ...(ctx.ip && !noProxyHeaders && { + 'Forwarded': `for=${ctx.ip}`, + 'X-Forwarded-For': ctx.ip, + 'X-Forwarded-Proto': url.protocol.slice(0, -1), + 'X-Real-IP': ctx.ip, + }), ...init?.headers, }, }; @@ -215,7 +223,7 @@ export class Fetcher { }; private async cachedFetch(ctx: Context, url: URL, init?: CustomRequestInit): Promise { - const newInit = this.getInit(url, init); + const newInit = this.getInit(ctx, url, init); const request: CachePolicy.Request = { url: url.href, method: newInit.method ?? 'GET', headers: {} };