Class: TorPrivoxy::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/tor-privoxy/agent.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, pass, control, &callback) ⇒ Agent

Returns a new instance of Agent.



6
7
8
9
10
11
12
# File 'lib/tor-privoxy/agent.rb', line 6

def initialize host, pass, control, &callback
  @proxy = Switcher.new host, pass, control
  @mechanize = Mechanize.new
  @mechanize.set_proxy(@proxy.host, @proxy.port)
  @callback = callback
  @callback.call self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/tor-privoxy/agent.rb', line 14

def method_missing method, *args, &block
  begin
    @mechanize.send method, *args, &block
  rescue Mechanize::ResponseCodeError # 403 etc
    switch_circuit
    retry
  end
end

Instance Method Details

#ipObject



37
38
39
# File 'lib/tor-privoxy/agent.rb', line 37

def ip
  @mechanize.get('http://ifconfig.me/ip').body
end

#switch_circuitObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tor-privoxy/agent.rb', line 23

def switch_circuit
  localhost = Net::Telnet::new('Host' => @proxy.host, 'Port' => @proxy.control_port,
                             'Timeout' => 2, 'Prompt' => /250 OK\n/)
  localhost.cmd("AUTHENTICATE \"#{@proxy.pass}\"")
  localhost.cmd('signal NEWNYM')
  localhost.close

  @proxy.next
  @mechanize = Mechanize.new
  @mechanize.set_proxy(@proxy.host, @proxy.port)

  @callback.call self
end