Skip to content

Commit

Permalink
Merge pull request #13 from khorezm0/proxy-debug (Proxy Support)
Browse files Browse the repository at this point in the history
push proxy update
  • Loading branch information
NoahCardoza authored Jul 31, 2020
2 parents 3805354 + b429ba1 commit e930fc4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
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

0 comments on commit e930fc4

Please sign in to comment.