mirror of
https://github.com/FlareSolverr/FlareSolverr.git
synced 2025-06-08 12:35:30 +00:00
Rewrite request timeout handling (maxTimeout) resolves #42
This commit is contained in:
parent
d2b680520d
commit
a23fa0983f
11
package-lock.json
generated
11
package-lock.json
generated
@ -74,6 +74,12 @@
|
|||||||
"defer-to-connect": "^2.0.0"
|
"defer-to-connect": "^2.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"@types/await-timeout": {
|
||||||
|
"version": "0.3.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/await-timeout/-/await-timeout-0.3.1.tgz",
|
||||||
|
"integrity": "sha512-H5PzROT4KuP7XQDua13Iw8did//OCKAZ/3TL15DjvMzDonrk4HvhH1+tLko96f2guU6XaD3AoqRa49ZOwbwNig==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"@types/cacheable-request": {
|
"@types/cacheable-request": {
|
||||||
"version": "6.0.1",
|
"version": "6.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/@types/cacheable-request/-/cacheable-request-6.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/@types/cacheable-request/-/cacheable-request-6.0.1.tgz",
|
||||||
@ -288,6 +294,11 @@
|
|||||||
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
||||||
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
|
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
|
||||||
},
|
},
|
||||||
|
"await-timeout": {
|
||||||
|
"version": "1.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/await-timeout/-/await-timeout-1.1.1.tgz",
|
||||||
|
"integrity": "sha512-gsDXAS6XVc4Jt+7S92MPX6Noq69bdeXUPEaXd8dk3+yVr629LTDLxNt4j1ycBbrU+AStK2PhKIyNIM+xzWMVOQ=="
|
||||||
|
},
|
||||||
"aws-sign2": {
|
"aws-sign2": {
|
||||||
"version": "0.7.0",
|
"version": "0.7.0",
|
||||||
"resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.7.0.tgz",
|
"resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.7.0.tgz",
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
"url": "https://github.com/ngosang/FlareSolverr"
|
"url": "https://github.com/ngosang/FlareSolverr"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"await-timeout": "^1.1.1",
|
||||||
"console-log-level": "^1.4.1",
|
"console-log-level": "^1.4.1",
|
||||||
"got": "^11.5.1",
|
"got": "^11.5.1",
|
||||||
"hcaptcha-solver": "^1.0.2",
|
"hcaptcha-solver": "^1.0.2",
|
||||||
@ -29,6 +30,7 @@
|
|||||||
"uuid": "^8.2.0"
|
"uuid": "^8.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/await-timeout": "^0.3.1",
|
||||||
"@types/node": "^14.0.23",
|
"@types/node": "^14.0.23",
|
||||||
"@types/puppeteer": "^3.0.1",
|
"@types/puppeteer": "^3.0.1",
|
||||||
"@types/uuid": "^8.0.0",
|
"@types/uuid": "^8.0.0",
|
||||||
|
@ -6,6 +6,7 @@ import { SetCookie, Request, Headers, HttpMethod, Overrides, Cookie } from 'pupp
|
|||||||
import { TimeoutError } from 'puppeteer/Errors'
|
import { TimeoutError } from 'puppeteer/Errors'
|
||||||
import getCaptchaSolver, { CaptchaType } from './captcha'
|
import getCaptchaSolver, { CaptchaType } from './captcha'
|
||||||
import * as Puppeteer from "puppeteer-extra/dist/puppeteer";
|
import * as Puppeteer from "puppeteer-extra/dist/puppeteer";
|
||||||
|
const Timeout = require('await-timeout');
|
||||||
|
|
||||||
export interface BaseAPICall {
|
export interface BaseAPICall {
|
||||||
cmd: string
|
cmd: string
|
||||||
@ -119,9 +120,22 @@ async function interceptResponse(page: Puppeteer.Page, callback: (payload: Chall
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolveChallenge(ctx: RequestContext, { url, maxTimeout, proxy, download, returnOnlyCookies }: BaseRequestAPICall, page: Puppeteer.Page): Promise<ChallengeResolutionT | void> {
|
async function resolveChallengeWithTimeout(ctx: RequestContext, params: BaseRequestAPICall, page: Puppeteer.Page) {
|
||||||
|
const maxTimeout = params.maxTimeout || 60000
|
||||||
|
const timer = new Timeout();
|
||||||
|
try {
|
||||||
|
const promise = resolveChallenge(ctx, params, page);
|
||||||
|
return await Promise.race([
|
||||||
|
promise,
|
||||||
|
timer.set(maxTimeout, `Maximum timeout reached. maxTimeout=${maxTimeout} (ms)`)
|
||||||
|
]);
|
||||||
|
} finally {
|
||||||
|
timer.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function resolveChallenge(ctx: RequestContext, { url, proxy, download, returnOnlyCookies }: BaseRequestAPICall, page: Puppeteer.Page): Promise<ChallengeResolutionT | void> {
|
||||||
|
|
||||||
maxTimeout = maxTimeout || 60000
|
|
||||||
let status = 'ok'
|
let status = 'ok'
|
||||||
let message = ''
|
let message = ''
|
||||||
|
|
||||||
@ -162,8 +176,7 @@ async function resolveChallenge(ctx: RequestContext, { url, maxTimeout, proxy, d
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: find out why these pages hang sometimes
|
while (true) {
|
||||||
while (Date.now() - ctx.startTimestamp < maxTimeout) {
|
|
||||||
await page.waitFor(1000)
|
await page.waitFor(1000)
|
||||||
try {
|
try {
|
||||||
// catch exception timeout in waitForNavigation
|
// catch exception timeout in waitForNavigation
|
||||||
@ -188,11 +201,6 @@ async function resolveChallenge(ctx: RequestContext, { url, maxTimeout, proxy, d
|
|||||||
log.html(await page.content())
|
log.html(await page.content())
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Date.now() - ctx.startTimestamp >= maxTimeout) {
|
|
||||||
ctx.errorResponse(`Maximum timeout reached. maxTimeout=${maxTimeout} (ms)`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.debug('Validating HTML code...')
|
log.debug('Validating HTML code...')
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
@ -418,7 +426,7 @@ const browserRequest = async (ctx: RequestContext, params: BaseRequestAPICall) =
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const page = await setupPage(ctx, params, session.browser)
|
const page = await setupPage(ctx, params, session.browser)
|
||||||
const data = await resolveChallenge(ctx, params, page)
|
const data = await resolveChallengeWithTimeout(ctx, params, page)
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
const { status } = data
|
const { status } = data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user