Skip to content

push proxy update #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface SessionsCreateAPICall extends BaseSessionsAPICall {
cookies?: SetCookie[],
headers?: Headers
maxTimeout?: number
proxy?: any
}

interface BaseRequestAPICall extends BaseAPICall {
Expand All @@ -28,6 +29,7 @@ interface BaseRequestAPICall extends BaseAPICall {
maxTimeout?: number
cookies?: SetCookie[],
headers?: Headers
proxy? : any
}


Expand All @@ -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
*/

Expand All @@ -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<ChallenegeResolutionT | void> {
async function resolveChallenge(ctx: RequestContext, { url, maxTimeout, proxy }: BaseRequestAPICall, page: Page): Promise<ChallenegeResolutionT | void> {

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' })

Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface SessionCreateOptions {
cookies?: SetCookie[]
headers?: Headers,
maxTimeout?: number
proxy? : any
}

const sessionCache: SessionsCache = {}
Expand All @@ -53,8 +54,13 @@ function prepareBrowserProfile(id: string): string {
}

export default {
create: async (id: string, { cookies, oneTimeSession, userAgent, headers, maxTimeout }: SessionCreateOptions): Promise<SessionsCacheItem> => {
const puppeteerOptions: LaunchOptions = {
create: async (id: string, { cookies, oneTimeSession, userAgent, headers, maxTimeout, proxy }: SessionCreateOptions): Promise<SessionsCacheItem> => {
let args = ["--no-sandbox"];
if(proxy && proxy.url){
args.push(`--proxy-server=${proxy.url}`);
}

const puppeteerOptions: LaunchOptions = {
product: 'chrome',
headless: true
}
Expand Down