|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const readline = require("readline"); |
| 4 | +const child_process = require("child_process"); |
| 5 | + |
| 6 | +const puppeteer = require("puppeteer-core"); |
| 7 | +const yargs = require("yargs"); |
| 8 | + |
| 9 | +function cliOpts() { |
| 10 | + return { |
| 11 | + "url": { |
| 12 | + describe: "The URL of the login page", |
| 13 | + type: "string", |
| 14 | + demandOption: true, |
| 15 | + }, |
| 16 | + |
| 17 | + "user": { |
| 18 | + describe: "The username for the login. If not specified, will be prompted", |
| 19 | + }, |
| 20 | + |
| 21 | + "password": { |
| 22 | + describe: "The password for the login. If not specified, will be prompted (recommended)", |
| 23 | + }, |
| 24 | + |
| 25 | + "filename": { |
| 26 | + describe: "The filename for the profile tarball", |
| 27 | + default: "/output/profile.tar.gz", |
| 28 | + }, |
| 29 | + |
| 30 | + "debugScreenshot": { |
| 31 | + describe: "If specified, take a screenshot after login and save as this filename" |
| 32 | + }, |
| 33 | + |
| 34 | + "headless": { |
| 35 | + describe: "Run in headless mode, otherwise start xvfb", |
| 36 | + type: "boolean", |
| 37 | + default: false, |
| 38 | + }, |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +async function main() { |
| 45 | + const params = yargs |
| 46 | + .usage("browsertrix-crawler profile [options]") |
| 47 | + .option(cliOpts()) |
| 48 | + .argv; |
| 49 | + |
| 50 | + if (!params.headless) { |
| 51 | + console.log("Launching XVFB"); |
| 52 | + child_process.spawn("Xvfb", [ |
| 53 | + process.env.DISPLAY, |
| 54 | + "-listen", |
| 55 | + "tcp", |
| 56 | + "-screen", |
| 57 | + "0", |
| 58 | + process.env.GEOMETRY, |
| 59 | + "-ac", |
| 60 | + "+extension", |
| 61 | + "RANDR" |
| 62 | + ]); |
| 63 | + } |
| 64 | + |
| 65 | + //await new Promise(resolve => setTimeout(resolve, 2000)); |
| 66 | + |
| 67 | + const args = { |
| 68 | + headless: !!params.headless, |
| 69 | + executablePath: "google-chrome", |
| 70 | + ignoreHTTPSErrors: true, |
| 71 | + args: [ |
| 72 | + "--no-xshm", |
| 73 | + "--no-sandbox", |
| 74 | + "--disable-background-media-suspend", |
| 75 | + "--autoplay-policy=no-user-gesture-required", |
| 76 | + "--disable-features=IsolateOrigins,site-per-process", |
| 77 | + "--user-data-dir=/tmp/profile" |
| 78 | + ] |
| 79 | + }; |
| 80 | + |
| 81 | + if (!params.user) { |
| 82 | + params.user = await promptInput("Enter username: "); |
| 83 | + } |
| 84 | + |
| 85 | + if (!params.password) { |
| 86 | + params.password = await promptInput("Enter password: ", true); |
| 87 | + } |
| 88 | + |
| 89 | + const browser = await puppeteer.launch(args); |
| 90 | + |
| 91 | + const page = await browser.newPage(); |
| 92 | + |
| 93 | + const waitUntil = ["load", "networkidle2"]; |
| 94 | + |
| 95 | + await page.setCacheEnabled(false); |
| 96 | + |
| 97 | + console.log("loading"); |
| 98 | + |
| 99 | + await page.goto(params.url, {waitUntil}); |
| 100 | + |
| 101 | + console.log("loaded"); |
| 102 | + |
| 103 | + let u, p; |
| 104 | + |
| 105 | + try { |
| 106 | + u = await page.waitForXPath("//input[contains(@name, 'user')]"); |
| 107 | + |
| 108 | + p = await page.waitForXPath("//input[contains(@name, 'pass') and @type='password']"); |
| 109 | + |
| 110 | + } catch (e) { |
| 111 | + if (params.debugScreenshot) { |
| 112 | + await page.screenshot({path: params.debugScreenshot}); |
| 113 | + } |
| 114 | + console.log("Login form could not be found"); |
| 115 | + await page.close(); |
| 116 | + process.exit(1); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + await u.type(params.user); |
| 121 | + |
| 122 | + await p.type(params.password); |
| 123 | + |
| 124 | + await Promise.allSettled([ |
| 125 | + p.press("Enter"), |
| 126 | + page.waitForNavigation({waitUntil}) |
| 127 | + ]); |
| 128 | + |
| 129 | + await page._client.send("Network.clearBrowserCache"); |
| 130 | + |
| 131 | + if (params.debugScreenshot) { |
| 132 | + await page.screenshot({path: params.debugScreenshot}); |
| 133 | + } |
| 134 | + |
| 135 | + await browser.close(); |
| 136 | + |
| 137 | + console.log("creating profile"); |
| 138 | + |
| 139 | + const profileFilename = params.filename || "/output/profile.tar.gz"; |
| 140 | + |
| 141 | + child_process.execFileSync("tar", ["cvfz", profileFilename, "./"], {cwd: "/tmp/profile"}); |
| 142 | + console.log("done"); |
| 143 | + |
| 144 | + process.exit(0); |
| 145 | +} |
| 146 | + |
| 147 | +function promptInput(msg, hidden = false) { |
| 148 | + const rl = readline.createInterface({ |
| 149 | + input: process.stdin, |
| 150 | + output: process.stdout |
| 151 | + }); |
| 152 | + |
| 153 | + if (hidden) { |
| 154 | + // from https://stackoverflow.com/a/59727173 |
| 155 | + rl.input.on("keypress", function () { |
| 156 | + // get the number of characters entered so far: |
| 157 | + const len = rl.line.length; |
| 158 | + // move cursor back to the beginning of the input: |
| 159 | + readline.moveCursor(rl.output, -len, 0); |
| 160 | + // clear everything to the right of the cursor: |
| 161 | + readline.clearLine(rl.output, 1); |
| 162 | + // replace the original input with asterisks: |
| 163 | + for (let i = 0; i < len; i++) { |
| 164 | + rl.output.write("*"); |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + return new Promise((resolve) => { |
| 170 | + rl.question(msg, function (res) { |
| 171 | + rl.close(); |
| 172 | + resolve(res); |
| 173 | + }); |
| 174 | + }); |
| 175 | +} |
| 176 | + |
| 177 | +main(); |
| 178 | + |
0 commit comments