Class: ProxyManager

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy/proxy_manager.rb

Constant Summary collapse

START_PORT =
5000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProxyManager

Returns a new instance of ProxyManager.



9
10
11
12
# File 'lib/proxy/proxy_manager.rb', line 9

def initialize
  @running_proxy_servers = {}
  @assigned_proxy_ports = []
end

Instance Attribute Details

#assigned_proxy_portsObject (readonly)

Returns the value of attribute assigned_proxy_ports.



7
8
9
# File 'lib/proxy/proxy_manager.rb', line 7

def assigned_proxy_ports
  @assigned_proxy_ports
end

#running_proxy_serversObject (readonly)

Returns the value of attribute running_proxy_servers.



7
8
9
# File 'lib/proxy/proxy_manager.rb', line 7

def running_proxy_servers
  @running_proxy_servers
end

Instance Method Details

#delete_proxiesObject



24
25
26
# File 'lib/proxy/proxy_manager.rb', line 24

def delete_proxies
  assigned_proxy_ports.clear
end

#find_proxy_portObject



55
56
57
58
# File 'lib/proxy/proxy_manager.rb', line 55

def find_proxy_port
  new_proxy_port = (assigned_proxy_ports.max || ProxyManager::START_PORT) + 1
  PortProber.above(new_proxy_port)
end

#get_proxy(port) ⇒ Object



14
15
16
# File 'lib/proxy/proxy_manager.rb', line 14

def get_proxy(port)
  running_proxy_servers[port]
end

#new_proxyObject



38
39
40
41
42
# File 'lib/proxy/proxy_manager.rb', line 38

def new_proxy
  port = find_proxy_port
  assigned_proxy_ports << port
  URI.parse("http://#{proxy_host_address}:#{port}")
end

#proxiesObject



28
29
30
# File 'lib/proxy/proxy_manager.rb', line 28

def proxies
  assigned_proxy_ports
end

#proxy_host_addressObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/proxy/proxy_manager.rb', line 44

def proxy_host_address
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true  # turn off reverse DNS resolution temporarily

  UDPSocket.open do |s|
    s.connect '64.233.187.99', 1
    s.addr.last
  end
ensure
  Socket.do_not_reverse_lookup = orig
end

#start_proxy(options) ⇒ Object



32
33
34
35
36
# File 'lib/proxy/proxy_manager.rb', line 32

def start_proxy(options)
  proxy_server = ProxyServer.new(options)
  proxy_server.start
  running_proxy_servers[options[:port]] = proxy_server
end

#stop_proxy(port) ⇒ Object



18
19
20
21
22
# File 'lib/proxy/proxy_manager.rb', line 18

def stop_proxy(port)
  proxy_server = running_proxy_servers[port]
  proxy_server.stop
  running_proxy_servers.delete proxy_server
end