From c333aab7912fd17de2c9419d699faced59d75de3 Mon Sep 17 00:00:00 2001 From: fluxion9 Date: Tue, 17 Nov 2026 21:37:55 +0100 Subject: [PATCH 01/17] feat: deploy central role with auto-compose.sh Read CONSOLE_ROLE from .env: central uses compose.central.yaml, defaults to ghcr.io/fluxion9/console:main, validates CENTRAL_DOMAIN and the first-start admin password, and starts the TLS proxy after Console readiness. Gateway role is refused because it needs the agent. Standalone is unchanged. --- auto-compose.sh | 42 +++++++++++++++++++++++++++---- compose.central.yaml | 2 +- tests/services/deployment.test.ts | 36 ++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/auto-compose.sh b/auto-compose.sh index 6b7f82c..3793bfb 100755 --- a/auto-compose.sh +++ b/auto-compose.sh @@ -19,6 +19,28 @@ while [[ $# -gt 0 ]]; do done if [[ -n "$project" && ! "$project" =~ ^[a-z0-9][a-z0-9_-]*$ ]]; then echo 'Invalid Compose project name.' >&2; exit 2; fi [[ -f .env ]] || { echo 'Missing .env: configure the existing services before deployment.' >&2; exit 1; } +# Read one .env setting without executing the file; strips surrounding quotes. +env_value() { + sed -n "s/^[[:space:]]*$1[[:space:]]*=[[:space:]]*//p" .env | tail -1 \ + | sed -e 's/[[:space:]]*$//' -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'\$/\1/" +} +role=$(env_value CONSOLE_ROLE | sed -n 's/^\([a-z]*\).*/\1/p') +role="${role:-standalone}" +case "$role" in + standalone) + compose_file=compose.yaml; default_image=ghcr.io/m3tering/console:main ;; + central) + compose_file=compose.central.yaml; default_image=ghcr.io/fluxion9/console:main + [[ -n "$(env_value CENTRAL_DOMAIN)" ]] || { echo 'Set CENTRAL_DOMAIN in .env to the DNS name pointing at this server.' >&2; exit 2; } + # The administrator is created only on first start; later starts ignore the password. + if [[ ! -s .data/central.db ]]; then + password=$(env_value CENTRAL_ADMIN_PASSWORD) + (( ${#password} >= 16 )) || { echo 'Set CENTRAL_ADMIN_PASSWORD in .env (at least 16 characters) for the first central start.' >&2; exit 2; } + fi ;; + gateway) + echo 'Gateway role needs the Console and agent together: see docs/centralized/deployment.md (compose.gateway.yaml).' >&2; exit 2 ;; + *) echo 'CONSOLE_ROLE in .env must be standalone or central for this script.' >&2; exit 2 ;; +esac command -v docker >/dev/null || { echo 'Docker must already be installed.' >&2; exit 1; } docker compose version >/dev/null docker info >/dev/null @@ -37,18 +59,20 @@ if [[ -z "$project" ]]; then project="${projects[0]:-$(basename "$PWD" | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9_-')}" fi [[ "$project" =~ ^[a-z0-9][a-z0-9_-]*$ ]] || { echo 'Invalid Compose project name.' >&2; exit 2; } -compose=(docker compose --project-name "$project" -f compose.yaml) +compose=(docker compose --project-name "$project" -f "$compose_file") old_container=$("${compose[@]}" ps -aq console | head -1) old_id='' if [[ -n "$old_container" ]]; then old_id=$(docker inspect --format '{{.Image}}' "$old_container") old_ref=$(docker inspect --format '{{.Config.Image}}' "$old_container") + old_role=$(docker inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "$old_container" | sed -n 's/^CONSOLE_ROLE=//p' | tail -1) echo "Current Console: $old_ref ($old_id)" - if [[ -z "$image" && "$old_ref" == ghcr.io/* ]]; then image="$old_ref"; fi + # Reuse the running image only for the same role: a standalone image may lack central support. + if [[ -z "$image" && "$old_ref" == ghcr.io/* && "${old_role:-standalone}" == "$role" ]]; then image="$old_ref"; fi fi -export CONSOLE_IMAGE="${image:-ghcr.io/m3tering/console:main}" +export CONSOLE_IMAGE="${image:-$default_image}" [[ "$CONSOLE_IMAGE" =~ ^ghcr\.io/[a-z0-9._/-]+(:[a-zA-Z0-9._-]+|@sha256:[a-f0-9]{64})$ ]] || { echo 'Specify a published GHCR image with an explicit tag or digest.' >&2; exit 2; } -echo "Project: $project; update image: $CONSOLE_IMAGE" +echo "Project: $project; role: $role; update image: $CONSOLE_IMAGE" "${compose[@]}" config --quiet # Keep the old service and image until the correct architecture is downloaded. "${compose[@]}" pull console @@ -68,5 +92,13 @@ if [[ -n "$old_id" && "$old_id" != "$new_id" && -z "$(docker ps -aq --filter "an # No force: Docker keeps images that still have other tags/references. docker image rm "$old_id" || echo 'Previous image retained because it is still referenced.' fi -"${compose[@]}" ps console +if [[ "$role" == central ]]; then + # TLS proxy (Caddy) publishes the loopback-only Console on ports 80/443. + "${compose[@]}" up --detach tls-proxy || { echo 'Console is running, but the TLS proxy failed to start; check ports 80/443.' >&2; exit 1; } +fi +"${compose[@]}" ps echo 'Console updated. Database, extension volumes and .env were preserved.' +if [[ "$role" == central ]]; then + admin=$(env_value CENTRAL_ADMIN_USERNAME) + echo "Central: https://$(env_value CENTRAL_DOMAIN) (sign in as ${admin:-admin})" +fi diff --git a/compose.central.yaml b/compose.central.yaml index f3d7a20..a3543a8 100644 --- a/compose.central.yaml +++ b/compose.central.yaml @@ -1,7 +1,7 @@ # Opt-in central test topology; supply CENTRAL_DOMAIN and central credentials. services: console: - image: ${CONSOLE_IMAGE:-ghcr.io/fluxion9/console:feat-centralized-console} + image: ${CONSOLE_IMAGE:-ghcr.io/fluxion9/console:main} network_mode: host restart: unless-stopped env_file: [.env] diff --git a/tests/services/deployment.test.ts b/tests/services/deployment.test.ts index 2075f4b..343766a 100644 --- a/tests/services/deployment.test.ts +++ b/tests/services/deployment.test.ts @@ -34,3 +34,39 @@ esac else expect(calls).toContain('image rm sha256:old'); } finally {fs.rmSync(dir,{recursive:true,force:true});} }); + +// Central role is chosen from .env; shares the same isolated Docker adapter. +function runCentral(environment:string, existingDb=false) { + const dir=fs.mkdtempSync(path.join(os.tmpdir(),'console-central-')); + try { + for(const file of ['auto-compose.sh','compose.yaml','compose.central.yaml'])fs.copyFileSync(file,path.join(dir,file)); + fs.writeFileSync(path.join(dir,'.env'),environment); + fs.mkdirSync(path.join(dir,'.data'));if(existingDb)fs.writeFileSync(path.join(dir,'.data','central.db'),'db'); + const log=path.join(dir,'calls');fs.writeFileSync(log,''); + fs.writeFileSync(path.join(dir,'docker'),`#!/bin/bash +printf '%s|%s\\n' "$*" "$CONSOLE_IMAGE" >> "$DEPLOY_LOG" +case "$*" in *'ps -q console'*) echo new-container ;; esac +`,{mode:0o755}); + const run=spawnSync('bash',['auto-compose.sh','--project-name','central'],{cwd:dir,encoding:'utf8',env:{...process.env,PATH:dir+':'+process.env.PATH,DEPLOY_LOG:log}}); + return {run,calls:fs.readFileSync(log,'utf8')}; + } finally {fs.rmSync(dir,{recursive:true,force:true});} +} +test('central role deploys console and TLS proxy from the central compose file', () => { + const {run,calls}=runCentral('CONSOLE_ROLE=central\nCENTRAL_DOMAIN="console.example.test"\nCENTRAL_ADMIN_PASSWORD=a-very-long-password-1\n'); + expect(run.status).toBe(0); + expect(calls).toContain('-f compose.central.yaml pull console|ghcr.io/fluxion9/console:main'); + expect(calls).toMatch(/up --detach tls-proxy/); + expect(calls).not.toContain('-f compose.yaml'); + expect(run.stdout).toContain('https://console.example.test'); +}); +test('central first start refuses a short administrator password before touching Docker', () => { + const {run,calls}=runCentral('CONSOLE_ROLE=central\nCENTRAL_DOMAIN=console.example.test\nCENTRAL_ADMIN_PASSWORD=short\n'); + expect(run.status).toBe(2);expect(run.stderr).toContain('at least 16 characters');expect(calls).toBe(''); +}); +test('central restart does not require the bootstrap password', () => { + expect(runCentral('CONSOLE_ROLE=central\nCENTRAL_DOMAIN=console.example.test\n',true).run.status).toBe(0); +}); +test('gateway role is refused because it needs the agent alongside Console', () => { + const {run,calls}=runCentral('CONSOLE_ROLE=gateway\n'); + expect(run.status).toBe(2);expect(run.stderr).toContain('compose.gateway.yaml');expect(calls).toBe(''); +}); From f605db515983d096109db514fba4570e7f036927 Mon Sep 17 00:00:00 2001 From: fluxion9 Date: Tue, 17 Nov 2026 22:44:14 +0100 Subject: [PATCH 02/17] feat: redesign gateway management with rename and delete Gateway dialog: add-gateway card, guided enrollment panel with copyable .env line and live token expiry, status-coloured gateway cards with activity stats, collapsible remote updates and confirmation dialogs for revoke and delete. Styles follow the active desktop theme. Rename: POST /api/gateways/:id/rename with the same name rules, audited. Delete: POST /api/gateways/:id/delete soft-deletes the gateway: its credential and pending enrollments are destroyed, the live agent link is terminated, device pins are released, and billing/audit history is kept. Existing central databases gain the deleted column on startup. --- src/central/hub.ts | 9 + src/central/identity-store.ts | 50 +++++- src/public/css/gateways.css | 124 +++++++++++++- src/public/js/gateways.js | 219 +++++++++++++++++++----- src/views/index.hbs | 11 +- tests/services/central-identity.test.ts | 37 ++++ tests/services/central-link.test.ts | 18 ++ tests/ui/gateways.spec.cjs | 61 ++++++- 8 files changed, 475 insertions(+), 54 deletions(-) diff --git a/src/central/hub.ts b/src/central/hub.ts index 1b13e62..04b1b73 100644 --- a/src/central/hub.ts +++ b/src/central/hub.ts @@ -86,6 +86,15 @@ export class GatewayHub { try{const g=this.identities.createGateway(String(req.body?.name||''),res.locals.centralUser.id);res.status(201).json({...g,token:this.identities.enrollment(g.id,res.locals.centralUser.id)});} catch{res.status(400).json({error:'Provide a valid gateway name.'});} }); + app.post('/api/gateways/:id/rename',(req,res)=>{ + try{res.json(this.identities.renameGateway(req.params.id,String(req.body?.name??''),res.locals.centralUser.id));} + catch{res.status(400).json({error:'Provide a name of 1–100 printable characters for an existing gateway.'});} + }); + app.post('/api/gateways/:id/delete',(req,res)=>{ + try{this.identities.deleteGateway(req.params.id,res.locals.centralUser.id);}catch{res.status(404).json({error:'Gateway not found.'});return;} + // Same fencing as revocation: the credential is gone, so drop the live link and its tunnels now. + this.clearRotation(req.params.id);this.sockets.get(req.params.id)?.terminate();res.json({deleted:true}); + }); app.post('/api/gateways/:id/revoke',(req,res)=>{this.identities.revoke(req.params.id,res.locals.centralUser.id);this.sockets.get(req.params.id)?.terminate();res.json({revoked:true});}); app.post('/api/gateways/:id/enrollment',(req,res)=>{ try{res.json({token:this.identities.enrollment(req.params.id,res.locals.centralUser.id)});}catch{res.status(409).json({error:'Revoke the previous identity before enrolling again.'});} diff --git a/src/central/identity-store.ts b/src/central/identity-store.ts index 67a6159..40e9fa0 100644 --- a/src/central/identity-store.ts +++ b/src/central/identity-store.ts @@ -6,9 +6,9 @@ import { validImageDigest } from './protocol'; import type { Health } from './protocol'; export type AuditAction = 'login-success' | 'login-failure' | 'logout' | 'enrolment-created' | 'enrolled' | - 'credential-rotated' | 'credential-rotation-acknowledged' | 'credential-revoked' | + 'credential-rotated' | 'credential-rotation-acknowledged' | 'credential-revoked' | 'gateway-renamed' | 'gateway-deleted' | 'device-assignment' | 'state-downlink-uncertain' | 'terminal-start' | 'terminal-end' | 'browser-start' | 'browser-end' | 'state-downlink' | 'update-status'; -export interface GatewayIdentity { id: string; name: string; revoked: boolean; lastHeartbeat: number | null; version: string | null } +export interface GatewayIdentity { id: string; name: string; revoked: boolean; enrolled: boolean; lastHeartbeat: number | null; version: string | null } export interface GatewayUpdate {id:string;gateway:string;image:string;status:UpdateStatus;history:Array<{status:UpdateStatus;time:number}>} export interface DeviceGateway { devEui:string; gateway:string; observedGateway:string; overridden:boolean; lastSeen:number } export interface UserIdentity { id: string; username: string; passwordHash: string } @@ -16,6 +16,8 @@ export interface SessionIdentity { id: string; username: string; expires: number export interface IdentityStore { createGateway(name: string, actor: string): GatewayIdentity; gateways(): GatewayIdentity[]; + renameGateway(gateway: string, name: string, actor: string): GatewayIdentity; + deleteGateway(gateway: string, actor: string): void; enrollment(gateway: string, actor: string, ttlMs?: number): string; exchange(token: string): { gateway: string; credential: string } | null; authenticateAgent(credential: string): string | null; @@ -75,23 +77,55 @@ export class SqliteIdentityStore implements IdentityStore { CREATE TABLE IF NOT EXISTS central_hostkeys(gateway TEXT PRIMARY KEY REFERENCES central_gateways(id),hash TEXT NOT NULL); INSERT OR IGNORE INTO central_schema VALUES (1); `))(); + // Version 2: deleted gateways are hidden and fenced, but their history is kept. + const columns = (this.db.prepare('PRAGMA table_info(central_gateways)').all() as Array<{ name: string }>).map(column => column.name); + if (!columns.includes('deleted')) this.db.transaction(() => { + this.db.exec('ALTER TABLE central_gateways ADD COLUMN deleted INTEGER NOT NULL DEFAULT 0'); + this.db.exec('INSERT OR IGNORE INTO central_schema VALUES (2)'); + })(); } - createGateway(name: string, actor: string): GatewayIdentity { + private static gatewayName(name: string): string { if (!name.trim() || name.length > 100 || /[\x00-\x1f]/.test(name)) throw new Error('Gateway name must contain 1–100 printable characters.'); + return name.trim(); + } + createGateway(name: string, actor: string): GatewayIdentity { + const clean = SqliteIdentityStore.gatewayName(name); const id = randomUUID(); - this.db.prepare('INSERT INTO central_gateways (id,name) VALUES (?,?)').run(id, name.trim()); + this.db.prepare('INSERT INTO central_gateways (id,name) VALUES (?,?)').run(id, clean); this.audit(actor, 'enrolment-created', id); - return { id, name: name.trim(), revoked: false, lastHeartbeat: null, version: null }; + return { id, name: clean, revoked: false, enrolled: false, lastHeartbeat: null, version: null }; } gateways(): GatewayIdentity[] { - return (this.db.prepare('SELECT id,name,revoked,lastHeartbeat,version FROM central_gateways ORDER BY name').all() as - Array & { revoked: number }>).map(row => ({ ...row, revoked: !!row.revoked })); + return (this.db.prepare(`SELECT id,name,revoked,credentialHash IS NOT NULL AS enrolled,lastHeartbeat,version + FROM central_gateways WHERE deleted=0 ORDER BY name COLLATE NOCASE`).all() as + Array & { revoked: number; enrolled: number }>) + .map(row => ({ ...row, revoked: !!row.revoked, enrolled: !!row.enrolled })); + } + renameGateway(gateway: string, name: string, actor: string): GatewayIdentity { + const clean = SqliteIdentityStore.gatewayName(name); + return this.db.transaction(() => { + if (!this.db.prepare('UPDATE central_gateways SET name=? WHERE id=? AND deleted=0').run(clean, gateway).changes) throw new Error('Gateway not found.'); + this.audit(actor, 'gateway-renamed', gateway, clean); + return this.gateways().find(g => g.id === gateway)!; + })(); + } + /** Removes a gateway from use: its credential and pending enrollments are destroyed + * and device pins released. Rows are kept so billing, audit and outcome history stay intact. */ + deleteGateway(gateway: string, actor: string): void { + this.db.transaction(() => { + const result = this.db.prepare(`UPDATE central_gateways SET deleted=1,revoked=1,credentialHash=NULL,previousHash=NULL,previousUntil=NULL + WHERE id=? AND deleted=0`).run(gateway); + if (!result.changes) throw new Error('Gateway not found.'); + this.db.prepare('DELETE FROM central_enrollments WHERE gateway=?').run(gateway); + this.db.prepare('UPDATE central_devices SET overrideGateway=NULL WHERE overrideGateway=?').run(gateway); + this.audit(actor, 'gateway-deleted', gateway); + })(); } enrollment(gateway: string, actor: string, ttlMs = 10 * 60000): string { if (!Number.isInteger(ttlMs) || ttlMs < 1000 || ttlMs > 3600000) throw new Error('Invalid enrollment lifetime.'); const token = newSecret(); this.db.transaction(() => { - const row = this.db.prepare('SELECT credentialHash,revoked FROM central_gateways WHERE id=?').get(gateway) as { credentialHash: string | null; revoked: number } | undefined; + const row = this.db.prepare('SELECT credentialHash,revoked FROM central_gateways WHERE id=? AND deleted=0').get(gateway) as { credentialHash: string | null; revoked: number } | undefined; if (!row || (row.credentialHash && !row.revoked)) throw new Error('Revoke an enrolled gateway before replacing its identity.'); this.db.prepare('DELETE FROM central_enrollments WHERE gateway=? OR expires<=?').run(gateway, this.now()); this.db.prepare('INSERT INTO central_enrollments (tokenHash,gateway,expires) VALUES (?,?,?)').run(hashSecret(token), gateway, this.now() + ttlMs); diff --git a/src/public/css/gateways.css b/src/public/css/gateways.css index a0ff3b5..064f14e 100644 --- a/src/public/css/gateways.css +++ b/src/public/css/gateways.css @@ -1 +1,123 @@ -.gateway-control{display:flex;align-items:center;gap:.5rem;min-width:0}.gateway-control form{margin:0}.gateway-control select{max-width:13rem;min-width:0;background:var(--surface);color:var(--text);border:1px solid var(--border);border-radius:8px;padding:.4rem}#gateway-status{position:absolute;bottom:78px;left:1rem;right:1rem;z-index:5;pointer-events:none;font:12px system-ui;color:var(--text);background:var(--surface);padding:.5rem;border-radius:8px;width:fit-content;max-width:calc(100% - 2rem)}#gateway-dialog{background:var(--surface);color:var(--text);border:1px solid var(--border);border-radius:14px;max-width:760px;width:calc(100% - 2rem);max-height:85dvh;overflow:auto;padding:1.5rem}#gateway-dialog::backdrop{background:#0008}#gateway-dialog header{display:flex;justify-content:space-between;align-items:center}#gateway-dialog h2{font-size:1.3rem}#gateway-list article{padding:1rem 0;border-bottom:1px solid var(--border)}#gateway-list article>div{display:flex;gap:.5rem;flex-wrap:wrap}#gateway-list p{font:14px system-ui}#gateway-create{display:flex;gap:1rem;align-items:end;flex-wrap:wrap;margin-top:1rem}#gateway-create label{display:grid;gap:.4rem}#gateway-enrollment code{display:block;overflow-wrap:anywhere;white-space:normal;margin-bottom:1rem}#gateway-error{color:var(--danger,#c44)}@media(max-width:600px){.gateway-control{gap:.15rem}.gateway-control select{max-width:9rem}.gateway-control button{font-size:11px}.topbar-location{display:none}#gateway-status{font-size:10px;bottom:70px}#gateway-dialog{padding:1rem}} +/* Central gateway selector and management. Colours come from the active desktop theme. */ +.gateway-control{display:flex;align-items:center;gap:.5rem;min-width:0} +.gateway-control form{margin:0} +.gateway-control select{max-width:13rem;min-width:0;background:var(--surface);color:var(--text);border:1px solid var(--border);border-radius:8px;padding:.4rem} +#gateway-status{position:absolute;bottom:78px;left:1rem;right:1rem;z-index:5;pointer-events:none;font:12px var(--font);color:var(--text);background:var(--surface);padding:.5rem .75rem;border:1px solid var(--border);border-radius:var(--small-radius);width:fit-content;max-width:calc(100% - 2rem)} + +#gateway-dialog{--gw-online:#2f8a57;--gw-offline:#b7832f;--gw-revoked:#b5434c;--gw-pending:var(--accent); + width:min(820px,calc(100% - 2rem));max-width:none;max-height:88dvh;overflow:auto;padding:0; + background:var(--surface);color:var(--text);font:14px/1.5 var(--font)} +#gateway-dialog::backdrop{background:#0b151c99;backdrop-filter:blur(4px)} +#gateway-dialog h2,#gateway-dialog h3{font-family:var(--display-font);margin:0} +#gateway-dialog p{margin:0} +#gateway-dialog code{font:12.5px/1.5 ui-monospace,SFMono-Regular,Menlo,Consolas,monospace} +#gateway-dialog > :not(.gw-header){margin-left:28px;margin-right:28px} + +.gw-header{position:sticky;top:0;z-index:2;display:flex;justify-content:space-between;align-items:flex-start;gap:1rem; + padding:24px 28px 18px;background:var(--surface);border-bottom:1px solid var(--border)} +.gw-header .eyebrow{font-size:11px;letter-spacing:.12em;color:var(--muted)} +.gw-header h2{font-size:24px;margin:2px 0 4px} +.gw-summary{color:var(--muted);font-size:13px} +#gateway-dialog-close{font-size:22px;line-height:1} + +.gw-add{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1.25fr);gap:20px;align-items:center;margin-top:22px; + padding:20px;background:var(--surface-alt);border:1px solid var(--border);border-radius:var(--radius)} +#gateway-dialog .gw-add h3{font-size:16px;margin-bottom:4px} +.gw-add p{color:var(--muted);font-size:13px} +#gateway-create{display:flex;gap:10px;margin:0} +#gateway-create .nes-input{flex:1;min-width:0} +#gateway-create .nes-btn{white-space:nowrap} + +.gw-enrollment{margin-top:16px;padding:20px;border:2px solid var(--accent);border-radius:var(--radius); + background:color-mix(in srgb,var(--accent) 7%,var(--surface))} +.gw-enrollment-head{display:flex;justify-content:space-between;align-items:center;gap:12px;flex-wrap:wrap;margin-bottom:14px} +.gw-enrollment-head h3{font-size:16px} +.gw-steps{margin:0 0 16px;padding:0;list-style:none;counter-reset:step;display:grid;gap:14px} +.gw-steps li{counter-increment:step;display:grid;grid-template-columns:28px minmax(0,1fr);column-gap:10px;row-gap:8px} +.gw-steps li::before{content:counter(step);grid-row:span 2;width:26px;height:26px;border-radius:50%;display:grid;place-items:center; + background:var(--accent);color:var(--accent-ink);font-weight:600;font-size:12px} +.gw-steps li>span{align-self:center} +.gw-token{display:flex;gap:8px;align-items:stretch;min-width:0} +.gw-token code,.gw-command{flex:1;min-width:0;display:block;padding:10px 12px;border-radius:var(--small-radius); + background:#122127;color:#e7f0ea;overflow-wrap:anywhere;user-select:all} +.gw-token-key{color:#9fc4ae} + +#gateway-error{margin-top:14px;color:var(--gw-revoked);font-weight:500} +#gateway-error:empty{display:none} +#gateway-dialog .gw-list-title{font-size:15px;margin-top:28px;margin-bottom:12px} + +#gateway-list{display:grid;gap:14px} +.gw-card{--gw-state:var(--gw-offline);position:relative;padding:18px 18px 14px 22px;background:var(--surface); + border:1px solid var(--border);border-radius:var(--radius);box-shadow:0 1px 2px #0000000d;overflow:hidden} +.gw-card::before{content:'';position:absolute;inset:0 auto 0 0;width:4px;background:var(--gw-state)} +.gw-card[data-state="online"]{--gw-state:var(--gw-online)} +.gw-card[data-state="revoked"]{--gw-state:var(--gw-revoked)} +.gw-card[data-state="pending"]{--gw-state:var(--gw-pending)} +.gw-card-head{display:flex;align-items:center;gap:12px;min-width:0} +.gw-dot{flex:none;width:10px;height:10px;border-radius:50%;background:var(--gw-state)} +.gw-card[data-state="online"] .gw-dot{box-shadow:0 0 0 4px color-mix(in srgb,var(--gw-online) 22%,transparent)} +.gw-title{display:grid;min-width:0;flex:1} +.gw-name{font-size:16px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap} +.gw-title small{color:var(--muted);font-size:12px} +.gw-tools{display:flex;gap:2px;margin-left:auto} +.gw-tools .quiet-button{min-height:34px;min-width:0;padding:6px 10px;font:500 12px var(--font);color:var(--muted)} +.gw-tools .quiet-button:hover{color:var(--text)} +.gw-tools .gw-danger:hover{color:var(--gw-revoked);background:color-mix(in srgb,var(--gw-revoked) 10%,transparent)} + +.gw-pill{display:inline-flex;align-items:center;white-space:nowrap;padding:3px 10px;border-radius:999px;font-size:11.5px;font-weight:600; + background:var(--surface-alt);color:var(--muted);border:1px solid var(--border)} +.gw-pill.is-on{color:var(--gw-online);border-color:color-mix(in srgb,var(--gw-online) 40%,transparent)} +.gw-pill.is-expired{color:var(--gw-revoked)} +.gw-state{color:var(--gw-state);border-color:color-mix(in srgb,var(--gw-state) 40%,transparent);background:color-mix(in srgb,var(--gw-state) 9%,var(--surface))} + +.gw-rename{display:flex;gap:8px;flex:1;min-width:0;margin:0} +.gw-rename .nes-input{flex:1;min-width:0;min-height:36px;padding:6px 10px} +.gw-rename .nes-btn{min-height:36px;padding:6px 12px} + +.gw-stats{display:grid;grid-template-columns:repeat(5,minmax(0,1fr));gap:8px;margin:16px 0 0} +.gw-actions{margin-top:14px} +.gw-stat{padding:9px 11px;background:var(--surface-alt);border-radius:var(--small-radius);min-width:0} +.gw-stat dt{font-size:11px;text-transform:uppercase;letter-spacing:.06em;color:var(--muted)} +.gw-stat dd{margin:2px 0 0;font-size:17px;font-weight:600;font-variant-numeric:tabular-nums;overflow:hidden;text-overflow:ellipsis;white-space:nowrap} + +.gw-actions{display:flex;gap:8px;flex-wrap:wrap} +.gw-actions .nes-btn:disabled{opacity:.5;cursor:not-allowed} + +.gw-updates{margin-top:12px;border-top:1px solid var(--border);padding-top:10px} +.gw-updates summary{display:flex;align-items:center;gap:10px;cursor:pointer;font-weight:500;font-size:13px;list-style:none} +.gw-updates summary::-webkit-details-marker{display:none} +.gw-updates summary::before{content:'▸';color:var(--muted);transition:transform var(--duration)} +.gw-updates[open] summary::before{transform:rotate(90deg)} +#gateway-dialog .gw-updates>p,.gw-update-form,.gw-job{margin-top:10px} +.gw-muted{color:var(--muted);font-size:13px} +.gw-update-form{display:grid;gap:10px} +.gw-check{display:flex;align-items:center;gap:8px;font-size:13px} +.gw-update-form .nes-btn{justify-self:start} +.gw-job{display:flex;align-items:center;gap:10px;font-size:12px;min-width:0} +.gw-job code{flex:1;min-width:0;overflow-wrap:anywhere;color:var(--muted)} + +.gw-empty{text-align:center;padding:34px 20px;border:1px dashed var(--border);border-radius:var(--radius);color:var(--muted)} +.gw-empty strong{display:block;color:var(--text);font-size:15px;margin-bottom:4px} +#gateway-dialog .gw-note{margin-top:20px;margin-bottom:24px;padding-top:14px;border-top:1px solid var(--border);font-size:12px;color:var(--muted)} + +#gateway-confirm{width:min(440px,calc(100% - 32px))} +#gateway-confirm p{line-height:1.55} + +@media(max-width:640px){ + .gateway-control{gap:.15rem}.gateway-control select{max-width:9rem}.gateway-control button{font-size:11px} + .topbar-location{display:none} + #gateway-status{font-size:10px;bottom:70px} + #gateway-dialog{width:calc(100% - 1rem);max-height:94dvh} + #gateway-dialog > :not(.gw-header){margin-left:16px;margin-right:16px} + .gw-header{padding:18px 16px 14px} + .gw-add{grid-template-columns:1fr;gap:12px;padding:16px} + #gateway-create{flex-direction:column} + .gw-card{padding:16px 14px 12px 18px} + .gw-card-head{flex-wrap:wrap} + .gw-tools{width:100%;margin-left:22px} + .gw-stats{grid-template-columns:repeat(3,minmax(0,1fr))} + .gw-rename{flex-wrap:wrap}.gw-rename .nes-input{flex-basis:100%} + .gw-token{flex-direction:column} +} +[data-theme="retro"] #gateway-dialog h2{font-size:16px;line-height:1.6} +[data-theme="retro"] #gateway-dialog h3{font-size:11px;line-height:1.7} diff --git a/src/public/js/gateways.js b/src/public/js/gateways.js index b1a10b3..086efbd 100644 --- a/src/public/js/gateways.js +++ b/src/public/js/gateways.js @@ -1,65 +1,202 @@ (() => { - const select=document.getElementById('gateway-select'),dialog=document.getElementById('gateway-dialog'),list=document.getElementById('gateway-list'); - let gateways=[],selected=null,busy=false; - const error=message=>{document.getElementById('gateway-error').textContent=message;}; + const $=id=>document.getElementById(id); + const select=$('gateway-select'),dialog=$('gateway-dialog'),list=$('gateway-list'),confirmDialog=$('gateway-confirm'); + const NETWORK_APPS=['terminal','chirpstack','browser'],TOKEN_LIFETIME=10*60000; + let gateways=[],selected=null,busy=false,again=false,editing=null,focusRename=false,tokenTimer=null; + const openUpdates=new Set(); + + const error=message=>{$('gateway-error').textContent=message||'';}; async function api(path,body){ const response=await fetch(path,{method:body?'POST':'GET',headers:body?{'Content-Type':'application/json'}:{},body:body?JSON.stringify(body):undefined}); if(response.status===401){location.assign('/login');throw new Error('Session expired.');} const result=await response.json();if(!response.ok)throw new Error(result.error||'Gateway request failed.');return result; } + // Small DOM builder; all user-provided text goes through textContent. + function el(tag,props={},...children){ + const node=document.createElement(tag); + for(const [key,value] of Object.entries(props)){ + if(value===undefined||value===false)continue; + if(key==='class')node.className=value;else if(key==='text')node.textContent=value; + else if(key.startsWith('on'))node[key]=value;else if(key in node&&key!=='list')node[key]=value;else node.setAttribute(key,value===true?'':value); + } + node.append(...children.filter(child=>child!==null&&child!==undefined&&child!==false));return node; + } + function ago(time){ + if(!time)return 'Never connected'; + const seconds=Math.max(0,Math.round((Date.now()-time)/1000)); + if(seconds<60)return 'Seen just now';if(seconds<3600)return `Seen ${Math.round(seconds/60)} min ago`; + if(seconds<86400)return `Seen ${Math.round(seconds/3600)} h ago`;return `Seen ${Math.round(seconds/86400)} d ago`; + } + function state(g){ + if(g.revoked)return ['revoked','Access revoked']; + if(!g.enrolled)return ['pending','Waiting to connect']; + return g.online?['online','Online']:['offline','Offline']; + } + function confirmAction({title,body,action}){ + $('gateway-confirm-title').textContent=title;$('gateway-confirm-body').textContent=body;$('gateway-confirm-action').textContent=action; + confirmDialog.returnValue='';confirmDialog.showModal(); + return new Promise(resolve=>confirmDialog.addEventListener('close',()=>resolve(confirmDialog.returnValue==='confirm'),{once:true})); + } + async function run(work){ + error(''); + try{await work();}catch(e){error(e.message);} + await refresh(); + } + function title(id){ - const win=document.getElementById(id),gateway=gateways.find(g=>g.id===selected);if(!win||!gateway)return; + const win=$(id),gateway=gateways.find(g=>g.id===selected);if(!win||!gateway)return; win.dataset.gatewayBaseTitle ||= win.dataset.windowTitle; win.dataset.gatewayName=gateway.name; win.dataset.windowTitle=`${win.dataset.gatewayBaseTitle} · ${gateway.name}`; const label=win.querySelector('.window-title');if(label)label.textContent=win.dataset.windowTitle; document.title=`${win.dataset.windowTitle} · M3tering Console`; - document.getElementById('active-app-name').textContent=win.dataset.windowTitle; + $('active-app-name').textContent=win.dataset.windowTitle; } - function render(){ + + function renderSelector(){ select.replaceChildren(new Option('Select a gateway','')); for(const gateway of gateways)if(!gateway.revoked)select.add(new Option(`${gateway.name}${gateway.online?'':' · offline'}`,gateway.id)); select.value=selected||''; const current=gateways.find(g=>g.id===selected),h=current?.health; - document.getElementById('gateway-status').textContent=current?`${current.name} · ${current.online?'Agent connected':'Agent offline'} · ${h?.uncertain||0} uncertain seen · ${h?.reconciled||0} reconciled · ${h?.superseded||0} superseded · ${h?.queueDepth||0} pending`:'Choose a gateway to access its applications.'; - list.replaceChildren(); - for(const g of gateways){ - const item=document.createElement('article'),name=document.createElement('strong'),status=document.createElement('p'),actions=document.createElement('div'); - name.textContent=g.name;const s=g.health; - status.textContent=`${g.revoked?'Revoked':g.online?'Connected':'Offline'} · MQTT ${s?.mqtt||'unknown'} · ${s?.queueDepth||0} pending · ${s?.uncertain||0} uncertain seen · ${s?.reconciled||0} reconciled · ${s?.superseded||0} superseded`; - for(const [action,label] of [['rotate','Rotate credential'],['revoke','Revoke access'],['enrollment','New enrollment']]){ - const button=document.createElement('button');button.className='nes-btn';button.textContent=label; - button.disabled=action==='rotate'&&(!g.online||g.revoked); - button.onclick=async()=>{try{const result=await api(`/api/gateways/${encodeURIComponent(g.id)}/${action}`,{});if(result.token)showToken(result.token);await refresh();}catch(e){error(e.message);}}; - actions.append(button); - } - const updates=document.createElement('div'),note=document.createElement('p'); - note.textContent=s?.updatesEnabled?'Remote updates enabled locally. Both Console and agent restart together.':'Updates disabled locally.';updates.append(note); - if(s?.updatesEnabled){ - const form=document.createElement('form'),image=document.createElement('input'),confirm=document.createElement('input'),label=document.createElement('label'),button=document.createElement('button'); - image.placeholder='ghcr.io/owner/console@sha256:…';image.required=true;image.setAttribute('aria-label',`Image digest for ${g.name}`);image.style.width='100%'; - confirm.type='checkbox';confirm.required=true;label.append(confirm,document.createTextNode(' Restart this gateway’s Console and agent')); - button.className='nes-btn';button.textContent='Queue signed update';button.disabled=!g.online; - form.append(image,label,button);form.onsubmit=async event=>{event.preventDefault();try{await api(`/api/gateways/${g.id}/updates`,{image:image.value.trim()});await refresh();}catch(e){error(e.message);}};updates.append(form); - } - for(const job of g.updates||[]){const row=document.createElement('p');row.textContent=`${job.status} · ${job.image}`;row.style.overflowWrap='anywhere'; - if(job.status==='queued'){const cancel=document.createElement('button');cancel.className='nes-btn';cancel.textContent='Cancel';cancel.onclick=async()=>{try{await api(`/api/gateways/${g.id}/updates/${job.id}/cancel`,{});await refresh();}catch(e){error(e.message);}};row.append(cancel);}updates.append(row); - } - item.append(name,status,actions,updates);list.append(item); + $('gateway-status').textContent=current?`${current.name} · ${current.online?'Agent connected':'Agent offline'} · ${h?.uncertain||0} uncertain seen · ${h?.reconciled||0} reconciled · ${h?.superseded||0} superseded · ${h?.queueDepth||0} pending`:'Choose a gateway to access its applications.'; + const online=gateways.filter(g=>g.online).length; + $('gateway-summary').textContent=gateways.length?`${gateways.length} gateway${gateways.length===1?'':'s'} · ${online} online`:'No gateways yet'; + } + + function stopEditing(){editing=null;if(list.contains(document.activeElement))document.activeElement.blur();} + function startEditing(id){editing=id;focusRename=true;render();} + function renameForm(g){ + const input=el('input',{class:'nes-input',value:g.name,maxLength:100,required:true,'aria-label':`New name for ${g.name}`,autocomplete:'off'}); + const form=el('form',{class:'gw-rename',onsubmit:event=>{event.preventDefault(); + const name=input.value.trim();if(!name||name===g.name){stopEditing();render();return;} + void run(async()=>{await api(`/api/gateways/${encodeURIComponent(g.id)}/rename`,{name});stopEditing();}); + }},input,el('button',{class:'nes-btn is-primary',text:'Save'}), + el('button',{class:'nes-btn',type:'button',text:'Cancel',onclick:()=>{stopEditing();render();}})); + input.addEventListener('keydown',event=>{if(event.key==='Escape'){event.preventDefault();event.stopPropagation();stopEditing();render();}}); + if(focusRename){focusRename=false;requestAnimationFrame(()=>{input.focus();input.select();});} + return form; + } + + function updatesPanel(g){ + const s=g.health,enabled=!!s?.updatesEnabled; + const details=el('details',{class:'gw-updates',open:openUpdates.has(g.id)}, + el('summary',{},el('span',{text:'Remote updates'}),el('span',{class:`gw-pill ${enabled?'is-on':''}`,text:enabled?'Enabled':'Disabled on gateway'}))); + details.addEventListener('toggle',()=>{details.open?openUpdates.add(g.id):openUpdates.delete(g.id);}); + if(!enabled)details.append(el('p',{class:'gw-muted',text:'The gateway owner enables remote updates locally. Central cannot turn this on.'})); + else{ + const image=el('input',{class:'nes-input',placeholder:'ghcr.io/owner/console@sha256:…',required:true,'aria-label':`Image digest for ${g.name}`}); + const check=el('input',{type:'checkbox',required:true}); + details.append(el('form',{class:'gw-update-form',onsubmit:event=>{event.preventDefault(); + void run(()=>api(`/api/gateways/${encodeURIComponent(g.id)}/updates`,{image:image.value.trim()}));}}, + image,el('label',{class:'gw-check'},check,el('span',{text:'Restart this gateway’s Console and agent'})), + el('button',{class:'nes-btn is-primary',text:'Queue signed update',disabled:!g.online}))); + } + for(const job of g.updates||[]){ + details.append(el('div',{class:'gw-job'},el('span',{class:'gw-pill',text:job.status}),el('code',{text:job.image}), + job.status==='queued'&&el('button',{class:'nes-btn',text:'Cancel',onclick:()=>void run(()=>api(`/api/gateways/${encodeURIComponent(g.id)}/updates/${encodeURIComponent(job.id)}/cancel`,{}))}))); } + return details; } - async function refresh(){if(busy)return;busy=true;try{const result=await api('/api/gateways');gateways=result.gateways;selected=result.selected;render();}catch(e){error(e.message);}finally{busy=false;}} - function showToken(token){document.getElementById('gateway-enrollment').hidden=false;document.getElementById('gateway-enrollment-token').textContent=token;} + + function card(g){ + const [kind,label]=state(g),s=g.health||{},id=encodeURIComponent(g.id); + const heading=editing===g.id?renameForm(g):el('div',{class:'gw-title'}, + el('strong',{class:'gw-name',text:g.name}), + el('small',{text:[g.version&&`v${g.version}`,ago(g.lastHeartbeat)].filter(Boolean).join(' · ')})); + const tools=editing===g.id?null:el('div',{class:'gw-tools'}, + el('button',{class:'quiet-button',text:'Rename','aria-label':`Rename ${g.name}`,onclick:()=>startEditing(g.id)}), + el('button',{class:'quiet-button gw-danger',text:'Delete','aria-label':`Delete ${g.name}`,onclick:()=>void deleteGateway(g)})); + const stat=(name,value,hint)=>el('div',{class:'gw-stat',title:hint},el('dt',{text:name}),el('dd',{text:String(value)})); + const stats=el('dl',{class:'gw-stats','aria-label':`${g.name} activity`}, + stat('MQTT',s.mqtt||'—','Broker connection reported by the agent'), + stat('Pending',s.queueDepth||0,'Reports waiting for a decision'), + stat('Uncertain',s.uncertain||0,'Replies held for reconciliation (uncertain seen)'), + stat('Reconciled',s.reconciled||0,'Uncertain replies later resolved'), + stat('Superseded',s.superseded||0,'Replies replaced by a newer report')); + const actions=el('div',{class:'gw-actions'}); + if(g.enrolled&&!g.revoked){ + actions.append(el('button',{class:'nes-btn',text:'Rotate credential',disabled:!g.online,title:g.online?undefined:'The gateway must be online to rotate', + onclick:()=>void run(()=>api(`/api/gateways/${id}/rotate`,{}))}), + el('button',{class:'nes-btn',text:'Revoke access',onclick:async()=>{ + if(await confirmAction({title:`Revoke ${g.name}?`,body:'The agent is disconnected immediately and its credential stops working. You can reconnect it later with a new enrollment token.',action:'Revoke access'})) + await run(()=>api(`/api/gateways/${id}/revoke`,{}));}})); + } else { + actions.append(el('button',{class:'nes-btn is-primary',text:g.revoked?'Re-enroll with new token':'New enrollment token', + onclick:()=>void run(async()=>{const result=await api(`/api/gateways/${id}/enrollment`,{});showToken(result.token,g.name);})})); + } + return el('article',{class:'gw-card','data-state':kind,'data-gateway':g.id}, + el('div',{class:'gw-card-head'},el('span',{class:'gw-dot','aria-hidden':'true'}),heading,el('span',{class:'gw-pill gw-state',text:label}),tools), + // Activity and updates only mean something once an agent has reported in. + g.health&&stats,actions,g.enrolled&&!g.revoked&&updatesPanel(g)); + } + + function render(){ + renderSelector(); + // Don't rebuild the list under someone who is typing in it (rename, image digest). + const active=document.activeElement; + if(list.contains(active)&&active.matches('input,select,textarea')&&active.type!=='checkbox')return; + if(!gateways.length){list.replaceChildren(el('div',{class:'gw-empty'},el('strong',{text:'No gateways yet'}),el('p',{text:'Add your first gateway above to get its enrollment token.'})));return;} + list.replaceChildren(...gateways.map(card)); + } + + async function refresh(){ + if(busy){again=true;return;}busy=true; + try{const result=await api('/api/gateways');gateways=result.gateways;selected=result.selected; + if(editing&&!gateways.some(g=>g.id===editing))editing=null;render();} + catch(e){error(e.message);} + finally{busy=false;if(again){again=false;void refresh();}} + } + + async function deleteGateway(g){ + const ok=await confirmAction({title:`Delete ${g.name}?`, + body:'The gateway is disconnected, its credential is destroyed and it disappears from this list. Billing records and audit history are kept. To use the device again, add it as a new gateway.', + action:'Delete gateway'}); + if(!ok)return; + await run(async()=>{ + await api(`/api/gateways/${encodeURIComponent(g.id)}/delete`,{}); + if(g.id===selected)for(const app of NETWORK_APPS)Desktop.close(app); + openUpdates.delete(g.id); + }); + } + + function clearToken(){ + clearInterval(tokenTimer);tokenTimer=null; + $('gateway-enrollment-token').textContent='';$('gateway-enrollment').hidden=true; + } + function showToken(token,name){ + clearInterval(tokenTimer); + const expires=Date.now()+TOKEN_LIFETIME,expiry=$('gateway-enrollment-expiry'); + $('gateway-enrollment-name').textContent=name||'your gateway'; + $('gateway-enrollment-token').textContent=token;$('gateway-enrollment').hidden=false; + const tick=()=>{const left=Math.max(0,expires-Date.now()); + expiry.textContent=left?`Expires in ${Math.floor(left/60000)}:${String(Math.floor(left/1000)%60).padStart(2,'0')}`:'Expired: create a new token'; + expiry.classList.toggle('is-expired',!left);if(!left)clearInterval(tokenTimer);}; + tick();tokenTimer=setInterval(tick,1000); + $('gateway-enrollment').scrollIntoView({block:'nearest',behavior:'smooth'}); + } + select.onchange=async()=>{ const id=select.value;if(!id)return; - for(const app of ['terminal','chirpstack','browser'])Desktop.close(app); + for(const app of NETWORK_APPS)Desktop.close(app); try{await api('/api/gateways/select',{id});await refresh();}catch(e){error(e.message);select.value=selected||'';} }; - document.getElementById('manage-gateways').onclick=()=>{dialog.showModal();refresh();}; - document.getElementById('gateway-dialog-close').onclick=()=>dialog.close(); - dialog.addEventListener('close',()=>{document.getElementById('gateway-enrollment-token').textContent='';document.getElementById('gateway-enrollment').hidden=true;}); - document.getElementById('gateway-copy-token').onclick=()=>navigator.clipboard.writeText(document.getElementById('gateway-enrollment-token').textContent).catch(()=>error('Select and copy the token manually.')); - document.getElementById('gateway-create').onsubmit=async event=>{event.preventDefault();try{const result=await api('/api/gateways',{name:new FormData(event.target).get('name')});showToken(result.token);event.target.reset();await refresh();}catch(e){error(e.message);}}; - window.addEventListener('desktop:open',event=>{if(['terminal','chirpstack','browser'].includes(event.detail.id))title(event.detail.id);}); - const timer=setInterval(refresh,5000);window.addEventListener('pagehide',()=>clearInterval(timer));refresh(); + $('manage-gateways').onclick=()=>{dialog.showModal();refresh();}; + $('gateway-dialog-close').onclick=()=>dialog.close(); + // The enrollment secret never outlives the dialog. + dialog.addEventListener('close',()=>{clearToken();editing=null;error('');}); + $('gateway-enrollment-dismiss').onclick=clearToken; + $('gateway-copy-token').onclick=async event=>{ + const button=event.currentTarget; + try{await navigator.clipboard.writeText(`AGENT_ENROLLMENT_TOKEN=${$('gateway-enrollment-token').textContent}`); + button.textContent='Copied';setTimeout(()=>{button.textContent='Copy';},1500);} + catch{error('Select and copy the token manually.');} + }; + $('gateway-create').onsubmit=async event=>{ + event.preventDefault();const form=event.target,name=String(new FormData(form).get('name')||'').trim(); + await run(async()=>{const result=await api('/api/gateways',{name});showToken(result.token,result.name);form.reset();}); + }; + window.addEventListener('desktop:open',event=>{if(NETWORK_APPS.includes(event.detail.id))title(event.detail.id);}); + // Keep titles of already-open network apps in step with a rename. + const retitle=()=>{for(const app of NETWORK_APPS)if($(app)&&!$(app).hidden)title(app);}; + const timer=setInterval(()=>{void refresh().then(retitle);},5000);window.addEventListener('pagehide',()=>clearInterval(timer));refresh(); })(); diff --git a/src/views/index.hbs b/src/views/index.hbs index f7b03ef..e39fb6d 100644 --- a/src/views/index.hbs +++ b/src/views/index.hbs @@ -8,7 +8,16 @@ {{#if central}}

Choose a gateway to access its applications.

-

Gateways

Uncertain replies are held for reconciliation. A newer report supersedes an older unresolved reply; uncertainty never sends a relay command.

+ +
CENTRAL WORKSPACE

Gateways

Loading gateways…

+

Add a gateway

Name it after its site so it's easy to find. You'll get a one-time token to install on the gateway.

+ + +

Your gateways

+
+

Uncertain replies are held for reconciliation. A newer report supersedes an older unresolved reply; uncertainty never sends a relay command.

+
+

{{/if}}
diff --git a/tests/services/central-identity.test.ts b/tests/services/central-identity.test.ts index e9564d7..7c58208 100644 --- a/tests/services/central-identity.test.ts +++ b/tests/services/central-identity.test.ts @@ -108,3 +108,40 @@ test('downlink outcome replay is idempotent and distinguishes uncertain attempts expect(inspect.prepare("SELECT action FROM central_audit WHERE subject='report' ORDER BY id").all()).toEqual([{action:'state-downlink-uncertain'},{action:'state-downlink'}]);inspect.close(); }finally{identity.close();fs.rmSync(directory,{recursive:true,force:true});} }); + +test('gateways can be renamed with the same name rules and the change is audited', () => { + const g = store.createGateway('Old name', 'admin'); + expect(store.renameGateway(g.id, ' Rooftop A ', 'admin')).toMatchObject({ id: g.id, name: 'Rooftop A', enrolled: false }); + expect(store.gateways().map(x => x.name)).toEqual(['Rooftop A']); + for (const bad of ['', ' ', 'x'.repeat(101), 'bad\nname']) expect(() => store.renameGateway(g.id, bad, 'admin')).toThrow(); + expect(() => store.renameGateway('missing', 'Name', 'admin')).toThrow('Gateway not found.'); +}); + +test('deleting a gateway fences its credential and hides it while keeping history', () => { + const g = store.createGateway('Doomed', 'admin'), keep = store.createGateway('Keeper', 'admin'); + const { credential } = store.exchange(store.enrollment(g.id, 'admin'))!; + expect(store.gateways().find(x => x.id === g.id)?.enrolled).toBe(true); + store.observeDevice('0011223344556677', g.id);store.assignDevice('0011223344556677', g.id, 'admin'); + store.deleteGateway(g.id, 'admin'); + expect(store.gateways().map(x => x.id)).toEqual([keep.id]); + expect(store.authenticateAgent(credential)).toBeNull(); + expect(() => store.enrollment(g.id, 'admin')).toThrow(); + expect(() => store.renameGateway(g.id, 'Back', 'admin')).toThrow(); + expect(() => store.deleteGateway(g.id, 'admin')).toThrow('Gateway not found.'); + expect(store.devices()[0]).toMatchObject({ devEui: '0011223344556677', overridden: false }); +}); + +test('existing central databases gain the deleted column without losing gateways', () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'central-migrate-')), file = path.join(dir, 'central.db'); + try { + const legacy = new Database(file); + legacy.exec(`CREATE TABLE central_gateways (id TEXT PRIMARY KEY, name TEXT NOT NULL, credentialHash TEXT UNIQUE, + previousHash TEXT UNIQUE, previousUntil INTEGER, revoked INTEGER NOT NULL DEFAULT 0, lastHeartbeat INTEGER, version TEXT); + INSERT INTO central_gateways (id,name) VALUES ('legacy-id','Legacy');`); + legacy.close(); + const migrated = new SqliteIdentityStore(file); + expect(migrated.gateways()).toMatchObject([{ id: 'legacy-id', name: 'Legacy', revoked: false, enrolled: false }]); + migrated.close(); + new SqliteIdentityStore(file).close(); // Re-opening is idempotent. + } finally { fs.rmSync(dir, { recursive: true, force: true }); } +}); diff --git a/tests/services/central-link.test.ts b/tests/services/central-link.test.ts index eefdab8..cf1f861 100644 --- a/tests/services/central-link.test.ts +++ b/tests/services/central-link.test.ts @@ -58,6 +58,24 @@ test('authenticated outbound link returns the same stored decision by ChirpStack }finally{client?.disconnect();await f.close();} }); +test('rename updates the list; delete disconnects the agent and removes the gateway',async()=>{ + const f=await fixture(); + try{ + const ws=await f.connect(); + const post=(path:string,body?:unknown)=>fetch(f.base+path,{method:'POST',headers:{cookie:f.cookie,origin:f.base,'Content-Type':'application/json'},body:body===undefined?undefined:JSON.stringify(body)}); + const renamed=await post(`/api/gateways/${f.enrolled.gateway}/rename`,{name:'Renamed gateway'}); + expect(renamed.status).toBe(200);expect(await renamed.json()).toMatchObject({name:'Renamed gateway'}); + expect((await post(`/api/gateways/${f.enrolled.gateway}/rename`,{name:''})).status).toBe(400); + const closed=once(ws,'close'); + expect((await post(`/api/gateways/${f.enrolled.gateway}/delete`)).status).toBe(200);await closed; + const list=await (await fetch(f.base+'/api/gateways',{headers:{cookie:f.cookie}})).json() as {gateways:unknown[]}; + expect(list.gateways).toEqual([]); + expect(f.store.authenticateAgent(f.enrolled.credential)).toBeNull(); + expect((await post(`/api/gateways/${f.enrolled.gateway}/delete`)).status).toBe(404); + expect((await fetch(`${f.base}/api/gateways/${f.enrolled.gateway}/delete`,{method:'POST',headers:{cookie:f.cookie,origin:'https://foreign.invalid'}})).status).toBe(403); + }finally{await f.close();} +}); + test('central routes and socket upgrades refuse missing credentials and cross-site sessions',async()=>{ const f=await fixture(); try{ diff --git a/tests/ui/gateways.spec.cjs b/tests/ui/gateways.spec.cjs index aabacf0..8a35dcc 100644 --- a/tests/ui/gateways.spec.cjs +++ b/tests/ui/gateways.spec.cjs @@ -3,8 +3,8 @@ const { test,expect }=require('@playwright/test'); test('central selector identifies the gateway and exposes uncertainty without sending relay commands',async({page})=>{ let selected='gateway-one'; const gateways=[ - {id:'gateway-one',name:'North gateway',online:true,revoked:false,health:{mqtt:'connected',queueDepth:1,uncertain:3,reconciled:2,superseded:1}}, - {id:'gateway-two',name:'South gateway',online:false,revoked:false,health:{mqtt:'disconnected',queueDepth:0,uncertain:0,reconciled:0,superseded:0}}, + {id:'gateway-one',name:'North gateway',online:true,revoked:false,enrolled:true,health:{mqtt:'connected',queueDepth:1,uncertain:3,reconciled:2,superseded:1}}, + {id:'gateway-two',name:'South gateway',online:false,revoked:false,enrolled:true,health:{mqtt:'disconnected',queueDepth:0,uncertain:0,reconciled:0,superseded:0}}, ]; await page.route('**/api/gateways',route=>route.fulfill({json:{selected,gateways}})); await page.route('**/api/gateways/select',route=>{selected=route.request().postDataJSON().id;return route.fulfill({json:{selected}});}); @@ -20,7 +20,7 @@ test('central selector identifies the gateway and exposes uncertainty without se await page.getByRole('button',{name:'Gateways',exact:true}).click(); await expect(page.locator('#gateway-dialog')).toBeVisible(); await expect(page.locator('#gateway-dialog')).toContainText('uncertainty never sends a relay command'); - await expect(page.locator('#gateway-list')).toContainText('3 uncertain seen'); + await expect(page.locator('[data-gateway="gateway-one"] .gw-stat',{hasText:'Uncertain'})).toContainText('3'); await page.setViewportSize({width:390,height:844}); const bounds=await page.locator('#gateway-dialog').boundingBox(); expect(bounds.x).toBeGreaterThanOrEqual(0);expect(bounds.x+bounds.width).toBeLessThanOrEqual(390); @@ -32,3 +32,58 @@ test('standalone desktop does not expose central enrollment or gateway controls' await expect(page.locator('#gateway-dialog')).toHaveCount(0); await expect(page.locator('#chirpstack-icon')).toBeVisible(); }); + +test('gateways can be added, renamed and deleted from the management dialog',async({page})=>{ + let gateways=[ + {id:'gateway-one',name:'North gateway',online:true,revoked:false,enrolled:true,lastHeartbeat:Date.now(),version:'2.0.0',health:{mqtt:'connected',queueDepth:0,uncertain:0,reconciled:0,superseded:0},updates:[]}, + ]; + const calls=[]; + await page.route('**/api/gateways',route=>{ + if(route.request().method()==='POST'){ + const {name}=route.request().postDataJSON();calls.push(['create',name]); + gateways=[...gateways,{id:'gateway-new',name,online:false,revoked:false,enrolled:false,lastHeartbeat:null,version:null,health:null,updates:[]}]; + return route.fulfill({status:201,json:{id:'gateway-new',name,token:'T'.repeat(43)}}); + } + return route.fulfill({json:{selected:null,gateways}}); + }); + await page.route('**/api/gateways/*/rename',route=>{ + const id=route.request().url().split('/').at(-2),{name}=route.request().postDataJSON();calls.push(['rename',id,name]); + gateways=gateways.map(g=>g.id===id?{...g,name}:g);return route.fulfill({json:gateways.find(g=>g.id===id)}); + }); + await page.route('**/api/gateways/*/delete',route=>{ + const id=route.request().url().split('/').at(-2);calls.push(['delete',id]); + gateways=gateways.filter(g=>g.id!==id);return route.fulfill({json:{deleted:true}}); + }); + await page.goto('/?central=1'); + await page.getByRole('button',{name:'Gateways',exact:true}).click(); + await expect(page.locator('#gateway-summary')).toHaveText('1 gateway · 1 online'); + + await page.getByLabel('Gateway name').fill('Rooftop A'); + await page.getByRole('button',{name:'Add gateway'}).click(); + await expect(page.locator('#gateway-enrollment')).toBeVisible(); + await expect(page.locator('#gateway-enrollment-name')).toHaveText('Rooftop A'); + await expect(page.locator('#gateway-enrollment-token')).toHaveText('T'.repeat(43)); + await expect(page.locator('#gateway-enrollment-expiry')).toContainText('Expires in 9:5'); + await expect(page.locator('[data-gateway="gateway-new"] .gw-state')).toHaveText('Waiting to connect'); + + await page.getByRole('button',{name:'Rename North gateway'}).click(); + const input=page.getByLabel('New name for North gateway'); + await expect(input).toBeFocused(); + await input.fill('North roof');await input.press('Enter'); + await expect(page.locator('[data-gateway="gateway-one"] .gw-name')).toHaveText('North roof'); + await expect(page.locator('#gateway-select option[value="gateway-one"]')).toHaveText('North roof'); + + // Cancelling the confirmation leaves the gateway alone. + await page.getByRole('button',{name:'Delete North roof'}).click(); + await expect(page.locator('#gateway-confirm')).toBeVisible(); + await page.locator('#gateway-confirm').getByRole('button',{name:'Cancel'}).click(); + expect(calls.filter(c=>c[0]==='delete')).toEqual([]); + await page.getByRole('button',{name:'Delete North roof'}).click(); + await page.getByRole('button',{name:'Delete gateway'}).click(); + await expect(page.locator('[data-gateway="gateway-one"]')).toHaveCount(0); + expect(calls).toEqual([['create','Rooftop A'],['rename','gateway-one','North roof'],['delete','gateway-one']]); + + // The enrollment secret is cleared when the dialog closes. + await page.locator('#gateway-dialog-close').click(); + await expect(page.locator('#gateway-enrollment-token')).toHaveText(''); +}); From 081228f0c8ca1a62da39435b0ddedbb16d6f11d4 Mon Sep 17 00:00:00 2001 From: fluxion9 Date: Tue, 17 Nov 2026 23:07:10 +0100 Subject: [PATCH 03/17] feat: deploy gateway role (Console + agent) with auto-compose.sh CONSOLE_ROLE=gateway validates CENTRAL_URL, ChirpStack settings and enrollment before touching Docker, creates owner-only agent secrets without replacing existing ones, stops the standalone Console, starts Console and agent together and waits for the agent to stay up with a credential. A failed first switch restores standalone only if the agent never created an operations journal, so two deciders never run at once. Compose defaults for gateway, updater and browser images now use :main. The central enrollment panel shows all .env lines (with this central's wss URL) and points at ./auto-compose.sh. --- auto-compose.sh | 92 ++++++++++++++++++++++++++----- compose.browser.yaml | 2 +- compose.gateway.yaml | 4 +- compose.updater.yaml | 2 +- src/public/css/gateways.css | 1 + src/public/js/gateways.js | 4 +- src/views/index.hbs | 2 +- tests/services/deployment.test.ts | 70 ++++++++++++++++++++++- 8 files changed, 155 insertions(+), 22 deletions(-) diff --git a/auto-compose.sh b/auto-compose.sh index 3793bfb..ef09c31 100755 --- a/auto-compose.sh +++ b/auto-compose.sh @@ -13,6 +13,7 @@ while [[ $# -gt 0 ]]; do --help|-h) echo 'Usage: ./auto-compose.sh [--project-name NAME] [--image ghcr.io/OWNER/console:TAG]' echo 'Pulls the published image for this device, updates only Console, and preserves .env and all volumes.' + echo 'CONSOLE_ROLE in .env selects standalone (default), central, or gateway (Console + agent).' exit 0 ;; *) echo "Unknown option: $1" >&2; exit 2 ;; esac @@ -26,6 +27,12 @@ env_value() { } role=$(env_value CONSOLE_ROLE | sed -n 's/^\([a-z]*\).*/\1/p') role="${role:-standalone}" +services=(console) +# Gateway secrets must belong to the user the containers run as (root in the published image). +as_root=(); [[ "$(id -u)" -eq 0 ]] || as_root=(sudo) +secret_uid="${AGENT_SECRET_UID:-0}" +[[ "$secret_uid" =~ ^[0-9]+$ ]] || { echo 'AGENT_SECRET_UID must be a numeric user ID.' >&2; exit 2; } +has_credential() { "${as_root[@]}" test -s .data/agent/credential; } case "$role" in standalone) compose_file=compose.yaml; default_image=ghcr.io/m3tering/console:main ;; @@ -38,8 +45,19 @@ case "$role" in (( ${#password} >= 16 )) || { echo 'Set CENTRAL_ADMIN_PASSWORD in .env (at least 16 characters) for the first central start.' >&2; exit 2; } fi ;; gateway) - echo 'Gateway role needs the Console and agent together: see docs/centralized/deployment.md (compose.gateway.yaml).' >&2; exit 2 ;; - *) echo 'CONSOLE_ROLE in .env must be standalone or central for this script.' >&2; exit 2 ;; + compose_file=compose.gateway.yaml; default_image=ghcr.io/fluxion9/console:main; services=(console agent) + central_url=$(env_value CENTRAL_URL) + insecure=$(env_value CENTRAL_ALLOW_INSECURE_DEVELOPMENT) + [[ "$central_url" =~ ^wss://[^/?#@[:space:]]+/?$ || ( "$insecure" == true && "$central_url" =~ ^ws://[^/?#@[:space:]]+/?$ ) ]] \ + || { echo 'Set CENTRAL_URL in .env to your central origin, for example wss://console.example.com (no path).' >&2; exit 2; } + for name in APPLICATION_ID API_TOKEN; do + [[ -n "$(env_value "$name")" ]] || { echo "Set $name in .env: the agent reads this gateway's ChirpStack uplinks with it." >&2; exit 2; } + done + if ! has_credential && [[ -z "$(env_value AGENT_ENROLLMENT_TOKEN)" ]]; then + echo 'This gateway is not enrolled yet. On central open Gateways, add this gateway, and paste its' >&2 + echo 'AGENT_ENROLLMENT_TOKEN=... line into .env. The token expires after ten minutes.' >&2; exit 2 + fi ;; + *) echo 'CONSOLE_ROLE in .env must be standalone, central or gateway.' >&2; exit 2 ;; esac command -v docker >/dev/null || { echo 'Docker must already be installed.' >&2; exit 1; } docker compose version >/dev/null @@ -61,7 +79,7 @@ fi [[ "$project" =~ ^[a-z0-9][a-z0-9_-]*$ ]] || { echo 'Invalid Compose project name.' >&2; exit 2; } compose=(docker compose --project-name "$project" -f "$compose_file") old_container=$("${compose[@]}" ps -aq console | head -1) -old_id='' +old_id='' old_role='' if [[ -n "$old_container" ]]; then old_id=$(docker inspect --format '{{.Image}}' "$old_container") old_ref=$(docker inspect --format '{{.Config.Image}}' "$old_container") @@ -74,19 +92,61 @@ export CONSOLE_IMAGE="${image:-$default_image}" [[ "$CONSOLE_IMAGE" =~ ^ghcr\.io/[a-z0-9._/-]+(:[a-zA-Z0-9._-]+|@sha256:[a-f0-9]{64})$ ]] || { echo 'Specify a published GHCR image with an explicit tag or digest.' >&2; exit 2; } echo "Project: $project; role: $role; update image: $CONSOLE_IMAGE" "${compose[@]}" config --quiet -# Keep the old service and image until the correct architecture is downloaded. -"${compose[@]}" pull console -"${compose[@]}" stop console -"${compose[@]}" rm --force console -if ! "${compose[@]}" up --detach --no-deps --no-build --pull never --wait --wait-timeout 240 console; then - echo 'Console failed readiness. Recent logs:' >&2 - "${compose[@]}" logs --tail 80 console >&2 || true - if [[ -n "$old_id" ]]; then + +if [[ "$role" == gateway ]]; then + # Owner-only local secrets shared by Console and agent. Existing secrets are never replaced. + "${as_root[@]}" sh -c ' + set -e; uid=$1; umask 077 + mkdir -p .data/agent .data/updater + chmod 700 .data/agent .data/updater; chown "$uid" .data/agent .data/updater + for name in fallback-secret updater-secret; do + file=.data/agent/$name + [ -e "$file" ] || head -c 32 /dev/urandom | base64 | tr "+/" "-_" | tr -d "=\n" > "$file" + chmod 600 "$file"; chown "$uid" "$file" + done + if [ -e .data/agent/credential ]; then chmod 600 .data/agent/credential; chown "$uid" .data/agent/credential; fi + ' sh "$secret_uid" +fi + +# The agent counts as ready once it has stayed up without restarting and holds a credential. +agent_ready() { + local container status restarts check + container=$("${compose[@]}" ps -q agent | head -1); [[ -n "$container" ]] || return 1 + for (( check=1; check<=${AGENT_READY_CHECKS:-45}; check++ )); do + read -r status restarts < <(docker inspect --format '{{.State.Status}} {{.RestartCount}}' "$container") + [[ "$status" == running && "$restarts" == 0 ]] || return 1 + if (( check >= ${AGENT_STABLE_CHECKS:-5} )) && has_credential; then return 0; fi + sleep "${AGENT_READY_INTERVAL:-2}" + done + return 1 +} +deploy_failed() { + echo "${services[*]} failed readiness. Recent logs:" >&2 + "${compose[@]}" logs --tail 80 "${services[@]}" >&2 || true + if [[ -z "$old_id" ]]; then exit 1; fi + if [[ "$role" != gateway || "${old_role:-standalone}" == gateway ]]; then echo 'Restoring the previous image; persistent data has not been removed.' >&2 - CONSOLE_IMAGE="$old_id" "${compose[@]}" up --detach --no-deps --no-build --pull never --force-recreate console || true + CONSOLE_IMAGE="$old_id" "${compose[@]}" up --detach --no-deps --no-build --pull never --force-recreate "${services[@]}" || true + elif ! "${as_root[@]}" test -e .data/agent/operations.db; then + # The agent never got past enrollment, so it handled no meter reports: standalone can safely resume. + echo 'Restoring the standalone Console; the agent never started handling reports.' >&2 + "${compose[@]}" stop agent || true; "${compose[@]}" rm --force agent || true + CONSOLE_IMAGE="$old_id" docker compose --project-name "$project" -f compose.yaml up --detach --no-deps --no-build --pull never --force-recreate console || true + else + # Never run a standalone subscriber beside an agent journal: two deciders could answer one meter. + echo 'The agent has an operations journal, so standalone was NOT restarted automatically.' >&2 + echo 'Fix the problem above and rerun this script. See docs/centralized/deployment.md to return to standalone.' >&2 fi exit 1 -fi +} + +# Keep the old service and image until the correct architecture is downloaded. +"${compose[@]}" pull "${services[@]}" +# Stopping Console first also stops a standalone MQTT subscriber before the agent takes over. +"${compose[@]}" stop "${services[@]}" +"${compose[@]}" rm --force "${services[@]}" +"${compose[@]}" up --detach --no-deps --no-build --pull never --wait --wait-timeout 240 "${services[@]}" || deploy_failed +if [[ "$role" == gateway ]]; then agent_ready || deploy_failed; fi new_id=$(docker inspect --format '{{.Image}}' "$("${compose[@]}" ps -q console)") if [[ -n "$old_id" && "$old_id" != "$new_id" && -z "$(docker ps -aq --filter "ancestor=$old_id")" ]]; then # No force: Docker keeps images that still have other tags/references. @@ -98,6 +158,12 @@ if [[ "$role" == central ]]; then fi "${compose[@]}" ps echo 'Console updated. Database, extension volumes and .env were preserved.' +if [[ "$role" == gateway ]]; then + echo 'Gateway agent is running. Its card on central should show Online within a minute.' + if [[ -n "$(env_value AGENT_ENROLLMENT_TOKEN)" ]]; then + echo 'Enrollment is complete: remove the AGENT_ENROLLMENT_TOKEN line from .env (it cannot be reused).' + fi +fi if [[ "$role" == central ]]; then admin=$(env_value CENTRAL_ADMIN_USERNAME) echo "Central: https://$(env_value CENTRAL_DOMAIN) (sign in as ${admin:-admin})" diff --git a/compose.browser.yaml b/compose.browser.yaml index 9abe239..e6da022 100644 --- a/compose.browser.yaml +++ b/compose.browser.yaml @@ -1,7 +1,7 @@ # Optional image override for central/standalone. Prepare data ownership for UID 1000. services: console: - image: ${BROWSER_IMAGE:-ghcr.io/fluxion9/console-browser:feat-centralized-console} + image: ${BROWSER_IMAGE:-ghcr.io/fluxion9/console-browser:main} shm_size: 256mb mem_limit: 2g cpus: 2 diff --git a/compose.gateway.yaml b/compose.gateway.yaml index a2f131f..9919d44 100644 --- a/compose.gateway.yaml +++ b/compose.gateway.yaml @@ -1,7 +1,7 @@ # Opt-in test topology. Default compose.yaml is unchanged. services: console: - image: ${CONSOLE_IMAGE:-ghcr.io/fluxion9/console:feat-centralized-console} + image: ${CONSOLE_IMAGE:-ghcr.io/fluxion9/console:main} network_mode: host restart: unless-stopped env_file: [.env] @@ -23,7 +23,7 @@ services: start_period: 30s retries: 24 agent: - image: ${CONSOLE_IMAGE:-ghcr.io/fluxion9/console:feat-centralized-console} + image: ${CONSOLE_IMAGE:-ghcr.io/fluxion9/console:main} # Agent does not initialize extensions, contracts or a blockchain wallet. entrypoint: [node] command: [dist/agent/index.js] diff --git a/compose.updater.yaml b/compose.updater.yaml index 98006f2..72f069e 100644 --- a/compose.updater.yaml +++ b/compose.updater.yaml @@ -1,7 +1,7 @@ # Add to compose.gateway.yaml only after owner review. This service alone has Docker access. services: updater: - image: ${UPDATER_IMAGE:-ghcr.io/fluxion9/console-updater:feat-centralized-console} + image: ${UPDATER_IMAGE:-ghcr.io/fluxion9/console-updater:main} restart: unless-stopped environment: REMOTE_UPDATES_ENABLED: ${REMOTE_UPDATES_ENABLED:-false} diff --git a/src/public/css/gateways.css b/src/public/css/gateways.css index 064f14e..6937d57 100644 --- a/src/public/css/gateways.css +++ b/src/public/css/gateways.css @@ -41,6 +41,7 @@ .gw-token code,.gw-command{flex:1;min-width:0;display:block;padding:10px 12px;border-radius:var(--small-radius); background:#122127;color:#e7f0ea;overflow-wrap:anywhere;user-select:all} .gw-token-key{color:#9fc4ae} +.gw-token-line{display:block} #gateway-error{margin-top:14px;color:var(--gw-revoked);font-weight:500} #gateway-error:empty{display:none} diff --git a/src/public/js/gateways.js b/src/public/js/gateways.js index 086efbd..630afe6 100644 --- a/src/public/js/gateways.js +++ b/src/public/js/gateways.js @@ -167,6 +167,7 @@ clearInterval(tokenTimer); const expires=Date.now()+TOKEN_LIFETIME,expiry=$('gateway-enrollment-expiry'); $('gateway-enrollment-name').textContent=name||'your gateway'; + $('gateway-enrollment-url').textContent=`wss://${location.host}`; $('gateway-enrollment-token').textContent=token;$('gateway-enrollment').hidden=false; const tick=()=>{const left=Math.max(0,expires-Date.now()); expiry.textContent=left?`Expires in ${Math.floor(left/60000)}:${String(Math.floor(left/1000)%60).padStart(2,'0')}`:'Expired: create a new token'; @@ -187,7 +188,8 @@ $('gateway-enrollment-dismiss').onclick=clearToken; $('gateway-copy-token').onclick=async event=>{ const button=event.currentTarget; - try{await navigator.clipboard.writeText(`AGENT_ENROLLMENT_TOKEN=${$('gateway-enrollment-token').textContent}`); + const lines=[...$('gateway-enrollment').querySelectorAll('.gw-token-line')].map(line=>line.textContent).join('\n'); + try{await navigator.clipboard.writeText(lines); button.textContent='Copied';setTimeout(()=>{button.textContent='Copy';},1500);} catch{error('Select and copy the token manually.');} }; diff --git a/src/views/index.hbs b/src/views/index.hbs index e39fb6d..61abebc 100644 --- a/src/views/index.hbs +++ b/src/views/index.hbs @@ -11,7 +11,7 @@
CENTRAL WORKSPACE

Gateways

Loading gateways…

Add a gateway

Name it after its site so it's easy to find. You'll get a one-time token to install on the gateway.

- +

Your gateways

diff --git a/tests/services/deployment.test.ts b/tests/services/deployment.test.ts index 343766a..7181f33 100644 --- a/tests/services/deployment.test.ts +++ b/tests/services/deployment.test.ts @@ -66,7 +66,71 @@ test('central first start refuses a short administrator password before touching test('central restart does not require the bootstrap password', () => { expect(runCentral('CONSOLE_ROLE=central\nCENTRAL_DOMAIN=console.example.test\n',true).run.status).toBe(0); }); -test('gateway role is refused because it needs the agent alongside Console', () => { - const {run,calls}=runCentral('CONSOLE_ROLE=gateway\n'); - expect(run.status).toBe(2);expect(run.stderr).toContain('compose.gateway.yaml');expect(calls).toBe(''); +test('an unknown role is refused before touching Docker', () => { + const {run,calls}=runCentral('CONSOLE_ROLE=manager\n'); + expect(run.status).toBe(2);expect(run.stderr).toContain('standalone, central or gateway');expect(calls).toBe(''); +}); + +// Gateway role: Console + agent, owner-only secrets, safe fallback. Fake docker/sudo only. +function runGateway(environment:string,options:{credential?:boolean;journal?:boolean;agentState?:string;enrollOnUp?:boolean}={}) { + const dir=fs.mkdtempSync(path.join(os.tmpdir(),'console-gateway-')); + try { + for(const file of ['auto-compose.sh','compose.yaml','compose.gateway.yaml'])fs.copyFileSync(file,path.join(dir,file)); + fs.writeFileSync(path.join(dir,'.env'),environment); + fs.mkdirSync(path.join(dir,'.data','agent'),{recursive:true,mode:0o700}); + if(options.credential)fs.writeFileSync(path.join(dir,'.data','agent','credential'),'existing-credential-'.padEnd(80,'x'),{mode:0o600}); + if(options.journal)fs.writeFileSync(path.join(dir,'.data','agent','operations.db'),'journal'); + fs.writeFileSync(path.join(dir,'.data','agent','fallback-secret'),'keep-this-existing-secret'.padEnd(43,'k'),{mode:0o644}); + const log=path.join(dir,'calls');fs.writeFileSync(log,''); + fs.writeFileSync(path.join(dir,'sudo'),'#!/bin/bash\nexec "$@"\n',{mode:0o755}); + fs.writeFileSync(path.join(dir,'docker'),`#!/bin/bash +printf '%s|%s\\n' "$*" "$CONSOLE_IMAGE" >> "$DEPLOY_LOG" +case "$*" in + *'ps -aq console'*) echo old-container ;; + *'ps -q agent'*) echo agent-container ;; + *'ps -q console'*) echo new-container ;; + 'inspect --format {{.Image}} old-container') echo sha256:old ;; + 'inspect --format {{.Image}} new-container') echo sha256:new ;; + 'inspect --format {{.Config.Image}} old-container') echo ghcr.io/m3tering/console:main ;; + *'{{.State.Status}}'*) echo "$AGENT_STATE" ;; + *'compose.gateway.yaml up --detach'*'--wait'*) [[ "$ENROLL_ON_UP" == true ]] && printf 'gateway-id:credential' > .data/agent/credential; true ;; +esac +`,{mode:0o755}); + const run=spawnSync('bash',['auto-compose.sh','--project-name','site'],{cwd:dir,encoding:'utf8',env:{...process.env,PATH:dir+':'+process.env.PATH,DEPLOY_LOG:log, + AGENT_SECRET_UID:String(process.getuid!()),AGENT_READY_INTERVAL:'0',AGENT_STABLE_CHECKS:'2',AGENT_READY_CHECKS:'4', + AGENT_STATE:options.agentState||'running 0',ENROLL_ON_UP:String(!!options.enrollOnUp)}}); + const stat=(name:string)=>{try{return fs.statSync(path.join(dir,'.data','agent',name)).mode&0o777;}catch{return null;}}; + return {run,calls:fs.readFileSync(log,'utf8'),env:fs.readFileSync(path.join(dir,'.env'),'utf8'), + fallback:fs.readFileSync(path.join(dir,'.data','agent','fallback-secret'),'utf8'),modes:{fallback:stat('fallback-secret'),updater:stat('updater-secret'),agentDir:fs.statSync(path.join(dir,'.data','agent')).mode&0o777}}; + } finally {fs.rmSync(dir,{recursive:true,force:true});} +} +const gatewayEnv='CONSOLE_ROLE=gateway\nCENTRAL_URL=wss://console.example.test\nAPPLICATION_ID=app\nAPI_TOKEN=token\n'; +test('gateway role enrolls: secrets are owner-only, standalone stops before Console and agent start together', () => { + const environment=gatewayEnv+'AGENT_ENROLLMENT_TOKEN='+'T'.repeat(43)+'\n'; + const {run,calls,env,fallback,modes}=runGateway(environment,{enrollOnUp:true}); + expect(run.stderr).toBe('');expect(run.status).toBe(0); + expect(calls).toContain('-f compose.gateway.yaml pull console agent|ghcr.io/fluxion9/console:main'); + expect(calls.indexOf('stop console agent')).toBeGreaterThan(calls.indexOf('pull console agent')); + expect(calls.indexOf('up --detach --no-deps --no-build --pull never --wait --wait-timeout 240 console agent')).toBeGreaterThan(calls.indexOf('rm --force console agent')); + expect(calls).not.toContain('-f compose.yaml'); + expect(modes).toEqual({fallback:0o600,updater:0o600,agentDir:0o700}); + expect(fallback).toBe('keep-this-existing-secret'.padEnd(43,'k')); + expect(env).toBe(environment); + expect(run.stdout).toContain('remove the AGENT_ENROLLMENT_TOKEN line'); +}); +test('gateway role refuses to start without enrollment or with a bad central URL, before touching Docker', () => { + const missing=runGateway(gatewayEnv); + expect(missing.run.status).toBe(2);expect(missing.run.stderr).toContain('AGENT_ENROLLMENT_TOKEN');expect(missing.calls).toBe(''); + const badUrl=runGateway(gatewayEnv.replace('wss://console.example.test','https://console.example.test/agent'),{credential:true}); + expect(badUrl.run.status).toBe(2);expect(badUrl.run.stderr).toContain('CENTRAL_URL');expect(badUrl.calls).toBe(''); +}); +test('a failed first switch restores standalone only when the agent never handled reports', () => { + const safe=runGateway(gatewayEnv,{credential:true,agentState:'restarting 1'}); + expect(safe.run.status).toBe(1); + expect(safe.calls).toContain('stop agent'); + expect(safe.calls).toContain('-f compose.yaml up --detach --no-deps --no-build --pull never --force-recreate console|sha256:old'); + const journal=runGateway(gatewayEnv,{credential:true,journal:true,agentState:'restarting 1'}); + expect(journal.run.status).toBe(1); + expect(journal.calls).not.toContain('-f compose.yaml up'); + expect(journal.run.stderr).toContain('standalone was NOT restarted'); }); From 99ae4b96c73711d7b1229d2bdc09f3e18542b13f Mon Sep 17 00:00:00 2001 From: fluxion9 Date: Tue, 17 Nov 2026 23:14:42 +0100 Subject: [PATCH 04/17] feat: one-command standalone setup and a deployment guide auto-compose.sh now creates an owner-only .env from .env.example on a fresh checkout and explains what to fill in, refuses template placeholders and malformed ChirpStack application IDs before touching Docker (central ignores the ChirpStack ones), and explains missing Docker, Compose or permissions. docs/deployment-guide.md covers standalone, central and managed gateway setup, gateway management on central, updates, returning to standalone, an .env reference and troubleshooting. README Quick Setup points to it. --- README.md | 65 ++----- auto-compose.sh | 33 +++- docs/centralized/deployment.md | 3 + docs/deployment-guide.md | 301 ++++++++++++++++++++++++++++++ tests/services/deployment.test.ts | 37 +++- tests/services/install.test.ts | 2 +- 6 files changed, 382 insertions(+), 59 deletions(-) create mode 100644 docs/deployment-guide.md diff --git a/README.md b/README.md index aa468ac..a090b66 100644 --- a/README.md +++ b/README.md @@ -10,54 +10,20 @@ A modular, extensible service console for providers on the M3tering protocol. Fe ## Quick Setup -1. **Clone/Update** - - ```bash - git clone https://github.com/M3tering/Console.git - # or - cd Console/ && git pull - ``` - -2. **Environment Variables** - Create `.env` file: - - ``` - # Server Configuration - PORT=3000 - CONSOLE_HOST=127.0.0.1 - CONSOLE_USERNAME=admin - CONSOLE_PASSWORD= - - # Module Configuration - BACKEND_MODULES="core/arweave,core/is_on,core/prover,core/streamr" - UI_MODULES="streamr:core/streamr/ui" - - # ChirpStack Configuration - API_TOKEN=... - APPLICATION_ID=... - CHIRPSTACK_HOST=localhost - - # Contract & Network Configuration - CONTRACT_LABEL=M3ters - MAINNET_RPC=https://ethereum-sepolia-rpc.publicnode.com #alt https://eth-sepolia.g.alchemy.com/v2/ - ETHEREUM_PRIVATE_KEY="..." - ROLLUP_CONTRACT_ADDRESS="0x9a9ED9b9A9BEC10d1B5E41b1873E78d19501D5Fa" - - # Streamr Configuration - STREAMR_STREAM_ID="0x567853282663b601bfdb9203819b1fbb3fe18926/m3tering/test" - STREAMR_CRONSCHEDULE="0 * * * *" # Every hour - - # Optional: Prover Node (publishing is disabled when unset) - # PREFERRED_PROVER_NODE="https://prover.m3ter.ing" - # PROVER_CRONSCHEDULE="0 0 * * *" # Every 24 hours - ``` - -3. **Docker Build and Run** - - ```bash - sudo docker compose down - sudo docker compose up -d - ``` +One command installs and updates every setup: a standalone gateway, a central +server, or a gateway managed by central. **Full instructions: +[docs/deployment-guide.md](docs/deployment-guide.md).** + +```bash +git clone https://github.com/fluxion9/Console.git +cd Console +./auto-compose.sh # first run creates .env from the template +nano .env # fill in API_TOKEN, APPLICATION_ID, ETHEREUM_PRIVATE_KEY +./auto-compose.sh # starts Console; open http://:3000 +``` + +Set `CONSOLE_ROLE=central` or `CONSOLE_ROLE=gateway` in `.env` for the other +setups. Update any machine later with `git pull && ./auto-compose.sh`. ## Other Docker Commands @@ -644,7 +610,8 @@ survive desktop theme changes and reloads. Native content, dialogs, and terminal colors follow the chosen palette. Embedded external websites retain their own appearance controls; the containing window follows the application setting. -To update the existing Docker deployment from GitHub Actions images: +To update the existing Docker deployment from GitHub Actions images (see +[docs/deployment-guide.md](docs/deployment-guide.md) for central and managed gateways): ```sh ./auto-compose.sh diff --git a/auto-compose.sh b/auto-compose.sh index ef09c31..537f355 100755 --- a/auto-compose.sh +++ b/auto-compose.sh @@ -19,7 +19,17 @@ while [[ $# -gt 0 ]]; do esac done if [[ -n "$project" && ! "$project" =~ ^[a-z0-9][a-z0-9_-]*$ ]]; then echo 'Invalid Compose project name.' >&2; exit 2; fi -[[ -f .env ]] || { echo 'Missing .env: configure the existing services before deployment.' >&2; exit 1; } +if [[ ! -f .env ]]; then + [[ -f .env.example ]] || { echo 'Missing .env: configure the existing services before deployment.' >&2; exit 1; } + # First run: start from the template, readable only by its owner because it holds keys. + (umask 077; cp .env.example .env) + echo 'Created .env from .env.example (permissions 600). Fill in at least:' + echo ' API_TOKEN, APPLICATION_ID ChirpStack API token and application UUID' + echo ' ETHEREUM_PRIVATE_KEY wallet key for this Console (use a test wallet first)' + echo 'For central or gateway mode also set CONSOLE_ROLE; see docs/deployment-guide.md.' + echo 'Then run ./auto-compose.sh again.' + exit 0 +fi # Read one .env setting without executing the file; strips surrounding quotes. env_value() { sed -n "s/^[[:space:]]*$1[[:space:]]*=[[:space:]]*//p" .env | tail -1 \ @@ -33,8 +43,19 @@ as_root=(); [[ "$(id -u)" -eq 0 ]] || as_root=(sudo) secret_uid="${AGENT_SECRET_UID:-0}" [[ "$secret_uid" =~ ^[0-9]+$ ]] || { echo 'AGENT_SECRET_UID must be a numeric user ID.' >&2; exit 2; } has_credential() { "${as_root[@]}" test -s .data/agent/credential; } +# Template values such as API_TOKEN=... crash the Console later with an unclear error; stop now instead. +placeholders=$(sed -n -E "s/^[[:space:]]*([A-Za-z0-9_]+)[[:space:]]*=[[:space:]]*[\"']?(\.\.\.|REPLACE_WITH_[A-Za-z0-9_]*)[\"']?[[:space:]]*(#.*)?$/\1/p" .env | paste -sd, - | sed 's/,/, /g') +# Central never talks to ChirpStack, so its template ChirpStack lines may stay as they are. +if [[ "$role" == central ]]; then placeholders=$(tr -d ' ' <<<"$placeholders" | tr ',' '\n' | grep -vxE 'API_TOKEN|APPLICATION_ID' | paste -sd, - | sed 's/,/, /g' || true); fi +[[ -z "$placeholders" ]] || { echo "Replace the placeholder values in .env: $placeholders" >&2; exit 2; } +require_chirpstack() { + [[ -n "$(env_value API_TOKEN)" ]] || { echo 'Set API_TOKEN in .env to a ChirpStack API token.' >&2; exit 2; } + [[ "$(env_value APPLICATION_ID)" =~ ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$ ]] \ + || { echo 'Set APPLICATION_ID in .env to your ChirpStack application UUID (Applications page in ChirpStack).' >&2; exit 2; } +} case "$role" in standalone) + require_chirpstack compose_file=compose.yaml; default_image=ghcr.io/m3tering/console:main ;; central) compose_file=compose.central.yaml; default_image=ghcr.io/fluxion9/console:main @@ -50,18 +71,16 @@ case "$role" in insecure=$(env_value CENTRAL_ALLOW_INSECURE_DEVELOPMENT) [[ "$central_url" =~ ^wss://[^/?#@[:space:]]+/?$ || ( "$insecure" == true && "$central_url" =~ ^ws://[^/?#@[:space:]]+/?$ ) ]] \ || { echo 'Set CENTRAL_URL in .env to your central origin, for example wss://console.example.com (no path).' >&2; exit 2; } - for name in APPLICATION_ID API_TOKEN; do - [[ -n "$(env_value "$name")" ]] || { echo "Set $name in .env: the agent reads this gateway's ChirpStack uplinks with it." >&2; exit 2; } - done + require_chirpstack if ! has_credential && [[ -z "$(env_value AGENT_ENROLLMENT_TOKEN)" ]]; then echo 'This gateway is not enrolled yet. On central open Gateways, add this gateway, and paste its' >&2 echo 'AGENT_ENROLLMENT_TOKEN=... line into .env. The token expires after ten minutes.' >&2; exit 2 fi ;; *) echo 'CONSOLE_ROLE in .env must be standalone, central or gateway.' >&2; exit 2 ;; esac -command -v docker >/dev/null || { echo 'Docker must already be installed.' >&2; exit 1; } -docker compose version >/dev/null -docker info >/dev/null +command -v docker >/dev/null || { echo 'Docker is not installed. Install it with: curl -fsSL https://get.docker.com | sudo sh' >&2; exit 1; } +docker compose version >/dev/null 2>&1 || { echo 'The Docker Compose plugin is missing; install docker-compose-plugin.' >&2; exit 1; } +docker info >/dev/null 2>&1 || { echo 'Cannot reach Docker. Run with sudo, or: sudo usermod -aG docker "$USER" and log in again.' >&2; exit 1; } # Discover deployments belonging to this checkout, not unrelated Compose projects. if [[ -z "$project" ]]; then projects=() diff --git a/docs/centralized/deployment.md b/docs/centralized/deployment.md index 076040a..36f64d7 100644 --- a/docs/centralized/deployment.md +++ b/docs/centralized/deployment.md @@ -1,5 +1,8 @@ # Opt-in test deployment +For everyday installation and updates, follow [../deployment-guide.md](../deployment-guide.md). +This page keeps the detailed security notes and advanced options. + This branch is for isolated evaluation, not a production rollout. Read [test-plan.md](test-plan.md) for executed checks and release gates. No live gateway, meter, ChirpStack server or blockchain was contacted during implementation. diff --git a/docs/deployment-guide.md b/docs/deployment-guide.md new file mode 100644 index 0000000..1bb003d --- /dev/null +++ b/docs/deployment-guide.md @@ -0,0 +1,301 @@ +# Deployment guide + +How to install, connect and update M3tering Console. Every setup uses the same +command, `./auto-compose.sh`. The `CONSOLE_ROLE` setting in `.env` decides what it +deploys. + +- [Pick a setup](#pick-a-setup) +- [Before you start](#before-you-start) +- [Standalone gateway](#standalone-gateway) +- [Central server](#central-server) +- [Managed gateway (connect to central)](#managed-gateway-connect-to-central) +- [Managing gateways on central](#managing-gateways-on-central) +- [Updating](#updating) +- [Going back to standalone](#going-back-to-standalone) +- [`.env` reference](#env-reference) +- [Troubleshooting](#troubleshooting) + +## Pick a setup + +| Setup | `CONSOLE_ROLE` | Runs | Use it when | +|---|---|---|---| +| Standalone gateway | *(unset)* or `standalone` | One Console, on the gateway | A single site, managed on its own LAN | +| Central server | `central` | Console behind HTTPS (Caddy) | You want one login to reach every site | +| Managed gateway | `gateway` | Console + agent, on the gateway | A site that connects to your central server | + +``` + browser ──HTTPS──▶ CENTRAL SERVER (login, billing decisions, publishing) + ▲ ▲ + outbound wss │ │ outbound wss + │ │ + MANAGED GATEWAY ─────────────┘ └──────────── MANAGED GATEWAY + Console (LAN UI, local fallback) Console + agent + agent (reads ChirpStack MQTT, replies to meters) +``` + +Gateways only make **outbound** connections to central. You never open a port on +a gateway. If central is unreachable, a managed gateway keeps billing locally and +catches up later. + +## Before you start + +On every machine: + +- **Docker with the Compose plugin.** If it's missing: + `curl -fsSL https://get.docker.com | sudo sh`, then + `sudo usermod -aG docker "$USER"` and log in again. +- **Git**, to fetch this repository. +- **At least 3 GB of free disk space.** The Console image is large. Check with `df -h /`. +- **Access to the images.** They're published to GitHub Container Registry by CI + on every push to `main`. If the package is private, log in once: + `docker login ghcr.io -u ` with a token that has `read:packages`. + +Get the code: + +```sh +git clone https://github.com/fluxion9/Console.git +cd Console +``` + +On an existing checkout, point it at this repository once: +`git remote set-url origin https://github.com/fluxion9/Console.git && git pull`. + +## Standalone gateway + +1. **Create the configuration.** On a fresh checkout, run: + ```sh + ./auto-compose.sh + ``` + This creates `.env` from the template, readable only by you, and lists the + settings you must fill in. Nothing is started yet. +2. **Fill in `.env`.** At minimum: + ```dotenv + API_TOKEN= + APPLICATION_ID= + CHIRPSTACK_HOST=localhost + ETHEREUM_PRIVATE_KEY= + ``` + Keep the template's contract and Streamr values unless you know you need + others. The script refuses to start while any `...` placeholder is left. +3. **Start it.** + ```sh + ./auto-compose.sh + ``` +4. **Open it** at `http://:3000` on the LAN. There's no login. + +## Central server + +You need a server with a public DNS name and free ports **80** and **443** (Caddy +uses them for HTTPS), plus port **3000**, which the Console uses on `localhost` +only. Check with `sudo ss -ltnp | grep -E ':(80|443|3000)\b'`. It should print nothing. + +1. Point your domain's DNS record at the server. +2. Create `.env` as in standalone step 1, then set: + ```dotenv + CONSOLE_ROLE=central + CENTRAL_DOMAIN=console.example.com + CENTRAL_ADMIN_USERNAME=admin + CENTRAL_ADMIN_PASSWORD= + ETHEREUM_PRIVATE_KEY= + ``` + Central doesn't use ChirpStack, so the template's `API_TOKEN` and + `APPLICATION_ID` lines can stay as they are. +3. Start it: + ```sh + ./auto-compose.sh + ``` +4. Open `https://console.example.com` and sign in. + +About the admin account: + +- It's created **only on the first start**. Changing `CENTRAL_ADMIN_PASSWORD` + later does nothing. +- A password shorter than 16 characters stops the first start with a clear error. + +Central holds the wallet key that signs billing decisions. Treat the server as +sensitive, and test with a test wallet first. + +## Managed gateway (connect to central) + +The gateway needs a working ChirpStack setup (`API_TOKEN`, `APPLICATION_ID`) and +contract settings in `.env`, just like a standalone gateway. Converting an +existing standalone gateway is the usual case: keep its `.env` and add three lines. + +1. **On central**, open **Gateways**, type a name (for example the site name) and + click **Add gateway**. +2. Click **Copy** in the panel that appears. It copies three lines: + ```dotenv + CONSOLE_ROLE=gateway + CENTRAL_URL=wss://console.example.com + AGENT_ENROLLMENT_TOKEN=… + ``` +3. **On the gateway**, paste them into `.env`, then run: + ```sh + git pull + ./auto-compose.sh + ``` + The token works **once** and expires after **10 minutes**. If it expires, + click **New enrollment token** on the gateway's card. +4. Within a minute the card on central turns green and shows **Online**. +5. Delete the `AGENT_ENROLLMENT_TOKEN` line from the gateway's `.env`. The script + reminds you. The gateway has stored a permanent credential in `.data/agent/`. + +What the script does for a gateway: + +- **Checks first:** it validates `CENTRAL_URL`, the ChirpStack settings and the + token before touching Docker. +- **Creates secrets:** it creates the gateway's owner-only secret files in + `.data/agent/`. It uses `sudo`, so it may ask for your password. Existing + secrets are never replaced. +- **Switches over:** it stops the standalone Console, then starts Console and + agent together. The two never run at the same time. +- **Waits:** it checks that the agent stays up and has enrolled. + +If the first switch fails, the script restores standalone, but only if the agent +never started handling meter reports. If the agent had already started, the +script leaves things as they are and tells you, so two processes never answer the +same meter. Fix the error shown and run the script again. + +The gateway's own LAN desktop at `http://:3000` keeps working, without +a login. + +## Managing gateways on central + +Open **Gateways** from the top bar. Each gateway is a card. + +| Status | Meaning | +|---|---| +| **Online** | The agent is connected | +| **Offline** | Enrolled but not connected: check the gateway is running and can reach central | +| **Waiting to connect** | Created on central, but the gateway hasn't used its token yet | +| **Access revoked** | Its credential was cancelled; it can't connect | + +The activity tiles appear once a gateway has reported in: + +| Tile | Meaning | +|---|---| +| **MQTT** | The agent's connection to ChirpStack's broker | +| **Pending** | Reports waiting for a decision | +| **Uncertain** | Running total of replies whose delivery couldn't be confirmed. They are held, never resent automatically. | +| **Reconciled** | Uncertain replies later resolved | +| **Superseded** | Old replies replaced by a newer report from the same meter | + +| Action | What it does | +|---|---| +| **Rename** | Changes the display name. Press Enter to save, Esc to cancel. | +| **Delete** | Disconnects the gateway, destroys its credential and removes it from the list. Billing and audit history are kept. | +| **Revoke access** | Disconnects the gateway and cancels its credential, but keeps the card | +| **Rotate credential** | Issues a new credential to an online gateway without interrupting it | +| **New enrollment token** / **Re-enroll** | Issues a fresh one-time token for a gateway that isn't connected | +| **Remote updates** | Pushes a signed image to the gateway. The gateway owner must enable this locally first; see [advanced options](centralized/deployment.md#optional-signed-updater). | + +**Reconnecting a revoked or deleted gateway.** The gateway still holds its old, +now useless credential. Remove it before using a new token: + +```sh +sudo rm .data/agent/credential +# add the new AGENT_ENROLLMENT_TOKEN line to .env, then: +./auto-compose.sh +``` + +For a deleted gateway, first add it again on central as a new gateway. + +## Updating + +Same command on every machine: + +```sh +git pull +./auto-compose.sh +``` + +The script: + +- pulls the newest image for the tag the machine already runs; +- replaces only the Console services and waits for them to be healthy; +- restores the previous image if they aren't; +- never touches `.env`, the database or volumes. + +To run a specific build, pass its tag or digest: + +```sh +./auto-compose.sh --image ghcr.io/fluxion9/console:main +./auto-compose.sh --image ghcr.io/fluxion9/console@sha256: +``` + +| Role | Default image on first install | +|---|---| +| Standalone | `ghcr.io/m3tering/console:main` | +| Central and managed gateways | `ghcr.io/fluxion9/console:main` | + +## Going back to standalone + +On the gateway: + +```sh +docker compose -f compose.gateway.yaml stop agent +``` + +1. Check the gateway's card on central shows **Pending 0** before going + further. If it doesn't, read [the reconciliation rules](centralized/design.md) + first. +2. Remove `CONSOLE_ROLE`, `CENTRAL_URL` and any token line from `.env`. +3. Run `./auto-compose.sh --image ghcr.io/fluxion9/console:main`. The `--image` + keeps the build you were running; without it, standalone uses its own default. +4. Revoke or delete the gateway on central. + +Keep `.data/agent/`. It holds the operations journal, which is needed if anything +has to be reconciled later. + +## `.env` reference + +Settings are needed (✓), optional (opt) or not used (–) per setup. + +| Setting | Standalone | Central | Gateway | Notes | +|---|---|---|---|---| +| `CONSOLE_ROLE` | – | `central` | `gateway` | Unset means standalone | +| `API_TOKEN` | ✓ | – | ✓ | ChirpStack API token | +| `APPLICATION_ID` | ✓ | – | ✓ | ChirpStack application UUID | +| `CHIRPSTACK_HOST` | ✓ | – | ✓ | Usually `localhost` | +| `MQTT_URL` | opt | – | opt | Defaults to `CHIRPSTACK_HOST:1883` | +| `ETHEREUM_PRIVATE_KEY` | ✓ | ✓ | ✓ | Wallet for this Console | +| `MAINNET_RPC`, `ROLLUP_CONTRACT_ADDRESS` | ✓ | ✓ | ✓ | Keep the template values unless told otherwise | +| `BACKEND_MODULES`, `UI_MODULES`, `STREAMR_*` | ✓ | ✓ | ✓ | Keep the template values | +| `PORT` | opt | opt | opt | Default `3000` | +| `CENTRAL_DOMAIN` | – | ✓ | – | DNS name for HTTPS | +| `CENTRAL_ADMIN_USERNAME` | – | opt | – | Default `admin` | +| `CENTRAL_ADMIN_PASSWORD` | – | ✓ first start | – | At least 16 characters | +| `CENTRAL_URL` | – | – | ✓ | `wss://` + central domain, no path | +| `AGENT_ENROLLMENT_TOKEN` | – | – | first start | From central; remove after enrolling | +| `CONSOLE_IMAGE` | opt | opt | opt | Overrides the image the script uses | + +Advanced gateway settings (Browser ports, SSH port, queue size, remote updates) +are listed in [`.env.example`](../.env.example) and +[docs/centralized/deployment.md](centralized/deployment.md). + +## Troubleshooting + +Useful commands, using the compose file for the machine's role +(`compose.yaml`, `compose.central.yaml` or `compose.gateway.yaml`): + +```sh +docker compose -f compose.gateway.yaml ps +docker compose -f compose.gateway.yaml logs -f console +docker compose -f compose.gateway.yaml logs -f agent +``` + +| Symptom | Cause and fix | +|---|---| +| `Replace the placeholder values in .env: …` | Template values are still in `.env`. Fill in the named settings. | +| `Set APPLICATION_ID … UUID` | Copy the application's ID from ChirpStack's Applications page. | +| `Cannot reach Docker` | Run with `sudo`, or add yourself to the `docker` group and log in again. | +| `no space left on device` while pulling | The disk is full. Free space with `docker builder prune` and `docker image prune`. Check what's unused with `docker image ls` first, because locally built images can't be pulled back. Nothing running is affected by the failed pull. | +| `unauthorized` / `denied` while pulling | The image package is private, or CI hasn't published it yet. Make the package public or `docker login ghcr.io`, and check the Actions run on `main` passed. | +| Central: Console keeps restarting on first start | Usually a short `CENTRAL_ADMIN_PASSWORD`. Check `logs console`. | +| Central: HTTPS doesn't work | DNS doesn't point at the server yet, or ports 80/443 are taken. | +| Gateway: `This gateway is not enrolled yet` | Add the `AGENT_ENROLLMENT_TOKEN` line from central. | +| Gateway: `Agent enrollment failed` in agent logs | The token expired or was already used. Get a new one from the gateway's card. | +| Gateway card stays **Waiting to connect** | The gateway can't reach central on port 443, or `CENTRAL_URL` is wrong. Try `curl -I https://console.example.com` from the gateway. | +| Gateway card shows **Offline** after a revoke/delete | Expected. To reconnect, see [Reconnecting a revoked or deleted gateway](#managing-gateways-on-central). | +| `Secret file must be owner-only` in logs | Someone changed permissions in `.data/agent/`. Run `./auto-compose.sh` again; it repairs them. | +| Script says *standalone was NOT restarted* | The agent had started handling reports when something failed. Fix the error shown and rerun; don't start standalone by hand. | diff --git a/tests/services/deployment.test.ts b/tests/services/deployment.test.ts index 7181f33..359fd20 100644 --- a/tests/services/deployment.test.ts +++ b/tests/services/deployment.test.ts @@ -7,7 +7,7 @@ test.each([false,true])('deployment preserves data and rolls back on readiness f const dir=fs.mkdtempSync(path.join(os.tmpdir(),'console-deploy-')); try { for(const file of ['auto-compose.sh','compose.yaml'])fs.copyFileSync(file,path.join(dir,file)); - const environment = 'M3TER_MANAGER_PASSWORD=preserve-me\nMQTT_URL=mqtts://existing-broker:8883\nCHEFSTACK_URL=https://network.example\nM3TER_TELEMETRY_LIMIT=250\n'; + const environment = 'API_TOKEN=token\nAPPLICATION_ID=0b8c1a6e-4f2d-4c8a-9d3e-7a6b5c4d3e2f\nM3TER_MANAGER_PASSWORD=preserve-me\nMQTT_URL=mqtts://existing-broker:8883\nCHEFSTACK_URL=https://network.example\nM3TER_TELEMETRY_LIMIT=250\n'; fs.writeFileSync(path.join(dir,'.env'),environment); fs.mkdirSync(path.join(dir,'.data')); fs.writeFileSync(path.join(dir,'.data','sentinel'),'keep'); const log=path.join(dir,'calls'); @@ -63,6 +63,12 @@ test('central first start refuses a short administrator password before touching const {run,calls}=runCentral('CONSOLE_ROLE=central\nCENTRAL_DOMAIN=console.example.test\nCENTRAL_ADMIN_PASSWORD=short\n'); expect(run.status).toBe(2);expect(run.stderr).toContain('at least 16 characters');expect(calls).toBe(''); }); +test('central ignores template ChirpStack placeholders but not other placeholders', () => { + const base='CONSOLE_ROLE=central\nCENTRAL_DOMAIN=console.example.test\nCENTRAL_ADMIN_PASSWORD=a-very-long-password-1\nAPI_TOKEN=...\nAPPLICATION_ID=...\n'; + expect(runCentral(base).run.status).toBe(0); + const missingKey=runCentral(base+'ETHEREUM_PRIVATE_KEY="..."\n'); + expect(missingKey.run.status).toBe(2);expect(missingKey.run.stderr).toContain('placeholder values in .env: ETHEREUM_PRIVATE_KEY'); +}); test('central restart does not require the bootstrap password', () => { expect(runCentral('CONSOLE_ROLE=central\nCENTRAL_DOMAIN=console.example.test\n',true).run.status).toBe(0); }); @@ -104,7 +110,7 @@ esac fallback:fs.readFileSync(path.join(dir,'.data','agent','fallback-secret'),'utf8'),modes:{fallback:stat('fallback-secret'),updater:stat('updater-secret'),agentDir:fs.statSync(path.join(dir,'.data','agent')).mode&0o777}}; } finally {fs.rmSync(dir,{recursive:true,force:true});} } -const gatewayEnv='CONSOLE_ROLE=gateway\nCENTRAL_URL=wss://console.example.test\nAPPLICATION_ID=app\nAPI_TOKEN=token\n'; +const gatewayEnv='CONSOLE_ROLE=gateway\nCENTRAL_URL=wss://console.example.test\nAPPLICATION_ID=0b8c1a6e-4f2d-4c8a-9d3e-7a6b5c4d3e2f\nAPI_TOKEN=token\n'; test('gateway role enrolls: secrets are owner-only, standalone stops before Console and agent start together', () => { const environment=gatewayEnv+'AGENT_ENROLLMENT_TOKEN='+'T'.repeat(43)+'\n'; const {run,calls,env,fallback,modes}=runGateway(environment,{enrollOnUp:true}); @@ -134,3 +140,30 @@ test('a failed first switch restores standalone only when the agent never handle expect(journal.calls).not.toContain('-f compose.yaml up'); expect(journal.run.stderr).toContain('standalone was NOT restarted'); }); + +// First run and configuration checks never reach Docker. +function runFresh(files:Record) { + const dir=fs.mkdtempSync(path.join(os.tmpdir(),'console-fresh-')); + try { + fs.copyFileSync('auto-compose.sh',path.join(dir,'auto-compose.sh')); + for(const [name,content] of Object.entries(files))fs.writeFileSync(path.join(dir,name),content); + const log=path.join(dir,'calls');fs.writeFileSync(log,''); + fs.writeFileSync(path.join(dir,'docker'),'#!/bin/bash\nprintf "%s\\n" "$*" >> "$DEPLOY_LOG"\n',{mode:0o755}); + const run=spawnSync('bash',['auto-compose.sh'],{cwd:dir,encoding:'utf8',env:{...process.env,PATH:dir+':'+process.env.PATH,DEPLOY_LOG:log}}); + const env=path.join(dir,'.env'); + return {run,calls:fs.readFileSync(log,'utf8'),env:fs.existsSync(env)?fs.readFileSync(env,'utf8'):null,mode:fs.existsSync(env)?fs.statSync(env).mode&0o777:null}; + } finally {fs.rmSync(dir,{recursive:true,force:true});} +} +test('first run creates an owner-only .env from the template and explains what to fill in', () => { + const template=fs.readFileSync('.env.example','utf8'); + const {run,calls,env,mode}=runFresh({'.env.example':template}); + expect(run.status).toBe(0);expect(env).toBe(template);expect(mode).toBe(0o600);expect(calls).toBe(''); + expect(run.stdout).toContain('API_TOKEN, APPLICATION_ID');expect(run.stdout).toContain('docs/deployment-guide.md'); +}); +test('template placeholders and a malformed application ID are refused before touching Docker', () => { + const placeholder=runFresh({'.env':fs.readFileSync('.env.example','utf8')}); + expect(placeholder.run.status).toBe(2);expect(placeholder.calls).toBe(''); + expect(placeholder.run.stderr).toContain('API_TOKEN, APPLICATION_ID, ETHEREUM_PRIVATE_KEY'); + const badId=runFresh({'.env':'API_TOKEN=token\nAPPLICATION_ID=not-a-uuid\n'}); + expect(badId.run.status).toBe(2);expect(badId.run.stderr).toContain('application UUID');expect(badId.calls).toBe(''); +}); diff --git a/tests/services/install.test.ts b/tests/services/install.test.ts index c874a5c..966a06f 100644 --- a/tests/services/install.test.ts +++ b/tests/services/install.test.ts @@ -5,7 +5,7 @@ import { spawnSync } from 'child_process'; let directory:string; beforeEach(()=>{ directory=mkdtempSync(join(tmpdir(),'console install ')); mkdirSync(join(directory,'bin')); - copyFileSync(resolve('auto-compose.sh'),join(directory,'auto-compose.sh')); writeFileSync(join(directory,'.env'),'# fixture\n'); + copyFileSync(resolve('auto-compose.sh'),join(directory,'auto-compose.sh')); writeFileSync(join(directory,'.env'),'# fixture\nAPI_TOKEN=token\nAPPLICATION_ID=0b8c1a6e-4f2d-4c8a-9d3e-7a6b5c4d3e2f\n'); writeFileSync(join(directory,'bin','docker'),`#!/usr/bin/env bash printf '%s\\n' "$*" >> "$INSTALL_TEST_LOG" if [[ "$*" == *"pull console"* && "\${INSTALL_TEST_FAIL_PULL:-}" == 1 ]]; then exit 1; fi From e33835d72fca0a12c45943750c5d8f1f531b1728 Mon Sep 17 00:00:00 2001 From: fluxion9 Date: Tue, 17 Nov 2026 23:25:06 +0100 Subject: [PATCH 05/17] feat: design the central sign-in page The sign-in page is served before authentication, so it cannot load the desktop's stylesheets. It now has its own inline design matching the desktop (brand mark, green palette, 'A little more connected.' hero), with dark mode and a single-card phone layout. The strict policy stays: default-src 'none', no scripts, and the inline stylesheet is allowed only by its exact SHA-256 hash. Failed sign-ins and rate limiting re-show the page with an inline error and keep a well-formed, escaped username; the password is never echoed. Signing out returns to the page with a confirmation. --- src/central/auth.ts | 23 ++++--- src/central/login-page.ts | 95 +++++++++++++++++++++++++++++ tests/services/central-auth.test.ts | 20 ++++++ 3 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 src/central/login-page.ts diff --git a/src/central/auth.ts b/src/central/auth.ts index 5397e09..2882afb 100644 --- a/src/central/auth.ts +++ b/src/central/auth.ts @@ -5,9 +5,10 @@ import { TLSSocket } from 'tls'; import type { IdentityStore, SessionIdentity } from './identity-store'; import { hashPassword, verifyPassword } from './secrets'; import { isSameOrigin } from '../services/auth'; +import { loginPage, loginPolicy, LoginView } from './login-page'; const cookieName = '__Host-console_session'; -const loginPage = `Sign in · M3tering Console

M3tering Console

Sign in to the central workspace.

`; +const validUsername = /^[A-Za-z0-9_.@-]{1,100}$/; /** Bounded fixed-window limiter. Uses the socket peer, never untrusted XFF. */ export class RateLimit { @@ -55,12 +56,13 @@ export class CentralAuth { if (!isSameOrigin(req)) { res.status(403).send('Cross-site requests are not permitted.'); return; } next(); }); - app.get('/login', (_req, res) => { - res.setHeader('Content-Security-Policy', "default-src 'none'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'"); - res.type('html').send(loginPage); + app.get('/login', (req, res) => { + CentralAuth.page(res, 200, 'signed-out' in req.query ? { notice: 'You have been signed out.' } : {}); }); app.post('/login', (req, res, next) => { - if (!this.globalLimiter.allow('login') || !this.limiter.allow(req.socket.remoteAddress || 'unknown')) { res.status(429).send('Too many attempts. Try again later.'); return; } + if (!this.globalLimiter.allow('login') || !this.limiter.allow(req.socket.remoteAddress || 'unknown')) { + CentralAuth.page(res, 429, { error: 'Too many sign-in attempts. Wait a minute and try again.' }); return; + } next(); }, express.urlencoded({ extended: false, limit: '3kb', parameterLimit: 2 }), (req, res, next) => { void this.login(req, res).catch(next); }); machineRoutes?.(app); @@ -70,9 +72,13 @@ export class CentralAuth { this.store.endSession(this.token(req)); this.store.audit(session?.id || 'unknown', 'logout'); res.setHeader('Set-Cookie', this.cookie('', true)); - res.redirect(303, '/login'); + res.redirect(303, '/login?signed-out'); }); } + private static page(res: Response, status: number, view: LoginView): void { + res.setHeader('Content-Security-Policy', loginPolicy); + res.status(status).type('html').send(loginPage(view)); + } requireLogin = (req: Request, res: Response, next: NextFunction): void => { const session = this.session(req); if (!session) { @@ -85,12 +91,13 @@ export class CentralAuth { }; private async login(req: Request, res: Response): Promise { const username = typeof req.body?.username === 'string' ? req.body.username : ''; - const user = /^[A-Za-z0-9_.@-]{1,100}$/.test(username) ? this.store.user(username) : null; + const user = validUsername.test(username) ? this.store.user(username) : null; const valid = await verifyPassword(req.body?.password, user?.passwordHash || await this.dummyHash); if (!user || !valid) { // Do not audit the attacker-supplied username/password or the request body. this.store.audit('anonymous', 'login-failure'); - res.status(401).send('Invalid username or password.'); return; + // Only a well-formed username is shown again (escaped); the password never is. + CentralAuth.page(res, 401, { error: 'That username and password don’t match. Try again.', username: validUsername.test(username) ? username : '' }); return; } this.store.endSession(this.token(req)); const session = this.store.createSession(user.id); diff --git a/src/central/login-page.ts b/src/central/login-page.ts new file mode 100644 index 0000000..53f6bfe --- /dev/null +++ b/src/central/login-page.ts @@ -0,0 +1,95 @@ +import { createHash } from 'crypto'; + +/** Central sign-in page. Served before authentication, so it cannot load the desktop's + * static CSS; everything is inline and allowed by an exact hash. No scripts at all. */ +const css = ` +:root{--ink:#1f332c;--muted:#5f6f67;--accent:#316a4d;--accent-ink:#fff;--card:#fbfcfa;--field:#fff;--line:#cdd6cb; + --danger:#a53a43;--danger-bg:#fbeeee;--ok-bg:#eaf4ee;--shadow:0 30px 80px #04191433,0 2px 10px #04191418;color-scheme:light} +@media (prefers-color-scheme:dark){:root{--ink:#eef2ef;--muted:#a9b7b0;--accent:#7cc39a;--accent-ink:#0f241a;--card:#1d2925; + --field:#15201c;--line:#36463f;--danger:#f19aa1;--danger-bg:#3a2226;--ok-bg:#1f3a2b;--shadow:0 30px 80px #0008;color-scheme:dark}} +*{box-sizing:border-box} +html,body{margin:0;min-height:100%} +body{min-height:100dvh;display:grid;grid-template-columns:minmax(0,1.1fr) minmax(0,1fr);color:var(--ink); + font:15px/1.5 Inter,ui-sans-serif,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif; + background:radial-gradient(ellipse at 15% 85%,#3f7a5c 0%,transparent 55%),radial-gradient(ellipse at 85% 10%,#2b5f63 0%,transparent 50%), + linear-gradient(140deg,#0d2a25 0%,#173c39 55%,#24524a 100%)} +.hero{display:flex;flex-direction:column;justify-content:space-between;padding:48px 56px;color:#eaf3ee} +.brand{display:flex;align-items:center;gap:12px;font-weight:600;font-size:16px} +.mark{display:grid;place-items:center;width:44px;height:44px;border-radius:12px;background:#eaf3ee;color:#173c39;font-weight:700;font-size:17px;letter-spacing:-.5px} +.brand small{display:block;font-weight:400;font-size:13px;color:#b8cec3} +.eyebrow{font-size:12px;letter-spacing:.16em;color:#a9c7b8;margin:0 0 14px} +.hero h2{font-size:clamp(34px,4.4vw,56px);line-height:1.05;letter-spacing:-.02em;margin:0;font-weight:650} +.hero p.lede{max-width:30em;color:#c9dbd2;margin:18px 0 0} +.hero footer{font-size:13px;color:#a9c7b8} +main{display:grid;place-items:center;padding:32px} +.card{width:min(410px,100%);background:var(--card);border-radius:20px;padding:36px 34px 30px;box-shadow:var(--shadow)} +.card .mark{display:none} +h1{font-size:24px;line-height:1.2;margin:0 0 6px;letter-spacing:-.01em} +.sub{margin:0 0 24px;color:var(--muted)} +.alert{display:flex;gap:10px;align-items:flex-start;margin:0 0 20px;padding:12px 14px;border-radius:12px;font-size:14px} +.alert::before{flex:none;display:grid;place-items:center;width:20px;height:20px;border-radius:50%;font-weight:700;font-size:12px;margin-top:1px} +.alert.error{background:var(--danger-bg);color:var(--danger)} +.alert.error::before{content:"!";background:var(--danger);color:var(--card)} +.alert.ok{background:var(--ok-bg);color:var(--accent)} +.alert.ok::before{content:"✓";background:var(--accent);color:var(--accent-ink)} +form{display:grid;gap:16px} +label{display:grid;gap:6px;font-size:13px;font-weight:600} +input{width:100%;height:46px;padding:0 14px;border:1px solid var(--line);border-radius:11px;background:var(--field);color:var(--ink); + font:inherit;font-size:15px;transition:border-color .15s,box-shadow .15s} +input:focus{outline:none;border-color:var(--accent);box-shadow:0 0 0 4px color-mix(in srgb,var(--accent) 22%,transparent)} +button{margin-top:6px;height:48px;border:0;border-radius:11px;background:var(--accent);color:var(--accent-ink);font:inherit;font-weight:600; + font-size:15px;cursor:pointer;transition:filter .15s,transform .05s} +button:hover{filter:brightness(1.07)} +button:active{transform:translateY(1px)} +button:focus-visible{outline:3px solid color-mix(in srgb,var(--accent) 45%,transparent);outline-offset:3px} +.note{display:flex;align-items:center;gap:8px;margin:22px 0 0;padding-top:18px;border-top:1px solid var(--line);font-size:12.5px;color:var(--muted)} +.note::before{content:"";flex:none;width:8px;height:8px;border-radius:50%;background:var(--accent)} +@media (max-width:860px){ + body{grid-template-columns:1fr} + .hero{display:none} + main{padding:24px 16px;align-content:center} + .card{padding:30px 22px 24px} + .card .mark{display:grid;margin-bottom:18px} +} +@media (prefers-reduced-motion:reduce){*{transition:none!important}} +`; + +const favicon = 'data:image/svg+xml,' + encodeURIComponent( + '' + + 'm³'); + +export const loginPolicy = `default-src 'none'; style-src 'sha256-${createHash('sha256').update(css).digest('base64')}'; ` + + "img-src data:; form-action 'self'; frame-ancestors 'none'; base-uri 'none'"; + +const escape = (value: string): string => value.replace(/[&<>"']/g, c => `&#${c.charCodeAt(0)};`); + +export interface LoginView { error?: string; notice?: string; username?: string } +export function loginPage({ error, notice, username = '' }: LoginView = {}): string { + const message = error ? `` + : notice ? `

${escape(notice)}

` : ''; + const focusPassword = !!error && !!username; + return ` +Sign in · M3tering Console + + +
+
+ +

Welcome back

+

Sign in to manage your gateways.

+ ${message} +
+ + + +
+

Encrypted connection · Sessions end after 8 hours

+
+
+`; +} diff --git a/tests/services/central-auth.test.ts b/tests/services/central-auth.test.ts index 8227ef0..d72126f 100644 --- a/tests/services/central-auth.test.ts +++ b/tests/services/central-auth.test.ts @@ -60,3 +60,23 @@ test('bad sign-ins use generic errors, never echo secrets, and are rate limited' } expect((await request(app,'/login',{method:'POST',headers:secure,body:'username=operator&password=sensitive-fixture'})).status).toBe(429); }); + +test('the sign-in page is styled under a strict policy whose hash matches its stylesheet',async()=>{ + const page=await request(app,'/login',{headers:secure}); + const policy=String(page.headers['content-security-policy']); + expect(policy).toContain("default-src 'none'");expect(policy).not.toContain('unsafe-inline');expect(policy).not.toMatch(/script-src/); + const style=/ -

Search with Google

Enter a search in the toolbar above. Google results open in a new tab because Google does not allow embedded search pages.

You can also enter an HTTP or HTTPS address to browse here. If a site blocks embedding, use “Open in new tab”.

diff --git a/src/public/css/global.css b/src/public/css/global.css index 9bc3e56..b644452 100644 --- a/src/public/css/global.css +++ b/src/public/css/global.css @@ -91,7 +91,6 @@ small { display:block; font-size:12px; line-height:1.6; color:var(--muted); } .app-art { width:var(--icon-size); height:var(--icon-size); display:inline-flex; align-items:center; justify-content:center; border-radius:var(--icon-radius); background:linear-gradient(140deg,#eeedd4,#bbcda2); color:#35513b; box-shadow:var(--icon-shadow); flex-shrink:0; overflow:hidden; } .app-art svg { width:56%; height:56%; stroke-width:1.6; } [data-app-id="terminal"] .app-art { background:linear-gradient(140deg,#384e48,#172d28); color:#d4eeb3; } -[data-app-id="browser"] .app-art { background:linear-gradient(140deg,#a9e1e5,#497fba); color:#fff; } [data-app-id="paint"] .app-art { background:linear-gradient(140deg,#f9d79d,#d97e7c); color:#713d46; } [data-app-id="settings"] .app-art { background:linear-gradient(140deg,#e6e8e8,#9da9b1); color:#43535d; } .extension-art { background:linear-gradient(140deg,#efdc8e,#cbae43); color:#5d4c22; } @@ -169,9 +168,9 @@ label { display:block; } #xterm-terminal { flex:1; min-height:100px; padding:10px; overflow:hidden; } #terminal-status { color:var(--muted); font-size:12px; padding-top:14px; } #terminal-status p { margin-bottom:0; } -.browser-toolbar { padding:9px 16px; display:flex; align-items:center; gap:10px; background:var(--surface-alt); font-size:12px; } -.browser-toolbar a { margin-left:auto; font-size:11px; } -#browser iframe, #chirpstack iframe { border:0; width:100%; flex:1; min-height:0; background:white; } +.app-toolbar { padding:9px 16px; display:flex; align-items:center; gap:10px; background:var(--surface-alt); font-size:12px; } +.app-toolbar a { margin-left:auto; font-size:11px; } +#chirpstack iframe { border:0; width:100%; flex:1; min-height:0; background:white; } .app-art[data-icon="radio"] { background:linear-gradient(145deg,#259aa0,#145275); color:#fff; } .chirpstack-toolbar { flex-wrap:wrap; gap:8px; } .chirpstack-toolbar small { display:block; font-size:10px; color:var(--muted); } @@ -312,7 +311,7 @@ dialog::backdrop { background:#14232e88; backdrop-filter:blur(3px); }dialog h2 { .settings-layout { flex-direction:column; }.settings-nav { width:100%; padding:8px; flex-direction:row; gap:5px; border-right:0; border-bottom:1px solid var(--border); }.settings-identity,.settings-footnote { display:none; }.settings-nav button { flex:1; justify-content:center; gap:6px; padding:10px 6px; min-height:44px; font-size:11px; }.settings-nav button svg { width:16px; height:16px; }.settings-content { padding:22px 18px; } .theme-grid { gap:12px; }.theme-preview { height:90px; }.theme-card-title { font-size:11px; }.theme-card small { font-size:10px; }.theme-card-copy { padding:8px 4px; }.theme-check { flex-shrink:0; } .setting-row { gap:12px; }.setting-row select { max-width:110px; }.setting-row .nes-btn { padding:8px 10px; }.switch { width:44px; height:28px; }.switch::after { width:20px; height:20px; } - .browser-toolbar { flex-wrap:wrap; gap:6px; font-size:11px; }.input-row { flex-wrap:wrap; }.input-row button { flex:1; }.input-row input { flex-basis:160px; } + .app-toolbar { flex-wrap:wrap; gap:6px; font-size:11px; }.input-row { flex-wrap:wrap; }.input-row button { flex:1; }.input-row input { flex-basis:160px; } .nes-btn,.quiet-button { min-height:44px; }.extension-window .window-content { overflow-wrap:anywhere; }.extension-window .nes-container { padding:14px; } .publisher-stat { padding:16px; }.publisher-stat #streamr-last-publish { font-size:15px; } } @@ -350,8 +349,6 @@ dialog::backdrop { background:#14232e88; backdrop-filter:blur(3px); }dialog h2 { .paint-surface { overflow:auto; flex:1; min-height:0; padding:12px; background:var(--surface-muted); } #paint-canvas { display:block; width:100%; max-width:1200px; height:auto; background:#fff; touch-action:none; cursor:crosshair; } -#browser-search { display:flex; flex:1; gap:8px; min-width:0; } -#browser-query { flex:1; width:100%; min-width:0; } /* Feedback is positioned and clipped by its owning app, never the desktop. */ .app-notification-area { position:absolute; z-index:20; right:12px; bottom:12px; width:min(380px,calc(100% - 24px)); max-height:40%; overflow:auto; pointer-events:none; } @@ -363,7 +360,6 @@ dialog::backdrop { background:#14232e88; backdrop-filter:blur(3px); }dialog h2 { .app-dialog-layer > #mm-dialog { width:min(540px,100%); max-height:100%; } @media(max-width:700px) { .app-dialog-layer { padding:8px; } .app-dialog-layer > dialog.app-dialog { padding:18px; } } @media(max-height:500px) and (min-width:701px) { .app-dialog-layer { top:40px; } } -.browser-controls{flex-wrap:wrap;gap:6px}.browser-controls button,.browser-controls select{min-height:34px;background:var(--surface);color:var(--text);border:1px solid var(--border);border-radius:var(--small-radius);padding:4px 8px}.browser-controls #browser-search{min-width:180px}#browser-status{font:12px var(--font);padding:7px 12px;background:var(--surface-alt);color:var(--muted)}#browser-view{position:relative;flex:1;min-height:0;overflow:hidden;background:var(--surface);touch-action:none}#browser-canvas{display:block;width:100%;height:100%;object-fit:fill}#browser-input{position:absolute;left:0;bottom:0;width:1px;height:1px;opacity:.01;border:0;padding:0;resize:none} /* Custom select arrow: a thin chevron (matching each theme's muted text) inset 14px from the edge, with the same breathing room before it. :not() outranks toolbar rules that reset backgrounds. */ @@ -373,4 +369,3 @@ dialog::backdrop { background:#14232e88; backdrop-filter:blur(3px); }dialog h2 { :root[data-theme="windows"]{--select-arrow:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%2364748b' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E")} select:not([multiple]):not([size]){appearance:none;-webkit-appearance:none;padding-right:36px; background-image:var(--select-arrow);background-position:right 14px center;background-size:10px 6px;background-repeat:no-repeat} -.browser-controls select:not([multiple]):not([size]){padding-right:28px;background-position:right 10px center} diff --git a/src/public/js/browser.js b/src/public/js/browser.js deleted file mode 100644 index aafd0b8..0000000 --- a/src/public/js/browser.js +++ /dev/null @@ -1,53 +0,0 @@ -(() => { - const win=document.getElementById('browser'),view=document.getElementById('browser-view'),canvas=document.getElementById('browser-canvas'),input=document.getElementById('browser-input'); - const query=document.getElementById('browser-query'),status=document.getElementById('browser-status'); - let socket,ready=false,remoteWidth=1024,remoteHeight=700,framePending=false; - const send=value=>{if(socket?.readyState===WebSocket.OPEN)socket.send(JSON.stringify(value));}; - const size=()=>{if(ready)send({type:'resize',width:Math.round(view.clientWidth),height:Math.round(view.clientHeight)});}; - function open(){ - if(socket&&[WebSocket.OPEN,WebSocket.CONNECTING].includes(socket.readyState))return; - status.textContent='Starting isolated Browser…'; - socket=new WebSocket(`${location.protocol==='https:'?'wss:':'ws:'}//${location.host}/browser/live`); - socket.onmessage=event=>{ - const message=JSON.parse(event.data); - if(message.type==='closed'&&ready){ready=false;status.textContent='Browser session ended. Close and reopen to start a new session.';} - if(message.type==='ready'){ready=true;status.textContent='Ready. Search or enter an address. Downloads and uploads are disabled.';size();} - if(message.type==='error'||message.type==='notice')status.textContent=message.message; - if(message.type==='loading')status.textContent=message.value?'Loading…':'Ready'; - if(message.type==='navigation'){ - if(message.url&&message.url!=='about:blank'&&document.activeElement!==query)query.value=message.url; - if(message.title){win.dataset.gatewayBaseTitle=`${message.title} · Browser`;const title=win.dataset.gatewayBaseTitle+(win.dataset.gatewayName?` · ${win.dataset.gatewayName}`:'');win.dataset.windowTitle=title;win.querySelector('.window-title').textContent=title;document.title=`${title} · M3tering Console`;} - } - if(message.type==='frame'&&!framePending){framePending=true;const image=new Image();image.onload=()=>{canvas.width=remoteWidth=image.width;canvas.height=remoteHeight=image.height;canvas.getContext('2d').drawImage(image,0,0);framePending=false;};image.onerror=()=>{framePending=false;};image.src=`data:image/jpeg;base64,${message.data}`;} - if(message.type==='clipboard')navigator.clipboard.writeText(message.text).then(()=>{status.textContent='Selected text copied.';}).catch(()=>{status.textContent='Clipboard permission unavailable.';}); - }; - socket.onclose=()=>{ready=false;if(status.textContent==='Starting isolated Browser…')status.textContent='Browser engine not available. Install the optional Chromium browser image.';}; - socket.onerror=()=>{status.textContent='Browser connection unavailable. Close and reopen the application to retry.';}; - } - document.getElementById('browser-search').onsubmit=event=>{event.preventDefault();try{if(/^[a-z][a-z0-9+.-]*:\/\//i.test(query.value)){const url=new URL(query.value);if(!['http:','https:'].includes(url.protocol)||url.username||url.password)throw new Error('Use an HTTP or HTTPS address without credentials.');}}catch(error){showNotification(error.message,'error',win);return;}if(ready)send({type:'navigate',url:query.value});else status.textContent='Browser engine not available yet.';}; - for(const [id,value] of [['back',{type:'history',direction:-1}],['forward',{type:'history',direction:1}],['reload',{type:'reload'}],['stop',{type:'stop'}],['home',{type:'navigate',url:'https://www.google.com/'}],['copy',{type:'copy'}]])document.getElementById(`browser-${id}`).onclick=()=>send(value); - document.getElementById('browser-zoom').onchange=event=>send({type:'zoom',factor:Number(event.target.value)}); - const point=event=>{const rect=canvas.getBoundingClientRect();return {x:(event.clientX-rect.left)*remoteWidth/rect.width,y:(event.clientY-rect.top)*remoteHeight/rect.height};}; - const button=event=>['left','middle','right'][event.button]||'none'; - let touch; - view.onpointerdown=event=>{if(!ready)return;event.preventDefault();input.focus({preventScroll:true});view.setPointerCapture(event.pointerId);if(event.pointerType==='touch'){touch={x:event.clientX,y:event.clientY,start:point(event),moved:false};return;}send({type:'pointer',event:'mousePressed',...point(event),button:button(event),clicks:event.detail||1});}; - view.onpointermove=event=>{if(touch){const dx=touch.x-event.clientX,dy=touch.y-event.clientY;if(touch.moved||Math.abs(dx)+Math.abs(dy)>6){touch.moved=true;send({type:'pointer',event:'mouseWheel',...point(event),dx,dy});touch.x=event.clientX;touch.y=event.clientY;}return;}if(ready)send({type:'pointer',event:'mouseMoved',...point(event),button:event.buttons?'left':'none'});}; - view.onpointercancel=()=>{touch=undefined;}; - view.onpointerup=event=>{if(touch){if(!touch.moved){send({type:'pointer',event:'mousePressed',...touch.start,button:'left',clicks:1});send({type:'pointer',event:'mouseReleased',...touch.start,button:'left',clicks:1});}touch=undefined;return;}if(ready)send({type:'pointer',event:'mouseReleased',...point(event),button:button(event),clicks:event.detail||1});}; - view.addEventListener('wheel',event=>{event.preventDefault();send({type:'pointer',event:'mouseWheel',...point(event),dx:event.deltaX,dy:event.deltaY});},{passive:false}); - view.oncontextmenu=event=>event.preventDefault(); - input.addEventListener('input',event=>{if(event.isComposing)return;if(input.value)send({type:'text',text:input.value});input.value='';}); - input.addEventListener('compositionend',()=>{if(input.value)send({type:'text',text:input.value});input.value='';}); - input.addEventListener('paste',event=>{event.preventDefault();send({type:'text',text:event.clipboardData.getData('text').slice(0,8192)});}); - for(const name of ['keydown','keyup'])input.addEventListener(name,event=>{ - if(event.isComposing)return; - if((event.ctrlKey||event.metaKey)&&event.key.toLowerCase()==='c'){event.preventDefault();if(name==='keydown')send({type:'copy'});return;} - if((event.ctrlKey||event.metaKey)&&event.key.toLowerCase()==='v')return; - if(event.key.length===1&&!event.ctrlKey&&!event.metaKey&&!event.altKey)return; - event.preventDefault();send({type:'key',event:name==='keydown'?'keyDown':'keyUp',key:event.key,code:event.code,keyCode:event.keyCode,modifiers:(event.altKey?1:0)|(event.ctrlKey?2:0)|(event.metaKey?4:0)|(event.shiftKey?8:0)}); - }); - new ResizeObserver(size).observe(view); - window.addEventListener('desktop:open',event=>{if(event.detail.id==='browser')open();}); - window.addEventListener('desktop:close',event=>{if(event.detail.id==='browser'){socket?.close();socket=undefined;ready=false;}}); - window.addEventListener('pagehide',()=>socket?.close()); -})(); diff --git a/src/public/js/desktop.js b/src/public/js/desktop.js index b914c98..2ac5817 100644 --- a/src/public/js/desktop.js +++ b/src/public/js/desktop.js @@ -11,7 +11,6 @@ radio: '', meter: '', terminal: '', - browser: '', paint: '', settings: '', appearance: '', diff --git a/src/public/js/gateways.js b/src/public/js/gateways.js index badce46..138200b 100644 --- a/src/public/js/gateways.js +++ b/src/public/js/gateways.js @@ -1,7 +1,7 @@ (() => { const $=id=>document.getElementById(id); const switcher=$('gateway-switcher'),menu=$('gateway-menu'),options=$('gateway-options'),dialog=$('gateway-dialog'),list=$('gateway-list'),confirmDialog=$('gateway-confirm'); - const NETWORK_APPS=['terminal','chirpstack','browser'],TOKEN_LIFETIME=10*60000,CENTRAL_NAME='Central server'; + const NETWORK_APPS=['terminal','chirpstack'],TOKEN_LIFETIME=10*60000,CENTRAL_NAME='Central server'; let gateways=[],selected=null,busy=false,again=false,editing=null,focusRename=false,tokenTimer=null; const openUpdates=new Set(); @@ -85,7 +85,7 @@ } else card.replaceChildren(el('div',{class:'gw-current-head','data-state':'central'}, el('span',{class:'gw-dot','aria-hidden':'true'}), el('div',{class:'gw-title'},el('strong',{class:'gw-name',text:CENTRAL_NAME}),el('small',{text:'No gateway selected'}))), - el('p',{class:'gw-current-empty',text:'Terminal, Browser and ChirpStack open on the central server. Choose a gateway to work on it instead.'})); + el('p',{class:'gw-current-empty',text:'Terminal and ChirpStack open on the central server. Choose a gateway to work on it instead.'})); const choices=gateways.filter(g=>!g.revoked); // A refresh rebuilds the list; keep keyboard focus on the same option. const focused=options.contains(document.activeElement)?document.activeElement.dataset.gateway:null; diff --git a/src/services/server.ts b/src/services/server.ts index 2aa0d87..dfe1c77 100644 --- a/src/services/server.ts +++ b/src/services/server.ts @@ -7,8 +7,7 @@ import { createLogger } from '../utils/logger'; import { getUIComponents, invokeUIAction } from '../lib/utils'; import { central, config } from '../central/runtime'; import { centralChirpstackUrl } from '../central/config'; -import { attachBrowser } from '../browser/service'; -import { protectBrowserSockets } from './socket-router'; +import { protectSessionSockets } from './socket-router'; const logger = createLogger('[server]'); export const app: Express = express(); @@ -83,20 +82,7 @@ app.post('/api/actions/:moduleId/:actionId', async (req: Request, res: Response, }); export const server = http.createServer(app); -if(central){protectBrowserSockets(server,central.auth.authorize);central.hub.mount(app,server);} - -const browserRuntime=central; -const browserActors=new WeakMap(); -attachBrowser(server,{role:config.role,gateway:request=>browserRuntime?.hub.selected(request)||null, - owner:browserRuntime?request=>browserRuntime.auth.session(request)?.id||'unauthorized':undefined, - tunnel:browserRuntime?(gateway,host,port)=>browserRuntime.hub.open(gateway,'browser',host,port):undefined, - audit:browserRuntime?(request,event,gateway)=>{ - if(event==='browser-start'){const actor=browserRuntime.auth.session(request)?.id;if(actor)browserActors.set(request,actor);} - const actor=browserActors.get(request);if(actor)browserRuntime.identities.audit(actor,event,gateway); - if(event==='browser-end')browserActors.delete(request); - }:undefined, - testUnsandboxed:process.env.NODE_ENV==='test'&&process.env.BROWSER_UNSANDBOXED_TEST==='true', -}); +if(central){protectSessionSockets(server,central.auth.authorize);central.hub.mount(app,server);} export function startServer() { server.once('error', (error: NodeJS.ErrnoException) => { diff --git a/src/services/socket-router.ts b/src/services/socket-router.ts index 73e3f26..4288025 100644 --- a/src/services/socket-router.ts +++ b/src/services/socket-router.ts @@ -3,8 +3,9 @@ import { Socket } from 'net'; import WebSocket from 'ws'; import { isSameOrigin } from './auth'; const servers = new WeakMap boolean; machine: boolean }>>(); -const browserGuards = new WeakMap boolean>(); -export function protectBrowserSockets(server:http.Server,authorize:(req:http.IncomingMessage)=>boolean):void {browserGuards.set(server,authorize);} +/** Central: every non-machine socket (such as Terminal) needs a signed-in session. */ +const sessionGuards = new WeakMap boolean>(); +export function protectSessionSockets(server:http.Server,authorize:(req:http.IncomingMessage)=>boolean):void {sessionGuards.set(server,authorize);} /** Shared origin-checked upgrade boundary for terminal and application streams. */ export function registerSocket(server: http.Server, path: string, wss: WebSocket.Server, authorize?: (req: http.IncomingMessage) => boolean, machine = false): void { @@ -14,7 +15,7 @@ export function registerSocket(server: http.Server, path: string, wss: WebSocket routes = new Map(); servers.set(server, routes); server.on('upgrade', (request, socket, head) => { const target = routes!.get(request.url || ''); - const guard=browserGuards.get(server); + const guard=sessionGuards.get(server); if (!target || (!target.machine && (!isSameOrigin(request, true) || (guard && !guard(request)))) || (target.authorize && !target.authorize(request))) { socket.end('HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n'); return; } diff --git a/src/views/index.hbs b/src/views/index.hbs index 3fafa02..554f341 100644 --- a/src/views/index.hbs +++ b/src/views/index.hbs @@ -46,7 +46,6 @@
- {{#each icons}} @@ -66,13 +65,8 @@

Enter your credentials to start a terminal session.

-