Sha256: eae666120163024554ed900a17b73aa86d3b263d3cffd16b30ee7a94f4fe4f37

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 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
        Process.kill("INT", self.pid) unless self.pid.to_s.empty?
      end
    
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
browsed-0.2.9 lib/browsed/proxies/proxy_chain.rb
browsed-0.2.8 lib/browsed/proxies/proxy_chain.rb