dongwei8440
2015-09-24 14:38
采纳率: 100%
浏览 98
已采纳

PHP tor安装

I have this code:

<?php
$ip = '127.0.0.1';
$port = '9051';
$auth = 'PASSWORD';
$command = 'signal NEWNYM';

$fp = fsockopen($ip,$port,$error_number,$err_string,10);
if(!$fp) { echo "ERROR: $error_number : $err_string";
    return false;
} else {
    fwrite($fp,"AUTHENTICATE \"".$auth."\"
");
    $received = fread($fp,512);
    fwrite($fp,$command."
");
    $received = fread($fp,512);
}

fclose($fp);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://whatismyip.org");
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9050");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$response = curl_exec($ch);
?>

The problem is it doesn't work. I am running on windows not linux. And I downloaded the tor browser. I understand there was supposed to be some configuration where I set a password and a port. However that did not happen during the install.

Is there anyone familiar with tor. What am I missing?

图片转代码服务由CSDN问答提供 功能建议

我有这段代码:

 &lt;?php \  n $ ip ='127.0.0.1'; 
 $ port ='9051'; 
 $ auth ='PASSWORD'; 
 $ command ='signal NEWNYM'; 
 
 $ fp = fsockopen($ ip,  $ port,$ error_number,$ err_string,10); 
if(!$ fp){echo“ERROR:$ error_number:$ err_string”; 
返回false; 
} else {
 fwrite($ fp,“AUTHENTICATE  \“”。$ auth。“\”
“); 
 $ received = fread($ fp,512); 
 fwrite($ fp,$ command。”
“); 
 $ received = fread  ($ fp,512); 
} 
 
 nclclose($ fp); 
 
 $ ch = curl_init(); 
curl_setopt($ ch,CURLOPT_URL,“http://whatismyip.org”); \  ncurl_setopt($ ch,CURLOPT_PROXY,“127.0.0.1:9050”); 
ncurl_setopt($ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5); 
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ ch,CURLOPT_VERBOSE,0); \  n $ response = curl_exec($ ch); 
?&gt; 
   
 
 

问题是它不起作用。 我在windows上运行而不是linux。 我下载了tor浏览器。 我知道应该有一些配置,我设置了密码和端口。 但是在安装过程中没有发生这种情况。

有没有人熟悉tor。 我错过了什么?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsstjqsr631426 2015-09-24 21:55
    已采纳

    By default Tor does not listen on the control port, and you will also need to configure a password or specify it use CookieAuthentication. If you are going to be scripting with Tor, you may want to download the expert bundle instead of the browser bundle.

    These values need to go in the torrc file in the Tor Data directory. See the default sample torrc.

    First, generate a hashed password (note piping to more is necessary on Windows or you won't see any output):

    C:\Path\To\Tor>tor.exe --hash-password PASSWORD|more
    

    Copy the output that looks like 16:*BUNCH_OF_HEX_DIGITS*

    Next, you need to edit your config and add or uncomment the line with ControlPort 9051 and add your hashed password.

    ControlPort 9051
    HashedControlPassword 16:YOUR_HASHED_PASSWORD_HERE
    

    Then restart the daemon, make sure it is using the config file with these values, and try your code again.

    See the docs on the ControlPort and HashedControlPassword settings.

    Perhaps overkill for what you are doing, but you may also be interested in this PHP Tor library I made which you can use to interact with the Tor control port.

    Using my library your code would look like:

    <?php
    
    use Dapphp\TorUtils\ControlClient;
    use Dapphp\TorUtils\ProtocolError;
    $tc = new ControlClient();
    
    try {
        $tc->connect(); // connect to 127.0.0.1:9051
        $tc->authenticate(); // uses cookie authentication, can also use $tc->authenticate('password_here');
    } catch (\Exception $ex) {
        echo "Failed to create Tor control connection: " . $ex->getMessage() . "
    ";
        exit;
    }
    
    try {
        echo "Sending NEWNYM signal to controller...";
        $tc->signal(ControlClient::SIGNAL_NEWNYM);
        echo "OK";
    } catch (ProtocolError $pe) {
        echo $pe->getMessage();
    }
    
    $tc->quit(); // close control connection
    
    已采纳该答案
    打赏 评论
  • douzhuiqiu4923 2016-01-16 12:46

    Note that in C:\Path\To\Tor>tor.exe --hash-password PASSWORD|more, "PASSWORD" needs to be replaced like the password you like. Suppose you use "my_password" as your password. Then use: C:\Path\To\Tor>tor.exe --hash-password my_password|more to generate a hashed password In your code to switch identity, use $auth = '"my_password"'; Yes, with double quotes

    打赏 评论

相关推荐 更多相似问题