diff --git a/src/routes.ts b/src/routes.ts index 41857a5..a4ece5d 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -19,6 +19,7 @@ interface SessionsCreateAPICall extends BaseSessionsAPICall { cookies?: SetCookie[], headers?: Headers maxTimeout?: number + proxy?: any } interface BaseRequestAPICall extends BaseAPICall { @@ -28,6 +29,7 @@ interface BaseRequestAPICall extends BaseAPICall { maxTimeout?: number cookies?: SetCookie[], headers?: Headers + proxy? : any } @@ -50,8 +52,8 @@ interface ChallenegeResolutionT { const addHeaders = (headers: Headers) => { - /* - added `once` flag since using removeListener causes + /* + added `once` flag since using removeListener causes page next page load to hang for some reason */ @@ -74,11 +76,17 @@ const addHeaders = (headers: Headers) => { const CHALLENGE_SELECTORS = ['.ray_id', '.attack-box'] const TOKEN_INPUT_NAMES = ['g-recaptcha-response', 'h-captcha-response'] -async function resolveChallenge(ctx: RequestContext, { url, maxTimeout }: BaseRequestAPICall, page: Page): Promise { +async function resolveChallenge(ctx: RequestContext, { url, maxTimeout, proxy }: BaseRequestAPICall, page: Page): Promise { maxTimeout = maxTimeout || 60000 let message = '' + if(proxy){ + log.debug("Apply proxy"); + if(proxy.username) + await page.authenticate({ username:proxy.username, password: proxy.password }); + } + log.debug(`Navegating to... ${url}`) const response = await page.goto(url, { waitUntil: 'domcontentloaded' }) @@ -209,7 +217,7 @@ async function resolveChallenge(ctx: RequestContext, { url, maxTimeout }: BaseRe function mergeSessionWithParams({ defaults }: SessionsCacheItem, params: BaseRequestAPICall): BaseRequestAPICall { const copy = { ...defaults, ...params } - // custom merging logic + // custom merging logic copy.headers = { ...defaults.headers || {}, ...params.headers || {} } || null return copy diff --git a/src/session.ts b/src/session.ts index 3d200ab..9b65dc9 100644 --- a/src/session.ts +++ b/src/session.ts @@ -29,6 +29,7 @@ interface SessionCreateOptions { cookies?: SetCookie[] headers?: Headers, maxTimeout?: number + proxy? : any } const sessionCache: SessionsCache = {} @@ -53,8 +54,13 @@ function prepareBrowserProfile(id: string): string { } export default { - create: async (id: string, { cookies, oneTimeSession, userAgent, headers, maxTimeout }: SessionCreateOptions): Promise => { - const puppeteerOptions: LaunchOptions = { + create: async (id: string, { cookies, oneTimeSession, userAgent, headers, maxTimeout, proxy }: SessionCreateOptions): Promise => { + let args = ["--no-sandbox"]; + if(proxy && proxy.url){ + args.push(`--proxy-server=${proxy.url}`); + } + + const puppeteerOptions: LaunchOptions = { product: 'chrome', headless: true }