Class: RProxy::ConnectionHandler

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

Instance Method Summary collapse

Constructor Details

#initialize(config, cache_pool) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/r_proxy/connection_handler.rb', line 3

def initialize(config, cache_pool)
  @config = config
  @logger = @config.logger
  @redis = RProxy::RedisService.instance(@config.redis_url)
  @disable_auth = @config.disable_auth
  @disable_unbind_cb = @config.disable_unbind_cb
  @buffer_size = @config.proxy_buffer
  @callback_url = @config.callback_url
  @username = nil
  @password = nil
  @target_connection = nil
  @cache_pool = cache_pool
  @usage_manager = RProxy::UsageManager.new(config, @cache_pool, @redis)
  @http_parser = HttpProxyParser.new(@usage_manager)
  @enable_force_quit = config.enable_force_quit
end

Instance Method Details

#post_initObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/r_proxy/connection_handler.rb', line 20

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)

    if @enable_force_quit
      @timer = EventMachine.add_timer(20) do
        self.close_connection(false)
        @timer = nil
      end
    end
  rescue => e
    if @logger
      @logger.error("id:#{@ip}, #{e.message}")
    end
    close_connection
  end
end

#proxy_target_unboundObject



76
77
78
# File 'lib/r_proxy/connection_handler.rb', line 76

def proxy_target_unbound
  close_connection
end

#receive_data(data) ⇒ Object



44
45
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
# File 'lib/r_proxy/connection_handler.rb', line 44

def receive_data(data)
  begin
    target_host, target_port, _= @http_parser.parse(data, !@disable_auth)

    @target_connection = EventMachine.
      connect(target_host,
              target_port,
              RProxy::TargetConnection,
              self,
              @disable_unbind_cb,
              @buffer_size,
              @usage_manager)
    @target_connection.assign_logger(@logger)
    if !@disable_auth
      @username = @http_parser.username
      @password = @http_parser.password
      @target_connection.assign_user_and_password(@username, @password)
    end
  rescue RProxy::HTTPAuthFailed
    send_data(RProxy::Constants::HTTP_FAILED_AUTH)
    close_connection_after_writing
  rescue RProxy::HTTPNotSupport
    send_data(RProxy::Constants::HTTP_BAD_REQUEST)
    close_connection_after_writing
  rescue => e
    if @logger
      @logger.error("client: #{@ip}, target: #{target_host}, port: #{target_port}, #{e.message}")
    end
    close_connection
  end
end

#unbindObject



80
81
82
83
84
85
86
# File 'lib/r_proxy/connection_handler.rb', line 80

def unbind
  if @timer
    EventMachine.cancel_timer(@timer)
  end
  return if @disable_unbind_cb
  @usage_manager.report_usage(@username, @password, get_proxied_bytes)
end