Class: OverSIP::WebSocket::WssTunnelServer

Inherits:
WsServer show all
Defined in:
lib/oversip/websocket/listeners/wss_tunnel_server.rb

Direct Known Subclasses

IPv4WssTunnelServer, IPv6WssTunnelServer

Constant Summary

Constants inherited from WsServer

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

Constants included from SIP::MessageProcessor

SIP::MessageProcessor::MSG_TYPE

Instance Attribute Summary

Attributes inherited from WsServer

#client_closed, #outbound_flow_token, #ws_established

Attributes inherited from Connection

#cvars

Instance Method Summary collapse

Methods inherited from WsServer

#accept_ws_handshake, #check_http_request, #do_on_connection_callback, #http_reject, #parse_http_headers, #receive_data, #remote_desc, #remote_ip, #remote_ip_type, #remote_port, #send_sip_msg, #transport

Methods inherited from Connection

#close, #initialize, #open?, outbound_listener?, reliable_transport_listener?

Methods included from Logger

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

Constructor Details

This class inherits a constructor from OverSIP::WebSocket::Connection

Instance Method Details

#parse_haproxy_protocolObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/oversip/websocket/listeners/wss_tunnel_server.rb', line 104

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/oversip/websocket/listeners/wss_tunnel_server.rb', line 5

def post_connection
  begin
    # Temporal @remote_ip and @remote_port until the HAProxy protocol line is parsed.
    @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

  # Create an Outbound (RFC 5626) flow token for this connection.
  @outbound_flow_token = ::OverSIP::SIP::TransportManager.add_outbound_connection self

  log_system_debug ("connection from the TLS tunnel " << remote_desc)  if $oversip_debug
end

#process_received_dataObject



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/oversip/websocket/listeners/wss_tunnel_server.rb', line 56

def process_received_data
  @state == :ignore and return

  while (case @state
    when :init
      @http_parser = ::OverSIP::WebSocket::HttpRequestParser.new
      @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 :on_connection_callback
      do_on_connection_callback
      false

    when :accept_ws_handshake
      accept_ws_handshake

    when :websocket
      @ws_established = true
      return false  if @buffer.size.zero?
      @ws_framing.receive_data
      false

    when :ignore
      false
    end)
  end  # while

end

#unbind(cause = nil) ⇒ Object



23
24
25
26
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
# File 'lib/oversip/websocket/listeners/wss_tunnel_server.rb', line 23

def unbind cause=nil
  @state = :ignore

  # Remove the connection.
  self.class.connections.delete @connection_id  if @connection_id

  # Remove the Outbound token flow.
  ::OverSIP::SIP::TransportManager.delete_outbound_connection @outbound_flow_token

  @local_closed = true  if cause == ::Errno::ETIMEDOUT
  @local_closed = false  if @client_closed

  if $oversip_debug
    log_msg = "connection from the TLS tunnel #{remote_desc} "
    log_msg << ( @local_closed ? "locally closed" : "remotely closed" )
    log_msg << " (cause: #{cause.inspect})"  if cause
    log_system_debug log_msg
  end unless $!

  if @ws_established
    # Run OverSIP::WebSocketEvents.on_disconnection
    ::Fiber.new do
      begin
        ::OverSIP::WebSocketEvents.on_disconnection self, !@local_closed
      rescue ::Exception => e
        log_system_error "error calling OverSIP::WebSocketEvents.on_disconnection():"
        log_system_error e
      end
    end.resume
  end unless $!
end