2016-09-12 16 views
8

Rails 4.2.7を使用していて、いくつかのTorのgemがインストールされています。 Ruby/Torを使用してIPアドレスを変更するにはどうすればよいですか?

gem 'tor' 
gem 'tor_requests' 
gem 'tor-privoxy' 
gem 'net-telnet' 

は、私は(マックエルキャピタン上で実行されている)私のTorのブラウザを起動し、私は定期的に(すべての20の要求は)私のTORのWebリクエストの発信元のIPアドレスを変更したいです。だから私はこれを試しました

agent = TorPrivoxy::Agent.new '127.0.0.1', '', {8118 => 9151} do |agent| 
    sleep 1 
    puts "New IP is #{agent.ip}" 
end 

しかし、これは以下のエラーになります。私は上記の作業をするために他にどのように設定する必要があるのか​​混乱しています。

Error during processing: Failed to open TCP connection to 127.0.0.1:8118 (general SOCKS server failure) 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:882:in `rescue in block in connect' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:879:in `block in connect' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/timeout.rb:91:in `block in timeout' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/timeout.rb:101:in `timeout' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:878:in `connect' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:863:in `do_start' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:858:in `start' 
/Users/davea/.rvm/gems/ruby-2.3.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:700:in `start' 
/Users/davea/.rvm/gems/ruby-2.3.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:631:in `connection_for' 
/Users/davea/.rvm/gems/ruby-2.3.0/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:994:in `request' 
/Users/davea/.rvm/gems/ruby-2.3.0/gems/mechanize-2.7.5/lib/mechanize/http/agent.rb:274:in `fetch' 
/Users/davea/.rvm/gems/ruby-2.3.0/gems/mechanize-2.7.5/lib/mechanize.rb:464:in `get' 
/Users/davea/.rvm/gems/ruby-2.3.0/gems/tor-privoxy-0.1.1/lib/tor-privoxy/agent.rb:38:in `ip' 
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:57:in `block in get_content' 
/Users/davea/.rvm/gems/ruby-2.3.0/gems/tor-privoxy-0.1.1/lib/tor-privoxy/agent.rb:11:in `initialize' 
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:55:in `new' 
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:55:in `rescue in get_content' 
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:50:in `get_content' 
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:14:in `get_url' 
/Users/davea/Documents/workspace/myproject/app/services/onlinerr_race_finder_service.rb:41:in `get_race_list' 
/Users/davea/Documents/workspace/myproject/app/services/abstract_race_finder_service.rb:26:in `process_data' 
/Users/davea/Documents/workspace/myproject/app/services/run_crawlers_service.rb:18:in `block in run_all_crawlers' 
/Users/davea/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each' 
/Users/davea/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/relation/delegation.rb:46:in `each' 
/Users/davea/Documents/workspace/myproject/app/services/run_crawlers_service.rb:5:in `run_all_crawlers' 
(irb):2:in `irb_binding' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/workspace.rb:87:in `eval' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/workspace.rb:87:in `evaluate' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/context.rb:380:in `evaluate' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:489:in `block (2 levels) in eval_input' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:623:in `signal_status' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:486:in `block in eval_input' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/ruby-lex.rb:246:in `block (2 levels) in each_top_level_statement' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/ruby-lex.rb:232:in `loop' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/ruby-lex.rb:232:in `block in each_top_level_statement' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/ruby-lex.rb:231:in `catch' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb/ruby-lex.rb:231:in `each_top_level_statement' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:485:in `eval_input' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:395:in `block in start' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:394:in `catch' 
/Users/davea/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/irb.rb:394:in `start' 

Ruby/Torを使用してプログラムで強制的にIPアドレスを変更するにはどうすればよいですか?

編集:ここではは私のヘルパーファイルに含まれて何であり、以下に

require 'rubygems' 
$:.unshift "./tor/lib" 
require 'tor' 
… 
    cookie_file = '/Users/davea/Library/Application Support/TorBrowser-Data/Tor/control_auth_cookie' 
    file = File.open(cookie_file, 'rb') 
    cookie = file.read # read contents of auth cookie to string 
    file.close 

    Tor::Controller.connect(:port => 9150, :cookie => cookie) do |tor| 
     tor.signal('NEWNYM') # send NEWNYM signal (gets new IP) 
    end 

レールコンソールにつながるエラーを結果エラーです...

Error during processing: undefined method `signal' for #<Tor::Controller:0x007fe044b1e550> 
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:87:in `block in get_content' 
/Users/davea/.rvm/gems/ruby-2.3.0/gems/tor-0.1.2/lib/tor/control.rb:38:in `connect' 
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:86:in `rescue in get_content' 

編集2 :

これを私のGemfileファイルに追加した後

gem 'tor', :git => 'https://github.com/dryruby/tor.rb.git' 

私はTorの中にあなたのIPを変更するには...

Error during processing: end of file reached 
/Users/davea/.rvm/gems/ruby-2.3.0/bundler/gems/tor.rb-08e589d17196/lib/tor/control.rb:301:in `readline' 
/Users/davea/.rvm/gems/ruby-2.3.0/bundler/gems/tor.rb-08e589d17196/lib/tor/control.rb:301:in `read_reply' 
/Users/davea/.rvm/gems/ruby-2.3.0/bundler/gems/tor.rb-08e589d17196/lib/tor/control.rb:194:in `authenticate' 
/Users/davea/.rvm/gems/ruby-2.3.0/bundler/gems/tor.rb-08e589d17196/lib/tor/control.rb:282:in `send_command' 
/Users/davea/.rvm/gems/ruby-2.3.0/bundler/gems/tor.rb-08e589d17196/lib/tor/control.rb:269:in `signal' 
/Users/davea/Documents/workspace/myproject/app/helpers/webpage_helper.rb:90:in `block in get_content' 

答えて

3

を上記のコードを実行し、現在のエラーを持って、あなたは(コントローラに直接接続する必要があり、それはtelnetのようなラインベースのコマンドを使用しています/応答protocol

Privoxyを介して接続しようとしているようですが、これは不要です。コントローラはデフォルトでローカル接続のみを許可します。

TorブラウザのTor config enforces認証では、オプションを変更せずにcookieメソッドを使用します。torrc接続するauth cookieの内容を読み取る必要があります。

私は、次のコードを使用してIPアドレスを変更することができました:

require 'rubygems' 
$:.unshift "./tor/lib" 
require 'tor' 

# the cookie file contains a "password" for authentication, 32 random bytes 
cookie_file = '/home/me/tor-browser_en-US/Browser/TorBrowser/Data/Tor/control_auth_cookie' 
file = File.open(cookie_file, 'rb') 
cookie = file.read # read contents of auth cookie to string 
file.close 

Tor::Controller.connect(:port => 9151, :cookie => cookie) do |tor| 
    p tor.signal('NEWNYM') # send NEWNYM signal (gets new IP) 
end 

tor.signal NEWNYMコマンドが成功した場合、「250 OK」をお読みください文字列を返します。これはあなたには問題ではないはずの(10秒ごとのような)あまり頻繁に実行されないようにする内部レートの制限があります。

編集:このGemの0.1.2(現リリース)には、マスターブランチのsignalメソッドが含まれていません。信号を呼び出す代わりに、最新のコードを使用するか、their codeを複製する必要があります。

+0

私が言及したように、私はMac El Capitanを使用していますので、 "cookie_file ="のために何を入れる必要がありますか?自分のマシンのどこにでも "control_auth_cookie"ファイルがありません。 – Dave

+0

Torブラウザが他のすべてのネットワークデータと状態ファイルと共に作成するTorデータディレクトリ内にある必要があります。 – drew010

+0

クール、私はそれを見つけた。あなたのコードを実行することに基づいて私の答えを更新しました(エラーは "処理中のエラーです:#のための未定義メソッド' signal ") – Dave

関連する問題