Class: OverSIP::WebSocket::TlsTunnelServer

Inherits:
TcpServer
  • Object
show all
Defined in:
lib/oversip/websocket/listeners/tls_tunnel_server.rb

Direct Known Subclasses

IPv4TlsTunnelServer, IPv6TlsTunnelServer

Constant Summary

Constants inherited from TcpServer

OverSIP::WebSocket::TcpServer::HDR_SUPPORTED_WEBSOCKET_VERSIONS, OverSIP::WebSocket::TcpServer::HEADERS_MAX_SIZE, OverSIP::WebSocket::TcpServer::WS_MAGIC_GUID_04, OverSIP::WebSocket::TcpServer::WS_VERSIONS

Constants included from Logger

Logger::SYSLOG_POSIXMQ_MAPPING

Instance Attribute Summary

Attributes inherited from TcpServer

#connection_log_id, #remote_ip, #remote_ip_type, #remote_port, #ws_app_klass, #ws_protocol

Instance Method Summary collapse

Methods inherited from TcpServer

#accept_ws_handshake, #check_http_request, #http_reject, #ignore_incoming_data, #parse_http_headers, #remote_desc, #unbind

Methods included from DefaultPolicy

#check_hostport, #check_origin, #check_request_uri

Methods included from Logger

close, #fatal, fg_system_msg2str, init_logger_mq, load_methods, #log_id, syslog_system_msg2str, syslog_user_msg2str

Constructor Details

#initializeTlsTunnelServer

Returns a new instance of TlsTunnelServer.



5
6
7
8
9
# File 'lib/oversip/websocket/listeners/tls_tunnel_server.rb', line 5

def initialize
  @http_parser = ::OverSIP::WebSocket::HttpRequestParser.new
  @buffer = ::IO::Buffer.new
  @state = :init
end

Instance Method Details

#parse_haproxy_protocolObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/oversip/websocket/listeners/tls_tunnel_server.rb', line 71

def parse_haproxy_protocol
  if (haproxy_protocol_data = ::OverSIP::Utils.parse_haproxy_protocol(@buffer.to_str))
    @haproxy_protocol_parsed = true

    # Update connection information.
    @remote_ip_type = haproxy_protocol_data[1]
    @remote_ip = haproxy_protocol_data[2]
    @remote_port = haproxy_protocol_data[3]

    # Update log information.
    remote_desc true

    # Remove the HAProxy Protocol line from the received data.
    @buffer.read haproxy_protocol_data[0]

    @state = :http_headers

  # If parsing fails then the TLS proxy has sent us a wrong HAProxy Protocol line ¿?
  else
    log_system_error "HAProxy Protocol parsing error, closing connection"
    close_connection_after_writing
    @state = :ignore
    return false
  end
end

#post_connectionObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/oversip/websocket/listeners/tls_tunnel_server.rb', line 12

def post_connection
  begin
    @remote_port, @remote_ip = ::Socket.unpack_sockaddr_in(get_peername)
  rescue => e
    log_system_error "error obtaining remote IP/port (#{e.class}: #{e.message}), closing connection"
    close_connection
    @state = :ignore
    return
  end
  @connection_log_id = ::SecureRandom.hex(4)

  log_system_info "connection from the TLS tunnel " << remote_desc
end

#receive_data(data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/oversip/websocket/listeners/tls_tunnel_server.rb', line 27

def receive_data data
  @state == :ignore and return
  @buffer << data

  while (case @state
    when :init
      @http_request = ::OverSIP::WebSocket::HttpRequest.new
      @http_parser.reset
      @http_parser_nbytes = 0
      @bytes_remaining = 0
      # If it's a TCP connection from the TLS proxy then parse the HAProxy Protocol line
      # if it's not yet done.
      unless @haproxy_protocol_parsed
        @state = :haproxy_protocol
      else
        @state = :http_headers
      end

    when :haproxy_protocol
      parse_haproxy_protocol

    when :http_headers
      parse_http_headers

    when :check_http_request
      check_http_request

    when :accept_ws_handshake
      accept_ws_handshake

    when :websocket_frames
      return false  if @buffer.size.zero?

      @ws_framing.receive_data
      false

    when :ignore
      false
    end)
  end  # while

end