【问题标题】:Tor: fputs of "SIGNAL NEWNYM\r\n" results in empty response from freadTor:“SIGNAL NEWNYM\r\n”的 fputs 导致 fread 的响应为空
【发布时间】:2016-01-15 15:29:06
【问题描述】:

有人知道原因吗

fputs($fp, "SIGNAL NEWNYM\r\n");
$response = fread($fp, 1024);

返回一个空的 $response(在 Windows 上)?在尝试了所有方法 5 小时后我无法让它工作 我使用以下代码:

$fp = fsockopen('localhost', 9051, $errno, $errstr, 30);
    $auth_code = 'a-password';
    if ($fp) {
        echo "Connected to TOR port<br />";
    }
    else {
        echo "Cant connect to TOR port<br />";
    }

    fputs($fp, "AUTHENTICATE \"".$auth_code."\"\r\n");
    $response = fread($fp, 1024);
    list($code, $text) = explode(' ', $response, 2);
    if ($code = '250') {
        echo "Authenticated 250 OK<br />";
    }
    else {
        echo "Authentication failed<br />";
    }

    fputs($fp, "SIGNAL NEWNYM\r\n");
    $response = fread($fp, 1024); 

我的目标是让 TOR 为每个请求使用一个新的 ip

【问题讨论】:

    标签: php curl tor


    【解决方案1】:

    我的猜测是身份验证失败并且连接正在关闭,因此 NEWNYM 信号的后续 fputsfread 只是返回 false

    在您检查身份验证响应时,这里有一个逻辑错误:

    if ($code = '250') {
    

    要将$code250 进行比较,您需要使用==

    如果身份验证失败,则评估结果为真,当它可能失败时,这会诱使您认为您已通过身份验证。

    身份验证失败可能是因为密码不正确,或者仅支持 cookie 身份验证。


    旁注,我创建了一个您可能感兴趣的名为TorUtils 的PHP 库,它提供了用于与Tor control 端口通信的类,还有一个cURL wrapper 用于确保cURL 请求正确地通过Tor 的SOCKS 代理.

    使用 curl 包装器和控制客户端,您的代码可以轻松地通过 Tor 包装 curl 请求,并向控制端口发出命令以请求新 IP。

    【讨论】:

      【解决方案2】:

      你是对的! 有时需要 8 秒才能选择一个新的 ip。 日志: Jan 16 18:27:54.000 [notice] 速率限制 NEWNYM 请求:延迟 3 秒 Jan 16 18:28:00.000 [notice] 从 127.0.0.1 打开新的控制连接。 Jan 16 18:28:00.000 [notice] 速率限制 NEWNYM 请求:延迟 7 秒

      【讨论】:

        【解决方案3】:

        感谢draw010!你在哪里对,我没有通过身份验证。现在是我了。

        但是,使用下面的代码,每次我运行此脚本时,它都会使用不同的 ip 获取 http://www.watismijnip.nl。 但它需要我们一个新的 ip,在每次获取页面之后。现在不是这样了。

        我意识到端口 9051 需要一个新的身份,而 curl 使用端口 9050。但是如果我让 curl 使用端口 9051,它会返回并返回空字符串。

        这样可以吗?

            function tor_new_identity($tor_ip='127.0.0.1', $control_port='9051', $auth_code=''){
            $fp = fsockopen($tor_ip, $control_port, $errno, $errstr, 30);
            if (!$fp) return false; //can't connect to the control port
        
            fputs($fp, "AUTHENTICATE $auth_code\r\n");
            $response = fread($fp, 1024);
            echo 'debug: $response='.$response."\n<br>";
            list($code, $text) = explode(' ', $response, 2);
            if ($code != '250') return false; //authentication failed
        
            //send the request to for new identity
            fputs($fp, "signal NEWNYM\r\n");
            $response = fread($fp, 1024);
            list($code, $text) = explode(' ', $response, 2);
            if ($code != '250') return false; //signal failed
        
            fclose($fp);
            return true;
        }
        
        
        if (tor_new_identity('127.0.0.1', '9051', '"my_password"')) {
            echo "Identity switched!";
        }else{
            echo "Identity NOT switched!";
        }
        
        $url='http://www.watismijnip.nl/';
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9050");
        curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');
        $output = curl_exec($ch);
        $curl_error = curl_error($ch);
        curl_close($ch);
        
        print_r($output);
        print_r($curl_error);
        
        
        if (tor_new_identity('127.0.0.1', '9051', '"my_password"')) {
            echo "Identity switched!";
        }else{
            echo "Identity NOT switched!";
        }
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9050");
        curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');
        $output = curl_exec($ch);
        $curl_error = curl_error($ch);
        curl_close($ch);
        
        print_r($output);
        print_r($curl_error);
        

        【讨论】:

        • :Tor 需要在身份检查之间暂停 5 秒。你能发布日志文件吗?
        猜你喜欢
        • 1970-01-01
        • 2016-01-13
        • 1970-01-01
        • 1970-01-01
        • 2019-02-24
        • 2015-08-23
        • 2016-01-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多