-
Notifications
You must be signed in to change notification settings - Fork 5
/
tor.rb
63 lines (56 loc) · 1.72 KB
/
tor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env ruby
require 'socksify'
require 'mechanize'
require 'net/telnet'
class Tor
#debug
#Socksify::debug = true
def initialize(control_port='9051',socks_port='9050')
@tor_control_port = control_port
@tor_socks_port = socks_port
TCPSocket::socks_server = "127.0.0.1"
TCPSocket::socks_port = @tor_socks_port
end
def get_current_ip_address
# rubyforge_www = TCPSocket.new("rubyforge.org", 80)
a = Mechanize.new do |agent|
agent.user_agent_alias = 'Mac FireFox'
end
a.get('http://ifconfig.me/ip').body.chomp
rescue Exception => ex
puts "error getting ip: #{ex.to_s}"
return ""
end
def get_new_ip
puts "get new ip address"
old_ip_address = get_current_ip_address
tor_switch_endpoint
sleep 10 # wait for connection
new_ip_address = get_current_ip_address
if (old_ip_address != new_ip_address) # Compare your old ip with your current one
puts "ip changed from #{old_ip_address} to #{new_ip_address}"
return true
else
puts "ip same #{old_ip_address}"
return false
end
end
def tor_switch_endpoint
puts "tor_switch_endpoint.."
localhost = Net::Telnet::new("Host" => "localhost", "Port" => "#{@tor_control_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
end
#block to be executed though the proxy
def proxy_mechanize
yield
end
end
#start tor first
#tor --controlport 9051
t=Tor.new
t.proxy_mecahnize {
a = Mechanize.new
puts a.get('http://ifconfig.me/ip').body.chomp
}