2017-08-17 17:54:27 +02: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.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import argparse
|
2018-10-20 02:16:55 +02:00
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
import sys
|
2018-06-07 19:14:08 +02:00
|
|
|
from urlparse import urlparse, urlunparse
|
|
|
|
|
2015-05-06 12:46:02 +02:00
|
|
|
import nacl.signing
|
|
|
|
import requests
|
|
|
|
import srvlookup
|
2017-08-17 17:54:27 +02:00
|
|
|
import yaml
|
2018-10-20 02:16:55 +02:00
|
|
|
from requests.adapters import HTTPAdapter
|
2015-05-06 12:46:02 +02:00
|
|
|
|
2018-06-07 19:14:08 +02:00
|
|
|
# uncomment the following to enable debug logging of http requests
|
2018-10-20 02:16:55 +02:00
|
|
|
# from httplib import HTTPConnection
|
|
|
|
# HTTPConnection.debuglevel = 1
|
|
|
|
|
2018-06-07 19:14:08 +02:00
|
|
|
|
2015-05-06 12:46:02 +02:00
|
|
|
def encode_base64(input_bytes):
|
|
|
|
"""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
|
|
|
|
|
|
|
|
|
|
|
|
def decode_base64(input_string):
|
|
|
|
"""Decode a base64 string to bytes inferring padding from the length of the
|
|
|
|
string."""
|
|
|
|
|
|
|
|
input_bytes = input_string.encode("ascii")
|
|
|
|
input_len = len(input_bytes)
|
|
|
|
padding = b"=" * (3 - ((input_len + 3) % 4))
|
|
|
|
output_len = 3 * ((input_len + 2) // 4) + (input_len + 2) % 4 - 2
|
|
|
|
output_bytes = base64.b64decode(input_bytes + padding)
|
|
|
|
return output_bytes[:output_len]
|
|
|
|
|
|
|
|
|
|
|
|
def encode_canonical_json(value):
|
|
|
|
return json.dumps(
|
2018-10-20 02:16:55 +02:00
|
|
|
value,
|
|
|
|
# Encode code-points outside of ASCII as UTF-8 rather than \u escapes
|
|
|
|
ensure_ascii=False,
|
|
|
|
# Remove unecessary white space.
|
|
|
|
separators=(',', ':'),
|
|
|
|
# Sort the keys of dictionaries.
|
|
|
|
sort_keys=True,
|
|
|
|
# Encode the resulting unicode as UTF-8 bytes.
|
|
|
|
).encode("UTF-8")
|
2015-05-06 12:46:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def sign_json(json_object, signing_key, signing_name):
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
NACL_ED25519 = "ed25519"
|
|
|
|
|
2018-10-20 02:16:55 +02:00
|
|
|
|
2015-05-06 12:46:02 +02:00
|
|
|
def decode_signing_key_base64(algorithm, version, key_base64):
|
|
|
|
"""Decode a base64 encoded signing key
|
|
|
|
Args:
|
|
|
|
algorithm (str): The algorithm the key is for (currently "ed25519").
|
|
|
|
version (str): Identifies this key out of the keys for this entity.
|
|
|
|
key_base64 (str): Base64 encoded bytes of the key.
|
|
|
|
Returns:
|
|
|
|
A SigningKey object.
|
|
|
|
"""
|
|
|
|
if algorithm == NACL_ED25519:
|
|
|
|
key_bytes = decode_base64(key_base64)
|
|
|
|
key = nacl.signing.SigningKey(key_bytes)
|
|
|
|
key.version = version
|
|
|
|
key.alg = NACL_ED25519
|
|
|
|
return key
|
|
|
|
else:
|
|
|
|
raise ValueError("Unsupported algorithm %s" % (algorithm,))
|
|
|
|
|
|
|
|
|
|
|
|
def read_signing_keys(stream):
|
|
|
|
"""Reads a list of keys from a stream
|
|
|
|
Args:
|
|
|
|
stream : A stream to iterate for keys.
|
|
|
|
Returns:
|
|
|
|
list of SigningKey objects.
|
|
|
|
"""
|
|
|
|
keys = []
|
|
|
|
for line in stream:
|
|
|
|
algorithm, version, key_base64 = line.split()
|
|
|
|
keys.append(decode_signing_key_base64(algorithm, version, key_base64))
|
|
|
|
return keys
|
|
|
|
|
|
|
|
|
2017-11-27 11:06:59 +01:00
|
|
|
def request_json(method, origin_name, origin_key, destination, path, content):
|
|
|
|
if method is None:
|
|
|
|
if content is None:
|
|
|
|
method = "GET"
|
|
|
|
else:
|
|
|
|
method = "POST"
|
|
|
|
|
|
|
|
json_to_sign = {
|
|
|
|
"method": method,
|
2015-05-06 12:46:02 +02:00
|
|
|
"uri": path,
|
|
|
|
"origin": origin_name,
|
|
|
|
"destination": destination,
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:06:59 +01: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 12:46:02 +02:00
|
|
|
|
|
|
|
authorization_headers = []
|
|
|
|
|
|
|
|
for key, sig in signed_json["signatures"][origin_name].items():
|
2018-10-20 02:16:55 +02:00
|
|
|
header = "X-Matrix origin=%s,key=\"%s\",sig=\"%s\"" % (origin_name, key, sig)
|
2016-08-02 16:06:08 +02:00
|
|
|
authorization_headers.append(bytes(header))
|
2018-10-20 02:16:55 +02:00
|
|
|
print("Authorization: %s" % header, file=sys.stderr)
|
2017-08-17 17:54:27 +02:00
|
|
|
|
2018-06-07 19:14:08 +02:00
|
|
|
dest = "matrix://%s%s" % (destination, path)
|
2018-10-20 02:16:55 +02:00
|
|
|
print("Requesting %s" % dest, file=sys.stderr)
|
2015-05-06 12:46:02 +02:00
|
|
|
|
2018-06-07 19:14:08 +02:00
|
|
|
s = requests.Session()
|
|
|
|
s.mount("matrix://", MatrixConnectionAdapter())
|
|
|
|
|
|
|
|
result = s.request(
|
2017-11-27 11:06:59 +01:00
|
|
|
method=method,
|
|
|
|
url=dest,
|
2018-10-20 02:16:55 +02:00
|
|
|
headers={"Host": destination, "Authorization": authorization_headers[0]},
|
2015-05-06 12:46:02 +02:00
|
|
|
verify=False,
|
2017-11-27 11:06:59 +01:00
|
|
|
data=content,
|
2015-05-06 12:46:02 +02:00
|
|
|
)
|
2016-08-03 14:46:47 +02:00
|
|
|
sys.stderr.write("Status Code: %d\n" % (result.status_code,))
|
2015-05-06 12:46:02 +02:00
|
|
|
return result.json()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2017-08-17 17:54:27 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
2018-10-20 02:16:55 +02:00
|
|
|
description="Signs and sends a federation request to a matrix homeserver"
|
2017-08-17 17:54:27 +02:00
|
|
|
)
|
|
|
|
|
2017-08-22 12:19:30 +02:00
|
|
|
parser.add_argument(
|
2018-10-20 02:16:55 +02:00
|
|
|
"-N",
|
|
|
|
"--server-name",
|
2017-08-22 12:19:30 +02:00
|
|
|
help="Name to give as the local homeserver. If unspecified, will be "
|
2018-10-20 02:16:55 +02:00
|
|
|
"read from the config file.",
|
2017-08-22 12:19:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2018-10-20 02:16:55 +02:00
|
|
|
"-k",
|
|
|
|
"--signing-key-path",
|
2017-08-22 12:19:30 +02:00
|
|
|
help="Path to the file containing the private ed25519 key to sign the "
|
2018-10-20 02:16:55 +02:00
|
|
|
"request with.",
|
2017-08-22 12:19:30 +02:00
|
|
|
)
|
|
|
|
|
2017-08-17 17:54:27 +02:00
|
|
|
parser.add_argument(
|
2018-10-20 02:16:55 +02:00
|
|
|
"-c",
|
|
|
|
"--config",
|
2017-08-17 17:54:27 +02:00
|
|
|
default="homeserver.yaml",
|
2017-08-22 12:19:30 +02:00
|
|
|
help="Path to server config file. Ignored if --server-name and "
|
2018-10-20 02:16:55 +02:00
|
|
|
"--signing-key-path are both given.",
|
2017-08-17 17:54:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2018-10-20 02:16:55 +02:00
|
|
|
"-d",
|
|
|
|
"--destination",
|
2017-08-17 17:54:27 +02:00
|
|
|
default="matrix.org",
|
|
|
|
help="name of the remote homeserver. We will do SRV lookups and "
|
2018-10-20 02:16:55 +02:00
|
|
|
"connect appropriately.",
|
2017-08-17 17:54:27 +02:00
|
|
|
)
|
|
|
|
|
2017-11-27 11:06:59 +01:00
|
|
|
parser.add_argument(
|
2018-10-20 02:16:55 +02:00
|
|
|
"-X",
|
|
|
|
"--method",
|
2017-11-27 11:06:59 +01:00
|
|
|
help="HTTP method to use for the request. Defaults to GET if --data is"
|
2018-10-20 02:16:55 +02:00
|
|
|
"unspecified, POST if it is.",
|
2017-11-27 11:06:59 +01:00
|
|
|
)
|
|
|
|
|
2018-10-20 02:16:55 +02:00
|
|
|
parser.add_argument("--body", help="Data to send as the body of the HTTP request")
|
2017-11-27 11:06:59 +01:00
|
|
|
|
2017-08-17 17:54:27 +02:00
|
|
|
parser.add_argument(
|
2018-10-20 02:16:55 +02:00
|
|
|
"path", help="request path. We will add '/_matrix/federation/v1/' to this."
|
2017-08-17 17:54:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2017-08-22 12:19:30 +02:00
|
|
|
if not args.server_name or not args.signing_key_path:
|
|
|
|
read_args_from_config(args)
|
2015-05-06 12:46:02 +02:00
|
|
|
|
2017-08-22 12:19:30 +02:00
|
|
|
with open(args.signing_key_path) as f:
|
2015-05-06 12:46:02 +02:00
|
|
|
key = read_signing_keys(f)[0]
|
|
|
|
|
2017-11-27 11:06:59 +01:00
|
|
|
result = request_json(
|
|
|
|
args.method,
|
2018-10-20 02:16:55 +02:00
|
|
|
args.server_name,
|
|
|
|
key,
|
|
|
|
args.destination,
|
2017-11-27 11:06:59 +01:00
|
|
|
"/_matrix/federation/v1/" + args.path,
|
|
|
|
content=args.body,
|
2015-05-06 12:46:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
json.dump(result, sys.stdout)
|
2018-10-20 02:16:55 +02:00
|
|
|
print("")
|
2015-05-06 12:46:02 +02:00
|
|
|
|
2017-08-22 12:19:30 +02:00
|
|
|
|
|
|
|
def read_args_from_config(args):
|
|
|
|
with open(args.config, 'r') as fh:
|
|
|
|
config = yaml.safe_load(fh)
|
|
|
|
if not args.server_name:
|
|
|
|
args.server_name = config['server_name']
|
|
|
|
if not args.signing_key_path:
|
|
|
|
args.signing_key_path = config['signing_key_path']
|
|
|
|
|
|
|
|
|
2018-06-07 19:14:08 +02:00
|
|
|
class MatrixConnectionAdapter(HTTPAdapter):
|
|
|
|
@staticmethod
|
|
|
|
def lookup(s):
|
|
|
|
if s[-1] == ']':
|
|
|
|
# ipv6 literal (with no port)
|
|
|
|
return s, 8448
|
|
|
|
|
|
|
|
if ":" in s:
|
2018-10-20 02:16:55 +02:00
|
|
|
out = s.rsplit(":", 1)
|
2018-06-07 19:14:08 +02:00
|
|
|
try:
|
|
|
|
port = int(out[1])
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError("Invalid host:port '%s'" % s)
|
|
|
|
return out[0], port
|
|
|
|
|
|
|
|
try:
|
|
|
|
srv = srvlookup.lookup("matrix", "tcp", s)[0]
|
|
|
|
return srv.host, srv.port
|
2018-10-20 02:16:55 +02:00
|
|
|
except Exception:
|
2018-06-07 19:14:08 +02:00
|
|
|
return s, 8448
|
|
|
|
|
|
|
|
def get_connection(self, url, proxies=None):
|
|
|
|
parsed = urlparse(url)
|
|
|
|
|
|
|
|
(host, port) = self.lookup(parsed.netloc)
|
|
|
|
netloc = "%s:%d" % (host, port)
|
|
|
|
print("Connecting to %s" % (netloc,), file=sys.stderr)
|
2018-10-20 02:16:55 +02:00
|
|
|
url = urlunparse(
|
|
|
|
("https", netloc, parsed.path, parsed.params, parsed.query, parsed.fragment)
|
|
|
|
)
|
2018-06-07 19:14:08 +02:00
|
|
|
return super(MatrixConnectionAdapter, self).get_connection(url, proxies)
|
|
|
|
|
|
|
|
|
2015-05-06 12:46:02 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|