百度空间 | 百度首页 
 
查看文章
 
用PHP如何更换Tor的身份(TOR : How To Switch To a New Identity Using PHP)
2009-07-19 21:13

TOR. Where would I be without it? TOR has helped me circumvent DNS issues and get around Google blocks more than once. I also use it in some of my programs/scripts. So here’s a simple PHP script that I use to switch TOR to a new identity.

Sidenote : In case you don’t know what TOR is, here are the important bits – it’s a free, volunteer-based netwok of anonymous proxies. There are three things I like about it – it’s free, it works, and it’s easy to switch to a new proxy chain – just get a “new identity”. No need to reconfigure your browser/whatever to use a new proxy address.

The PHP script itself is below, followed by an explanation on how to use it.

New Identity Script

/**
* Switch TOR to a new identity.
**/

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

/**
* Load the TOR's "magic cookie" from a file and encode it in hexadecimal.
**/

function tor_get_cookie($filename){
$cookie = file_get_contents($filename);
//convert the cookie to hexadecimal
$hex = '';
for ($i=0;$i<strlen($cookie);$i++){
$h = dechex(ord($cookie[$i]));
$hex .= str_pad($h, 2, '0', STR_PAD_LEFT);
}
return strtoupper($hex);
}

Basic Usage

tor_new_identity() takes three optional parameters –

  • $tor_ip – The IP address of the TOR server you want to access. Defaults to 127.0.0.1
  • $control_port – The TOR control port. Defaults to 9051.
  • $auth_code – The authentication code. Defaults to an empty string. See the next section for more details on this parameter.

The function returns TRUE if it successfuly sent the “new identity” command, FALSE otherwise. If your TOR proxy doesn’t require authentication you can force a new identity very easily :

if (tor_new_identity('127.0.0.01', '9051')) {
echo "Identity switched!";
}

TOR Authentication

Depending on your configuration, the script may need to authenticate before it can signal TOR to switch to a new identity.

There are three authentication modes TOR supports :

  • None – no authentication required. You don’t need to set the third parameter of tor_new_identity().
  • Cookie – TOR stores a “magic cookie” in the control_auth_cookie file inside it’s data directory. You have to authenticate by sending this cookie encoded in hexadecimal form. You can use tor_get_cookie() to read and properly encode the cookie.
    Example :
    $cookie = tor_get_cookie('/path/to/tor/data/dir/control_auth_cookie');
    tor_new_identity('127.0.0.1', 9051, $cookie);
  • Password – your basic password authentication. To authenticate you will need to use the password in “quotes” or a hex-encoded version of the password.
    Example :
    tor_new_identity('127.0.0.1', '9051', '"secret_passw0rd"');

P.S.
Is this too dry? Add some milk. Reconciliation ;)

注意了:要采用tor_new_identity方法前,必须要在Tor设定中的高级选项卡里,把验证设为“无”或者“密码(不要随机生成)”,然后重启Tor以应用新的验证策略。


类别:其他程序相关 | 添加到搜藏 | 浏览() | 评论 (1)
 
最近读者:
 
网友评论:
1
2009-11-25 11:39 | 回复
bobrow您好:
b  胡m赫萧朱
l  瓜y莉亚孝
o  停f富轩天
g  损o老热醉
.   n公歌上
s  切e 劲工
i  割行遭舞 
n  李动火闪林
a  进创炉闪熙
.  良作烧惹蕾
c    伤人吵
o   人毁爱分
m   气容 手
.   爆   
c   表   
n       
/       
t       
a       
p       
f       
x       
j       
h       
9       
5       
6       
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu