# Mamserver central relay deployment

Deployed on 2026-10-09 to `mamserver:/var/www/html/mam`.

## Status

- Relay enabled at `https://mam-app.org/api/clinic-relay/packages` through the existing Cloudflare Tunnel and Access protection.
- Existing mamserver API routes preserved. The central API requires the isolated `routes/clinic_relay.php` file.
- Central deployment contains the relay controller, private transfer store, relay configuration, route file and setup documentation. Clinic import/export UI and the source cleanup worker were not installed on this central server.
- Twelve clinic IDs registered with independent random 256-bit relay tokens: `a_admin`, `b_admin`, `c1_admin`, `spt_admin`, `sdg_admin`, `tl_admin`, `pto_admin`, `phakant_admin`, `taze_admin`, `winka_admin`, `tbzy_admin`, `nap_admin`.
- Clinic client settings are stored on mamserver at `/home/zeya/.config/mam-relay/<clinic_id>.env`, with directory mode 0700 and file mode 0600. Tokens are intentionally omitted from this document and chat.
- Active relay storage is `/var/www/html/mam/storage/app/clinic-transfers`, owned by `www-data` with mode 0700.

## Verified

Using temporary test clinic credentials and synthetic encrypted bytes, the public HTTPS tests passed:

1. Requests without clinic credentials are rejected after passing Cloudflare Access.
2. A package larger than 3 MiB uploads in chunks, including a full 2 MiB HTTP request.
3. An interrupted upload exposes received chunk hashes for safe resume.
4. Assembly verifies the encrypted package's SHA-256 digest; completed retries are idempotent.
5. Only the addressed receiving clinic can download its chunks.
6. Downloaded encrypted bytes match the sender's checksum and decrypt correctly.
7. Only the receiving clinic can submit its import confirmation.
8. The sender retrieves and verifies that confirmation locally without creating any real cleanup task.
9. The actual Laravel `RelayClient` uploads, retries, lists the target inbox, downloads and retrieves/verifies receipts over the deployed public endpoint.

A large download initially stalled. An HTTP/1.1 request completed successfully; the clinic client now explicitly requests HTTP/1.1 and retains its request retries. The relay has a separate 300-requests-per-clinic/minute limit and an additional 360-requests-per-IP/minute limit, so ordinary 2 MiB chunk transfers are not constrained by the application's default 60-request API limit.

Local relay tests passed: **7 tests, 101 assertions**. Existing patient-search and mobile-release routes remain registered. Deployed controller, store, route and configuration hashes match the main source.

Temporary clinic credentials were removed and their rejection was checked over public HTTPS. Synthetic test packages were moved out of active relay storage into a private test archive. A configured real clinic's origin request returns HTTP 200 with an empty inbox.

No clinical data was transferred or imported, no patient identity/laboratory/excluded-module data was touched, and no source cleanup was scheduled.

## Backups

Original environment and API routes, plus a deployment manifest, are backed up on mamserver:

`/home/zeya/.local/share/mam-relay-backups/20261009-fb2aef938985`

The synthetic test archive is separate:

`/var/www/html/mam/storage/app/clinic-transfers-deployment-test-backup-20261009-fb2aef938985`

It contains deployment-test ciphertext only and is not an active clinic inbox. It retains its private web-user ownership and permissions.

## Clinic installation remains required

Merge the correct clinic's transfer entries into that clinic's existing `.env`; preserve its database and other application settings. Install the updated clinic code, configure the clinic's scheduler, and run `php artisan config:clear` and `php artisan view:clear`. Each clinic needs the service credentials authorized by the existing Cloudflare Access policy. Existing `CF_ACCESS_CLIENT_ID` / `CF_ACCESS_CLIENT_SECRET` values are used when transfer-specific values are empty.

Flow: sending clinic uploads an encrypted package → mamserver stores it → target clinic selects **Check central inbox**, downloads and validates it → target user approves the merge → verified receipt returns to the source → unchanged eligible source records become due for cleanup 15 days after the source verifies that receipt.

The main source project's `.env` was not changed during this deployment. See [clinic-transfers.md](clinic-transfers.md) for the clinic flow, exclusions and setup details.
