• Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training
Sick Codes - Security Research, Hardware & Software Hacking, Consulting, Linux, IoT, Cloud, Embedded, Arch, Tweaks & Tips!
  • Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training
No Result
View All Result
Sick Codes - Security Research, Hardware & Software Hacking, Consulting, Linux, IoT, Cloud, Embedded, Arch, Tweaks & Tips!
  • Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training
No Result
View All Result
Sick Codes - Security Research, Hardware & Software Hacking, Consulting, Linux, IoT, Cloud, Embedded, Arch, Tweaks & Tips!
No Result
View All Result
Home Tutorials

Puppeteer & NodeJS Timeout Killswitch (SIMPLE!)

by Sick Codes
May 26, 2020 - Updated on June 24, 2020
in Tutorials
0

The following will always kill a node script after a timeout duration is reached.

There are two ways to kill a nodejs/npm script: inside the node script, or inside the shell.

Consider the following script running direct from the command line:

node << EOF
const puppeteer = require('puppeteer-extra')
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());
puppeteer.launch(
    { args: [ '--no-sandbox', '--disable-setuid-sandbox' ], headless: true }
  ).then(async browser => {
  const page = await browser.newPage()
  await page.goto("https://google.com/${username}", {
    waitUntil : ['load', 'domcontentloaded'] 
  });
  let bodyHTML = await page.evaluate(() => document.body.innerHTML);
  console.log(bodyHTML)
  await browser.close();
});
EOF

This simple script returns the HTML of google.com to stdout.

However, if the page fails to load, the script will never timeout and your app or container will hang forever.

We can try the following things:

This will put nodejs into a subshell. It will record the process id of node, sleep 5 and then kill it no matter what.
Method 1: precise PID but no output captured

# use this kill switch if you don't care about the node output
node file.js & { PID=$!; sleep 5; kill $PID 2> /dev/null; }

However, what if we want to use the output of the nodejs file?

Since we cannot read the variables from the subshell, we need to run the PID script first.

Method 2: closes node by name but you can capture the shell variable!

# use this kill switch if you are capturing the nodejs output in a variable
{ sleep 5; pgrep node | xargs kill 2> /dev/null ; } &
OUTPUT_VAR="$(node file.js)"

Method 2.5: Dynamic nodejs script variables using bash and native shell killswitch

# Use this kill switch if you are capturing the nodejs output in a variable
# and use want to use End of Function tags to dynamically change your node script (${username})
{ sleep 5; pgrep node | xargs kill 2> /dev/null ; } &
OUTPUT_VAR="$(node << EOF
    const puppeteer = require('puppeteer-extra')
    const StealthPlugin = require('puppeteer-extra-plugin-stealth');
    puppeteer.use(StealthPlugin());
    puppeteer.launch(
        { args: [ '--no-sandbox', '--disable-setuid-sandbox' ], headless: true }
      ).then(async browser => {
      const page = await browser.newPage()
      await page.goto("https://instagram.com/${username}", {
        waitUntil : ['load', 'domcontentloaded'] 
      });
      let bodyHTML = await page.evaluate(() => document.body.innerHTML);
      console.log(bodyHTML)
      await browser.close();
    });
EOF
)"

In any of the above cases, if your script finishes successfully nothing will happen, unless you launch another node within the race condition window!

Next Post

ULTIMATE stdin, stdout Guide: How To Duplicate stdin, stdout in BASH/Redirect Output to 2 files, Send Output to Multiple Variables and more!

How To Run Teamviewer 15 PERFECTLY on Arch/Manjaro/Debian/Ubutnu

move ssd from old to new

How to Move Linux OS from old NVMe to new NVMe drive.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result
  • Home
  • Releases
  • Submit Vuln
  • Press
  • About
  • PGP
  • Contact
    • Contact
    • Submit Vuln
    • VDP
  • Tutorials
    • All Posts
    • Photoshop on Linux
    • macOS on Linux
  • Supporters
  • Projects
  • Training

© 2017-2021 Sick.Codes

@sickcodes

@sickcodes

@sickcodes

Discord Server

sickcodes.slack.com

t.me/sickcodeschat

./contact_form