Tor: fputs of “SIGNAL NEWNYM\r\n” results in empty response from fread

有些话、适合烂在心里 提交于 2019-12-10 12:35:46

问题


does anyone please know why

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

return an empty $response (on windows)? i can't get it to work after 5 hours trying everything i use the code in:

$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); 

it is my goal to have TOR usinga new ip for every request


回答1:


You are right! sometimes it takes like 8 seconds before an new ip is optained. logs: Jan 16 18:27:54.000 [notice] Rate limiting NEWNYM request: delaying by 3 second(s) Jan 16 18:28:00.000 [notice] New control connection opened from 127.0.0.1. Jan 16 18:28:00.000 [notice] Rate limiting NEWNYM request: delaying by 7 second(s)




回答2:


My guess is that authentication is failing and the connection is being closed so the subsequent fputs and fread for the NEWNYM signal are just returning false.

In your check of the authenticate response, there is a logic error here:

if ($code = '250') {

To compare $code to 250, you need to use ==.

If the auth fails, this evaluates to true tricking you into thinking you authenticated when it may have failed.

Authentication may be failing because the password is simply incorrect, or only cookie authentication is supported.


Side note, I have created a PHP library you may be interested in called TorUtils which provides classes for communication with the Tor control port, and also a cURL wrapper for ensuring cURL requests properly go through Tor's SOCKS proxy.

Using the curl wrapper and control client, your code can easily wrap curl requests through Tor and issue commands to the control port to request a new IP.




回答3:


Thanks drew010! you where right, i was not authenticated. Now i am.

However, with code below, each time i run this script, it fetches http://www.watismijnip.nl with a different ip. But it needs to us a new ip, after each fetching of page. This is not the case now.

I realise that a new identity is required wih port 9051, while curl uses port 9050. But if i let curl use port 9051, it give and empty string back.

Can can that be done?

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


来源:https://stackoverflow.com/questions/34814454/tor-fputs-of-signal-newnym-r-n-results-in-empty-response-from-fread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!