Class: TorProtocol::Tor

Inherits:
Object
  • Object
show all
Defined in:
lib/instabot/tor_protocol.rb

Instance Method Summary collapse

Constructor Details

#initialize(control_port = '9051', socks_port = '9050') ⇒ Tor

debug Socksify::debug = true



7
8
9
10
11
12
13
14
# File 'lib/instabot/tor_protocol.rb', line 7

def initialize(control_port='9051',socks_port='9050')
  puts '[+] '.cyan + "[TOR] STARTED"
  @tor_control_port       = control_port
  @tor_socks_port         = socks_port
  TCPSocket::socks_server = "127.0.0.1"
  TCPSocket::socks_port   = @tor_socks_port
  puts '[+] '.cyan + "[TOR] current ip [#{current_ip.yellow}]"
end

Instance Method Details

#current_ipObject



16
17
18
19
20
21
22
23
24
# File 'lib/instabot/tor_protocol.rb', line 16

def current_ip
  a = Mechanize.new
  a.user_agent_alias = 'Mac Safari'
  return a.get('http://www.myexternalip.com/raw').body.chomp
rescue Exception => e
  puts "error getting ip: #{e.class} #{e.message}\n#{e.backtrace.inspect}".red
  # break
  # puts "An error occured ... #{e.class} #{e.message}\n#{e.backtrace.inspect}\n[ignored]"
end

#get_new_ipObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/instabot/tor_protocol.rb', line 26

def get_new_ip
  puts '[+] '.cyan + "[TOR] trying to get a new ip address.."
  old_ip_address = current_ip
  tor_switch_endpoint
  sleep 10
  new_ip_address = current_ip
  if (old_ip_address != new_ip_address)
    puts '[+] '.cyan + "[TOR] ip changed from [#{old_ip_address.yellow}] to [#{new_ip_address.yellow}]"
    return true
   else
    puts '[+] '.yellow + "[TOR] ip same [#{old_ip_address}]"
    get_new_ip
   end
end

#tor_switch_endpointObject



41
42
43
44
45
46
47
# File 'lib/instabot/tor_protocol.rb', line 41

def tor_switch_endpoint
  # puts "tor_switch_endpoint.."
  tor_protocol = Net::Telnet::new("Host" => "localhost", "Port" => "#{@tor_control_port}", "Timeout" => 10, "Prompt" => /250 OK\n/)
  tor_protocol.cmd('AUTHENTICATE ""') { |c| throw "Cannot authenticate to Tor" if c != "250 OK\n" }
  tor_protocol.cmd('signal NEWNYM') { |c| throw "Cannot switch Tor to new route" if c != "250 OK\n" }
  tor_protocol.close
end