dashPi: a self-hosted home dashboard, its sync server and phone app
The dashboard (Raspberry Pi 3.5" screen + web pages), the end-to-end encrypted sync mailbox (PHP + SQLite) and the Android companion app (Flutter), with docs, clean-install checks and screenshots made from demo data. Built mostly with an AI coding assistant; not yet reviewed line by line by a human (see README and SECURITY.md). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
fb3c01cdd7
333 changed files with 45151 additions and 0 deletions
109
server/PROTOCOL.md
Normal file
109
server/PROTOCOL.md
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
# dash-sync protocol, version 1
|
||||
|
||||
How the dashboard and the app keep their own copies of the data in step through this
|
||||
server. The server is a mailbox: it stores encrypted batches in order and hands them out; it can't
|
||||
read them and never merges anything. Every device does the merging itself, the same way.
|
||||
|
||||
Reference implementation: `dashboard/sync.py` (client) + `dashboard/syncdb.py` (stamps and merging).
|
||||
|
||||
## Devices, accounts, tokens
|
||||
|
||||
- An **account** is one person's data (e.g. `me`). A **device** belongs to one account and has a
|
||||
name (`dashboard`, `phone`) and a secret **token** (`php bin/admin.php add-device me phone`).
|
||||
- Every request: `Authorization: Bearer <token>`, over HTTPS.
|
||||
- Each device also makes itself a random **device id** (12 hex characters) when it first starts.
|
||||
It goes into every change it makes (`d`) and every batch (`dev`); it is not the server's device name.
|
||||
|
||||
## HTTP
|
||||
|
||||
| Request | Answer |
|
||||
|---|---|
|
||||
| `GET /v1/hello` | `{"account", "device", "latest", "server": "dash-sync/1"}` — `device` = the server's name for you |
|
||||
| `GET /v1/key` | the wrapped data key (JSON below); `404` until the first device sets it |
|
||||
| `PUT /v1/key` | store it; `409` if there is one — `PUT /v1/key?replace=1` to change the passphrase (only the device that set it: `403` otherwise; the old one is kept on the server) |
|
||||
| `POST /v1/batches` | body = one encrypted batch (`application/octet-stream`, ≤ 8 MB) → `{"seq"}`; `507` when the account is full |
|
||||
| `GET /v1/batches?after=N&limit=50` | `{"batches": [{"seq", "device", "at", "size", "blob"}], "latest"}`, oldest first, `blob` base64; fewer than `limit` when they're big (≤ 32 MB an answer): ask again until empty |
|
||||
| `GET /v1/devices` | `{"devices": [{"name", "created", "last_seen", "revoked", "you", "manager"}], "manager"}` — `manager` = the device that set the key (it may manage the others) |
|
||||
| `POST /v1/devices` | `{"name"}` → `{"name", "token"}`, the new device's token, shown once; the managing device only (`403`), `409` = name taken (a revoked one is reused) |
|
||||
| `POST /v1/devices/revoke` | `{"name"}` → its token stops working; the managing device only, not itself |
|
||||
| `GET /v1/app` | the newest phone app build `{"version", "build", "size", "sha256", "published", "notes"}`, 404 = none |
|
||||
| `GET /v1/app/apk` | that APK (`application/vnd.android.package-archive`); uploaded over SSH by `app/tool/publish.sh` |
|
||||
| `GET /v1/food/search?q=…` | `{"items": [food]}` from the Open Food Facts archive — `q` (every word a prefix of the name / brand / category, ≥ 3 letters unless a filter is given), `brand`, `country` (a name or `ro` / `sk` / `de` …), `category`, `nutriscore` (`a`, `ab` …), `barcode` (exact; merged duplicates' barcodes too), `limit` ≤ 50. A food = the dashboard's shape (`ref` "usda:ID", `kind` "off", `per100`, `portions` [{label, grams}], brand, barcode, countries, nutriscore, nova, allergens, labels, ingredients) + the added-sugar facts `sugars_in`, `dairy`, `added_sugar` (the clients' sugar.py / sugar.dart decide). Not account data: any device may search |
|
||||
| `GET /v1/food/info` | `{"products", "built", "source"}`; 404 = no archive uploaded (`food/off-archive.db`, built by the dashboard's `plugins/food/off.py --archive`) |
|
||||
|
||||
Errors: `{"error": "…"}` with 400 / 401 / 403 / 404 / 409 / 413 / 500 / 507.
|
||||
|
||||
## Keys
|
||||
|
||||
- **Data key**: 32 random bytes, made once by the first device. Everything is encrypted with it.
|
||||
Devices keep it locally (the Pi: `dash_data/sync/state.json`, chmod 600; the app: the platform's
|
||||
secure storage).
|
||||
- **Wrapped key** (on the server, so a new device can join with the passphrase):
|
||||
|
||||
```json
|
||||
{"v": 1, "kdf": "argon2id13", "ops": 3, "mem": 67108864,
|
||||
"salt": "<16 bytes, base64>", "nonce": "<24 bytes, base64>", "box": "<base64>"}
|
||||
```
|
||||
`kek = Argon2id(passphrase UTF-8, salt, opslimit=ops, memlimit=mem, 32 bytes)` (libsodium
|
||||
`crypto_pwhash`, ALG_ARGON2ID13); `box = XChaCha20-Poly1305-IETF(kek, nonce, data key, ad = "dash-sync/key/1")`.
|
||||
Optional `"check"`: a sealed batch-format blob (`"DS1\0"` + nonce + ciphertext) of the bytes `ok` under the data key with
|
||||
ad = `"dash-sync/key-check/1"` (base64) — lets a device test a recovery key without the passphrase. Wrapped keys
|
||||
made before it have none; a device writing the key adds it.
|
||||
- **Recovery key**: the data key itself in base32 (RFC 4648, no padding), in groups of 4 with
|
||||
dashes. With it a device can wrap the key again under a new passphrase.
|
||||
|
||||
## A batch
|
||||
|
||||
On the wire: `"DS1\0"` (4 bytes) + nonce (24 random bytes) +
|
||||
`XChaCha20-Poly1305-IETF(data key, nonce, plaintext, ad = "dash-sync/1/<account>")`.
|
||||
|
||||
Plaintext = zlib (deflate, RFC 1950) of UTF-8 JSON:
|
||||
|
||||
```json
|
||||
{"v": 1, "dev": "aa5403e5376a", "made": 1792482314874, "changes": [
|
||||
{"t": "food.entry", "u": "91019d2e…", "h": 1792482314899, "d": "aa5403e5376a",
|
||||
"row": {"day": "2026-10-20", "meal": "snack", "name": "Apple", "kcal": 60, "my_food_id": "0aef8cce…", …}},
|
||||
{"t": "food.entry", "u": "91019d2e…", "h": 1792482314917, "d": "aa5403e5376a", "del": true}
|
||||
]}
|
||||
```
|
||||
|
||||
- `t` = `<plugin>.<table>`; `u` = the row's id (`_uid`); `h` = its change stamp (`_hlc`); `d` = the device that made the change.
|
||||
- `row` = every column but the local integer id; foreign keys (`refs`) and ids inside text
|
||||
(`refs_in`, e.g. `"my:~<uid>"` in a meal's recipe) are already ids — see each plugin's
|
||||
`META["sync"]` for which columns. Columns a device doesn't know are ignored.
|
||||
- At most 2000 changes a batch; a device sends its changes since its last send, in stamp order.
|
||||
|
||||
## Merging (every device, the same rules)
|
||||
|
||||
- **Stamps** (`h`): milliseconds since 1970, but each new local stamp = max(now, last stamp + 1),
|
||||
and on receiving, the local clock moves up to the highest stamp seen. So stamps keep their order
|
||||
even if a device's clock is off.
|
||||
- **Last change wins**, per row: apply a change only if `(h, d)` is greater than the local row's
|
||||
`(_hlc, _dev)` (compare `h` first, then `d` as text). Otherwise skip it.
|
||||
- **Deletes** (`del`): keep a marker (table, id, h, d). Delete the local row if the marker wins over
|
||||
it. A row change that doesn't win over a marker for its id is skipped (it was deleted later
|
||||
elsewhere); a newer one brings the row back.
|
||||
- **Order inside a batch**: tables in the order the plugin lists them; rows whose reference (a
|
||||
parent node, a meal's ingredient) isn't there yet wait for another round; then the deletes.
|
||||
- **Natural ids**: some rows have an id made from what they are (`day:2026-10-07`,
|
||||
`measure:2026-10-17`, `src:<import id>`, `<node id>:<date>` for rates) so two devices making
|
||||
"the same" row agree on it.
|
||||
- **Reading**: page through `GET /v1/batches?after=<last seq read>` until it's empty; skip
|
||||
batches your own device sent (`dev`); remember the last `seq` only after a batch is applied
|
||||
(applying again is harmless).
|
||||
|
||||
## What syncs (dashboard v1)
|
||||
|
||||
| Table | Id |
|
||||
|---|---|
|
||||
| `food.food` (My foods + meals) | random |
|
||||
| `food.entry` (the diary) | `src:<src_id>` for imports, else random |
|
||||
| `cycle.day` | `day:<date>` |
|
||||
| `health.measure` | `measure:<date>` |
|
||||
| `health.vital` | `vital:<time>` for imports, else random |
|
||||
| `health.layout` | `layout:<owner>:<tab>` |
|
||||
| `workload.node`, `workload.entry` | random |
|
||||
| `workload.rate` | `<node id>:<since>` |
|
||||
|
||||
Planned: Health summaries from the Pi (nights, steps, heart-rate days, workouts — Pi → app only),
|
||||
snapshots so a new device needn't replay every batch.
|
||||
Loading…
Add table
Add a link
Reference in a new issue