Class: Browsed::Proxies::ProxyChain

Inherits:
Object
  • Object
show all
Defined in:
lib/browsed/proxies/proxy_chain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_id = nil) ⇒ ProxyChain

Returns a new instance of ProxyChain.



6
7
8
9
10
11
# File 'lib/browsed/proxies/proxy_chain.rb', line 6

def initialize(instance_id = nil)
  self.script_path          =   File.expand_path("../proxy-chain-server/server.js", __FILE__)
  self.instance_id          =   instance_id.to_s.empty? ? SecureRandom.hex : instance_id
  self.command              =   "ps aux | awk '/proxy-chain-instance-id-#{instance_id}/'"
  self.generated_proxy_url  =   nil
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



4
5
6
# File 'lib/browsed/proxies/proxy_chain.rb', line 4

def command
  @command
end

#generated_proxy_urlObject

Returns the value of attribute generated_proxy_url.



4
5
6
# File 'lib/browsed/proxies/proxy_chain.rb', line 4

def generated_proxy_url
  @generated_proxy_url
end

#instance_idObject

Returns the value of attribute instance_id.



4
5
6
# File 'lib/browsed/proxies/proxy_chain.rb', line 4

def instance_id
  @instance_id
end

#pidObject

Returns the value of attribute pid.



4
5
6
# File 'lib/browsed/proxies/proxy_chain.rb', line 4

def pid
  @pid
end

#script_pathObject

Returns the value of attribute script_path.



4
5
6
# File 'lib/browsed/proxies/proxy_chain.rb', line 4

def script_path
  @script_path
end

Instance Method Details

#identify_pidObject



26
27
28
29
30
31
# File 'lib/browsed/proxies/proxy_chain.rb', line 26

def identify_pid
  process                   =   `#{self.command}`.split("\n")&.select { |p| p =~ /proxy-chain-instance-id-#{self.instance_id}$/i }&.first
  parts                     =   process&.split(' ')
  pid                       =   parts && parts.any? ? parts[1] : nil
  self.pid                  =   !pid.to_s.empty? ? pid.to_i : nil
end

#start_server(proxy_url, wait: 3) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/browsed/proxies/proxy_chain.rb', line 13

def start_server(proxy_url, wait: 3)
  IO.popen("((node #{self.script_path} \"#{proxy_url}\" proxy-chain-instance-id-#{instance_id} &)&)") do |io|
    self.generated_proxy_url   =   io.gets&.strip
  end
  
  # Wait a couple of seconds to let proxy-chain initiate the connection with the target proxy server
  sleep wait
  
  identify_pid

  self.generated_proxy_url
end

#stop_server(retries: 3) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/browsed/proxies/proxy_chain.rb', line 33

def stop_server(retries: 3)
  begin
    Process.kill("INT", self.pid) unless self.pid.to_s.empty?
  
  rescue Errno::ESRCH => e
    identify_pid
    retries         -= 1
    retry if retries > 0
  end
end