diff options
-rw-r--r-- | index.js | 12 |
1 files changed, 8 insertions, 4 deletions
@@ -29,6 +29,7 @@ const responseHeadersToRemove = ["Accept-Ranges", "Content-Length", "Keep-Alive" if (ctx.query.url) { const url = ctx.url.replace("/?url=", ""); let responseBody; + let responseData; let responseHeaders; const page = await browser.newPage(); if (ctx.method == "POST") { @@ -58,7 +59,7 @@ const responseHeadersToRemove = ["Accept-Ranges", "Content-Length", "Keep-Alive" interceptionId: e.interceptionId }).then((result) => { if (result.base64Encoded) { - responseBody = Buffer.from(result.body, 'base64'); + responseData = Buffer.from(result.body, 'base64'); } }); obj['errorReason'] = 'BlockedByClient'; @@ -78,9 +79,11 @@ const responseHeadersToRemove = ["Accept-Ranges", "Content-Length", "Keep-Alive" let tryCount = 0; response = await page.goto(url, { timeout: 30000, waitUntil: 'domcontentloaded' }); responseBody = await response.text(); + responseData = await response.buffer(); while (responseBody.includes("cf-browser-verification") && tryCount <= 10) { response = await page.waitForNavigation({ timeout: 30000, waitUntil: 'domcontentloaded' }); responseBody = await response.text(); + responseData = await response.buffer(); tryCount++; } responseHeaders = response.headers(); @@ -90,20 +93,21 @@ const responseHeadersToRemove = ["Accept-Ranges", "Content-Length", "Keep-Alive" const { name, value, secure, expires, domain, ...options } = cookie; ctx.cookies.set(cookie.name, cookie.value, options); }); - await page.close(); } catch (error) { if (!error.toString().includes("ERR_BLOCKED_BY_CLIENT")) { ctx.status = 500; ctx.body = error; } } + + await page.close(); responseHeadersToRemove.forEach(header => delete responseHeaders[header]); Object.keys(responseHeaders).forEach(header => ctx.set(header, jsesc(responseHeaders[header]))); - ctx.body = responseBody; + ctx.body = responseData; } else { ctx.body = "Please specify the URL in the 'url' query string."; } }); app.listen(3000); -})();
\ No newline at end of file +})(); |