# The sync server A mailbox: devices send it encrypted batches of changes and read each other's. It stores them in order and can't read them, because the key never leaves the devices. Plain PHP + SQLite, no daemon. The protocol is in [server/PROTOCOL.md](../server/PROTOCOL.md). ``` server/public/index.php the API (the only thing the web server serves) server/public/.htaccess Apache rewrite server/src/db.php storage (one SQLite file) server/src/food.php the optional food search server/bin/admin.php accounts + device tokens (command line) server/bin/setup-server.sh one-time setup for Apache + certbot server/config.example.php → config.php ``` **Needs:** PHP 8.1+ with `pdo_sqlite` (Debian: `php-sqlite3`), HTTPS, and its own (sub)domain, which is simplest. ## Quick setup (Debian/Ubuntu + Apache + certbot) ```bash sudo apt install apache2 libapache2-mod-php php-sqlite3 certbot python3-certbot-apache rsync sudo rsync -a server/ /srv/dash-sync/ # anywhere, but NOT inside another site's folder sudo bash /srv/dash-sync/bin/setup-server.sh sync.example.com me ``` It creates `data/` (owned by the web server, mode 700), turns on Apache's `mod_rewrite`, an Apache site for `sync.example.com` with `DocumentRoot /srv/dash-sync/public`, and a Let's Encrypt certificate. Then it creates the account `me` and its first device, `dashboard`, and prints that device's **token once**. Paste it into the dashboard's Settings ▸ General ▸ Sync. That's all the server needs: phones and other dashboards get their tokens from the dashboard (Settings ▸ General ▸ Devices ▸ Add a device, as a QR code). Running the script again is safe. > Keep the folder out of other sites' document roots. Only `public/` may be reachable from the web. > `data/sync.db` holds the token hashes and the wrapped key. ## By hand (nginx, another layout) 1. Copy `server/` anywhere, e.g. `/srv/dash-sync`. 2. `cp config.example.php config.php`, then `mkdir data && sudo chown www-data: data && chmod 700 data`. 3. Web server, with the document root at `public/`: ```nginx server { server_name sync.example.com; root /srv/dash-sync/public; client_max_body_size 9m; location / { try_files $uri /index.php$is_args$args; } location = /index.php { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.2-fpm.sock; # your PHP version } # + the TLS lines certbot adds } ``` Under a path instead of a subdomain: set `'base_path' => '/dash-sync'` in `config.php`. 4. The account and its first device (as the web server's user, so it owns the database): ```bash sudo -u www-data php bin/admin.php add-account me sudo -u www-data php bin/admin.php add-device me dashboard # prints its token once ``` The rest come from the dashboard's Settings ▸ General ▸ Devices. 5. Check: `curl -H "Authorization: Bearer " https://sync.example.com/v1/hello`. ## config.php | Key | Default | What | |---|---|---| | `db` | `data/sync.db` | The database | | `base_path` | `''` | URL prefix when not at the root | | `max_batch_bytes` | 8 MB | One batch (also mind PHP's `post_max_size`) | | `max_account_bytes` | 1 GB | All of an account's batches (`0` = no limit) → `507` when full | | `max_answer_bytes` | 32 MB | Batches per download answer; clients just ask again | | `key_replace_any` | `false` | `true`: any device may change the passphrase, not only the one that set the key | | `app_dir` | `app/` | The phone app's published builds ([app.md](app.md)) | | `food_db` | `food/off-archive.db` | The food search archive (below) | | `food_home` | `[]` | Food search: these countries' products rank first, e.g. `['germany', 'austria']` | ## Day to day - Devices: the dashboard that set up the key manages them in Settings ▸ General ▸ Devices (add, revoke). The same on the server: `php bin/admin.php list`, `revoke me phone`. For a stolen, unlocked phone, also change the passphrase (Settings ▸ General ▸ Sync on that dashboard). - `php bin/admin.php stats`: batches and size per account. - **Back up** `data/sync.db`. It's useless without the key, and every device has all the data anyway. - Batches aren't pruned yet. Normal use stays in the tens of MB a year. ## Optional: the food search archive `GET /v1/food/search` searches every Open Food Facts product. Build the archive on any computer from [the OFF export](https://static.openfoodfacts.org/data/en.openfoodfacts.org.products.csv.gz) (~1 GB, ~10 min) and upload it: ```bash cd dashboard FOOD_COUNTRIES=germany python3 plugins/food/off.py --archive en.openfoodfacts.org.products.csv.gz /tmp/off-archive.db rsync -t /tmp/off-archive.db you@host:/srv/dash-sync/food/off-archive.db.new ssh you@host 'mv /srv/dash-sync/food/off-archive.db.new /srv/dash-sync/food/off-archive.db' ``` The data is under the ODbL ([NOTICE.md](../NOTICE.md)). Serving it to your own devices is fine. If you open the search to others, credit Open Food Facts.