class RSocks::ConnectionHandler

Public Class Methods

new(config, *args) click to toggle source
Calls superclass method
# File lib/r_socks/connection_handler.rb, line 13
def initialize(config, *args)
  super(*args)
  @state_machine = RSocks::StateMachine.new
  @config = config
  @parser = create_proxy_parser
  @force_expire = 20 # seconds
end

Public Instance Methods

post_init() click to toggle source
# File lib/r_socks/connection_handler.rb, line 21
def post_init
  begin
    if @config.enable_ssl?
      start_tls(
        private_key_file: @config.ssl_private_key,
        cert_chain_file: @config.ssl_cert
      )
    end
    @port, @ip = Socket.unpack_sockaddr_in(get_peername)

    white_list = @config.forward_white_list
    if !white_list.empty? && !white_list.include?(@ip.to_s)
      raise Error, "#{@ip} not in white list"
    end

    @timer = EventMachine.add_timer(20) do
      self.close_connection(false)
      @timer = nil
    end
  rescue => e
    puts "post_init error: #{e.message}"
    close_connection
  end
end
proxy_target_unbound() click to toggle source
# File lib/r_socks/connection_handler.rb, line 102
def proxy_target_unbound
  close_connection
end
receive_data(data) click to toggle source
# File lib/r_socks/connection_handler.rb, line 46
def receive_data(data)

  return send_data(not_accept) if data.nil? || data == ''

  begin
    begin
      @addr, @port = @parser.call(data)
    rescue RSocks::HttpAuthFailed, RSocks::HttpNotSupport
      send_data(RSocks::HttpProxyResponseCodes::FAILED_AUTH)
      close_connection_after_writing
    rescue RSocks::NotSupport
      send_data(RSocks::FAILED_RESPONSE)
      close_connection_after_writing

    rescue RSocks::HealthChecking
      send_data(RSocks::HttpProxyResponseCodes::SUCCESS)
      close_connection_after_writing
    end

    return unless @state_machine.start?

    if @config.forward_server? && @config.proxy_type == :http
      @target = EventMachine.connect(@config.forward_addr,
                                     @config.forward_port,
                                     RSocks::TargetConnectionHandler,
                                     self,
                                     @config,
                                     data)
      @target.assign_user_and_password(@username, @password)
    end

    @username = @parser.username
    @password = @parser.password
    if @target.nil?
      @target = EventMachine.connect(@addr, @port, RSocks::TargetConnectionHandler, self, @config)
      @target.assign_user_and_password(@username, @password)
    end
  rescue => error
    puts "Error at #{@ip}:#{@port}, message: #{data}, error: #{error.message}"
    puts error.backtrace
  end
end
unbind() click to toggle source
# File lib/r_socks/connection_handler.rb, line 89
def unbind

  EventMachine.cancel_timer(@timer) if @timer

  stop_proxying

  @target.close_connection_after_writing if @target

  if @config.unbind_handler
    @config.unbind_handler.call(get_proxied_bytes, @username, @password)
  end
end

Private Instance Methods

create_proxy_parser() click to toggle source
# File lib/r_socks/connection_handler.rb, line 113
def create_proxy_parser
  if @config.proxy_type == :http
    return RSocks::HttpProxyParser.new(@state_machine, @config)
  end

  if @config.proxy_type == :socks5
    return RSocks::Socks5ProxyParser.new(@state_machine, @config, self)
  end
end
forward_request() click to toggle source
# File lib/r_socks/connection_handler.rb, line 108
def forward_request
  @port = @config.forward_port
  @addr = @config.forward_addr
end