Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Updates to the federation_client script #5447

Merged
merged 5 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5447.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update federation_client dev script to support `.well-known` and work with python3.
43 changes: 38 additions & 5 deletions scripts-dev/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -250,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
Expand All @@ -263,19 +264,51 @@ 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(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)
Expand Down