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



23
24
25
26
27
28
# File 'lib/browsed/proxies/proxy_chain.rb', line 23

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) ⇒ Object



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

def start_server(proxy_url)
  IO.popen("((node #{self.script_path} \"#{proxy_url}\" proxy-chain-instance-id-#{instance_id} &)&)") do |io|
    self.generated_proxy_url   =   io.gets&.strip
  end

  identify_pid

  self.generated_proxy_url
end

#stop_serverObject



30
31
32
# File 'lib/browsed/proxies/proxy_chain.rb', line 30

def stop_server
  Process.kill("INT", self.pid) unless self.pid.to_s.empty?
end