我试图通过Tor / Privoxy通过PHP连接到不同的网站。 PHP脚本和Tor / Privoxy在同一台服务器上运行。
Privoxy配置如下
forward-socks4a / 127.0.0.1:9050 .
forward-socks5 / 127.0.0.1:9050 .
confdir /etc/privoxy
logdir /var/log/privoxy
logfile logfile
actionsfile match-all.action
actionsfile default.action
actionsfile user.action
filterfile default.filter
debug 32
debug 128
debug 1024
debug 4096
debug 8192
user-manual /usr/share/doc/privoxy/user-manual
listen-address 127.0.0.1:8118
toggle 1
enable-remote-toggle 0
enable-edit-actions 0
enable-remote-http-toggle 0
buffer-limit 4096
Tors配置
SocksPort 9050
SocksPolicy accept 127.0.0.1
SocksPolicy reject *
Log warn syslog
RunAsDaemon 1
DataDirectory /var/lib/tor
我正在尝试使用我在PHP SSL stream_socket_client won't use created $context找到的脚本进行连接,我稍微修改了该脚本以满足我的需求
<?php
$desthost = "derdualstudent.de";
$port = 80;
$conflag = STREAM_CLIENT_CONNECT;
try
{
$socket = stream_socket_client( "tcp://127.0.0.1:8118", $errno, $errstr, 15, $conflag );
fwrite( $socket, pack( "C3", 0x05, 0x00, 0x00 ) );
$server_status = fread( $socket, 2048 );
if ( $server_status == pack( "C2", 0x05, 0x00 ) )
{
// Connection succeeded
}
else
{
die( "SOCKS Server does not support this version and/or authentication method of SOCKS.\r\n" );
}
fwrite( $socket, pack( "C5", 0x05, 0x01, 0x00, 0x03, strlen( $desthost ) ) . $desthost . pack( "n", $port ) );
$server_buffer = fread( $socket, 10 );
if ( ord( $server_buffer[0] ) == 5 && ord( $server_buffer[1] ) == 0 && ord( $server_buffer[2] ) == 0 )
{
// Connection succeeded
}
else
{
die( "The SOCKS server failed to connect to the specificed host and port. ( " . $desthost . ":" . $port . " )\r\n" );
}
stream_socket_enable_crypto( $socket, TRUE, STREAM_CRYPTO_METHOD_SSLv23_CLIENT );
}
catch ( Exception $e )
{
die( $e->getMessage() );
}
if ( $socket === FALSE )
{
die( "bad socket" );
}
fwrite( $socket, "GET /\n" );
echo fread( $socket, 10255 );
?>
脚本丢失,“SOCKS Server不支持此版本和/或SOCKS的身份验证方法。”。我尝试了身份验证(0x00和0x01都不起作用)。
还有一个我的同事在同一台服务器上写的脚本
<?php
$vOpts = array('http'=>array(
'method'=>"GET",
'request_fulluri'=>TRUE,
'proxy'=>'tcp://localhost:8118',
'header'=>"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE; rv:1.8) Gecko/20051111 Firefox/16.0\r\n"."Accept-language: de\r\n"."\r\n"
));
$vContext = stream_context_create($vOpts);
$vFile = file_get_contents("http://www.derdualstudent.de?tor=1",FALSE,$vContext);
?>
这会产生错误
PHP Warning: file_get_contents(http://www.derdualstudent.de?tor=1): failed to open stream: HTTP request failed! HTTP/1.0 503 Forwarding failure
in /home/pi/php_scripts/temp.php on line 9
Tor和Privoxy的日志中没有任何有用的信息。