feat(server): give each installation a key for the TREK services and report its allowance

A new service-identity domain registers the installation with places.liketrek.com on its first Places request, in the background and behind a light scrypt proof of work, keeps the key encrypted in app_settings, renews it from day 60 through a daily job, drops it after three 401s in a row with up to six hours of jitter, and freezes it on a revocation until an admin starts over. TREK_SERVICES_KEY wins and turns registration off; TREK_SERVICES=off sends no key; a mirror set with TREK_PLACES_URL never registers and never sees the key, which goes only to the hosts in TREK_SERVICES_HOSTS over https.

The Places client takes its headers from the identity port, sends a TREK/<version> User-Agent and keeps the instance token. A 401 or 403 does not open its breaker. GET /api/admin/service-identity reports the state, tier and today's allowance, asking the issuer at most every 20 seconds; POST .../regenerate starts a new identity.
This commit is contained in:
Maurice
2026-10-29 08:59:07 +01:00
committed by Maurice
parent f19f0a33b2
commit 01ae91c939
47 changed files with 2570 additions and 16 deletions
@@ -85,6 +85,15 @@ export class AppSettingsRepository extends TrekRepository<AppSettings> {
return rows.map((row) => toRow(row) as AppSettingsRow);
}
/**
* `INSERT INTO app_settings (key, value) VALUES (?, ?) ON CONFLICT(key) DO NOTHING`:
* the first writer keeps the row, so several processes on one database can
* race to create a value and all read back the same one afterwards.
*/
async insertIfAbsent(key: string, value: string): Promise<void> {
await this.upsert({ key, value }, { onConflictFields: ['key'], onConflictAction: 'ignore' });
}
/** `DELETE FROM app_settings WHERE key = ?` */
async deleteValue(key: string): Promise<number> {
return this.nativeDelete({ key });