import { Dispatcher, ProxyAgent } from 'undici'; import { socksDispatcher } from 'fetch-socks'; import CachePolicy from 'http-cache-semantics'; import TTLCache from '@isaacs/ttlcache'; import winston from 'winston'; import { Mutex, Semaphore, SemaphoreInterface, withTimeout } from 'async-mutex'; import { Cookie, CookieJar } from 'tough-cookie'; import { BlockedReason, Context } from '../types'; import { BlockedError, HttpError, NotFoundError, QueueIsFullError, TimeoutError, TooManyTimeoutsError, TooManyRequestsError } from '../error'; import { clearTimeout } from 'node:timers'; import { envGet } from './env'; interface HttpCacheItem { policy: CachePolicy; status: number; statusText: string; body: string; } interface FlareSolverrResult { status: string; message: string; solution: { url: string; status: number; cookies: { domain: string; expires: number; httpOnly: boolean; name: string; path: string; sameSite: string; secure: boolean; value: string; }[]; userAgent: string; headers: Record; response: string; }; startTimeStamp: number; endTimeStamp: number; version: string; } export type CustomRequestInit = RequestInit & { timeoutsCountThrow?: number; noFlareSolverr?: boolean; queueLimit?: number; queueTimeout?: number; timeout?: number; }; export class Fetcher { private readonly MIN_CACHE_TTL = 900000; // 15m private readonly DEFAULT_TIMEOUT = 10000; private readonly DEFAULT_QUEUE_LIMIT = 5; private readonly DEFAULT_QUEUE_TIMEOUT = 5000; private readonly DEFAULT_FAILED_REQUEST_COUNT_THROW = 10; private readonly TIMEOUT_CACHE_TTL = 3600000; // 1h private readonly logger: winston.Logger; private readonly dispatcher: Dispatcher | undefined; private readonly httpCache = new TTLCache(); private readonly rateLimitedCache = new TTLCache(); private readonly semaphores = new Map(); private readonly hostUserAgentMap = new Map(); private readonly cookieJar = new CookieJar(); private readonly timeoutsCountCache = new TTLCache(); private readonly timeoutsCountMutex = new Mutex(); public constructor(logger: winston.Logger) { this.logger = logger; if (process.env['ALL_PROXY']) { const proxyUrl = new URL(process.env['ALL_PROXY']); if (proxyUrl.protocol === 'socks5:') { this.dispatcher = socksDispatcher({ type: 5, host: proxyUrl.hostname, port: parseInt(proxyUrl.port) }); } else { this.dispatcher = new ProxyAgent(proxyUrl.href); } } } public async text(ctx: Context, url: URL, init?: CustomRequestInit): Promise { return (await this.cachedFetch(ctx, url, init)).body; }; public async textPost(ctx: Context, url: URL, body: string, init?: CustomRequestInit): Promise { return (await this.cachedFetch(ctx, url, { ...init, method: 'POST', body })).body; }; public async head(ctx: Context, url: URL, init?: CustomRequestInit): Promise { return (await this.cachedFetch(ctx, url, { ...init, method: 'HEAD' })).policy.responseHeaders(); }; private getInit(ctx: Context, url: URL, init?: CustomRequestInit): CustomRequestInit { const cookieString = this.cookieJar.getCookieStringSync(url.href); return { ...init, headers: { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en', 'Priority': 'u=0', 'User-Agent': this.hostUserAgentMap.get(url.host) ?? 'node', ...(cookieString && { Cookie: cookieString }), ...(ctx.ip && { 'Forwarded': `for=${ctx.ip}`, 'X-Forwarded-For': ctx.ip, 'X-Forwarded-Proto': url.protocol.slice(0, -1), 'X-Real-IP': ctx.ip, }), ...init?.headers, }, }; }; private async handleHttpCacheItem(ctx: Context, httpCacheItem: HttpCacheItem, url: URL, init?: CustomRequestInit): Promise { if (httpCacheItem.status && httpCacheItem.status >= 200 && httpCacheItem.status <= 299) { return httpCacheItem; } if (httpCacheItem.status === 404) { throw new NotFoundError(); } const responseHeaders = httpCacheItem.policy.responseHeaders(); if (httpCacheItem.policy.responseHeaders()['cf-mitigated'] === 'challenge') { const noFlareSolverr = init?.noFlareSolverr ?? false; const flareSolverrEndpoint = envGet('FLARESOLVERR_ENDPOINT'); if (noFlareSolverr || !flareSolverrEndpoint) { throw new BlockedError(BlockedReason.cloudflare_challenge, httpCacheItem.policy.responseHeaders()); } this.logger.info(`Query FlareSolverr for ${url.href}`, ctx); const body = { cmd: 'request.get', url: url.href, session: 'default' }; const challengeResult = await (await this.queuedFetch(ctx, new URL(flareSolverrEndpoint), { method: 'POST', body: JSON.stringify(body), headers: { 'Content-Type': 'application/json' }, queueLimit: 1, timeout: 15000 })).json() as FlareSolverrResult; if (challengeResult.status !== 'ok') { this.logger.warn(`FlareSolverr issue: ${JSON.stringify(challengeResult)}`, ctx); throw new BlockedError(BlockedReason.flaresolverr_failed, {}); } challengeResult.solution.cookies.forEach((cookie) => { if (!['cf_clearance'].includes(cookie.name)) { return; } this.cookieJar.setCookie( new Cookie({ key: cookie.name, value: cookie.value, expires: new Date(cookie.expires * 1000), domain: cookie.domain.replace(/^.+/, ''), }), url.href, ); }); this.hostUserAgentMap.set(url.host, challengeResult.solution.userAgent); httpCacheItem.body = challengeResult.solution.response; const ttl = this.determineCacheTtl(httpCacheItem); /* istanbul ignore next */ if (ttl > 0) { this.httpCache.set(this.determineCacheKey(url, init), httpCacheItem, { ttl }); } return httpCacheItem; } if (httpCacheItem.status === 403) { throw new BlockedError(BlockedReason.unknown, httpCacheItem.policy.responseHeaders()); } if (httpCacheItem.status === 429) { const retryAfter = parseInt(`${httpCacheItem.policy.responseHeaders()['retry-after']}`); if (!isNaN(retryAfter)) { this.rateLimitedCache.set(url.host, undefined, { ttl: retryAfter * 1000 }); } throw new TooManyRequestsError(retryAfter); } throw new HttpError(httpCacheItem.status, httpCacheItem.statusText, responseHeaders); }; private determineCacheKey(url: URL, init?: CustomRequestInit): string { return `${url.href}_${init?.body?.toString()}`; } private determineCacheTtl(httpCacheItem: HttpCacheItem): number { if (httpCacheItem.status === 200) { return Math.max(httpCacheItem.policy.timeToLive(), this.MIN_CACHE_TTL); } return httpCacheItem.policy.timeToLive(); }; private headersToObject(headers: Headers): Record { const obj: Record = {}; headers.forEach((value, name) => { obj[name] = value; }); return obj; }; private async cachedFetch(ctx: Context, url: URL, init?: CustomRequestInit): Promise { const newInit = this.getInit(ctx, url, init); const request: CachePolicy.Request = { url: url.href, method: newInit.method ?? 'GET', headers: {} }; const cacheKey = this.determineCacheKey(url, init); let httpCacheItem: HttpCacheItem | undefined = this.httpCache.get(cacheKey); if (httpCacheItem) { this.logger.info(`Cached fetch ${request.method} ${url}`, ctx); return this.handleHttpCacheItem(ctx, httpCacheItem, url, init); } const response = await this.queuedFetch(ctx, url, newInit); const body = await response.text(); const policy = new CachePolicy(request, { status: response.status, headers: this.headersToObject(response.headers) }, { shared: false }); httpCacheItem = { policy, status: response.status, statusText: response.statusText, body }; const ttl = this.determineCacheTtl(httpCacheItem); if (ttl > 0) { this.httpCache.set(cacheKey, httpCacheItem, { ttl }); } return this.handleHttpCacheItem(ctx, httpCacheItem, url, init); }; protected async fetchWithTimeout(ctx: Context, url: URL, init?: CustomRequestInit): Promise { this.logger.info(`Fetch ${init?.method ?? 'GET'} ${url}`, ctx); if (this.rateLimitedCache.has(url.host)) { throw new TooManyRequestsError(this.rateLimitedCache.getRemainingTTL(url.host) / 1000); } if ((this.timeoutsCountCache.get(url.host) ?? 0) >= (init?.timeoutsCountThrow ?? this.DEFAULT_FAILED_REQUEST_COUNT_THROW)) { throw new TooManyTimeoutsError(); } const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), init?.timeout ?? this.DEFAULT_TIMEOUT); let response; try { response = await fetch(url, { ...init, dispatcher: this.dispatcher, keepalive: true, signal: controller.signal } as RequestInit); } catch (error) { if (error instanceof DOMException && error.name === 'AbortError') { await this.increaseTimeoutsCount(url); throw new TimeoutError(); } throw error; } finally { clearTimeout(timer); } await this.decreaseTimeoutsCount(url); return response; }; private async increaseTimeoutsCount(url: URL) { await this.timeoutsCountMutex.runExclusive(async () => { const count = (this.timeoutsCountCache.get(url.host) ?? 0) + 1; this.timeoutsCountCache.set(url.host, count, { ttl: this.TIMEOUT_CACHE_TTL }); }); } private async decreaseTimeoutsCount(url: URL) { await this.timeoutsCountMutex.runExclusive(async () => { const count = Math.max(0, (this.timeoutsCountCache.get(url.host) ?? 0) - 1); this.timeoutsCountCache.set(url.host, count, { ttl: this.TIMEOUT_CACHE_TTL }); }); } private getSemaphore(url: URL, queueLimit: number, queueTimeout: number): SemaphoreInterface { let sem = this.semaphores.get(url.host); if (!sem) { sem = withTimeout(new Semaphore(queueLimit), queueTimeout, new QueueIsFullError()); this.semaphores.set(url.host, sem); } return sem; } private async queuedFetch(ctx: Context, url: URL, init?: CustomRequestInit): Promise { const queueLimit = init?.queueLimit ?? this.DEFAULT_QUEUE_LIMIT; const queueTimeout = init?.queueTimeout ?? this.DEFAULT_QUEUE_TIMEOUT; const semaphore = this.getSemaphore(url, queueLimit, queueTimeout); const [,release] = await semaphore.acquire(); try { return await this.fetchWithTimeout(ctx, url, init); } finally { release(); } } }