# Encrypted direct sync Direct sync between two devices now runs over an encrypted, authenticated connection: the same Noise channel the relay uses, set up on the sync listener's port. Each side proves the key the other has pinned, so a device on the network path can't read or change what the devices exchange. Devices that haven't updated can't direct-sync with updated ones, in either direction, until they update. They still sync through the relay when both devices share a coordinator that runs one. ## What changes when you update - An updated device first tries the encrypted connection with each peer. When the peer answers over it, the device records that the peer uses encrypted direct sync and never sends it plaintext again, even if the connection is later refused. - An updated device no longer falls back to plaintext with a peer that doesn't offer the encrypted connection. The sync error says the peer refused the encrypted connection, and the device keeps syncing with that peer through the relay when one is available. - The sync listener no longer serves sync data in plaintext. A device that hasn't updated gets `401 unauthorized` from it. - Pairing payloads now list the `noise1` transport, and devices publish a signed statement of it to their coordinator. A peer that receives either one records the device as using encrypted direct sync before they first connect. - The sync listener caps sockets at 256 in total and 16 per address, and each address may start 600 requests a minute. See `SYNC_LISTENER_CAPS` in the viewer server for how these were measured. ## Before and after you update Update every device you pair with directly. Until a peer updates, direct sync with it stops, and the relay carries it if both devices use one. Run `codemem sync doctor` after updating. Its **Direct sync** lines name each peer that needs attention: | Doctor line | What to do | |---|---| | `peer … has no saved public key` | On that device, run `codemem sync pair --payload-only` and save the output. Then run `codemem sync pair --accept-file ` here. The payload's public key must match the pinned fingerprint. Older versions could pin a peer by fingerprint alone, and the encrypted connection needs the key. | | `peer …: Peer address … has a path` | Pair the device again with an address that has no path, such as `http://host:7337`. codemem's sync listener serves sync at the root of its address, so neither transport can reach it through a path. | | `peer …: Peer address https://… uses https://` | Encrypted direct sync upgrades a plain HTTP connection, so it needs an `http://` address. Pair the device again with its `http://` address, such as `http://host:7337`, or turn on `sync_allow_legacy_plaintext` to keep syncing over HTTPS. A peer that already uses encrypted direct sync is listed even with the setting on, because it is never synced over HTTPS: save its `http://` address, or let the relay carry it. | | `peer … hasn't synced over an encrypted connection yet` | Update codemem on that device. Peers you accepted with an older CLI, which dropped the `noise1` transport from the pairing payload, are recorded once they connect over the encrypted connection, or once you accept their pairing payload again. | The Viewer shows the same problems. In **Devices**, each affected device's sync status names the problem, such as **Sync: Needs update**, with the next step below it. The device list in **Advanced → Sync** shows the same badge in place of **Offline**. Both show it with diagnostics hidden, because the Viewer reports only the problem, not the address: | Badge | Cause | What to do | |---|---|---| | **Needs re-pairing** | No public key is saved for the device. | Pair again using a pairing code from the other device. | | **Needs update** | The device doesn't offer encrypted sync. | Update codemem on it. Until then, it can sync only through a relay. | | **Encrypted sync failed** | The device used encrypted sync before and didn't offer it this time. | Check that it runs a current codemem. If it does, something on the network may be blocking the encrypted connection. | | **Fix address** | A saved address has a path or uses `https://`. | Pair again using the device's `http://` address without a path. | A device shows **Offline** only when none of its addresses could be reached. Direct sync to an `https://` address doesn't use the encrypted connection, so it counts as sync without encryption. With `sync_allow_legacy_plaintext` off, codemem sends nothing to such an address and reports "Peer address … uses https://. Encrypted direct sync needs an http:// address. Save the peer's http:// address, or turn on sync_allow_legacy_plaintext." A peer recorded as using encrypted direct sync is never synced over HTTPS. Direct sync never follows a redirect, over either transport, so a peer can't send sync data on to another address. codemem now refuses to save a peer address that has a path, in `codemem sync pair` and in **Devices → Pair a device**, with the message "Peer address … has a path. codemem needs the address without a path, such as http://host:7337." ## Turning plaintext fallback back on If you need direct sync with a device that can't update yet, set `sync_allow_legacy_plaintext` to `true` in the codemem config, or `CODEMEM_SYNC_ALLOW_LEGACY_PLAINTEXT=1`, and run `codemem service restart`. With it on, this device: - sends plaintext to a peer that answers the encrypted connection with a complete `404`, as a listener without encrypted sync does, unless the peer is already recorded as using encrypted direct sync - syncs with a peer saved at an `https://` address over HTTPS, as before, unless the peer is recorded as using encrypted direct sync - serves plaintext sync to devices not recorded as using encrypted direct sync Peers recorded as using encrypted direct sync stay encrypted either way. Turn the setting off again once the other device updates, because plaintext sync can be read and changed by anyone on the network path. The two directions pick up a change at different times. This device reads the setting at the start of each sync pass, so it stops sending plaintext from the next pass after you turn the setting off. The sync listener reads it only when it starts, so it keeps serving plaintext until you run `codemem service restart`, or restart the Viewer if it runs the sync listener.