Module: ProxyProtocol::Header

Included in:
Socket, TCPSocket
Defined in:
lib/proxyprotocol/header.rb

Instance Method Summary collapse

Instance Method Details

#ip_version(source_ip, dest_ip) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/proxyprotocol/header.rb', line 16

def ip_version(source_ip, dest_ip)
  if source_ip.ipv4? and dest_ip.ipv4?
    "TCP4"
  elsif source_ip.ipv6? and dest_ip.ipv6?
    "TCP6"
  end
end

#proxy_protocol_header(source_ip, source_port, dest_ip, dest_port) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/proxyprotocol/header.rb', line 5

def proxy_protocol_header(source_ip, source_port, dest_ip, dest_port)
  if [source_ip, source_port, dest_ip, dest_port].include?(nil)
    "PROXY UNKNOWN\r\n"
  else
    source_ip = IPAddr.new(source_ip) unless source_ip.kind_of?(IPAddr)
    dest_ip = IPAddr.new(dest_ip) unless dest_ip.kind_of?(IPAddr)
    version = ip_version(source_ip, dest_ip)
    "PROXY #{version} #{source_ip.to_s} #{dest_ip.to_s} #{source_port} #{dest_port}\r\n"
  end
end