r47179 - Merge haproxy-endpoint-8203-2: PROXY endpoint.

glyph-TA+aISz0psMTMxyoc4vAAJOcrHinNvQL0E9HWUfgJXw@public.gmane.org Wed, 6 Apr 2016 19:11:19 -0600 (MDT)
Newsgroups gmane.comp.python.twisted.commits
Message-ID <[email protected]>
Author: glyph
Date: Wed Apr  6 19:11:12 2016
New Revision: 47179

Added:
   trunk/twisted/protocols/haproxy/
   trunk/twisted/protocols/haproxy/__init__.py
   trunk/twisted/protocols/haproxy/_exceptions.py
   trunk/twisted/protocols/haproxy/_info.py
   trunk/twisted/protocols/haproxy/_interfaces.py
   trunk/twisted/protocols/haproxy/_parser.py
   trunk/twisted/protocols/haproxy/_v1parser.py
   trunk/twisted/protocols/haproxy/_v2parser.py
   trunk/twisted/protocols/haproxy/_wrapper.py
   trunk/twisted/protocols/haproxy/test/
   trunk/twisted/protocols/haproxy/test/__init__.py
   trunk/twisted/protocols/haproxy/test/test_parser.py
   trunk/twisted/protocols/haproxy/test/test_v1parser.py
   trunk/twisted/protocols/haproxy/test/test_v2parser.py
   trunk/twisted/protocols/haproxy/test/test_wrapper.py
   trunk/twisted/topfiles/8203.feature
Modified:
   trunk/docs/core/howto/endpoints.rst
   trunk/twisted/internet/endpoints.py
   trunk/twisted/plugins/twisted_core.py
   trunk/twisted/python/dist3.py

Log:
Merge haproxy-endpoint-8203-2: PROXY endpoint.

Author: kconwayatlassian

Reviewer: glyph

Fixes: #8203

twisted.protocols.haproxy.proxyEndpoint provides an endpoint that wraps
any other stream server endpoint with the PROXY protocol that retains
information about the original client connection handled by the proxy;
this wrapper is also exposed via the string description prefix
'haproxy'; for example 'twistd web --port haproxy:tcp:8765'.

Modified: trunk/docs/core/howto/endpoints.rst
==============================================================================
--- trunk/docs/core/howto/endpoints.rst	(original)
+++ trunk/docs/core/howto/endpoints.rst	Wed Apr  6 19:11:12 2016
@@ -353,3 +353,10 @@
    For example, ``systemd:domain=INET6:index=3``.
 
    See also :doc:`Deploying Twisted with systemd <systemd>`.
+
+PROXY
+  The PROXY protocol is a stream wrapper and can be applied any of the other server endpoints by placing ``haproxy:`` in front of a normal port definition.
+
+  For example, ``haproxy:tcp:port=80:interface=192.168.1.1`` or ``haproxy:ssl:port=443:privateKey=/etc/ssl/server.pem:extraCertChain=/etc/ssl/chain.pem:sslmethod=SSLv3_METHOD:dhParameters=dh_param_1024.pem``.
+
+  The PROXY protocol provides a way for load balancers and reverse proxies to send down the real IP of a connection's source and destination without relying on X-Forwarded-For headers. A Twisted service using this endpoint wrapper must run behind a service that sends valid PROXY protocol headers. For more on the protocol see `the formal specification <http://www.haproxy.org/download/1.5/doc/proxy-protocol.txt>`_. Both version one and two of the protocol are currently supported.

Modified: trunk/twisted/internet/endpoints.py
==============================================================================
--- trunk/twisted/internet/endpoints.py	(original)
+++ trunk/twisted/internet/endpoints.py	Wed Apr  6 19:11:12 2016
@@ -1904,6 +1904,30 @@
 
 
 
+@implementer(interfaces.IStreamServerEndpoint)
+class _WrapperServerEndpoint(object):
+    """
+    A server endpoint that wraps another server endpoint.
+    """
+
+    def __init__(self, wrappedEndpoint, wrapperFactory):
+        """
+        Construct a L{_WrapperServerEndpoint}.
+        """
+        self._wrappedEndpoint = wrappedEndpoint
+        self._wrapperFactory = wrapperFactory
+
+
+    def listen(self, protocolFactory):
+        """
+        Connect the given protocol factory and unwrap its result.
+        """
+        return self._wrappedEndpoint.listen(
+            self._wrapperFactory(protocolFactory)
+        )
+
+
+
 def wrapClientTLS(connectionCreator, wrappedEndpoint):
     """
     Wrap an endpoint which upgrades to TLS as soon as the connection is

Modified: trunk/twisted/plugins/twisted_core.py
==============================================================================
--- trunk/twisted/plugins/twisted_core.py	(original)
+++ trunk/twisted/plugins/twisted_core.py	Wed Apr  6 19:11:12 2016
@@ -7,7 +7,12 @@
     _SystemdParser, _TCP6ServerParser, _StandardIOParser,
     _TLSClientEndpointParser)
 
+from twisted.protocols.haproxy._parser import (
+    HAProxyServerParser as _HAProxyServerParser
+)
+
 systemdEndpointParser = _SystemdParser()
 tcp6ServerEndpointParser = _TCP6ServerParser()
 stdioEndpointParser = _StandardIOParser()
 tlsClientEndpointParser = _TLSClientEndpointParser()
+_haProxyServerEndpointParser = _HAProxyServerParser()

Modified: trunk/twisted/python/dist3.py
==============================================================================
--- trunk/twisted/python/dist3.py	(original)
+++ trunk/twisted/python/dist3.py	Wed Apr  6 19:11:12 2016
@@ -159,6 +159,19 @@
     "twisted.protocols.policies",
     "twisted.protocols.telnet",
     "twisted.protocols.test.__init__",
+    "twisted.protocols.haproxy.__init__",
+    "twisted.protocols.haproxy._exceptions",
+    "twisted.protocols.haproxy._info",
+    "twisted.protocols.haproxy._interfaces",
+    "twisted.protocols.haproxy._v1parser",
+    "twisted.protocols.haproxy._v2parser",
+    "twisted.protocols.haproxy._wrapper",
+    "twisted.protocols.haproxy._parser",
+    "twisted.protocols.haproxy.test.__init__",
+    "twisted.protocols.haproxy.test.test_v1parser",
+    "twisted.protocols.haproxy.test.test_v2parser",
+    "twisted.protocols.haproxy.test.test_wrapper",
+    "twisted.protocols.haproxy.test.test_parser",
     "twisted.protocols.tls",
     "twisted.python.__init__",
     "twisted.python._appdirs",