尝试使 Tor 自动化以在站点上执行某些操作并每次都更改身份。需要一些指导

Trying to automate Tor to do something on a site and change identity each time. Need some guidance

我真的需要一些帮助来自动化 Tor 在网站上做一些事情(在这种情况下,检查民意调查中的一些事情)然后用新的身份重新启动 Tor。我从来没有做过任何接近于此的事情。我只对HTML、CSS和JS比较了解

现在,总而言之,我想制作一个循环,重复访问 Tor 上的站点,检查该站点上的内容,然后使用新身份重新启动 Tor。

如果有人能给我一些指导并告诉我我可以使用什么,将不胜感激。我有时间和耐心学习,所以什么都行。

以下是使用 PHP 和 Python 3 实现您想要的结果的示例。它们是通过 Tor 发出请求和按需更改您的身份的简单起点。

PHP 示例使用 TorUtils 与控制器通信并通过 Tor 包装 cURL。

Python 示例使用 stem to communicate with the controller and Requests 通过 Tor 的 SOCKS 代理发送请求。

这些示例假设您已经让 Tor 工作并且 SocksPort 设置为 9050,并且 ControlPort 设置为 9051 且 cookie 身份验证有效,或者控制器密码为 password

PHP

设置

  • Install Composer安装TorUtils包(也可以下载zipball并解压)
  • 一旦 composer 开始工作,运行 composer require dapphp/torutils 从您的项目目录下载并安装依赖项

代码

<?php

use Dapphp\TorUtils\ControlClient;
use Dapphp\TorUtils\TorCurlWrapper;

require_once 'vendor/autoload.php'; // composer autoloader
// include TorUtils/src/ControlClient.php and TorUtils/src/TorCurlWrapper.php if using without composer

$controller = new ControlClient; // get a new controller object

try {
    $controller->connect('127.0.0.1', 9051); // connect to Tor controller on localhost:9051
    $controller->authenticate('password');   // attempt to authenticate using "password" as password
} catch (\Exception $ex) {
    die("Failed to open connection to Tor controller.  Reason: " . $ex->getMessage() . "\n");
}   

// issue 10 requests, changing identity after each request
for ($i = 0; $i < 10; ++$i) {
    try {
        $curl = new TorCurlWrapper('127.0.0.1', 9050); // connect to Tor SOCKS proxy on localhost:9050
        $curl->httpGet('https://drew-phillips.com/ip-info/'); // issue request
        $body = strip_tags($curl->getResponseBody());

        if (preg_match('/Using Tor:\s*Yes/i', $body)) {
            echo "You appear to be using Tor successfully.  ";
        } else {
            echo "Proxy worked but this Tor IP is not known.  ";
        }

        if (preg_match('/IP Address:\s*(\d+\.\d+\.\d+\.\d+)/i', $body, $ip)) {
            echo "Source IP = {$ip[1]}\n";
        } else {
            echo "Couldn't determine IP!\n";
        }
    } catch (\Exception $ex) {
        echo "HTTP request failed!  " . $ex->getMessage() . "\n";
    }

    // TODO: issue more requests as needed here

    echo "\n";
    sleep(10);

    try {
        // send signal to controller to request new identity (IP)
        $controller->signal(ControlClient::SIGNAL_NEWNYM);
    } catch (\Exception $ex) {
        echo "Failed to issue NEWNYM signal: " . $ex->getMessage() . "\n";
    }
}   

Python 3

设置

此示例使用 Python 3 并假设您已启动 Python 解释器并 运行ning 并安装了以下软件包:requestsrequests[socks], socks, urllib3, stem.

在 Debian/Ubuntu 上:sudo -H pip3 install requests requests[socks] socks urllib3 stem

代码

#!/usr/bin/env python3

import requests
from stem.control import Controller, Signal
import time
import sys
import re

# specify Tor's SOCKS proxy for http and https requests
proxies = {
    'http': 'socks5h://127.0.0.1:9050',
    'https': 'socks5h://127.0.0.1:9050',
}

try:
    controller = Controller.from_port(9051) # try to connect to controller at localhost:9051
except stem.SocketError as exc:
    print("Unable to connect to tor on port 9051: %s" % exc)
    sys.exit(1)

try:
    controller.authenticate('password') # try to authenticate with password "password"
except stem.connection.PasswordAuthFailed:
    print("Unable to authenticate, password is incorrect")
    sys.exit(1)

# issue 10 requests, changing identity after each request
for i in range(1,10):
    # issue request, passing proxies to request
    r = requests.get('https://drew-phillips.com/ip-info/', proxies=proxies)

    #print(r.text)

    m = re.search('<dt>Using Tor:</dt><dd><span[^>]*>Yes', r.text)
    if m:
        print("You appear to be using Tor successfully.  ", end="")
    else:
        print("Proxy worked but this Tor IP is not known.  ", end="")

    m = re.search('<dt>IP Address:</dt><dd>(\d+\.\d+\.\d+\.\d+)</dd>', r.text)
    if m:
        print("Source IP = %s" % m.groups(1))
    else:
        print("Failed to scrape IP from page")

    try:
        # send signal to controller to request new identity (IP)
        controller.signal(Signal.NEWNYM)
    except Exception as ex:
        print("NEWNYM failed: %s" % ex)

    time.sleep(10)