【发布时间】:2017-09-09 08:43:16
【问题描述】:
我正在尝试使用 EasyBitcoin-PHP 的修改版本连接到比特币节点。以下是我为使其在 TOR 上工作而进行的修改:
我为 TOR 代理创建了新字段:code-listing 1.1
...
public $response;//was there already
// TOR and other SOCK5 proxies
private $using_proxy; #new
private $proxy_host; #new
private $proxy_port; #new
private $id = 0;//was there already
...
然后我创建了两个新方法:code-listing 1.2
...
/**
* @param string $host
* @param int $port
*/
public function set_proxy($host, $port)
{
$this->proxy_host = $host;
$this->proxy_port = $port;
}
/**
* @param boolean|TRUE $proxy_usage
*/
public function use_proxy($proxy_usage = TRUE)
{
$this->using_proxy = $proxy_usage;
}
...
最后我在 __call() 方法中添加了几行代码:code-listing 1.3
// Build the cURL session
$curl = curl_init("{$this->proto}://{$this->host}:{$this->port}/{$this->url}");
$options = array(
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->username . ':' . $this->password,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $request
);
//What Follows is new!!!
if($this->using_proxy){
$options[CURLOPT_PROXY] = $this->proxy_host;
$options[CURLOPT_PROXYPORT] = $this->proxy_port;
$options[CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS5_HOSTNAME;
}
我创建的字段和方法,以及我在 __call() 方法中添加的额外行与我在其他 TOR PHP/cURL 项目中使用的相同(当我希望 PHP/cURL 通过 TOR 连接到站点时)他们工作得很好。
然后我安装并运行 TOR Expert Bundle(在 Windows 上)并使用以下脚本从比特币节点获取信息。
代码清单 2
<?php
require_once('easybitcoin.php');
$bitcoin = new Bitcoin('username','password','6cmgzwu3x57dr6z7.onion');
$bitcoin->set_proxy("127.0.0.1", "9050");
$bitcoin->use_proxy();
if($bitcoin->getinfo()){
} else {
var_dump($bitcoin->status);
var_dump($bitcoin->error);
}
这是我在命令提示符下运行脚本时得到的结果:
int(0)
string(50) "Can't complete SOCKS5 connection to 0.0.0.0:0. (5)"
有时我会得到这个:
int(0)
string(50) "Can't complete SOCKS5 connection to 0.0.0.0:0. (1)"
【问题讨论】: