From 6e18805ac2906ea52d2374024392332af1a603b7 Mon Sep 17 00:00:00 2001 From: Glyph Date: Sun, 11 Dec 2016 01:44:02 -0800 Subject: [PATCH 1/5] IPv6 support for client.py This is an (untested) general sketch of how to use wrapClientTLS to implement TLS over IPv6, as well as faster connections over IPv4. --- synapse/http/client.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/synapse/http/client.py b/synapse/http/client.py index 3ec9bc7faf..c60e3c2ac0 100644 --- a/synapse/http/client.py +++ b/synapse/http/client.py @@ -386,26 +386,21 @@ class SpiderEndpointFactory(object): def endpointForURI(self, uri): logger.info("Getting endpoint for %s", uri.toBytes()) + if uri.scheme == "http": - return SpiderEndpoint( - reactor, uri.host, uri.port, self.blacklist, self.whitelist, - endpoint=TCP4ClientEndpoint, - endpoint_kw_args={ - 'timeout': 15 - }, - ) + endpoint_factory = HostnameEndpoint elif uri.scheme == "https": - tlsPolicy = self.policyForHTTPS.creatorForNetloc(uri.host, uri.port) - return SpiderEndpoint( - reactor, uri.host, uri.port, self.blacklist, self.whitelist, - endpoint=SSL4ClientEndpoint, - endpoint_kw_args={ - 'sslContextFactory': tlsPolicy, - 'timeout': 15 - }, - ) + tlsCreator = self.policyForHTTPS.creatorForNetloc(uri.host, uri.port) + def endpoint_factory(reactor, host, port, **kw): + return wrapClientTLS(tlsCreator, HostnameEndpoint(reactor, host, port, **kw) else: logger.warn("Can't get endpoint for unrecognised scheme %s", uri.scheme) + return None + return SpiderEndpoint( + reactor, uri.host, uri.port, self.blacklist, self.whitelist, + endpoint=endpoint_factory, endpoint_kw_args=dict(timeout=15), + ) + class SpiderHttpClient(SimpleHttpClient): From 9f07f4c5595b0eff19c9740c44803700b01b14af Mon Sep 17 00:00:00 2001 From: Glyph Date: Sun, 11 Dec 2016 01:46:43 -0800 Subject: [PATCH 2/5] IPv6 support for endpoint.py Similar to https://github.com/matrix-org/synapse/pull/1689, but for endpoint.py --- synapse/http/endpoint.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/synapse/http/endpoint.py b/synapse/http/endpoint.py index 442696d393..5e2e428dbf 100644 --- a/synapse/http/endpoint.py +++ b/synapse/http/endpoint.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from twisted.internet.endpoints import SSL4ClientEndpoint, TCP4ClientEndpoint +from twisted.internet.endpoints import HostnameEndpoint, wrapClientTLS from twisted.internet import defer from twisted.internet.error import ConnectError from twisted.names import client, dns @@ -58,11 +58,11 @@ def matrix_federation_endpoint(reactor, destination, ssl_context_factory=None, endpoint_kw_args.update(timeout=timeout) if ssl_context_factory is None: - transport_endpoint = TCP4ClientEndpoint + transport_endpoint = HostnameEndpoint default_port = 8008 else: - transport_endpoint = SSL4ClientEndpoint - endpoint_kw_args.update(sslContextFactory=ssl_context_factory) + def transport_endpoint(reactor, host, port): + return wrapClientTLS(ssl_context_factory, HostnameEndpoint(reactor, host, port)) default_port = 8448 if port is None: From d3bd94805f6ef68e75d8c2e39b8c97ea5ce88286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20L=C3=B6thberg?= Date: Mon, 12 Dec 2016 16:19:54 +0100 Subject: [PATCH 3/5] Fixup for #1689 and #1690 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Löthberg --- synapse/http/client.py | 11 +++++++---- synapse/http/endpoint.py | 10 ++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/synapse/http/client.py b/synapse/http/client.py index c60e3c2ac0..37988716e7 100644 --- a/synapse/http/client.py +++ b/synapse/http/client.py @@ -25,7 +25,7 @@ from synapse.http.endpoint import SpiderEndpoint from canonicaljson import encode_canonical_json from twisted.internet import defer, reactor, ssl, protocol, task -from twisted.internet.endpoints import SSL4ClientEndpoint, TCP4ClientEndpoint +from twisted.internet.endpoints import HostnameEndpoint, wrapClientTLS from twisted.web.client import ( BrowserLikeRedirectAgent, ContentDecoderAgent, GzipDecoder, Agent, readBody, PartialDownloadError, @@ -386,13 +386,16 @@ class SpiderEndpointFactory(object): def endpointForURI(self, uri): logger.info("Getting endpoint for %s", uri.toBytes()) - + if uri.scheme == "http": endpoint_factory = HostnameEndpoint elif uri.scheme == "https": tlsCreator = self.policyForHTTPS.creatorForNetloc(uri.host, uri.port) + def endpoint_factory(reactor, host, port, **kw): - return wrapClientTLS(tlsCreator, HostnameEndpoint(reactor, host, port, **kw) + return wrapClientTLS( + tlsCreator, + HostnameEndpoint(reactor, host, port, **kw)) else: logger.warn("Can't get endpoint for unrecognised scheme %s", uri.scheme) return None @@ -400,7 +403,7 @@ class SpiderEndpointFactory(object): reactor, uri.host, uri.port, self.blacklist, self.whitelist, endpoint=endpoint_factory, endpoint_kw_args=dict(timeout=15), ) - + class SpiderHttpClient(SimpleHttpClient): diff --git a/synapse/http/endpoint.py b/synapse/http/endpoint.py index 5e2e428dbf..1c17a28406 100644 --- a/synapse/http/endpoint.py +++ b/synapse/http/endpoint.py @@ -61,8 +61,10 @@ def matrix_federation_endpoint(reactor, destination, ssl_context_factory=None, transport_endpoint = HostnameEndpoint default_port = 8008 else: - def transport_endpoint(reactor, host, port): - return wrapClientTLS(ssl_context_factory, HostnameEndpoint(reactor, host, port)) + def transport_endpoint(reactor, host, port, timeout): + return wrapClientTLS( + ssl_context_factory, + HostnameEndpoint(reactor, host, port, timeout=timeout)) default_port = 8448 if port is None: @@ -80,7 +82,7 @@ class SpiderEndpoint(object): Implements twisted.internet.interfaces.IStreamClientEndpoint. """ def __init__(self, reactor, host, port, blacklist, whitelist, - endpoint=TCP4ClientEndpoint, endpoint_kw_args={}): + endpoint=HostnameEndpoint, endpoint_kw_args={}): self.reactor = reactor self.host = host self.port = port @@ -118,7 +120,7 @@ class SRVClientEndpoint(object): """ def __init__(self, reactor, service, domain, protocol="tcp", - default_port=None, endpoint=TCP4ClientEndpoint, + default_port=None, endpoint=HostnameEndpoint, endpoint_kw_args={}): self.reactor = reactor self.service_name = "_%s._%s.%s" % (service, protocol, domain) From 0648e76979e4626cf3719edc5958eb4f170e0d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20L=C3=B6thberg?= Date: Mon, 12 Dec 2016 18:40:39 +0100 Subject: [PATCH 4/5] Remove spurious newline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apparently I just removed the spaces instead... Signed-off-by: Johannes Löthberg --- synapse/http/client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/synapse/http/client.py b/synapse/http/client.py index 37988716e7..ca2f770f5d 100644 --- a/synapse/http/client.py +++ b/synapse/http/client.py @@ -405,7 +405,6 @@ class SpiderEndpointFactory(object): ) - class SpiderHttpClient(SimpleHttpClient): """ Separate HTTP client for spidering arbitrary URLs. From a9c1b419a9c913f8cfb373335c3b7824abcf7406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20L=C3=B6thberg?= Date: Sun, 18 Dec 2016 23:16:43 +0100 Subject: [PATCH 5/5] Bump twisted dependency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At least 16.0.0 is needed for wrapClientTLS support. Signed-off-by: Johannes Löthberg --- synapse/python_dependencies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/python_dependencies.py b/synapse/python_dependencies.py index 3742a25b37..7817b0cd91 100644 --- a/synapse/python_dependencies.py +++ b/synapse/python_dependencies.py @@ -24,7 +24,7 @@ REQUIREMENTS = { "signedjson>=1.0.0": ["signedjson>=1.0.0"], "pynacl==0.3.0": ["nacl==0.3.0", "nacl.bindings"], "service_identity>=1.0.0": ["service_identity>=1.0.0"], - "Twisted>=15.1.0": ["twisted>=15.1.0"], + "Twisted>=16.0.0": ["twisted>=16.0.0"], "pyopenssl>=0.14": ["OpenSSL>=0.14"], "pyyaml": ["yaml"], "pyasn1": ["pyasn1"],