如何正确使用tor-privoxy Ruby gem?

时间:2014-07-08 16:28:20

标签: ruby linux web-scraping mechanize tor

我正在使用tor-privoxy Ruby gem。根据此页面:https://github.com/pirj/tor-privoxy 我在Arch Linux安装上安装了“tor”和“privoxy”软件包。我发出命令:

sudo systemctl start privoxy.service
sudo systemctl start tor.service

服务状态,“systemctl status privoxy.service”和“systemctl status tor.service”:

● tor.service - Anonymizing Overlay Network
   Loaded: loaded (/usr/lib/systemd/system/tor.service; enabled)
   Active: active (running) since Thu 2014-06-26 16:27:44 CEST; 1 weeks 5 days ago
 Main PID: 454 (tor)
   CGroup: /system.slice/tor.service
           └─454 /usr/bin/tor -f /etc/tor/torrc

Jul 08 16:28:28 bridgelinux Tor[454]: Application request when we haven't used client functionality late...gain.
Jul 08 16:28:40 bridgelinux Tor[454]: We now have enough directory information to build circuits.
Jul 08 16:28:41 bridgelinux Tor[454]: Tor has successfully opened a circuit. Looks like client functiona...king.
Jul 08 17:20:05 bridgelinux Tor[454]: Socks version 65 not recognized. (Tor is not an http proxy.)
Jul 08 17:20:05 bridgelinux Tor[454]: Fetching socks handshake failed. Closing.
Jul 08 18:01:25 bridgelinux Tor[454]: Socks version 65 not recognized. (Tor is not an http proxy.)
Jul 08 18:01:25 bridgelinux Tor[454]: Fetching socks handshake failed. Closing.
Jul 08 18:10:04 bridgelinux systemd[1]: Started Anonymizing Overlay Network.
Jul 08 18:10:13 bridgelinux systemd[1]: Started Anonymizing Overlay Network.
Jul 08 18:14:34 bridgelinux systemd[1]: Started Anonymizing Overlay Network.
Hint: Some lines were ellipsized, use -l to show in full.

● privoxy.service - Privoxy Web Proxy With Advanced Filtering Capabilities
   Loaded: loaded (/usr/lib/systemd/system/privoxy.service; disabled)
   Active: active (running) since Tue 2014-07-08 16:09:16 CEST; 2h 8min ago
  Process: 8554 ExecStart=/usr/bin/privoxy --pidfile /run/privoxy.pid --user privoxy.privoxy /etc/privoxy/config (code=exited, status=0/SUCCESS)
 Main PID: 8555 (privoxy)
   CGroup: /system.slice/privoxy.service
           └─8555 /usr/bin/privoxy --pidfile /run/privoxy.pid --user privoxy.privoxy /etc/privoxy/config

Jul 08 16:09:16 bridgelinux systemd[1]: Started Privoxy Web Proxy With Advanced Filtering Capabilities.
Jul 08 18:17:55 bridgelinux systemd[1]: Started Privoxy Web Proxy With Advanced Filtering Capabilities.

我的Ruby脚本如下:

require 'mechanize'
require 'tor-privoxy'
require 'net/telnet'

def tor
  privoxy_agent ||= TorPrivoxy::Agent.new '127.0.0.1', '', {8118 => 9050} do |agent|
  sleep 20
   puts "New IP is #{agent.ip}"
  end
  return privoxy_agent
end

def switch_endpoint
  localhost = Net::Telnet::new("Host" => "localhost", "Port" => "9050", "Timeout" => 10, "Prompt" => /250 OK\n/)
  localhost.cmd('AUTHENTICATE ""') { |c| print c; throw "Cannot authenticate to Tor" if c != "250 OK\n" }
  localhost.cmd('signal NEWNYM') { |c| print c; throw "Cannot switch Tor to new route" if c != "250 OK\n" }
  localhost.close
end    

agent=tor

它表明我的IP地址仍然是原始地址。当我尝试调用“switch_endpoint”方法时,我收到一个错误:“ArgumentError:uncaught throw”无法验证Tor“

但是当我在bash提示符下发出此命令时:

torify wget -qO- https://check.torproject.org/ | grep -i congratulations

我没有错误,它表明我能够连接到Tor网络。

如何使Tor-Privoxy与Ruby和Mechanize一起使用?

1 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,你可以在日志中看到你的身份验证命令被tor拒绝了:

  

袜子版本65无法识别。 (Tor不是http代理。)

我设法使用Socksify而不是tor-privoxy将telnet命令发送给Tor。如果你使用socksify,你就不再需要私密了。

以下是动态切换Tor电路的工作示例:

首先启动Tor指定密码,控制端口和socks端口:

tor --CookieAuthentication 0 --HashedControlPassword "" --ControlPort 9050 --SocksPort 50001

然后你可以在ruby中试试这个:

require 'net/telnet'
require 'socksify'
require 'mechanize'

original_ip = Mechanize.new.get("http://bot.whatismyipaddress.com").content
puts "original IP is : #{original_ip}"

# socksify will forward traffic to Tor so you dont need to set a proxy for Mechanize from there
TCPSocket::socks_server = "127.0.0.1"
TCPSocket::socks_port = "50001"
tor_port = 9050

2.times do
  #Switch IP
  localhost = Net::Telnet::new("Host" => "localhost", "Port" => "#{tor_port}", "Timeout" => 10, "Prompt" => /250 OK\n/)
  localhost.cmd('AUTHENTICATE ""') { |c| print c; throw "Cannot authenticate to Tor" if c != "250 OK\n" }
  localhost.cmd('signal NEWNYM') { |c| print c; throw "Cannot switch Tor to new route" if c != "250 OK\n" }
  localhost.close      
  sleep 5

  new_ip = Mechanize.new.get("http://bot.whatismyipaddress.com").content
  puts "new IP is #{new_ip}"
end