From 6efc8a597dd0a08b756c8826b39dae987b3ed5dd Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 13 Jun 2019 12:33:04 +0100 Subject: [PATCH 1/4] py3 fixes for federation_client --- scripts-dev/federation_client.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts-dev/federation_client.py b/scripts-dev/federation_client.py index e0287c8c6ccf..02607e07fb07 100755 --- a/scripts-dev/federation_client.py +++ b/scripts-dev/federation_client.py @@ -21,7 +21,8 @@ import base64 import json import sys -from urlparse import urlparse, urlunparse + +from six.moves.urllib import parse as urlparse import nacl.signing import requests @@ -145,7 +146,7 @@ def request_json(method, origin_name, origin_key, destination, path, content): for key, sig in signed_json["signatures"][origin_name].items(): header = "X-Matrix origin=%s,key=\"%s\",sig=\"%s\"" % (origin_name, key, sig) - authorization_headers.append(bytes(header)) + authorization_headers.append(header.encode("ascii")) print("Authorization: %s" % header, file=sys.stderr) dest = "matrix://%s%s" % (destination, path) @@ -270,12 +271,12 @@ def lookup(s): return s, 8448 def get_connection(self, url, proxies=None): - parsed = urlparse(url) + parsed = urlparse.urlparse(url) (host, port) = self.lookup(parsed.netloc) netloc = "%s:%d" % (host, port) print("Connecting to %s" % (netloc,), file=sys.stderr) - url = urlunparse( + url = urlparse.urlunparse( ("https", netloc, parsed.path, parsed.params, parsed.query, parsed.fragment) ) return super(MatrixConnectionAdapter, self).get_connection(url, proxies) From fc59a39c3bb376cf60e330237b32bea0e3cf671e Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 13 Jun 2019 12:36:05 +0100 Subject: [PATCH 2/4] .well-known support for federation_client --- scripts-dev/federation_client.py | 34 +++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/scripts-dev/federation_client.py b/scripts-dev/federation_client.py index 02607e07fb07..41e7b244187a 100755 --- a/scripts-dev/federation_client.py +++ b/scripts-dev/federation_client.py @@ -251,7 +251,7 @@ def read_args_from_config(args): class MatrixConnectionAdapter(HTTPAdapter): @staticmethod - def lookup(s): + def lookup(s, skip_well_known=False): if s[-1] == ']': # ipv6 literal (with no port) return s, 8448 @@ -264,12 +264,44 @@ def lookup(s): raise ValueError("Invalid host:port '%s'" % s) return out[0], port + # try a .well-known lookup + if not skip_well_known: + well_known = MatrixConnectionAdapter.get_well_known(s) + if well_known: + return MatrixConnectionAdapter.lookup( + well_known, skip_well_known=True + ) + try: srv = srvlookup.lookup("matrix", "tcp", s)[0] return srv.host, srv.port except Exception: return s, 8448 + @staticmethod + def get_well_known(server_name): + uri = "https://%s/.well-known/matrix/server" % (server_name, ) + print("fetching %s" % (uri, ), file=sys.stderr) + + try: + resp = requests.get(uri) + if resp.status_code != 200: + print("%s gave %i" % (uri, resp.status_code), file=sys.stderr) + return None + + parsed_well_known = resp.json() + if not isinstance(parsed_well_known, dict): + raise Exception("not a dict") + if "m.server" not in parsed_well_known: + raise Exception("Missing key 'm.server'") + new_name = parsed_well_known['m.server'] + print("well-known lookup gave %s" % (new_name, ), file=sys.stderr) + return new_name + + except Exception as e: + print("Invalid response from %s: %s" % (uri, e, ), file=sys.stderr) + return None + def get_connection(self, url, proxies=None): parsed = urlparse.urlparse(url) From 3da247213332d9c90ef30603e32c9197c90018ca Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 13 Jun 2019 13:52:42 +0100 Subject: [PATCH 3/4] changelog --- changelog.d/5447.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/5447.misc diff --git a/changelog.d/5447.misc b/changelog.d/5447.misc new file mode 100644 index 000000000000..ce097b3629c8 --- /dev/null +++ b/changelog.d/5447.misc @@ -0,0 +1 @@ +Update federation_client dev script. From 02c120113a3c61020d8843afe322d8e4055bdffb Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Thu, 13 Jun 2019 14:49:03 +0100 Subject: [PATCH 4/4] Update changelog.d/5447.misc Co-Authored-By: Erik Johnston --- changelog.d/5447.misc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/5447.misc b/changelog.d/5447.misc index ce097b3629c8..dd520684044a 100644 --- a/changelog.d/5447.misc +++ b/changelog.d/5447.misc @@ -1 +1 @@ -Update federation_client dev script. +Update federation_client dev script to support `.well-known` and work with python3.