Files
bubble/server/bubble_relay.py

180 lines
7.4 KiB
Python

#!/usr/bin/env python3
"""bubble-relay: a mailbox between the desktop app and the Rabbit R1. Runs on a
server of yours that both can reach (a tailscale address is the easy way).
It never sees a message: every blob is AES-256-GCM sealed by the apps with a
key the server doesn't have (the desktop makes it and hands it to the Rabbit over
USB). This only stores opaque base64 strings, hands each device what the
other one posted, and forgets them once picked up (or after KEEP_DAYS).
Standard library only. Bind it to the tailscale address; every request but
/health needs "Authorization: Bearer <token>" (made on first start in
<data>/token, mode 600).
GET /health -> {"ok": true}
POST /sync {"device": "desktop"|"rabbit", "since": <cursor>, "push": ["<blob>", ...]}
-> {"cursor": <n>, "events": ["<blob>", ...], "more": bool}
`since` is the cursor the last answer gave; it also acknowledges everything up
to it, which the other side posted, so those are deleted. `more` = there's
more than fits in one answer: call again with the new cursor. A cursor past
anything this server ever issued (a wiped server) means "from the start".
python3 bubble_relay.py --host <tailscale address> --port 8080 --data DIR
"""
import argparse
import json
import os
import re
import secrets
import sqlite3
import sys
import threading
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
DEVICES = ("desktop", "rabbit")
KEEP_DAYS = 14
MAX_BODY = 24 * 1024 * 1024 # one request
MAX_BLOB = 12 * 1024 * 1024 # one sealed blob (a few photos)
MAX_PUSH = 2000 # blobs in one request
MAX_ANSWER = 8 * 1024 * 1024 # roughly, per answer (`more` past it)
MAX_STORED = 512 * 1024 * 1024 # everything waiting; pushes past it are refused
BLOB_RE = re.compile(r"^[A-Za-z0-9+/]+={0,2}$")
LOCK = threading.Lock()
class Store:
def __init__(self, path):
self.db = sqlite3.connect(path, check_same_thread=False)
self.db.execute("PRAGMA journal_mode=WAL")
self.db.execute("CREATE TABLE IF NOT EXISTS events (seq INTEGER PRIMARY KEY AUTOINCREMENT,"
" author TEXT NOT NULL, blob TEXT NOT NULL, size INTEGER NOT NULL, created INTEGER NOT NULL)")
self.db.commit()
def issued(self):
"""The highest cursor ever handed out (survives deletions)."""
row = self.db.execute("SELECT seq FROM sqlite_sequence WHERE name = 'events'").fetchone()
return row[0] if row else 0
def stored(self):
return self.db.execute("SELECT COALESCE(SUM(size), 0) FROM events").fetchone()[0]
def sync(self, device, since, pushed):
with LOCK:
now = int(time.time())
issued = self.issued()
if since > issued:
since = 0
# Picked up by this device: gone. Too old for anyone: gone.
self.db.execute("DELETE FROM events WHERE author != ? AND seq <= ?", (device, since))
self.db.execute("DELETE FROM events WHERE created < ?", (now - KEEP_DAYS * 86400,))
if pushed:
incoming = sum(len(b) for b in pushed)
if self.stored() + incoming > MAX_STORED:
self.db.commit()
return None
self.db.executemany("INSERT INTO events (author, blob, size, created) VALUES (?, ?, ?, ?)",
[(device, b, len(b), now) for b in pushed])
self.db.commit()
events, size, cursor, more = [], 0, since, False
for seq, blob in self.db.execute("SELECT seq, blob FROM events WHERE author != ? AND seq > ? ORDER BY seq",
(device, since)):
if events and size + len(blob) > MAX_ANSWER:
more = True
break
events.append(blob)
size += len(blob)
cursor = seq
if not more:
# Nothing left for this device: skip past the other device's own posts too.
cursor = max(cursor, self.issued())
return {"cursor": cursor, "events": events, "more": more}
class Handler(BaseHTTPRequestHandler):
server_version = "bubble-relay"
sys_version = ""
timeout = 60 # a stalled client doesn't hold a thread forever
def _send(self, code, obj):
body = json.dumps(obj, separators=(",", ":")).encode()
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.send_header("Cache-Control", "no-store")
self.end_headers()
self.wfile.write(body)
def _authed(self):
got = self.headers.get("Authorization", "")
return secrets.compare_digest(got.encode(), ("Bearer " + self.server.token).encode())
def do_GET(self):
if self.path == "/health":
return self._send(200, {"ok": True})
self._send(404, {"error": "not found"})
def do_POST(self):
if self.path != "/sync":
return self._send(404, {"error": "not found"})
if not self._authed():
return self._send(401, {"error": "unauthorized"})
try:
n = int(self.headers.get("Content-Length") or 0)
except ValueError:
return self._send(400, {"error": "bad length"})
if n < 0 or n > MAX_BODY:
return self._send(413, {"error": "too large"})
try:
req = json.loads(self.rfile.read(n) or b"{}")
device = req.get("device")
since = int(req.get("since", 0))
push = req.get("push", [])
if device not in DEVICES:
raise ValueError("device")
if since < 0:
raise ValueError("since")
if not isinstance(push, list) or len(push) > MAX_PUSH:
raise ValueError("push")
for b in push:
if not isinstance(b, str) or not b or len(b) > MAX_BLOB or not BLOB_RE.match(b):
raise ValueError("blob")
except (ValueError, TypeError, AttributeError) as e:
return self._send(400, {"error": "bad request: %s" % e})
out = self.server.store.sync(device, since, push)
if out is None:
return self._send(507, {"error": "relay full"})
self._send(200, out)
def log_message(self, fmt, *args):
# Method, path and status only: nothing a request carries.
sys.stderr.write("%s %s\n" % (self.log_date_time_string(), fmt % args))
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--host", default="127.0.0.1")
ap.add_argument("--port", type=int, default=8080)
ap.add_argument("--data", required=True)
a = ap.parse_args()
os.makedirs(a.data, mode=0o700, exist_ok=True)
os.chmod(a.data, 0o700)
os.umask(0o077)
tok_path = os.path.join(a.data, "token")
if not os.path.exists(tok_path):
fd = os.open(tok_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(fd, "w") as f:
f.write(secrets.token_urlsafe(32))
srv = ThreadingHTTPServer((a.host, a.port), Handler)
srv.daemon_threads = True
srv.token = open(tok_path).read().strip()
srv.store = Store(os.path.join(a.data, "relay.db"))
print("bubble-relay on %s:%d" % (a.host, a.port), flush=True)
srv.serve_forever()
if __name__ == "__main__":
main()