【问题标题】:How to properly use tor-privoxy Ruby gem?如何正确使用 tor-privoxy Ruby gem?
【发布时间】:2014-07-08 16:28:20
【问题描述】:

我正在使用 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 “Cannot authenticate to Tor”

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

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

我没有收到任何错误,这表明我能够连接到 Tor 网络。

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

【问题讨论】:

    标签: ruby linux web-scraping mechanize tor


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 2013-08-12
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-07
      相关资源
      最近更新 更多