首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Node.js:如何重新启动Tor客户端

Node.js:如何重新启动Tor客户端
EN

Stack Overflow用户
提问于 2021-12-14 10:50:49
回答 2查看 250关注 0票数 0

我有一个简单的脚本,它使用PuppeteerTor client来使用不同的IP导航web:

代码语言:javascript
运行
复制
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终端中这样做:

代码语言:javascript
运行
复制
sudo /etc/init.d/tor restart

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

代码语言:javascript
运行
复制
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,但仍然无法工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-14 13:35:43

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

编辑哇,谢谢你下次这么多的不喜欢,当你没有得到你最好的设计和如果你能读到答案:

代码语言:javascript
运行
复制
/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 =等待page.evaluate(() => {返回page.evaluate })

所以..。请具体说明你在找什么,或者告诉我我的回答有什么不对,谢谢:)

票数 1
EN

Stack Overflow用户

发布于 2022-04-10 08:19:04

当您登录“Tor #2 IP:”时,您将指向下面一行中的tor1_ip : console.log('Tor #2 IP:‘+ tor1_ip),因此它应该是: console.log('Tor #2 IP:’+ tor2_ip)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70347639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档