Ruby, Tor and Net::HTTP::Proxy

后端 未结 1 1699
一整个雨季
一整个雨季 2021-02-04 10:08

My apologies in advance if this is a noobish doubt: I want to use a proxy in my Ruby code to fetch a few web pages. And I want to be sneaky about it! So I am using Tor.

相关标签:
1条回答
  • 2021-02-04 10:41

    You are using HTTP proxy class, so you must provide IP of HTTP proxy. Tor Browser has not HTTP proxy bundled.

    So you can either install some proxy software e.g. Privoxy and configure it to use Tor's SOCKS:

    In config.txt forward-socks4a / 127.0.0.1:9050 .

    then use Privoxy's default listen-address in your script:

    proxy = Net::HTTP::Proxy('127.0.0.1',8118)
    

    or use SOCKSify. According to docs:

    require 'socksify/http'
    uri = URI.parse('http://rubyforge.org/')
    Net::HTTP.SOCKSProxy('127.0.0.1', 9050).start(uri.host, uri.port) do |http|
      http.get(uri.path)
    end
    

    No need for additional software..

    Third solution is to use SOCKSify as follows:

    $ socksify_ruby localhost 9050 script.rb
    

    which redirect all TCP connections of a Ruby script, which means you don't need to use any Proxy code at all.

    For clarification you have to understand that 127.0.0.1:9050 is Tor's SOCKS address and 127.0.0.1:8118 is address of Privoxy.

    0 讨论(0)
提交回复
热议问题