Class: ProxyChainRb::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_chain_rb/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_id = nil) ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
10
# File 'lib/proxy_chain_rb/server.rb', line 5

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

Instance Attribute Details

#generated_proxy_urlObject

Returns the value of attribute generated_proxy_url.



3
4
5
# File 'lib/proxy_chain_rb/server.rb', line 3

def generated_proxy_url
  @generated_proxy_url
end

#instance_idObject

Returns the value of attribute instance_id.



3
4
5
# File 'lib/proxy_chain_rb/server.rb', line 3

def instance_id
  @instance_id
end

#pidObject

Returns the value of attribute pid.



3
4
5
# File 'lib/proxy_chain_rb/server.rb', line 3

def pid
  @pid
end

#pid_commandObject

Returns the value of attribute pid_command.



3
4
5
# File 'lib/proxy_chain_rb/server.rb', line 3

def pid_command
  @pid_command
end

#script_pathObject

Returns the value of attribute script_path.



3
4
5
# File 'lib/proxy_chain_rb/server.rb', line 3

def script_path
  @script_path
end

Instance Method Details

#identify_pidObject



27
28
29
30
31
32
# File 'lib/proxy_chain_rb/server.rb', line 27

def identify_pid
  process                   =   `#{self.pid_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(proxy_url, tag: "default", started_at: Time.now, wait: 3) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/proxy_chain_rb/server.rb', line 12

def start(proxy_url, tag: "default", started_at: Time.now, wait: 3)
  command                   =   "((node #{self.script_path} \"#{proxy_url}\" proxy-chain-instance-id-#{self.instance_id} tag-#{tag} started-#{started_at.to_i} &)&)"
  
  IO.popen(command) 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

  return self.generated_proxy_url
end

#stop(retries: 3) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/proxy_chain_rb/server.rb', line 34

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