Sha256: 3b35df4a0288086a73a8060e042fac75066a7e7d52185fac963541612c59bf5a

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

module Browsed
  module Proxies
    class ProxyChain
      attr_accessor :script_path, :instance_id, :command, :pid, :generated_proxy_url
    
      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
    
      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
    
      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
    
      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
    
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
browsed-0.3.0 lib/browsed/proxies/proxy_chain.rb