-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhostr.py
71 lines (54 loc) · 1.96 KB
/
hostr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import sys
import logging
from aiohttp import web, ClientSession
from monstr.client.client import Client, ClientPool
from monstr.encrypt import Keys
SERVER_DOMAIN = "nostr.hu"
RELAY = "ws://127.0.0.1:8080"
BLOSSOM = "http://127.0.0.1:3000"
FILEMAP_KIND = "34128"
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
async def serve_file(request):
host = request.host
if ':' in host:
# Remove port from the end.
host = host.split(':')[0]
assert host.endswith(f".{SERVER_DOMAIN}"), f'web server configuration error: host={host}'
npub = host.split(f".{SERVER_DOMAIN}")[0]
path = request.path
if path.endswith("/"):
path += "index.html"
if path.startswith("/"):
path = path[1:]
try:
pubkey = Keys(pub_k=npub).public_key_hex()
except Exception as e:
log.debug(str(e))
raise web.HTTPNotFound(reason="Subdomain is not a valid npub.")
async with Client(RELAY) as c:
#TODO: make it a subscription, group authors per relay!
evs = await c.query({
'kinds': [FILEMAP_KIND],
'authors': [pubkey],
'#d': [path],
})
if len(evs) == 0:
raise web.HTTPNotFound(
reason=f"Nostr filemap event for [{path}] not found on relay [{RELAY}].")
ev = evs[0]
sha256 = ev.tags.get_tags_value("sha256")[0]
async with ClientSession() as sess:
async with sess.get(f"{BLOSSOM}/{sha256}") as resp:
if resp.status >= 300:
t = await resp.text()
return web.Response(
status=resp.status, text=f"Blossom server returned [{t}]")
data = await resp.read()
return web.Response(content_type=resp.content_type, body=data)
app = web.Application()
app.add_routes([
web.get("/", serve_file),
web.get("/{path:.*}", serve_file)
])
web.run_app(app, host='0.0.0.0', port=8000)