如何在PHP中使用cURL连接到Tor隐藏服务?

本文翻译自:How can I connect to a Tor hidden service using cURL in PHP?

I'm trying to connect to a Tor hidden service using the following PHP code: 我正在尝试使用以下PHP代码连接到Tor隐藏服务:

$url = 'http://jhiwjjlqpyawmpjx.onion/'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1:9050/");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
$output = curl_exec($ch);
$curl_error = curl_error($ch);
curl_close($ch);

print_r($output);
print_r($curl_error);

When I run it, I get the following error: 运行它时,出现以下错误:

Couldn't resolve host name 无法解析主机名

However, when I run the following command from my command line in Ubuntu: 但是,当我从Ubuntu中的命令行运行以下命令时:

curl -v --socks5-hostname localhost:9050 http://jhiwjjlqpyawmpjx.onion

I get a response as expected 我得到预期的回应

The PHP cURL documentations says this: PHP cURL文档说:

--socks5-hostname
Use  the  specified  SOCKS5 proxy (and let the proxy resolve the host name).

I believe the reason it works from the command line is because Tor (the proxy) is resolving the .onion hostname, which it recognizes. 我相信它可以从命令行运行的原因是因为Tor(代理)正在解析它可以识别的.onion主机名。 When running the PHP code above, my guess is that cURL or PHP is trying to resolve the .onion hostname and doesn't recognize it. 当运行上面的PHP代码时,我的猜测是cURL或PHP试图解析.onion主机名,但无法识别它。 I've searched for a way to tell cURL/PHP to let the proxy resolve the hostname, but I can't find a way. 我已经在寻找一种方法来告诉cURL / PHP让代理解析主机名,但是我找不到方法。

There is a very similar Stack Overflow question, cURL request using socks5 proxy fails when using PHP, but it works through the command line . 有一个非常相似的Stack Overflow问题, 使用PHP时使用socks5代理进行的cURL请求失败,但可通过命令行运行


#1楼

参考:https://stackoom.com/question/12o1V/如何在PHP中使用cURL连接到Tor隐藏服务


#2楼

Try to add this: 尝试添加以下内容:

curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); 

#3楼

I use Privoxy and cURL to scrape Tor pages: 我使用Privoxy和cURL抓取Tor页面:

<?php
    $ch = curl_init('http://jhiwjjlqpyawmpjx.onion'); // Tormail URL
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    curl_setopt($ch, CURLOPT_PROXY, "localhost:8118"); // Default privoxy port
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
    curl_exec($ch);
    curl_close($ch);
?>

After installing Privoxy you need to add this line to the configuration file ( /etc/privoxy/config ). 安装Privoxy之后,您需要将此行添加到配置文件( /etc/privoxy/config )中。 Note the space and '.' 注意空格和“。” a the end of line. 行尾。

forward-socks4a / localhost:9050 .

Then restart Privoxy. 然后重新启动Privoxy。

/etc/init.d/privoxy restart

#4楼

You need to set option CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME , which sadly wasn't defined in old PHP versions, circa pre-5.6 ; 您需要将选项CURLOPT_PROXYTYPECURLPROXY_SOCKS5_HOSTNAME ,遗憾的是,旧的PHP版本(约5.6之前)中并未定义该选项; if you have earlier in but you can explicitly use its value, which is equal to 7 : 如果您拥有更早的版本,但是可以显式使用其值,该值等于7

curl_setopt($ch, CURLOPT_PROXYTYPE, 7);

#5楼

TL;DR: Set CURLOPT_PROXYTYPE to use CURLPROXY_SOCKS5_HOSTNAME if you have a modern PHP, the value 7 otherwise, and/or correct the CURLOPT_PROXY value. TL; DR:如果您拥有现代PHP,则将CURLOPT_PROXYTYPE为使用CURLPROXY_SOCKS5_HOSTNAME否则CURLOPT_PROXYTYPE7 ,和/或更正CURLOPT_PROXY值。

As you correctly deduced, you cannot resolve .onion domains via the normal DNS system, because this is a reserved top-level domain specifically for use by Tor and such domains by design have no IP addresses to map to. 正确推断出,您无法通过常规DNS系统解析.onion域,因为这是专门供Tor使用的保留的顶级域,并且此类域在设计上没有IP地址可映射到。

Using CURLPROXY_SOCKS5 will direct the cURL command to send its traffic to the proxy, but will not do the same for domain name resolution. 使用CURLPROXY_SOCKS5将指示cURL命令将其流量发送到代理,但对于域名解析则不会这样做。 The DNS requests, which are emitted before cURL attempts to establish the actual connection with the Onion site, will still be sent to the system's normal DNS resolver. cURL尝试与Onion站点建立实际连接之前发出的DNS请求仍将发送到系统的常规DNS解析器。 These DNS requests will surely fail, because the system's normal DNS resolver will not know what to do with a .onion address unless it, too, is specifically forwarding such queries to Tor. 这些DNS请求肯定会失败,因为系统的常规DNS解析器将不知道如何处理.onion地址,除非它也专门将此类查询转发给Tor。

Instead of CURLPROXY_SOCKS5 , you must use CURLPROXY_SOCKS5_HOSTNAME . 取而代之的CURLPROXY_SOCKS5 ,你必须使用CURLPROXY_SOCKS5_HOSTNAME Alternatively, you can also use CURLPROXY_SOCKS4A , but SOCKS5 is much preferred. 另外,您也可以使用CURLPROXY_SOCKS4A ,但是首选SOCKS5。 Either of these proxy types informs cURL to perform both its DNS lookups and its actual data transfer via the proxy. 这些代理类型中的任何一种都通知cURL通过代理执行其DNS查找和实际数据传输。 This is required to successfully resolve any .onion domain. 这是成功解析任何.onion域所必需的。

There are also two additional errors in the code in the original question that have yet to be corrected by previous commenters. 原始问题中的代码中还有两个其他错误,以前的评论者尚未纠正。 These are: 这些是:

  • Missing semicolon at end of line 1. 第1行末尾缺少分号。
  • The proxy address value is set to an HTTP URL, but its type is SOCKS; 代理地址值设置为HTTP URL,但其类型为SOCKS。 these are incompatible. 这些是不兼容的。 For SOCKS proxies, the value must be an IP or domain name and port number combination without a scheme/protocol/prefix. 对于SOCKS代理,该值必须是IP或域名和端口号的组合,且不带方案/协议/前缀。

Here is the correct code in full, with comments to indicate the changes. 这是完整的正确代码,带有注释以指示所做的更改。

<?php
$url = 'http://jhiwjjlqpyawmpjx.onion/'; // Note the addition of a semicolon.
$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"); // Note the address here is just `IP:port`, not an HTTP URL.
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME); // Note use of `CURLPROXY_SOCKS5_HOSTNAME`.
$output = curl_exec($ch);
$curl_error = curl_error($ch);
curl_close($ch);

print_r($output);
print_r($curl_error);

You can also omit setting CURLOPT_PROXYTYPE entirely by changing the CURLOPT_PROXY value to include the socks5h:// prefix: 您还可以通过更改CURLOPT_PROXY值以包含socks5h://前缀来完全省略设置CURLOPT_PROXYTYPEsocks5h://

// Note no trailing slash, as this is a SOCKS address, not an HTTP URL.
curl_setopt(CURLOPT_PROXY, 'socks5h://127.0.0.1:9050');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值