|
| 1 | +const puppeteer = require('puppeteer-core') |
| 2 | +const { extract, cleanup } = require('aws-puppeteer-lambda') |
| 3 | + |
| 4 | +exports.handler = async (evt, ctx, cb) => { |
| 5 | + // Extract the headless chrome executable and return its path. |
| 6 | + // If a previous Lambda invocation has extracted the executable, it will be reused. |
| 7 | + const executablePath = await extract() |
| 8 | + |
| 9 | + const browser = await puppeteer.launch({ |
| 10 | + ignoreHTTPSErrors: true, |
| 11 | + args: [ |
| 12 | + '--disable-dev-shm-usage', |
| 13 | + '--disable-gpu', |
| 14 | + '--single-process', |
| 15 | + '--no-zygote', |
| 16 | + '--no-sandbox' |
| 17 | + ], |
| 18 | + executablePath |
| 19 | + }) |
| 20 | + |
| 21 | + // Use evt.queryStringParameters to get the params from GET |
| 22 | + const url = evt.queryStringParameters.url |
| 23 | + |
| 24 | + const done = (result = { success: false, data: null }) => |
| 25 | + cb(null, { |
| 26 | + statusCode: 200, |
| 27 | + body: JSON.stringify(result), |
| 28 | + isBase64Encoded: false |
| 29 | + }) |
| 30 | + |
| 31 | + const page = await browser.newPage() |
| 32 | + |
| 33 | + // Pass the User-Agent Test. |
| 34 | + const userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36' |
| 35 | + await page.setUserAgent(userAgent) |
| 36 | + |
| 37 | + // Pass the Webdriver Test. |
| 38 | + await page.evaluateOnNewDocument(() => { |
| 39 | + const newProto = navigator.__proto__ |
| 40 | + delete newProto.webdriver |
| 41 | + navigator.__proto__ = newProto |
| 42 | + }) |
| 43 | + |
| 44 | + // Pass the Chrome Test. |
| 45 | + await page.evaluateOnNewDocument(() => { |
| 46 | + // We can mock this in as much depth as we need for the test. |
| 47 | + window.chrome = { |
| 48 | + runtime: {} |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + // Pass the Permissions Test. |
| 53 | + await page.evaluateOnNewDocument(() => { |
| 54 | + const originalQuery = window.navigator.permissions.query |
| 55 | + window.navigator.permissions.__proto__.query = (parameters) => |
| 56 | + parameters.name === 'notifications' |
| 57 | + ? Promise.resolve({ state: Notification.permission }) |
| 58 | + : originalQuery(parameters) |
| 59 | + |
| 60 | + // Inspired by: https://github.com/ikarienator/phantomjs_hide_and_seek/blob/master/5.spoofFunctionBind.js |
| 61 | + const oldCall = Function.prototype.call |
| 62 | + function call() { |
| 63 | + return oldCall.apply(this, arguments) |
| 64 | + } |
| 65 | + Function.prototype.call = call |
| 66 | + |
| 67 | + const nativeToStringFunctionString = Error.toString().replace( |
| 68 | + /Error/g, |
| 69 | + 'toString' |
| 70 | + ) |
| 71 | + const oldToString = Function.prototype.toString |
| 72 | + |
| 73 | + function functionToString() { |
| 74 | + if (this === window.navigator.permissions.query) { |
| 75 | + return 'function query() { [native code] }' |
| 76 | + } |
| 77 | + if (this === functionToString) { |
| 78 | + return nativeToStringFunctionString |
| 79 | + } |
| 80 | + return oldCall.call(oldToString, this) |
| 81 | + } |
| 82 | + Function.prototype.toString = functionToString |
| 83 | + }) |
| 84 | + |
| 85 | + // Pass the Plugins Length Test. |
| 86 | + await page.evaluateOnNewDocument(() => { |
| 87 | + // Overwrite the `plugins` property to use a custom getter. |
| 88 | + Object.defineProperty(navigator, 'plugins', { |
| 89 | + // This just needs to have `length > 0` for the current test, |
| 90 | + // but we could mock the plugins too if necessary. |
| 91 | + get: () => [1, 2, 3, 4, 5] |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + // Pass the Languages Test. |
| 96 | + await page.evaluateOnNewDocument(() => { |
| 97 | + // Overwrite the `plugins` property to use a custom getter. |
| 98 | + Object.defineProperty(navigator, 'languages', { |
| 99 | + get: () => ['en-US', 'en'] |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + // Pass the iframe Test |
| 104 | + await page.evaluateOnNewDocument(() => { |
| 105 | + Object.defineProperty(HTMLIFrameElement.prototype, 'contentWindow', { |
| 106 | + get: function() { |
| 107 | + return window |
| 108 | + } |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + // Pass toString test, though it breaks console.debug() from working |
| 113 | + await page.evaluateOnNewDocument(() => { |
| 114 | + window.console.debug = () => { |
| 115 | + return null |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | + try { |
| 120 | + await page.goto(url); |
| 121 | + } catch(e) { |
| 122 | + await browser.close(); |
| 123 | + |
| 124 | + done() |
| 125 | + } |
| 126 | + |
| 127 | + const result = await page.evaluate(() => { |
| 128 | + // Put your code here |
| 129 | + return { success: true, data: 'Hello AWS Lambda!' } |
| 130 | + }) |
| 131 | + |
| 132 | + await browser.close() |
| 133 | + |
| 134 | + done(result) |
| 135 | + cleanup() |
| 136 | +} |
0 commit comments