_v2parser.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # -*- test-case-name: twisted.protocols.haproxy.test.test_v2parser -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. IProxyParser implementation for version two of the PROXY protocol.
  6. """
  7. import binascii
  8. import struct
  9. from constantly import Values, ValueConstant
  10. from zope.interface import implementer
  11. from twisted.internet import address
  12. from twisted.python import compat
  13. from ._exceptions import (
  14. convertError, InvalidProxyHeader, InvalidNetworkProtocol,
  15. MissingAddressData
  16. )
  17. from . import _info
  18. from . import _interfaces
  19. class NetFamily(Values):
  20. """
  21. Values for the 'family' field.
  22. """
  23. UNSPEC = ValueConstant(0x00)
  24. INET = ValueConstant(0x10)
  25. INET6 = ValueConstant(0x20)
  26. UNIX = ValueConstant(0x30)
  27. class NetProtocol(Values):
  28. """
  29. Values for 'protocol' field.
  30. """
  31. UNSPEC = ValueConstant(0)
  32. STREAM = ValueConstant(1)
  33. DGRAM = ValueConstant(2)
  34. _HIGH = 0b11110000
  35. _LOW = 0b00001111
  36. _LOCALCOMMAND = 'LOCAL'
  37. _PROXYCOMMAND = 'PROXY'
  38. @implementer(_interfaces.IProxyParser)
  39. class V2Parser(object):
  40. """
  41. PROXY protocol version two header parser.
  42. Version two of the PROXY protocol is a binary format.
  43. """
  44. PREFIX = b'\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A'
  45. VERSIONS = [32]
  46. COMMANDS = {0: _LOCALCOMMAND, 1: _PROXYCOMMAND}
  47. ADDRESSFORMATS = {
  48. # TCP4
  49. 17: '!4s4s2H',
  50. 18: '!4s4s2H',
  51. # TCP6
  52. 33: '!16s16s2H',
  53. 34: '!16s16s2H',
  54. # UNIX
  55. 49: '!108s108s',
  56. 50: '!108s108s',
  57. }
  58. def __init__(self):
  59. self.buffer = b''
  60. def feed(self, data):
  61. """
  62. Consume a chunk of data and attempt to parse it.
  63. @param data: A bytestring.
  64. @type data: bytes
  65. @return: A two-tuple containing, in order, a L{_interfaces.IProxyInfo}
  66. and any bytes fed to the parser that followed the end of the
  67. header. Both of these values are None until a complete header is
  68. parsed.
  69. @raises InvalidProxyHeader: If the bytes fed to the parser create an
  70. invalid PROXY header.
  71. """
  72. self.buffer += data
  73. if len(self.buffer) < 16:
  74. raise InvalidProxyHeader()
  75. size = struct.unpack('!H', self.buffer[14:16])[0] + 16
  76. if len(self.buffer) < size:
  77. return (None, None)
  78. header, remaining = self.buffer[:size], self.buffer[size:]
  79. self.buffer = b''
  80. info = self.parse(header)
  81. return (info, remaining)
  82. @staticmethod
  83. def _bytesToIPv4(bytestring):
  84. """
  85. Convert packed 32-bit IPv4 address bytes into a dotted-quad ASCII bytes
  86. representation of that address.
  87. @param bytestring: 4 octets representing an IPv4 address.
  88. @type bytestring: L{bytes}
  89. @return: a dotted-quad notation IPv4 address.
  90. @rtype: L{bytes}
  91. """
  92. return b'.'.join(
  93. ('%i' % (ord(b),)).encode('ascii')
  94. for b in compat.iterbytes(bytestring)
  95. )
  96. @staticmethod
  97. def _bytesToIPv6(bytestring):
  98. """
  99. Convert packed 128-bit IPv6 address bytes into a colon-separated ASCII
  100. bytes representation of that address.
  101. @param bytestring: 16 octets representing an IPv6 address.
  102. @type bytestring: L{bytes}
  103. @return: a dotted-quad notation IPv6 address.
  104. @rtype: L{bytes}
  105. """
  106. hexString = binascii.b2a_hex(bytestring)
  107. return b':'.join(
  108. ('%x' % (int(hexString[b:b+4], 16),)).encode('ascii')
  109. for b in range(0, 32, 4)
  110. )
  111. @classmethod
  112. def parse(cls, line):
  113. """
  114. Parse a bytestring as a full PROXY protocol header.
  115. @param line: A bytestring that represents a valid HAProxy PROXY
  116. protocol version 2 header.
  117. @type line: bytes
  118. @return: A L{_interfaces.IProxyInfo} containing the
  119. parsed data.
  120. @raises InvalidProxyHeader: If the bytestring does not represent a
  121. valid PROXY header.
  122. """
  123. prefix = line[:12]
  124. addrInfo = None
  125. with convertError(IndexError, InvalidProxyHeader):
  126. # Use single value slices to ensure bytestring values are returned
  127. # instead of int in PY3.
  128. versionCommand = ord(line[12:13])
  129. familyProto = ord(line[13:14])
  130. if prefix != cls.PREFIX:
  131. raise InvalidProxyHeader()
  132. version, command = versionCommand & _HIGH, versionCommand & _LOW
  133. if version not in cls.VERSIONS or command not in cls.COMMANDS:
  134. raise InvalidProxyHeader()
  135. if cls.COMMANDS[command] == _LOCALCOMMAND:
  136. return _info.ProxyInfo(line, None, None)
  137. family, netproto = familyProto & _HIGH, familyProto & _LOW
  138. with convertError(ValueError, InvalidNetworkProtocol):
  139. family = NetFamily.lookupByValue(family)
  140. netproto = NetProtocol.lookupByValue(netproto)
  141. if (
  142. family is NetFamily.UNSPEC or
  143. netproto is NetProtocol.UNSPEC
  144. ):
  145. return _info.ProxyInfo(line, None, None)
  146. addressFormat = cls.ADDRESSFORMATS[familyProto]
  147. addrInfo = line[16:16+struct.calcsize(addressFormat)]
  148. if family is NetFamily.UNIX:
  149. with convertError(struct.error, MissingAddressData):
  150. source, dest = struct.unpack(addressFormat, addrInfo)
  151. return _info.ProxyInfo(
  152. line,
  153. address.UNIXAddress(source.rstrip(b'\x00')),
  154. address.UNIXAddress(dest.rstrip(b'\x00')),
  155. )
  156. addrType = 'TCP'
  157. if netproto is NetProtocol.DGRAM:
  158. addrType = 'UDP'
  159. addrCls = address.IPv4Address
  160. addrParser = cls._bytesToIPv4
  161. if family is NetFamily.INET6:
  162. addrCls = address.IPv6Address
  163. addrParser = cls._bytesToIPv6
  164. with convertError(struct.error, MissingAddressData):
  165. info = struct.unpack(addressFormat, addrInfo)
  166. source, dest, sPort, dPort = info
  167. return _info.ProxyInfo(
  168. line,
  169. addrCls(addrType, addrParser(source), sPort),
  170. addrCls(addrType, addrParser(dest), dPort),
  171. )