2017-08-17 11:54:27 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
# Copyright 2017 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-12-02 10:18:40 -05:00
|
|
|
|
|
|
|
"""
|
|
|
|
Script for signing and sending federation requests.
|
|
|
|
|
|
|
|
Some tips on doing the join dance with this:
|
|
|
|
|
|
|
|
room_id=...
|
|
|
|
user_id=...
|
|
|
|
|
|
|
|
# make_join
|
|
|
|
federation_client.py "/_matrix/federation/v1/make_join/$room_id/$user_id?ver=5" > make_join.json
|
|
|
|
|
|
|
|
# sign
|
|
|
|
jq -M .event make_join.json | sign_json --sign-event-room-version=$(jq -r .room_version make_join.json) -o signed-join.json
|
|
|
|
|
|
|
|
# send_join
|
|
|
|
federation_client.py -X PUT "/_matrix/federation/v2/send_join/$room_id/x" --body $(<signed-join.json) > send_join.json
|
|
|
|
"""
|
|
|
|
|
2017-08-17 11:54:27 -04:00
|
|
|
import argparse
|
2018-10-19 20:16:55 -04:00
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
import sys
|
2022-04-27 09:10:31 -04:00
|
|
|
from typing import Any, Dict, Optional, Tuple
|
2020-06-16 08:51:47 -04:00
|
|
|
from urllib import parse as urlparse
|
2018-06-07 13:14:08 -04:00
|
|
|
|
2015-05-06 06:46:02 -04:00
|
|
|
import requests
|
2021-03-18 17:12:07 -04:00
|
|
|
import signedjson.key
|
2020-08-26 09:11:38 -04:00
|
|
|
import signedjson.types
|
2015-05-06 06:46:02 -04:00
|
|
|
import srvlookup
|
2017-08-17 11:54:27 -04:00
|
|
|
import yaml
|
2022-11-20 12:41:17 -05:00
|
|
|
from requests import PreparedRequest, Response
|
2018-10-19 20:16:55 -04:00
|
|
|
from requests.adapters import HTTPAdapter
|
2022-04-27 09:10:31 -04:00
|
|
|
from urllib3 import HTTPConnectionPool
|
2015-05-06 06:46:02 -04:00
|
|
|
|
2018-06-07 13:14:08 -04:00
|
|
|
# uncomment the following to enable debug logging of http requests
|
2022-11-20 12:41:17 -05:00
|
|
|
# from http.client import HTTPConnection
|
2018-10-19 20:16:55 -04:00
|
|
|
# HTTPConnection.debuglevel = 1
|
|
|
|
|
2018-06-07 13:14:08 -04:00
|
|
|
|
2022-04-27 09:10:31 -04:00
|
|
|
def encode_base64(input_bytes: bytes) -> str:
|
2015-05-06 06:46:02 -04:00
|
|
|
"""Encode bytes as a base64 string without any padding."""
|
|
|
|
|
|
|
|
input_len = len(input_bytes)
|
|
|
|
output_len = 4 * ((input_len + 2) // 3) + (input_len + 2) % 3 - 2
|
|
|
|
output_bytes = base64.b64encode(input_bytes)
|
|
|
|
output_string = output_bytes[:output_len].decode("ascii")
|
|
|
|
return output_string
|
|
|
|
|
|
|
|
|
2022-04-27 09:10:31 -04:00
|
|
|
def encode_canonical_json(value: object) -> bytes:
|
2015-05-06 06:46:02 -04:00
|
|
|
return json.dumps(
|
2018-10-19 20:16:55 -04:00
|
|
|
value,
|
|
|
|
# Encode code-points outside of ASCII as UTF-8 rather than \u escapes
|
|
|
|
ensure_ascii=False,
|
|
|
|
# Remove unecessary white space.
|
2019-06-20 05:32:02 -04:00
|
|
|
separators=(",", ":"),
|
2018-10-19 20:16:55 -04:00
|
|
|
# Sort the keys of dictionaries.
|
|
|
|
sort_keys=True,
|
|
|
|
# Encode the resulting unicode as UTF-8 bytes.
|
|
|
|
).encode("UTF-8")
|
2015-05-06 06:46:02 -04:00
|
|
|
|
|
|
|
|
2020-08-26 09:11:38 -04:00
|
|
|
def sign_json(
|
|
|
|
json_object: Any, signing_key: signedjson.types.SigningKey, signing_name: str
|
|
|
|
) -> Any:
|
2015-05-06 06:46:02 -04:00
|
|
|
signatures = json_object.pop("signatures", {})
|
|
|
|
unsigned = json_object.pop("unsigned", None)
|
|
|
|
|
|
|
|
signed = signing_key.sign(encode_canonical_json(json_object))
|
|
|
|
signature_base64 = encode_base64(signed.signature)
|
|
|
|
|
|
|
|
key_id = "%s:%s" % (signing_key.alg, signing_key.version)
|
|
|
|
signatures.setdefault(signing_name, {})[key_id] = signature_base64
|
|
|
|
|
|
|
|
json_object["signatures"] = signatures
|
|
|
|
if unsigned is not None:
|
|
|
|
json_object["unsigned"] = unsigned
|
|
|
|
|
|
|
|
return json_object
|
|
|
|
|
|
|
|
|
2020-08-26 09:11:38 -04:00
|
|
|
def request(
|
|
|
|
method: Optional[str],
|
|
|
|
origin_name: str,
|
|
|
|
origin_key: signedjson.types.SigningKey,
|
|
|
|
destination: str,
|
|
|
|
path: str,
|
|
|
|
content: Optional[str],
|
2022-11-20 12:41:17 -05:00
|
|
|
verify_tls: bool,
|
2020-08-26 09:11:38 -04:00
|
|
|
) -> requests.Response:
|
2017-11-27 05:06:59 -05:00
|
|
|
if method is None:
|
|
|
|
if content is None:
|
|
|
|
method = "GET"
|
|
|
|
else:
|
|
|
|
method = "POST"
|
|
|
|
|
|
|
|
json_to_sign = {
|
|
|
|
"method": method,
|
2015-05-06 06:46:02 -04:00
|
|
|
"uri": path,
|
|
|
|
"origin": origin_name,
|
|
|
|
"destination": destination,
|
|
|
|
}
|
|
|
|
|
2017-11-27 05:06:59 -05:00
|
|
|
if content is not None:
|
|
|
|
json_to_sign["content"] = json.loads(content)
|
|
|
|
|
|
|
|
signed_json = sign_json(json_to_sign, origin_key, origin_name)
|
2015-05-06 06:46:02 -04:00
|
|
|
|
|
|
|
authorization_headers = []
|
|
|
|
|
|
|
|
for key, sig in signed_json["signatures"][origin_name].items():
|
2022-04-19 11:23:53 -04:00
|
|
|
header = 'X-Matrix origin=%s,key="%s",sig="%s",destination="%s"' % (
|
|
|
|
origin_name,
|
|
|
|
key,
|
|
|
|
sig,
|
|
|
|
destination,
|
|
|
|
)
|
2022-04-27 09:10:31 -04:00
|
|
|
authorization_headers.append(header)
|
2018-10-19 20:16:55 -04:00
|
|
|
print("Authorization: %s" % header, file=sys.stderr)
|
2017-08-17 11:54:27 -04:00
|
|
|
|
2023-06-20 05:05:31 -04:00
|
|
|
dest = "matrix-federation://%s%s" % (destination, path)
|
2018-10-19 20:16:55 -04:00
|
|
|
print("Requesting %s" % dest, file=sys.stderr)
|
2015-05-06 06:46:02 -04:00
|
|
|
|
2018-06-07 13:14:08 -04:00
|
|
|
s = requests.Session()
|
2023-06-20 05:05:31 -04:00
|
|
|
s.mount("matrix-federation://", MatrixConnectionAdapter())
|
2018-06-07 13:14:08 -04:00
|
|
|
|
2022-04-27 09:10:31 -04:00
|
|
|
headers: Dict[str, str] = {
|
|
|
|
"Authorization": authorization_headers[0],
|
|
|
|
}
|
2018-11-09 05:41:34 -05:00
|
|
|
|
|
|
|
if method == "POST":
|
|
|
|
headers["Content-Type"] = "application/json"
|
|
|
|
|
2020-08-26 09:11:38 -04:00
|
|
|
return s.request(
|
|
|
|
method=method,
|
|
|
|
url=dest,
|
|
|
|
headers=headers,
|
2022-11-20 12:41:17 -05:00
|
|
|
verify=verify_tls,
|
2020-08-26 09:11:38 -04:00
|
|
|
data=content,
|
|
|
|
stream=True,
|
2015-05-06 06:46:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-04-27 09:10:31 -04:00
|
|
|
def main() -> None:
|
2017-08-17 11:54:27 -04:00
|
|
|
parser = argparse.ArgumentParser(
|
2018-10-19 20:16:55 -04:00
|
|
|
description="Signs and sends a federation request to a matrix homeserver"
|
2017-08-17 11:54:27 -04:00
|
|
|
)
|
|
|
|
|
2017-08-22 06:19:30 -04:00
|
|
|
parser.add_argument(
|
2018-10-19 20:16:55 -04:00
|
|
|
"-N",
|
|
|
|
"--server-name",
|
2017-08-22 06:19:30 -04:00
|
|
|
help="Name to give as the local homeserver. If unspecified, will be "
|
2018-10-19 20:16:55 -04:00
|
|
|
"read from the config file.",
|
2017-08-22 06:19:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2018-10-19 20:16:55 -04:00
|
|
|
"-k",
|
|
|
|
"--signing-key-path",
|
2017-08-22 06:19:30 -04:00
|
|
|
help="Path to the file containing the private ed25519 key to sign the "
|
2018-10-19 20:16:55 -04:00
|
|
|
"request with.",
|
2017-08-22 06:19:30 -04:00
|
|
|
)
|
|
|
|
|
2017-08-17 11:54:27 -04:00
|
|
|
parser.add_argument(
|
2018-10-19 20:16:55 -04:00
|
|
|
"-c",
|
|
|
|
"--config",
|
2017-08-17 11:54:27 -04:00
|
|
|
default="homeserver.yaml",
|
2017-08-22 06:19:30 -04:00
|
|
|
help="Path to server config file. Ignored if --server-name and "
|
2018-10-19 20:16:55 -04:00
|
|
|
"--signing-key-path are both given.",
|
2017-08-17 11:54:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2018-10-19 20:16:55 -04:00
|
|
|
"-d",
|
|
|
|
"--destination",
|
2017-08-17 11:54:27 -04:00
|
|
|
default="matrix.org",
|
|
|
|
help="name of the remote homeserver. We will do SRV lookups and "
|
2018-10-19 20:16:55 -04:00
|
|
|
"connect appropriately.",
|
2017-08-17 11:54:27 -04:00
|
|
|
)
|
|
|
|
|
2017-11-27 05:06:59 -05:00
|
|
|
parser.add_argument(
|
2018-10-19 20:16:55 -04:00
|
|
|
"-X",
|
|
|
|
"--method",
|
2018-11-09 05:41:34 -05:00
|
|
|
help="HTTP method to use for the request. Defaults to GET if --body is"
|
2018-10-19 20:16:55 -04:00
|
|
|
"unspecified, POST if it is.",
|
2017-11-27 05:06:59 -05:00
|
|
|
)
|
|
|
|
|
2018-10-19 20:16:55 -04:00
|
|
|
parser.add_argument("--body", help="Data to send as the body of the HTTP request")
|
2017-11-27 05:06:59 -05:00
|
|
|
|
2022-11-20 12:41:17 -05:00
|
|
|
parser.add_argument(
|
|
|
|
"--insecure",
|
|
|
|
action="store_true",
|
|
|
|
help="Disable TLS certificate verification",
|
|
|
|
)
|
|
|
|
|
2017-08-17 11:54:27 -04:00
|
|
|
parser.add_argument(
|
2021-03-18 16:31:47 -04:00
|
|
|
"path", help="request path, including the '/_matrix/federation/...' prefix."
|
2017-08-17 11:54:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-03-18 17:12:07 -04:00
|
|
|
args.signing_key = None
|
|
|
|
if args.signing_key_path:
|
|
|
|
with open(args.signing_key_path) as f:
|
|
|
|
args.signing_key = f.readline()
|
|
|
|
|
|
|
|
if not args.server_name or not args.signing_key:
|
2017-08-22 06:19:30 -04:00
|
|
|
read_args_from_config(args)
|
2015-05-06 06:46:02 -04:00
|
|
|
|
2022-04-27 09:10:31 -04:00
|
|
|
assert isinstance(args.signing_key, str)
|
2021-03-18 17:12:07 -04:00
|
|
|
algorithm, version, key_base64 = args.signing_key.split()
|
|
|
|
key = signedjson.key.decode_signing_key_base64(algorithm, version, key_base64)
|
2015-05-06 06:46:02 -04:00
|
|
|
|
2020-08-26 09:11:38 -04:00
|
|
|
result = request(
|
2017-11-27 05:06:59 -05:00
|
|
|
args.method,
|
2018-10-19 20:16:55 -04:00
|
|
|
args.server_name,
|
|
|
|
key,
|
|
|
|
args.destination,
|
2021-03-18 16:31:47 -04:00
|
|
|
args.path,
|
2017-11-27 05:06:59 -05:00
|
|
|
content=args.body,
|
2022-11-20 12:41:17 -05:00
|
|
|
verify_tls=not args.insecure,
|
2015-05-06 06:46:02 -04:00
|
|
|
)
|
|
|
|
|
2020-08-26 09:11:38 -04:00
|
|
|
sys.stderr.write("Status Code: %d\n" % (result.status_code,))
|
|
|
|
|
|
|
|
for chunk in result.iter_content():
|
|
|
|
# we write raw utf8 to stdout.
|
|
|
|
sys.stdout.buffer.write(chunk)
|
|
|
|
|
2018-10-19 20:16:55 -04:00
|
|
|
print("")
|
2015-05-06 06:46:02 -04:00
|
|
|
|
2017-08-22 06:19:30 -04:00
|
|
|
|
2022-04-27 09:10:31 -04:00
|
|
|
def read_args_from_config(args: argparse.Namespace) -> None:
|
2023-08-15 08:11:20 -04:00
|
|
|
with open(args.config) as fh:
|
2017-08-22 06:19:30 -04:00
|
|
|
config = yaml.safe_load(fh)
|
2021-03-18 17:12:07 -04:00
|
|
|
|
2017-08-22 06:19:30 -04:00
|
|
|
if not args.server_name:
|
2019-06-20 05:32:02 -04:00
|
|
|
args.server_name = config["server_name"]
|
2021-03-18 17:12:07 -04:00
|
|
|
|
|
|
|
if not args.signing_key:
|
|
|
|
if "signing_key" in config:
|
|
|
|
args.signing_key = config["signing_key"]
|
|
|
|
else:
|
|
|
|
with open(config["signing_key_path"]) as f:
|
|
|
|
args.signing_key = f.readline()
|
2017-08-22 06:19:30 -04:00
|
|
|
|
|
|
|
|
2018-06-07 13:14:08 -04:00
|
|
|
class MatrixConnectionAdapter(HTTPAdapter):
|
2022-11-20 12:41:17 -05:00
|
|
|
def send(
|
|
|
|
self,
|
|
|
|
request: PreparedRequest,
|
|
|
|
*args: Any,
|
|
|
|
**kwargs: Any,
|
|
|
|
) -> Response:
|
|
|
|
# overrides the send() method in the base class.
|
|
|
|
|
|
|
|
# We need to look for .well-known redirects before passing the request up to
|
|
|
|
# HTTPAdapter.send().
|
|
|
|
assert isinstance(request.url, str)
|
|
|
|
parsed = urlparse.urlsplit(request.url)
|
|
|
|
server_name = parsed.netloc
|
|
|
|
well_known = self._get_well_known(parsed.netloc)
|
|
|
|
|
|
|
|
if well_known:
|
|
|
|
server_name = well_known
|
|
|
|
|
|
|
|
# replace the scheme in the uri with https, so that cert verification is done
|
|
|
|
# also replace the hostname if we got a .well-known result
|
|
|
|
request.url = urlparse.urlunsplit(
|
|
|
|
("https", server_name, parsed.path, parsed.query, parsed.fragment)
|
|
|
|
)
|
|
|
|
|
|
|
|
# at this point we also add the host header (otherwise urllib will add one
|
|
|
|
# based on the `host` from the connection returned by `get_connection`,
|
|
|
|
# which will be wrong if there is an SRV record).
|
|
|
|
request.headers["Host"] = server_name
|
|
|
|
|
|
|
|
return super().send(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_connection(
|
|
|
|
self, url: str, proxies: Optional[Dict[str, str]] = None
|
|
|
|
) -> HTTPConnectionPool:
|
|
|
|
# overrides the get_connection() method in the base class
|
|
|
|
parsed = urlparse.urlsplit(url)
|
|
|
|
(host, port, ssl_server_name) = self._lookup(parsed.netloc)
|
|
|
|
print(
|
|
|
|
f"Connecting to {host}:{port} with SNI {ssl_server_name}", file=sys.stderr
|
|
|
|
)
|
|
|
|
return self.poolmanager.connection_from_host(
|
|
|
|
host,
|
|
|
|
port=port,
|
|
|
|
scheme="https",
|
|
|
|
pool_kwargs={"server_hostname": ssl_server_name},
|
|
|
|
)
|
|
|
|
|
2018-06-07 13:14:08 -04:00
|
|
|
@staticmethod
|
2022-11-20 12:41:17 -05:00
|
|
|
def _lookup(server_name: str) -> Tuple[str, int, str]:
|
|
|
|
"""
|
|
|
|
Do an SRV lookup on a server name and return the host:port to connect to
|
|
|
|
Given the server_name (after any .well-known lookup), return the host, port and
|
|
|
|
the ssl server name
|
|
|
|
"""
|
|
|
|
if server_name[-1] == "]":
|
2018-06-07 13:14:08 -04:00
|
|
|
# ipv6 literal (with no port)
|
2022-11-20 12:41:17 -05:00
|
|
|
return server_name, 8448, server_name
|
2018-06-07 13:14:08 -04:00
|
|
|
|
2022-11-20 12:41:17 -05:00
|
|
|
if ":" in server_name:
|
|
|
|
# explicit port
|
|
|
|
out = server_name.rsplit(":", 1)
|
2018-06-07 13:14:08 -04:00
|
|
|
try:
|
|
|
|
port = int(out[1])
|
|
|
|
except ValueError:
|
2022-11-20 12:41:17 -05:00
|
|
|
raise ValueError("Invalid host:port '%s'" % (server_name,))
|
|
|
|
return out[0], port, out[0]
|
2019-06-13 09:49:25 -04:00
|
|
|
|
2023-09-05 15:45:39 -04:00
|
|
|
# Look up SRV for Matrix 1.8 `matrix-fed` service first
|
|
|
|
try:
|
|
|
|
srv = srvlookup.lookup("matrix-fed", "tcp", server_name)[0]
|
|
|
|
print(
|
|
|
|
f"SRV lookup on _matrix-fed._tcp.{server_name} gave {srv}",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
return srv.host, srv.port, server_name
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
# Fall back to deprecated `matrix` service
|
2018-06-07 13:14:08 -04:00
|
|
|
try:
|
2022-11-20 12:41:17 -05:00
|
|
|
srv = srvlookup.lookup("matrix", "tcp", server_name)[0]
|
|
|
|
print(
|
|
|
|
f"SRV lookup on _matrix._tcp.{server_name} gave {srv}",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
|
|
|
return srv.host, srv.port, server_name
|
2018-10-19 20:16:55 -04:00
|
|
|
except Exception:
|
2023-09-05 15:45:39 -04:00
|
|
|
# Fall even further back to just port 8448
|
2022-11-20 12:41:17 -05:00
|
|
|
return server_name, 8448, server_name
|
2018-06-07 13:14:08 -04:00
|
|
|
|
2019-06-13 09:49:25 -04:00
|
|
|
@staticmethod
|
2022-11-20 12:41:17 -05:00
|
|
|
def _get_well_known(server_name: str) -> Optional[str]:
|
|
|
|
if ":" in server_name:
|
|
|
|
# explicit port, or ipv6 literal. Either way, no .well-known
|
|
|
|
return None
|
|
|
|
|
|
|
|
# TODO: check for ipv4 literals
|
|
|
|
|
|
|
|
uri = f"https://{server_name}/.well-known/matrix/server"
|
|
|
|
print(f"fetching {uri}", file=sys.stderr)
|
2019-06-13 09:49:25 -04:00
|
|
|
|
|
|
|
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'")
|
2019-06-20 05:32:02 -04:00
|
|
|
new_name = parsed_well_known["m.server"]
|
|
|
|
print("well-known lookup gave %s" % (new_name,), file=sys.stderr)
|
2019-06-13 09:49:25 -04:00
|
|
|
return new_name
|
|
|
|
|
|
|
|
except Exception as e:
|
2019-06-20 05:32:02 -04:00
|
|
|
print("Invalid response from %s: %s" % (uri, e), file=sys.stderr)
|
2019-06-13 09:49:25 -04:00
|
|
|
return None
|
|
|
|
|
2018-06-07 13:14:08 -04:00
|
|
|
|
2015-05-06 06:46:02 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|