簡體   English   中英

Node.js:如何重啟 Tor 客戶端

[英]Node.js: how to restart Tor client

我有一個簡單的腳本,它使用PuppeteerTor客戶端來導航具有不同 IP 的 web:

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執行命令的方法,但我無法讓它工作。 這是我的代碼的 output:

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但仍然無法正常工作。

我認為錯誤不是關於重新啟動它關於網站的評估?!

編輯哇,感謝您下次使用最佳設計沒有得到答案時有這么多不喜歡,如果您能閱讀:

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

所以...請指定您要查找的內容,或者只是告訴我我的答案有什么錯誤謝謝:)

當您記錄“Tor #2 IP:”時,您在下一行中指向 tor1_ip:console.log('Tor #2 IP:' + tor1_ip) 所以它應該是:console.log('Tor #2 IP:' + tor2_ip)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM