Class: RSocks::ConnectionHandler

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/r_socks/connection_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, *args) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



13
14
15
16
17
18
19
# 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

Instance Method Details

#post_initObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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_unboundObject



102
103
104
# File 'lib/r_socks/connection_handler.rb', line 102

def proxy_target_unbound
  close_connection
end

#receive_data(data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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

#unbindObject



89
90
91
92
93
94
95
96
97
98
99
100
# 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