v0.5.0: bubble for Android 0.5.0 — text from the Rabbit's SIM on the desktop, and chat names and read status sync, through jai
A relay on jai (server/bubble_relay.py, stdlib + SQLite, tailscale address only, bearer token) carries sealed blobs between the desktop and the Rabbit: AES-256-GCM with a key the desktop makes and hands over USB (Sync to Rabbit), so jai can neither read nor forge anything. It deletes what's been picked up, and anything after 14 days. - Desktop (src/main/relay.ts): "Rabbit SIM" is a number to text from, groups included; the SIM's history and new texts arrive; older photos are fetched when shown; names and read/unread/archived go both ways. Settings → Rabbit relay connects it. - Rabbit (Relay.java, RelayJob): no timer of its own for the battery; it rides along with the background checks, plus one network job after a text, a send or a swipe. Each send from the desktop goes out at most once and not after a day. Plain http only to jai and localhost (network_security_config). - scripts/deploy-jai.sh; tests: server unit tests, an end-to-end relay test, and DEVICE=1 relay.device.test.ts against the dev app.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
"""python3 -m unittest server/test_bubble_relay.py (a real server on localhost, made-up blobs)"""
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
class RelayTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.dir = tempfile.mkdtemp()
|
||||
s = socket.socket()
|
||||
s.bind(("127.0.0.1", 0))
|
||||
cls.port = s.getsockname()[1]
|
||||
s.close()
|
||||
cls.proc = subprocess.Popen([sys.executable, os.path.join(HERE, "bubble_relay.py"), "--port", str(cls.port),
|
||||
"--data", cls.dir], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
|
||||
for _ in range(50):
|
||||
try:
|
||||
urllib.request.urlopen("http://127.0.0.1:%d/health" % cls.port, timeout=1)
|
||||
break
|
||||
except OSError:
|
||||
time.sleep(0.1)
|
||||
cls.token = open(os.path.join(cls.dir, "token")).read().strip()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.proc.terminate()
|
||||
cls.proc.wait()
|
||||
|
||||
def call(self, body, token=None, raw=None):
|
||||
data = raw if raw is not None else json.dumps(body).encode()
|
||||
req = urllib.request.Request("http://127.0.0.1:%d/sync" % self.port, data=data, method="POST",
|
||||
headers={"Authorization": "Bearer " + (token or self.token)})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=5) as r:
|
||||
return r.status, json.loads(r.read())
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, json.loads(e.read())
|
||||
|
||||
def test_flow(self):
|
||||
st, d = self.call({"device": "desktop", "since": 0, "push": ["QUJD", "REVG"]})
|
||||
self.assertEqual(st, 200)
|
||||
self.assertEqual(d["events"], []) # never your own
|
||||
st, r = self.call({"device": "rabbit", "since": 0, "push": ["R0hJ"]})
|
||||
self.assertEqual(r["events"], ["QUJD", "REVG"])
|
||||
st, d2 = self.call({"device": "desktop", "since": d["cursor"]})
|
||||
self.assertEqual(d2["events"], ["R0hJ"])
|
||||
# Acknowledged by the rabbit: deleted, so nobody gets them again.
|
||||
self.call({"device": "rabbit", "since": r["cursor"]})
|
||||
st, again = self.call({"device": "rabbit", "since": 0})
|
||||
self.assertEqual(again["events"], [])
|
||||
# A cursor from a wiped server means "from the start".
|
||||
st, w = self.call({"device": "desktop", "since": 10 ** 9})
|
||||
self.assertEqual(w["events"], ["R0hJ"])
|
||||
|
||||
def test_rejects(self):
|
||||
self.assertEqual(self.call({"device": "desktop", "since": 0}, token="wrong")[0], 401)
|
||||
self.assertEqual(self.call({"device": "laptop", "since": 0})[0], 400)
|
||||
self.assertEqual(self.call({"device": "desktop", "since": 0, "push": ["not base64!"]})[0], 400)
|
||||
self.assertEqual(self.call({"device": "desktop", "since": 0, "push": [5]})[0], 400)
|
||||
self.assertEqual(self.call(None, raw=b"{nope")[0], 400)
|
||||
with urllib.request.urlopen("http://127.0.0.1:%d/health" % self.port) as r:
|
||||
self.assertEqual(json.loads(r.read()), {"ok": True})
|
||||
|
||||
def test_token_file_private(self):
|
||||
self.assertEqual(os.stat(os.path.join(self.dir, "token")).st_mode & 0o777, 0o600)
|
||||
self.assertEqual(os.stat(self.dir).st_mode & 0o777, 0o700)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user