Host our public key, and add proper ways to sign JSON + send requests.
This commit is contained in:
68
src/s2s.py
68
src/s2s.py
@@ -1,21 +1,20 @@
|
||||
from flask import Flask, jsonify, Response, request, send_file, abort, Blueprint
|
||||
from globals import vona_version, make_event_id, hash_event
|
||||
from config import *
|
||||
import requests
|
||||
import globals
|
||||
import json
|
||||
import time
|
||||
import os
|
||||
|
||||
server = Blueprint('matrix_server', __name__)
|
||||
|
||||
def send_join(request, roomId):
|
||||
# We may have to include signatures here
|
||||
# as well, which will be a pain
|
||||
# We may have to include signatures here.
|
||||
|
||||
eventIds = [
|
||||
f"${make_event_id()}:{server_name}",
|
||||
f"${make_event_id()}:{server_name}",
|
||||
f"${make_event_id()}:{server_name}",
|
||||
f"${make_event_id()}:{server_name}"
|
||||
f"${globals.make_event_id()}:{server_name}",
|
||||
f"${globals.make_event_id()}:{server_name}",
|
||||
f"${globals.make_event_id()}:{server_name}",
|
||||
f"${globals.make_event_id()}:{server_name}"
|
||||
]
|
||||
|
||||
events = {
|
||||
@@ -67,7 +66,7 @@ def send_join(request, roomId):
|
||||
}
|
||||
|
||||
for event in events['state']:
|
||||
event_hash = hash_event(event)
|
||||
event_hash = globals.hash_event(event)
|
||||
if event['type'] != "m.room.create":
|
||||
event['prev_events'] = [eventIds[event['origin_server_ts'] - 1], {"sha256": event_hash}]
|
||||
|
||||
@@ -75,6 +74,7 @@ def send_join(request, roomId):
|
||||
join_event['prev_events'] = [eventIds[3], {"sha256": events['state'][3]['prev_events'][1]['sha256']}]
|
||||
events['event'] = join_event
|
||||
|
||||
# debug
|
||||
print(json.dumps(events, indent='\t'))
|
||||
print(json.dumps(eventIds, indent='\t'))
|
||||
|
||||
@@ -82,12 +82,20 @@ def send_join(request, roomId):
|
||||
|
||||
@server.route('/_matrix/federation/v1/version')
|
||||
def version():
|
||||
return jsonify({"server": {"version": vona_version,"name": "Vona"}})
|
||||
return jsonify({"server": {"version": globals.vona_version,"name": "Vona"}})
|
||||
|
||||
@server.route('/_matrix/key/v2/server')
|
||||
def keys():
|
||||
# todo
|
||||
return jsonify({})
|
||||
return jsonify(globals.sign_json({
|
||||
"old_verify_keys": {},
|
||||
"server_name": server_name,
|
||||
"valid_until_ts": int(time.time() * 1000 + 604800000),
|
||||
"verify_keys": {
|
||||
f"ed25519:{signing_key.split()[1]}": {
|
||||
"key": globals.pubkey()
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
@server.route('/_matrix/federation/v1/query/directory')
|
||||
def room_query():
|
||||
@@ -116,6 +124,7 @@ def download_media(media_id):
|
||||
|
||||
response = Response(response_body, content_type=f'multipart/mixed; boundary={boundary}')
|
||||
response.status_code = 200
|
||||
|
||||
return response
|
||||
|
||||
@server.route('/_matrix/federation/v1/media/thumbnail/<media_id>')
|
||||
@@ -182,6 +191,7 @@ def user_profile():
|
||||
return jsonify({"displayname":"Vona"})
|
||||
else:
|
||||
return jsonify({"errcode": "M_NOT_FOUND","error": "The requested profile key does not exist."}), 404
|
||||
|
||||
return jsonify({"avatar_url": f"mxc://{server_name}/cat","displayname": "Vona"})
|
||||
|
||||
@server.route('/_matrix/federation/v1/user/devices/<user>')
|
||||
@@ -196,9 +206,39 @@ def user_devices(user):
|
||||
def user_keys():
|
||||
return jsonify({"device_keys":{f"@vona:{server_name}":{}}})
|
||||
|
||||
|
||||
# https://spec.matrix.org/v1.15/server-server-api/#inviting-to-a-room
|
||||
@server.route('/_matrix/federation/v2/invite/<room>/<txnId>', methods=['PUT'])
|
||||
def fed_invite_user(room, txnId):
|
||||
return jsonify({"errcode":"M_INCOMPATIBLE_ROOM_VERSION","error": "Don't touch my users mofo","room_version": str(the_funny_number)}), 400
|
||||
def invite_user_v2(room, txnId):
|
||||
# This endpoint is not allowed for room
|
||||
# versions over v2 as per spec, so any
|
||||
# invites received here can be discarded
|
||||
# safely.
|
||||
|
||||
return jsonify({
|
||||
"errcode": "M_INCOMPATIBLE_ROOM_VERSION",
|
||||
"error": "Vona only supports room version 2.",
|
||||
"room_version": str(the_funny_number)
|
||||
}), 400
|
||||
|
||||
@server.route('/_matrix/federation/v1/invite/<room>/<txnId>', methods=['PUT'])
|
||||
def invite_user(room, txnId):
|
||||
# TODO: Sign provided JSON and return it.
|
||||
|
||||
data = request.data.decode('utf-8')
|
||||
invite_data = json.loads(data)
|
||||
|
||||
if "room_version" in invite_data:
|
||||
if invite_data["room_version"] != "2":
|
||||
return jsonify({
|
||||
"errcode": "M_INCOMPATIBLE_ROOM_VERSION",
|
||||
"error": "Vona only supports room version 2.",
|
||||
"room_version": invite_data["room_version"]
|
||||
})
|
||||
|
||||
# Placeholder
|
||||
abort(500)
|
||||
|
||||
|
||||
@server.route('/_matrix/federation/v1/hierarchy/<roomId>')
|
||||
def space_hierachy(roomId):
|
||||
|
||||
Reference in New Issue
Block a user