【问题标题】:Node.js: how to restart Tor clientNode.js:如何重启 Tor 客户端
【发布时间】:2022-01-17 16:36:07
【问题描述】:

我有一个简单的脚本,它使用 PuppeteerTor 客户端通过不同的 IP 浏览网页:

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()

当我想重启 Tor 客户端以获得新 IP 时,问题就来了。 通常我会在 Linux 终端中使用以下命令执行此操作:

sudo /etc/init.d/tor restart

所以我找到了一种在 Node.js 中使用 child_process 执行命令的方法,但我无法让它工作。 这是我的代码的输出:

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)

我试图自己解决这个问题,我什至实现了 tor_request 但仍然无法让它工作。

【问题讨论】:

    标签: javascript node.js tor


    【解决方案1】:

    我认为错误不是关于重新启动它关于网站的评估?!

    编辑哇,感谢您下次使用您最好的设计并没有得到答复时有这么多不喜欢,如果您能阅读:

    /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)
    

    它说它在第 31:21 行的评估中有错误,我认为应该是:

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

    所以...请指定您要查找的内容,或者告诉我我的回答有什么问题,谢谢:)

    【讨论】:

    • 我实际上投了你的票,因为你是对的,请不要关注你的答案投票。无论如何,我会再发一篇文章来添加我发现的内容。
    猜你喜欢
    • 2011-09-11
    • 2016-10-11
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多