为什么Perl可以允许使用TOR访问HTTP网站,但无法访问HTTPS网站?

6
我在使用Perl通过TOR访问HTTPS网站时遇到了困难,但对于HTTP网站则没有问题。
#!/usr/bin/perl
use strict;

use WWW::Mechanize;
use LWP::Protocol::socks;
use LWP::Protocol::https;
use utf8;

my $mech = WWW::Mechanize->new(timeout => 60*5);
$mech->proxy(['http', 'https'], 'socks://localhost:9150');
$mech->get("https://www.google.com");

我收到了错误信息:“在第10行GET https://www.google.com时发生错误:状态读取失败:坏文件描述符”,其中第10行是程序的最后一行。

在TOR浏览器中,我可以成功查看端口为9150的“https://www.google.com”。 我正在使用ActivePerl 5.16.2;Vadalia 0.2.21和Tor 0.2.3.25。 我有一台Windows机器,我的主要互联网浏览器是Mozilla。

我尝试使用以下命令安装软件包:

cpan LWP::UserAgent
ppm install LWP::Protocol::https
cpan LWP::Protocol::https
ppm install LWP::Protocol::socks
cpan LWP::Protocol::socks
ppm install Mozilla::CA
ppm install IO::Socket::SSL
ppm install Crypt::SSLeay
cpan Crypt::SSLeay

感谢您的帮助!请告知我是否需要提供更多信息。

你尝试过使用HTTP而不是SOCKS吗?例如:使用'ht tp://localhost:9150'代替'socks://localhost:9150'? - ugexe
是的,最终的结果(和错误)并没有什么不同。 - paso
你是用这个来查看错误吗?链接 - 在奇怪的错误下。代理日志告诉你什么? - Jim Black
你可以尝试设置 $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; - ddoxey
3个回答

2
以前,我发现使用WWW :: Curl :: Easy可以穿过Tor浏览https网站的方法,因为使用LWP时遇到了同样的问题。之后,我将所有HTML保存在文件中,并使用WWW :: Mechanzie或HTML :: TreeBuilder解析它们。
如果您想要与网站进行更多交互,例如发布表单等,则此解决方案可能更加繁琐,因为您需要与curl进行交互。
以下是一个示例代码:
``` package Curl; use warnings; use WWW::Curl::Easy; use WWW::UserAgent::Random;
my $curl = WWW::Curl::Easy->new; my $useragent = rand_ua("browsers"); my $host = 'localhost'; my $port = '9070';
my $timeout = '20'; my $connectTimeOut= '20';
&init;
sub get { my $url = shift;
$curl->setopt(CURLOPT_URL, $url); my $response_body; $curl->setopt(CURLOPT_WRITEDATA,\$response_body);
my $retcode = $curl->perform;
if ($retcode == 0) { print("Transfer went ok Http::Code = ".$curl->strerror($retcode)."\n"); my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE); # judge result and next action based on $response_code
return \$response_body; } else { # Error code, type of error, error message print("An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n"); return 0; }
}
sub init { #setejem el proxy $curl->setopt(CURLOPT_PROXY,"$host:".$port); $curl->setopt(CURLOPT_PROXYTYPE,CURLPROXY_SOCKS4);
#posem les altres dades $curl->setopt(CURLOPT_USERAGENT, $useragent); $curl->setopt(CURLOPT_CONNECTTIMEOUT, $connectTimeOut); $curl->setopt(CURLOPT_TIMEOUT, $timeout); $curl->setopt(CURLOPT_SSL_VERIFYPEER,0); $curl->setopt(CURLOPT_HEADER,0); } ```
希望这能帮助到您!

1

1
也许你正在使用的代理已经是HTTPS代理(即CONNECT代理)。在这种情况下,这应该可以正常工作(未经测试):
#!/usr/bin/perl
use strict;

use WWW::Mechanize;
use LWP::Protocol::socks;
use LWP::Protocol::https;
use utf8;

my $mech = WWW::Mechanize->new(timeout => 60*5);
$mech->proxy(['http'], 'socks://localhost:9150');
$mech->proxy(['https'], 'https://localhost:9150'); ### <-- make https go over https-connect proxy

$mech->get("https://www.google.com");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接