我用的是tor privoxy红宝石。根据本页:https://github.com/pirj/tor-privoxy
我在ArchLinux安装中安装了“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一起工作并实现机械化?

最佳答案

我遇到了同样的问题,您可以在日志中看到您的authenticate命令被tor拒绝:
无法识别Socks版本65。(tor不是http代理。)
我设法用Socksify而不是tor privoxy向tor发送telnet命令。如果你用袜子,你就不需要Privoxy了。
下面是一个动态切换电路的工作示例:
首先开始指定密码、控制端口和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

关于ruby - 如何正确使用tor-privoxy Ruby gem?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24637016/

10-12 21:24