_wrapper.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # -*- test-case-name: twisted.protocols.haproxy.test.test_wrapper -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Protocol wrapper that provides HAProxy PROXY protocol support.
  6. """
  7. from twisted.protocols import policies
  8. from twisted.internet import interfaces
  9. from twisted.internet.endpoints import _WrapperServerEndpoint
  10. from ._exceptions import InvalidProxyHeader
  11. from ._v1parser import V1Parser
  12. from ._v2parser import V2Parser
  13. class HAProxyProtocolWrapper(policies.ProtocolWrapper, object):
  14. """
  15. A Protocol wrapper that provides HAProxy support.
  16. This protocol reads the PROXY stream header, v1 or v2, parses the provided
  17. connection data, and modifies the behavior of getPeer and getHost to return
  18. the data provided by the PROXY header.
  19. """
  20. def __init__(self, factory, wrappedProtocol):
  21. policies.ProtocolWrapper.__init__(self, factory, wrappedProtocol)
  22. self._proxyInfo = None
  23. self._parser = None
  24. def dataReceived(self, data):
  25. if self._proxyInfo is not None:
  26. return self.wrappedProtocol.dataReceived(data)
  27. if self._parser is None:
  28. if (
  29. len(data) >= 16 and
  30. data[:12] == V2Parser.PREFIX and
  31. ord(data[12:13]) & 0b11110000 == 0x20
  32. ):
  33. self._parser = V2Parser()
  34. elif len(data) >= 8 and data[:5] == V1Parser.PROXYSTR:
  35. self._parser = V1Parser()
  36. else:
  37. self.loseConnection()
  38. return None
  39. try:
  40. self._proxyInfo, remaining = self._parser.feed(data)
  41. if remaining:
  42. self.wrappedProtocol.dataReceived(remaining)
  43. except InvalidProxyHeader:
  44. self.loseConnection()
  45. def getPeer(self):
  46. if self._proxyInfo and self._proxyInfo.source:
  47. return self._proxyInfo.source
  48. return self.transport.getPeer()
  49. def getHost(self):
  50. if self._proxyInfo and self._proxyInfo.destination:
  51. return self._proxyInfo.destination
  52. return self.transport.getHost()
  53. class HAProxyWrappingFactory(policies.WrappingFactory):
  54. """
  55. A Factory wrapper that adds PROXY protocol support to connections.
  56. """
  57. protocol = HAProxyProtocolWrapper
  58. def logPrefix(self):
  59. """
  60. Annotate the wrapped factory's log prefix with some text indicating
  61. the PROXY protocol is in use.
  62. @rtype: C{str}
  63. """
  64. if interfaces.ILoggingContext.providedBy(self.wrappedFactory):
  65. logPrefix = self.wrappedFactory.logPrefix()
  66. else:
  67. logPrefix = self.wrappedFactory.__class__.__name__
  68. return "%s (PROXY)" % (logPrefix,)
  69. def proxyEndpoint(wrappedEndpoint):
  70. """
  71. Wrap an endpoint with PROXY protocol support, so that the transport's
  72. C{getHost} and C{getPeer} methods reflect the attributes of the proxied
  73. connection rather than the underlying connection.
  74. @param wrappedEndpoint: The underlying listening endpoint.
  75. @type wrappedEndpoint: L{IStreamServerEndpoint}
  76. @return: a new listening endpoint that speaks the PROXY protocol.
  77. @rtype: L{IStreamServerEndpoint}
  78. """
  79. return _WrapperServerEndpoint(wrappedEndpoint, HAProxyWrappingFactory)