Class: OverSIP::SIP::TlsTunnelReactor

Inherits:
TcpReactor show all
Defined in:
lib/oversip/sip/listeners/tls_tunnel_reactor.rb

Direct Known Subclasses

TlsTunnelServer

Constant Summary collapse

HEADERS_MAX_SIZE =

Max size (bytes) of the buffered data when receiving message headers (avoid DoS attacks).

16384

Constants included from MessageProcessor

MessageProcessor::MSG_TYPE

Constants included from Logger

Logger::SYSLOG_POSIXMQ_MAPPING

Instance Method Summary collapse

Methods inherited from TcpReactor

#get_body, #parse_headers, #send_sip_msg

Methods inherited from Reactor

#initialize, #receive_senderror, reliable_transport_listener?

Methods included from Logger

close, #fatal, 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::SIP::Reactor

Instance Method Details

#parse_client_pemsObject

TODO: Not terminated yet.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/oversip/sip/listeners/tls_tunnel_reactor.rb', line 92

def parse_client_pems
  # TODO: Wrong, it could occur that here the last PEMs byte arries.
  return false if @buffer.size < 3  # 3 bytes = 0\r\n (minimum data).

  @pems_str ||= ""
  @pems_str << @buffer.read(2)

  # No PEMS.
  if @pems_str == "\r\n"
    @state = :headers
    return true
  end

  #@pem_size_str = 

  @state = :headers
end

#parse_haproxy_protocolObject



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
# File 'lib/oversip/sip/listeners/tls_tunnel_reactor.rb', line 57

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]

    # Add the connection with the client's source data. Note that we pass a TlsServer as class, but
    # the server instance is a TcpServer.
    @connection_id = case @remote_ip_type
      when :ipv4
        ::OverSIP::SIP::TransportManager.add_connection self, ::OverSIP::SIP::IPv4TlsServer, :ipv4, @remote_ip, @remote_port
      when :ipv6
        ::OverSIP::SIP::TransportManager.add_connection self, ::OverSIP::SIP::IPv6TlsServer, :ipv6, @remote_ip, @remote_port
      end

    # Update log information.
    remote_desc true

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

    @state = :headers

  else
    log_system_error "HAProxy Protocol parsing error, closing connection"
    close_connection_after_writing
    @state = :ignore
    return false
  end
end

#receive_data(data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
54
55
# File 'lib/oversip/sip/listeners/tls_tunnel_reactor.rb', line 9

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

  while (case @state
    when :init
      @parser.reset
      @parser_nbytes = 0
      # If it's a TCP connection from the TLS tunnel then parse the HAProxy Protocol line
      # if it's not yet done.
      unless @haproxy_protocol_parsed
        @state = :haproxy_protocol
      else
        @state = :headers
      end

    when :haproxy_protocol
      parse_haproxy_protocol

    when :client_pems
      parse_client_pems

    when :headers
      parse_headers

    when :body
      get_body

    when :finished
      # Invoke the custom logic for requests.
      if @msg.request?
        process_request
      else
        process_response
      end

      # Set state to :init.
      @state = :init
      # Return true to continue processing possible remaining data.
      true

    when :ignore
      false
    end)
  end  # while

end