diff options
author | Emilien Devos <contact@emiliendevos.be> | 2020-05-22 22:25:17 +0200 |
---|---|---|
committer | Emilien Devos <contact@emiliendevos.be> | 2020-05-22 22:25:17 +0200 |
commit | f6d2eb6a698a13c6bc2a800793c3331a52a5e47a (patch) | |
tree | e347b081f6fe244fb073959aff14482666d8e18d /index.js | |
parent | 49a3b14499b01486c3fc196f0b01c69f9c34faed (diff) |
add ability to pass headers
Diffstat (limited to 'index.js')
-rw-r--r-- | index.js | 17 |
1 files changed, 14 insertions, 3 deletions
@@ -6,6 +6,11 @@ puppeteer.use(StealthPlugin()); const Koa = require('koa'); const app = new Koa(); +const headersToRemove = [ + "host", "user-agent", "accept", "accept-encoding", "content-length", + "forwarded", "x-forwarded-proto", "x-forwarded-for", "x-cloud-trace-context" +]; + (async () => { let options = { headless: true, @@ -16,15 +21,21 @@ const app = new Koa(); const browser = await puppeteer.launch(options); app.use(async ctx => { if (ctx.query.url) { + const url = ctx.url.replace("/?url=", ""); const page = await browser.newPage(); - await page.goto(ctx.query.url, {timeout: 30000, waitUntil: 'domcontentloaded'}); + let headers = ctx.headers; + headersToRemove.forEach(header => { + delete headers[header]; + }); + await page.setExtraHTTPHeaders(headers); + await page.goto(url, { timeout: 30000, waitUntil: 'domcontentloaded' }); if ((await page.content()).includes("cf-browser-verification")) - await page.waitForNavigation({timeout: 30000, waitUntil: 'domcontentloaded'}); + await page.waitForNavigation({ timeout: 30000, waitUntil: 'domcontentloaded' }); ctx.body = await page.content(); await page.close(); } else { - ctx.body = "Please specify the URL query string."; + ctx.body = "Please specify the URL in the 'url' query string."; } }); app.listen(3000); |