Tor 不是 HTTP 代理

Tor is not an HTTP Proxy

我正在使用云服务器 Ubuntu 12.04 作为 tor 代理服务器用于报废目的。我现在面临的问题是显示错误

HTTP/1.0 501 Tor 不是 HTTP 代理内容类型:text/html;字符集=iso-8859-1

$url = 'http://whatismyipaddress.com';
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

$ch = curl_init('http://whatismyipaddress.com'); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); 
curl_setopt($ch, CURLOPT_PROXY, 'https://127.0.01:9050/'); 
curl_exec($ch); 
curl_close($ch);

$result=curl_exec($ch);

需要帮助我缺少的东西。 我已经尝试使用

curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); 
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);

请求正在加载,会话过期但没有结果。

没错,Tor 不是 HTTP 代理,而是 SOCKS v5 代理。

根据您的 cURL 选项 CURLOPT_HTTPPROXYTUNNEL,您告诉 cURL 尝试错误地使用 Tor 的代理(作为 HTTP 代理)。

正确的方法是取消代理隧道选项,只设置代理和 SOCKS 代理类型:

$proxy = '127.0.0.1:9050';  // no https:// or http://; just host:port
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);

如果您没有 PHP 5.5.23 或更高版本(引入 CURLPROXY_SOCKS5_HOSTNAME),您可以使用 curl_setopt($ch, CURLOPT_PROXYTYPE, 7);

如果编译的 cURL 版本 PHP 低于 7.18.0,它不支持带有主机名查找的 SOCSK5,因此您必须退回到 CURLPROXY_SOCKS5 并知道您的 DNS 查询将不会通过 Tor 并可能被暴露。

旁注:我编写了一个名为 TorUtils which provides a number of classes for interacting with Tor in PHP. One class is provides is the TorCurlWrapper which abstracts the logic above and forces cURL to use Tor's SOCKS proxy correctly. There is a usage example here 的 PHP 库。您可以使用 composer require dapphp/torutils 或通过下载包并将其添加到代码中来安装库。