Home > Back-end >  Node.js: how to restart Tor client
Node.js: how to restart Tor client

Time:12-15

I have a simple script that use Puppeteer and Tor client to navigate the web with differents IPs:

const puppeteer = require('puppeteer-extra');
const puppeteer_stealth = require('puppeteer-extra-plugin-stealth');
const puppeteer_proxy = require('puppeteer-page-proxy')
const exec = require('child_process').exec

puppeteer.use(puppeteer_stealth())

async function test() {
    // This function will test the bot and ooutput the results
    const browser = await puppeteer.launch({
        headless: true,
        args: ['--no-sandbox']
    });
    const page = (await browser.pages())[0];
    console.log('Running test...');
    
    // IP test
    console.log('\nIP test')

    await page.goto('https://api.ipify.org/');
    const original_ip = await page.evaluate(() => { return document.querySelector('body').children[0].innerHTML })
    console.log('Original IP: '   original_ip)
    
    await puppeteer_proxy(page, 'socks://127.0.0.1:9050');
    await page.reload();
    const tor1_ip = await page.evaluate(() => { return document.querySelector('body').children[0].innerHTML })
    console.log('Tor #1 IP: '   tor1_ip)

    exec('sudo /etc/init.d/tor restart');
    await page.reload()
    const tor2_ip = await page.evaluate(() => { return document.querySelector('body').children[0].innerHTML })
    console.log('Tor #2 IP: '   tor1_ip)

    await browser.close()
}

test()

The problem comes when I want to retart Tor client to have a new IP. Normally i'd do this in Linux terminal with the command:

sudo /etc/init.d/tor restart

So i found a way to execute commands in Node.js with child_process, but I can't get it to work. This is the output of my code:

Running test...

IP test
Original IP: ***.***.***.*** // censored by me
Tor #1 IP: ***.***.***.*** // censored by me
/home/flavio/Documents/arbitrage-betting/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:221
            throw new Error('Evaluation failed: '   helper_js_1.helper.getExceptionMessage(exceptionDetails));
                  ^

Error: Evaluation failed: TypeError: Cannot read properties of undefined (reading 'innerHTML')
    at __puppeteer_evaluation_script__:1:59
    at ExecutionContext._evaluateInternal (/home/flavio/Documents/arbitrage-betting/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:221:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ExecutionContext.evaluate (/home/flavio/Documents/arbitrage-betting/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:110:16)
    at async test (/home/flavio/Documents/arbitrage-betting/index.js:31:21)

I tried to solve the problem by myself, i even implemented tor_request but still couldn't get it to work.

CodePudding user response:

I think the Error isnt about restarting its about evaluation of the website?!

Edit wow thanks for so many dislikes next time you get no answer with your best designed and if you could read:

/home/flavio/Documents/arbitrage-betting/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:221
            throw new Error('Evaluation failed: '   helper_js_1.helper.getExceptionMessage(exceptionDetails));
                  ^

Error: Evaluation failed: TypeError: Cannot read properties of undefined (reading 'innerHTML')
    at __puppeteer_evaluation_script__:1:59
    at ExecutionContext._evaluateInternal (/home/flavio/Documents/arbitrage-betting/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:221:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ExecutionContext.evaluate (/home/flavio/Documents/arbitrage-betting/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:110:16)
    at async test (/home/flavio/Documents/arbitrage-betting/index.js:31:21)

It says it has an error in Evaluation in line 31:21 and i think it should be:

const tor2_ip = await page.evaluate(() => { return document.querySelector('body').children[0].innerHTML })

So... please specify what you looking for or just tell me what is with my answer wrong thanks :)

  • Related