如何正确使用privoxy Ruby宝石?

如何正确使用privoxy Ruby宝石?,ruby,linux,web-scraping,mechanize,tor,Ruby,Linux,Web Scraping,Mechanize,Tor,我正在使用tor privoxy Ruby gem。根据本页: 我在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

我正在使用tor privoxy Ruby gem。根据本页: 我在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.

我的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_端点方法时,我得到一个错误:ArgumentError:uncaught throw无法对Tor进行身份验证

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

torify wget -qO- https://check.torproject.org/ | grep -i congratulations
我没有收到任何错误,这表明我能够连接到Tor网络


如何使Tor Privoxy与Ruby和Mechanize一起工作?

我遇到了同样的问题,您可以在日志中看到您的authenticate命令被Tor拒绝:

无法识别Socks版本65。Tor不是http代理

我设法用telnet命令而不是Tor privoxy发送到Tor。如果你使用socksify,你就不再需要privoxy了

下面是一个动态切换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

我遇到了同样的问题,您可以在日志中看到您的authenticate命令被tor拒绝:

无法识别Socks版本65。Tor不是http代理

我设法用telnet命令而不是Tor privoxy发送到Tor。如果你使用socksify,你就不再需要privoxy了

下面是一个动态切换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