aboutsummaryrefslogtreecommitdiff
path: root/server.py
diff options
context:
space:
mode:
authorAnthony Wang2023-01-18 06:23:36 +0000
committerAnthony Wang2023-01-18 06:23:36 +0000
commita2e773ebd1a64ac67479ceb05ee3836957ac0e7b (patch)
treed332f5a166ec4064b6245914bc1a9cdd83d2a41f /server.py
parentf4631349520ae823843ae5f5f3adda42ad047fae (diff)
Add latest working code
Diffstat (limited to 'server.py')
-rw-r--r--server.py61
1 files changed, 43 insertions, 18 deletions
diff --git a/server.py b/server.py
index dc8f1c8..86f4872 100644
--- a/server.py
+++ b/server.py
@@ -9,6 +9,9 @@ from os.path import isfile
from urllib.parse import quote_plus
+domain = 'https://0.exozy.me'
+
+
def collection_append(file, item):
with open(file) as f:
collection = load(f)
@@ -18,6 +21,20 @@ def collection_append(file, item):
dump(collection, f)
+def iri_to_actor(iri):
+ if domain in iri:
+ name = search(f'^{domain}/users/(.*?)#main-key$', iri).group(1)
+ actorfile = f'users/{name}'
+ else:
+ actorfile = f'users/{quote_plus(iri)}'
+ if not isfile(actorfile):
+ with open(actorfile, 'w') as f:
+ resp = get(iri, headers={'Accept': 'application/activity+json'})
+ f.write(resp.text)
+ with open(actorfile) as f:
+ return load(f)
+
+
class fuwuqi(SimpleHTTPRequestHandler):
def do_POST(self):
body = self.rfile.read(int(self.headers['Content-Length']))
@@ -26,21 +43,22 @@ class fuwuqi(SimpleHTTPRequestHandler):
print(self.headers)
print(self.path)
+ username = search('^/users/(.*)\.(in|out)box$', self.path).group(1)
+
# Get actor public key
keyid = search('keyId="(.*?)"', self.headers['Signature']).group(1)
- actorfile = f'users/{quote_plus(keyid)}'
- if not isfile(actorfile):
- with open(actorfile, 'w') as f:
- f.write(get(keyid).text)
- with open(actorfile) as f:
- pubkeypem = load(f)['publicKey']['publicKeyPem'].encode('utf8')
- pubkey = serialization.load_pem_public_key(pubkeypem, None)
+ actor = iri_to_actor(keyid)
+ pubkeypem = actor['publicKey']['publicKeyPem'].encode('utf8')
+ pubkey = serialization.load_pem_public_key(pubkeypem, None)
# Assemble headers
headers = search('headers="(.*?)"', self.headers['Signature']).group(1)
message = ''
for header in headers.split():
- headerval = self.headers[header]
+ if header == '(request-target)':
+ headerval = f'post {self.path}'
+ else:
+ headerval = self.headers[header]
message += f'{header}: {headerval}\n'
# Verify HTTP signature
@@ -61,27 +79,34 @@ class fuwuqi(SimpleHTTPRequestHandler):
self.send_response(401)
return
- username = search('^/users/(.*)\.(in|out)box$', self.path).group(1)
if self.path.endswith('inbox'):
# S2S
- id = activity['id'].split('/')[-1]
- with open(f'users/{username}/{id}', 'w') as f:
- dump(activity, f)
+ collection_append(f'users/{username}.inbox', activity)
elif self.path.endswith('outbox'):
# C2S
collection_append(f'users/{username}.outbox', activity)
-
if activity['type'] == 'Create':
id = activity['id'].split('/')[-1]
with open(f'users/{username}.statuses/{id}', 'w') as f:
dump(activity['object'], f)
- print(self.headers)
- print(body)
- resp = post('https://social.exozy.me/users/a/inbox', headers=self.headers, data=body)
- print(resp)
- print(resp.text)
+ # Send to followers
+ with open(f'users/{username}.following') as f:
+ for followed in load(f)['orderedItems']:
+ actor = iri_to_actor(followed)
+ self.headers['Host'] = followed.split('/')[2]
+ resp = post(actor['inbox'], headers=self.headers, data=body)
+ print(resp)
+ print(resp.text)
+ elif activity['type'] == 'Follow':
+ object = iri_to_actor(activity['object'])
+ self.headers['Host'] = activity['object'].split('/')[2]
+ resp = post(object['inbox'], headers=self.headers, data=body)
+ print(resp)
+ print(resp.text)
+ collection_append(f'users/{username}.following', activity['object'])
self.send_response(200)
+ self.end_headers()
ThreadingHTTPServer(('localhost', 4200), fuwuqi).serve_forever()