diff --git a/.env.example b/.env.example index ca3dafc..3d67fc2 100644 --- a/.env.example +++ b/.env.example @@ -32,6 +32,7 @@ STREAMR_STREAM_ID="0x567853282663b601bfdb9203819b1fbb3fe18926/m3tering/test" STREAMR_CRONSCHEDULE="0 * * * *" # Every hour # Optional: Prover Node (disabled publishing to prover if not set) +# Set it on managed gateways too: they send readings they decide while central is away. # PREFERRED_PROVER_NODE="https://prover.m3ter.ing" # PROVER_CRONSCHEDULE="0 0 * * *" # Every 24 hours @@ -58,6 +59,10 @@ STREAMR_CRONSCHEDULE="0 * * * *" # Every hour # GATEWAY_FALLBACK_PORT=3011 (not 3001: ChirpStack's Basic Station bridge uses it) # Gateway-owned tunnel policy; central cannot override these targets. # AGENT_SSH_PORT=22 +# How long a Terminal shell waits for its page to reconnect (phones drop background pages), in ms. +# TERMINAL_RESUME_MS=600000 +# Containers restart themselves if their DNS settings stay empty this long after a reboot (ms). +# CONSOLE_DNS_RESTART_MS=180000 # Used only by compose.central.yaml's TLS proxy. # CENTRAL_DOMAIN=console.example.invalid # Central with no gateway selected: ChirpStack opens this dashboard (an origin, no path), diff --git a/auto-compose.sh b/auto-compose.sh index 28040aa..a55ce0c 100755 --- a/auto-compose.sh +++ b/auto-compose.sh @@ -253,7 +253,28 @@ if [[ "$role" == central ]]; then "${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.' +# Plain `docker compose logs -f`, `ps` and so on in this folder follow the role that is now running: +# Compose reads COMPOSE_FILE and COMPOSE_PROJECT_NAME from .env. Only these two lines are touched; +# rewriting in place keeps the file's owner-only permissions. +set_env_value() { + local name=$1 value=$2 tmp + [[ "$(env_value "$name")" == "$value" ]] && return 0 + tmp=$(mktemp) || return 1 + if grep -q "^[[:space:]]*$name[[:space:]]*=" .env; then + sed "s|^[[:space:]]*$name[[:space:]]*=.*|$name=$value|" .env > "$tmp" + else + { cat .env; [[ -z "$(tail -c 1 .env)" ]] || echo + [[ "$name" != COMPOSE_FILE ]] || echo "# Set by ./auto-compose.sh: plain 'docker compose logs -f' follows the role running here." + echo "$name=$value"; } > "$tmp" + fi + cat "$tmp" > .env; rm -f "$tmp" +} +if set_env_value COMPOSE_FILE "$compose_file" && set_env_value COMPOSE_PROJECT_NAME "$project"; then + echo "Logs: run 'docker compose logs -f' here (it follows $compose_file)." +else + echo "Note: could not record the role in .env; use: docker compose -f $compose_file logs -f" >&2 +fi +echo 'Console updated. Database, extension volumes and your .env settings 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 diff --git a/docs/M3tering-Console-Manual.pdf b/docs/M3tering-Console-Manual.pdf new file mode 100644 index 0000000..d17fa29 Binary files /dev/null and b/docs/M3tering-Console-Manual.pdf differ diff --git a/docs/deployment-guide.md b/docs/deployment-guide.md index 1f54523..6a1fb20 100644 --- a/docs/deployment-guide.md +++ b/docs/deployment-guide.md @@ -164,6 +164,38 @@ 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. +## The prover + +Readings are sent to the prover (`PREFERRED_PROVER_NODE`) in two ways: + +- **As each reading arrives:** once per reading. On central this covers every gateway's readings, + including those handed over after a gateway decided them on its own. +- **On `PROVER_CRONSCHEDULE`:** every stored reading whose nonce is not yet confirmed on-chain is sent + again, on every run, until the chain confirms it. Each run first checks the chain and prunes the + readings that are confirmed. + +While central is unreachable, a managed gateway sends the readings it decides itself to the prover, if +its own `.env` sets `PREFERRED_PROVER_NODE`. Central's scheduled runs resend them later anyway. A gateway +never publishes to Streamr or Arweave; central does that. + +Logs: `Sending N transactions to prover at …`, then `Prover response (text): …`. + +## The Streamr app + +Every publish, the hourly `STREAMR_CRONSCHEDULE` run or **Publish Now**, sends all stored readings +whose nonce is not yet confirmed on-chain. They are sent again on every run until the chain confirms +them; then they are pruned. Only Arweave uploads happen once per reading. + +| Where | What it shows | Publish Now | +|---|---|---| +| Standalone | Readings not yet confirmed on-chain | Publishes them to every configured stream | +| Central, **Central server** selected | Readings not yet confirmed on-chain, from every gateway | Publishes them, for every gateway | +| Central, a gateway selected | The same, for that gateway's meters only | Same as above: central publishes for every gateway together | +| A managed gateway's own Console | How many readings are kept locally; central publishes them | Hidden | + +The window refreshes when you open it, after **Publish Now**, when you switch gateway and every 30 +seconds while open. **Last publish** covers both the hourly job and **Publish Now**. + ## Managing gateways on central Open **Gateways** from the top bar. Each gateway is a card. @@ -219,7 +251,8 @@ 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. +- never touches the database or volumes, and in `.env` only updates its own `COMPOSE_FILE` and + `COMPOSE_PROJECT_NAME` lines (so plain `docker compose logs -f` follows the role). To run a specific build, pass its tag or digest: @@ -279,7 +312,7 @@ a gateway) and Terminal and ChirpStack work on the central machine instead of a | **Terminal** | SSH into the central server itself. Sign in with a Linux user and password on that server. | | **ChirpStack** | Opens the dashboard set in `CENTRAL_CHIRPSTACK_URL`. | -Choosing a gateway switches both back to that gateway. Open windows close when +Choosing a gateway switches both back to that gateway. Each browser tab keeps its own choice, so you can work on one gateway in one tab and another gateway (or central) in a second tab; a new tab starts on **Central server**. Open windows close when you switch, so you never type into the wrong machine. Settings in central's `.env` (run `./auto-compose.sh` after changing them): @@ -289,6 +322,12 @@ CENTRAL_CHIRPSTACK_URL=https://chirpstack.example.com # address only, no path CENTRAL_SSH_PORT=22 # only if sshd uses another port ``` +**Terminal sessions survive switching apps.** If the browser drops the connection (a phone putting +the page in the background, switching tabs, a network blip), the shell keeps running for 10 minutes +(`TERMINAL_RESUME_MS`). When you come back, Terminal reconnects to the same session by itself and shows +what you missed, even if the browser reloaded the page. **Disconnect** or closing the Terminal window +ends the session at once. On central, only the same signed-in user on the same target can resume it. + **Terminal needs password login if your server doesn't already use it.** The Terminal signs in with a username and password, and many cloud servers allow SSH keys only. To use it, the user needs a password (`sudo passwd `) and sshd must accept passwords for it. For example, add this @@ -344,15 +383,37 @@ are listed in [`.env.example`](../.env.example) and ## Troubleshooting -Useful commands, using the compose file for the machine's role -(`compose.yaml`, `compose.central.yaml` or `compose.gateway.yaml`): +**Logs on any machine.** In the Console folder, plain Compose commands follow whatever role is +running there: central (Console and HTTPS proxy), a managed gateway (Console and agent) or standalone. + +```sh +docker compose logs -f # every service of this role +docker compose logs -f agent # one service +docker compose ps +``` + +This works because `./auto-compose.sh` keeps two lines in `.env` up to date after each successful +deploy: `COMPOSE_FILE` (the role's compose file) and `COMPOSE_PROJECT_NAME`. It doesn't change +anything else in the file. The explicit form still works and is what you need for the gateway +updater, whose compose file is only added by the script: ```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 +docker compose -f compose.gateway.yaml -f compose.updater.yaml logs -f updater ``` +**Following a meter report.** Each step logs one line with the reading and the reply: + +| Where | Lines | +|---|---| +| Gateway `logs -f agent` | `Listening for meter reports on …`, `Report received from ChirpStack: devEui=… nonce=… energy=…kWh …`, `Report … is now …`, `Reply enqueued to ChirpStack for … (queue id …): state=ON …` | +| Central `logs -f console` | `Report received: …`, `Reading recorded: …`, `Reply decided (the gateway agent enqueues it): state=…` or `Report rejected: ` | +| Gateway `logs -f console` | The same as central, but only while central is unreachable and the gateway decides locally | +| Standalone `logs -f console` | `Report received: …`, `Reading recorded: …`, `Reply enqueued: state=…` | + +Messages on ports other than 2 (telemetry, network-control frames) aren't billed and are dropped without a log line. +The full signature and meter key are shortened in the logs; keys and tokens never appear. + | Symptom | Cause and fix | |---|---| | `Replace the placeholder values in .env: …` | Template values are still in `.env`. Fill in the named settings. | @@ -365,10 +426,53 @@ docker compose -f compose.gateway.yaml logs -f agent | Central Terminal: `SSH connection failed` | With **Central server** selected, the server's sshd refused the password. Allow password login for that user from `127.0.0.1` (see [Working on the central server itself](#working-on-the-central-server-itself)). | | Central ChirpStack: `No ChirpStack dashboard set` | Set `CENTRAL_CHIRPSTACK_URL` in `.env` and rerun `./auto-compose.sh`, or select a gateway. | | Gateway: `Port 3011 is already used by …` | Another program uses the gateway Console's local fallback port. Add `GATEWAY_FALLBACK_PORT=3101` (any free port) to `.env` and rerun. Older setups used 3001, which clashes with ChirpStack's Basic Station bridge. | +| Gateway shows **Offline** after a reboot although the gateway has internet; agent log says `Cannot reach central (EAI_AGAIN)` | Docker started the containers before the network (often Wi-Fi) had DNS, so they got empty DNS settings. They now restart themselves after 3 minutes without DNS and connect. To fix it at once: `docker compose restart console agent`. | | Gateway: agent online once, then **Offline** for good | Usually the gateway Console couldn't start, often because of a port clash. Check `logs console` and rerun `./auto-compose.sh`; it now says what's wrong. | | Gateway: `This gateway is not enrolled yet` | Add the `AGENT_ENROLLMENT_TOKEN` line from central. | +| Every reply is `state=OFF`; logs show `Failed to resolve ENS name: base-sepolia.verifier.unruggable.eth` | Known issue, see [Every meter is switched OFF](#known-issue-every-meter-is-switched-off). | | 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. | + +## Known issue: every meter is switched OFF + +**Status:** open, not fixed in this repository (checked 2026-11-18). + +**Symptom.** Every state reply is OFF (`hex=000000000000000000`), whoever decides it. The deciding +Console logs: + +```text +[error] [utils] Extension hook isOnStateCompute failed: Failed to resolve ENS name: base-sepolia.verifier.unruggable.eth +[error] [Device-…] Could not determine meter power state, so the reply is OFF: 1 extension(s) failed in isOnStateCompute +``` + +**Cause.** The ON/OFF rule (`core/is_on`) switches a meter ON only when its revenue covers what it +owes. Revenue is read through the verifiers listed in the CCIP revenue reader contract +(`CCIP_REVENUE_READER_ADDRESS`, default `0xD648cdF47e9534B2FCfb18C1E94CA9AAff07BA0E` on Sepolia). +That contract lists exactly one verifier, `base-sepolia.verifier.unruggable.eth` (target +`0xC08E39789A93230069c956e76B0c10cbC83D903e`). That ENS name has no address record, neither +on Sepolia (no resolver) nor on Ethereum mainnet (a resolver, but no address for any chain), so +the revenue lookup fails and the safe default, OFF, is used. + +This is not a Console bug and not specific to one machine: central and gateways give the same +result. Check it from any Console with: + +```sh +docker compose -f compose.central.yaml exec console node -e "const {JsonRpcProvider}=require('ethers');new JsonRpcProvider(process.env.MAINNET_RPC).resolveName('base-sepolia.verifier.unruggable.eth').then(a=>console.log('resolved',a))" +``` + +`resolved null` means the issue is still present. + +**Possible fixes (not done yet).** + +- Have the revenue reader contract's owner update its verifier entry to a name (or verifier) that + currently resolves. Unruggable's [verifier deployments](https://gateway-docs.unruggable.com/verifiers/deployments) + page says its verifiers are deployed on Ethereum mainnet and Sepolia. +- Point `CCIP_REVENUE_READER_ADDRESS` at a revenue reader whose verifiers resolve. +- Add a Console setting that maps a verifier name to a known verifier address, so a broken ENS + record can be bypassed. + +Until then, meters stay OFF. Billing still records every reading; only the state reply is affected. + diff --git a/src/agent/index.ts b/src/agent/index.ts index ad6ffe6..039baeb 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -15,6 +15,9 @@ import { mqttConnection } from '../lib/mqtt-config'; import { applicationId } from '../lib/chirpstack-config'; import { managedQueue } from '../services/grpc'; import { createLogger } from '../utils/logger'; +import { watchResolver } from '../utils/dns-watch'; +import { describeReport, describeReply, errorText } from '../utils/report-log'; +import type { ChirpstackQueue } from '../central/coordinator'; import { RateLimit } from '../central/auth'; import { UpdaterClient } from './updater-client'; import { TunnelMux, gatewayTarget } from '../central/tunnels'; @@ -41,15 +44,27 @@ export async function runAgent():Promise<()=>Promise> { const updater=new UpdaterClient(process.env.UPDATER_SOCKET||path.join(path.dirname(config.credentialPath),'updater.sock'),process.env.UPDATER_SECRET_PATH||path.join(path.dirname(config.credentialPath),'updater-secret')); let updatesEnabled=false;const updateStatuses=new Map(); const stateDirectory=path.dirname(config.credentialPath); - const journal=new OperationJournal(path.join(stateDirectory,'operations.db'),config.queueLimit,event=>logger.info('Operation status',event)); + const lastStates=new Map(); + const journal=new OperationJournal(path.join(stateDirectory,'operations.db'),config.queueLimit,event=>{ + // The journal can record the same state twice (for example the decision); log each change once. + if(lastStates.get(event.id)===event.state)return; + lastStates.set(event.id,event.state);if(lastStates.size>1000)lastStates.delete(lastStates.keys().next().value!); + const by=event.owner==='central'?' (decided by central)':event.owner==='local'?' (decided by this gateway: central unreachable)':''; + logger.info(`Report ${event.id} is now ${event.state}${by}`); + }); const local=new LocalDecider(gatewayFallbackPort(),readOwnerSecret(config.fallbackSecretPath),config.decisionTimeoutMs); let socket:WebSocket|undefined,stopped=false,reconnect:ReturnType|undefined; let connectedAt=0,attempt=0,mqttState:Health['mqtt']='connecting',lastError:Health['lastError']='none'; const central=new SocketDecider(()=>socket,config.decisionTimeoutMs); const replication=new Replication(journal,central,local); const healthy=()=>socket?.readyState===WebSocket.OPEN&&Date.now()-connectedAt>=config.recoveryHealthyMs; - const coordinator=new UplinkCoordinator(journal,replication.guarded('central'),replication.guarded('local'),managedQueue, - {centralHealthy:healthy,timeoutMs:config.decisionTimeoutMs,reconcileSnapshot:(owner,outcome)=>replication.observe(owner,outcome)}); + // Log every reply the agent puts in ChirpStack's queue, and every failed attempt. + const queue:ChirpstackQueue={list:devEui=>managedQueue.list(devEui),enqueue:async decision=>{ + try{const id=await managedQueue.enqueue(decision);logger.info(`Reply enqueued to ChirpStack for ${decision.devEui} (queue id ${id}): ${describeReply(decision.bytes)}`);return id;} + catch(error){logger.warn(`Reply for ${decision.devEui} may not have been enqueued (${errorText(error)}); it is held, never resent: ${describeReply(decision.bytes)}`);throw error;} + }}; + const coordinator=new UplinkCoordinator(journal,replication.guarded('central'),replication.guarded('local'),queue, + {centralHealthy:healthy,timeoutMs:config.decisionTimeoutMs,reconcileSnapshot:(owner,outcome)=>replication.observe(owner,outcome),log:message=>logger.warn(message)}); const sendHealth=()=>{ if(socket?.readyState!==WebSocket.OPEN)return; const counts=journal.counts(gateway); @@ -60,6 +75,7 @@ export async function runAgent():Promise<()=>Promise> { socket.send(JSON.stringify({v:1,type:'health',health})); if(socket.bufferedAmount{ if(stopped)return; const ws=socket=new WebSocket(new URL('/agent',config.centralUrl),{headers:{Authorization:`Bearer ${currentCredential}`},maxPayload:MAX_MESSAGE_BYTES, @@ -67,6 +83,7 @@ export async function runAgent():Promise<()=>Promise> { const inputLimit=new RateLimit(512,1000,1); const tunnels=new TunnelMux(ws,request=>gatewayTarget(request)); ws.on('open',()=>{connectedAt=Date.now();attempt=0;lastError='none'; + logger.info(failing?`Connected to central again (${config.centralUrl}).`:`Connected to central (${config.centralUrl}).`);failing=null; if(process.env.CONSOLE_DEPLOY_IMAGE)writeOwnerSecret(path.join(stateDirectory,'reconnected.json'),JSON.stringify({image:process.env.CONSOLE_DEPLOY_IMAGE,time:Date.now()})); updateStatuses.clear();sendHealth();}); ws.on('message',data=>{ @@ -96,7 +113,11 @@ export async function runAgent():Promise<()=>Promise> { } else if(message.type!=='pong')ws.close(1008,'Unsupported operation for this agent version'); }catch{lastError='central';ws.close(1008,'Invalid central message');} }); - ws.on('error',()=>{lastError='central';}); + ws.on('error',error=>{lastError='central'; + // Say once that central can't be reached, and why; stay quiet while retrying. + const reason=(error as NodeJS.ErrnoException).code||error.message; + if(failing!==reason){failing=reason;logger.warn(`Cannot reach central (${reason}); retrying.${['EAI_AGAIN','ENOTFOUND'].includes(reason)?' Its name does not resolve from this container.':''}`);} + }); ws.on('close',()=>{ central.disconnect();connectedAt=0;lastError='central'; if(!stopped){const delay=Math.min(60000,1000*2**Math.min(attempt++,6))*(.75+Math.random()*.5);reconnect=setTimeout(connectCentral,delay);} @@ -108,7 +129,7 @@ export async function runAgent():Promise<()=>Promise> { const application=applicationId(),topic=`application/${application}/device/+/event/up`; mqtt.on('connect',()=>{mqtt.subscribe(topic,{qos:1},(error,granted)=>{ if(error||granted?.some(grant=>grant.qos===128)){mqttState='disconnected';lastError='mqtt';mqtt.stream.destroy();} - else mqttState='connected';sendHealth(); + else {mqttState='connected';logger.info(`Listening for meter reports on ${topic}`);}sendHealth(); });}); mqtt.on('reconnect',()=>{mqttState='connecting';});mqtt.on('close',()=>{mqttState='disconnected';}); mqtt.on('error',()=>{lastError='mqtt';logger.warn('MQTT connection unavailable; reconnecting.');}); @@ -119,8 +140,13 @@ export async function runAgent():Promise<()=>Promise> { const parsed:unknown=JSON.parse(packet.payload.toString()); if(typeof parsed!=='object'||parsed===null||Array.isArray(parsed)){done();return;} const event=parsed as UplinkEvent; - if(event.fPort!==2||typeof event.deviceInfo?.devEui!=='string'||typeof event.deviceInfo?.applicationId!=='string'||event.deviceInfo.devEui.toLowerCase()!==match[2].toLowerCase()||event.deviceInfo.applicationId.toLowerCase()!==application){done();return;} - try{eventIdentity(event);}catch{logger.warn('Invalid billing event ignored.');done();return;} + // Only fPort 2 carries billing reports. MQTT can't filter by port (it isn't in the topic), + // so other ports (telemetry, network-control frames) are dropped here, silently. + if(event.fPort!==2){done();return;} + if(typeof event.deviceInfo?.devEui!=='string'||typeof event.deviceInfo?.applicationId!=='string'||event.deviceInfo.devEui.toLowerCase()!==match[2].toLowerCase()||event.deviceInfo.applicationId.toLowerCase()!==application){ + logger.warn(`Report ignored: its device or application does not match its topic: ${describeReport(event)}`);done();return;} + try{eventIdentity(event);}catch{logger.warn(`Invalid billing event ignored: ${describeReport(event)}`);done();return;} + logger.info(`Report received from ChirpStack: ${describeReport(event)}`); // receive persists synchronously before returning its processing promise. const processing=coordinator.receive(gateway,event); done();void processing.then(sendHealth).catch(()=>{lastError='storage';logger.error('Managed operation unavailable; inspect gateway status.');}); @@ -141,7 +167,12 @@ export async function runAgent():Promise<()=>Promise> { try{await replication.flush('local');}catch{}try{await replication.flush('central');}catch{}await coordinator.drain();})() .catch(()=>{lastError='storage';}).finally(()=>{draining=undefined;}); },5000);timer.unref();connectCentral(); - return async()=>{stopped=true;clearTimeout(reconnect);clearInterval(timer);central.disconnect();socket?.terminate();await mqtt.endAsync(false);await coordinator.stop();await draining;journal.close();}; + const stop=async()=>{stopped=true;stopResolverWatch();clearTimeout(reconnect);clearInterval(timer);central.disconnect();socket?.terminate();await mqtt.endAsync(false);await coordinator.stop();await draining;journal.close();}; + const stopResolverWatch=watchResolver(()=>{ + logger.error('This container has no DNS server (Docker copied the host\'s DNS settings before its network was up). Restarting so Docker provides fresh ones.'); + void stop().finally(()=>process.exit(1)); + }); + return stop; } if(require.main===module){ void runAgent().then(stop=>{for(const signal of ['SIGINT','SIGTERM'] as const)process.once(signal,()=>{void stop().finally(()=>process.exit(0));});}) diff --git a/src/central/billing.ts b/src/central/billing.ts index 6b29156..6a6508d 100644 --- a/src/central/billing.ts +++ b/src/central/billing.ts @@ -5,6 +5,7 @@ import { managedBilling } from './billing-context'; import type { UplinkEvent } from './protocol'; import { verifyPayloadSignature, distributeManaged } from '../lib/utils'; import { meterIdFromPublicKeyContractCall, meterLatestNonceContractCall } from '../utils/contracts'; +import { createLogger } from '../utils/logger'; export function billingSnapshot(event:UplinkEvent):BillingSnapshot { return readBillingSnapshot(event.deviceInfo.devEui); @@ -14,7 +15,13 @@ export function createBillingProcessor(filename:string):DurableProcessor { const context={id:event.deduplicationId!,distribution:[]}; await managedBilling.run(context,()=>handleMessage(Buffer.from(JSON.stringify(event)),sink)); void distributeManaged(context.distribution).catch(()=>{}); - },billingSnapshot,async snapshot=>{ + },billingSnapshot,importBillingSnapshot); +} + +const logger=createLogger('[central]'); +/** Accept another decider's billing state (a gateway's fallback decisions) after + * checking it against the meter's signatures, its token and the chain. */ +export async function importBillingSnapshot(snapshot:BillingSnapshot):Promise { const meter=snapshot.meter;if(!meter)return; const known=getMeterByPublicKey(meter.publicKey); const token=known?.tokenId ?? await meterIdFromPublicKeyContractCall(meter.publicKey); @@ -22,15 +29,25 @@ export function createBillingProcessor(filename:string):DurableProcessor { const chainNonce=await meterLatestNonceContractCall(token); if(!Number.isSafeInteger(chainNonce)||chainNonce<0)throw new Error('Cannot verify reconciliation against the chain.'); let supportedNonce=Math.max(known?.latestNonce??-1,chainNonce); - for(const record of [...snapshot.transactions].sort((a,b)=>a.nonce-b.nonce)){ + const records=[...snapshot.transactions].sort((a,b)=>a.nonce-b.nonce); + // First contact: central has never seen this meter, for example a gateway that ran + // standalone before joining central. Its history is not here, so start from the oldest + // meter-signed record (still checked below). Once central knows the meter, gaps are refused. + if(!known&&records.length&&records[0].nonce>supportedNonce+1){ + logger.warn(`Meter ${token} is new to central: starting its history at nonce ${records[0].nonce} (chain has ${chainNonce}); earlier readings stay with the gateway that billed them.`); + supportedNonce=records[0].nonce-1; + } + for(const record of records){ const bytes=Buffer.from(record.raw,'hex'); if(record.identifier!==token || record.nonce!==bytes.readUInt32BE(0) || bytes.length<74 || !verifyPayloadSignature(bytes,Buffer.from(meter.publicKey.replace(/^0x/,''),'hex')))throw new Error('Invalid signed reconciliation record.'); - if(record.nonce>supportedNonce+1)throw new Error('Reconciliation has a gap before this nonce.'); + if(record.nonce>supportedNonce+1)throw new Error(`Reconciliation has a gap before nonce ${record.nonce} (central can confirm up to ${supportedNonce}).`); supportedNonce=Math.max(supportedNonce,record.nonce); } - if(meter.latestNonce>supportedNonce)throw new Error('Reconciliation nonce is not supported by signed records or the chain.'); + if(meter.latestNonce>supportedNonce)throw new Error(`Reconciliation nonce ${meter.latestNonce} is not supported by signed records or the chain (up to ${supportedNonce}).`); mergeBillingSnapshot(snapshot); + // Only central publishes what another decider handed over. (Older gateways may list readings they + // already sent to the prover; that is ignored: the prover is resent until the chain confirms.) + if(process.env.CONSOLE_ROLE!=='central')return; void distributeManaged(snapshot.transactions).catch(()=>{}); - }); } diff --git a/src/central/coordinator.ts b/src/central/coordinator.ts index cfd7ff4..e14cd33 100644 --- a/src/central/coordinator.ts +++ b/src/central/coordinator.ts @@ -12,14 +12,18 @@ export interface CoordinatorOptions { centralHealthy:()=>boolean; timeoutMs:number; reconcileSnapshot?:(owner:Owner,outcome:Outcome)=>Promise; + /** Explains why a report is not decided by central. */ + log?:(message:string)=>void; } function deadline(promise:Promise,ms:number):Promise { return new Promise((resolve,reject)=>{ const timeout=setTimeout(()=>reject(new Error('Decision deadline elapsed.')),ms); - promise.then(value=>{clearTimeout(timeout);resolve(value);},()=>{clearTimeout(timeout);reject(new Error('Decider unavailable.'));}); + promise.then(value=>{clearTimeout(timeout);resolve(value);},error=>{clearTimeout(timeout);reject(new Error(`Decider unavailable: ${error instanceof Error?error.message:String(error)}`));}); }); } const terminal = (state:string) => ['enqueued','queue-observed','superseded','rejected'].includes(state); +/** The reply went out (or is in ChirpStack's queue) and the outcome was replayed to the other side. */ +const settled = (state:string) => state==='enqueued'||state==='queue-observed'; /** Only this component may enqueue a managed state reply. Network code never * retries enqueue. New reports revoke permission to send previous replies before @@ -47,7 +51,10 @@ export class UplinkCoordinator { // commit conflicting records for one nonce. Drop its reply, but reconcile // its billing snapshot before handing the newer report to another side. const prior=this.journal.previous(row); - if(prior?.owner) { + // A finished prior report (its reply enqueued or seen in the queue) already had its outcome + // replayed to the other decider, so there is nothing in flight to wait for. Asking its owner + // anyway would leave every new report undecided whenever that owner (central) is unreachable. + if(prior?.owner && !settled(prior.state)) { try { const outcome=await deadline(this.decider(prior.owner).outcome(prior.gateway,prior.id),this.options.timeoutMs); if(!outcome || outcome.active)return; @@ -55,9 +62,14 @@ export class UplinkCoordinator { } catch {return;} } let chosen:Owner|null=null; - for(const owner of this.options.centralHealthy()?['central','local'] as const:['local'] as const) { + const healthy=this.options.centralHealthy(); + if(!healthy)this.options.log?.(`Report ${id}: central link is not ready (offline or connected under 15 s); this gateway decides`); + for(const owner of healthy?['central','local'] as const:['local'] as const) { try {await deadline(this.decider(owner).offer(row.gateway,row.event),this.options.timeoutMs);chosen=owner;break;} - catch {/* Offers never process billing, so pre-commit fallback is safe. */} + catch(error) { + // Offers never process billing, so pre-commit fallback is safe. + this.options.log?.(`Report ${id}: ${owner==='central'?'central':'this gateway'} did not take it (${error instanceof Error?error.message:String(error)})${owner==='central'?'; this gateway decides':''}`); + } } if(!chosen || !this.journal.own(id,chosen))return; row=this.journal.get(id)!; diff --git a/src/central/distribution.ts b/src/central/distribution.ts index ce18d8a..1b0796a 100644 --- a/src/central/distribution.ts +++ b/src/central/distribution.ts @@ -22,9 +22,20 @@ export class PublicationStore { } close():void{this.db.close();} } +/** Which sends are recorded and never repeated: + * - "arweave": permanent, paid storage, so each reading is uploaded once; + * - "hook:": central's first send of each new reading to each module (its catch-up loop + * runs every minute, so these must not repeat). + * The scheduled prover and Streamr runs ("prover:", "streamr:") are NOT recorded: they + * resend every stored reading until the chain confirms its nonce, and confirmed ones are pruned first. */ +const recorded=(destination:string)=>destination==='arweave'||destination.startsWith('hook:'); export function publishManaged(destination:string,records:TransactionRecord[],send:(records:TransactionRecord[])=>Promise):Promise{ - if(process.env.CONSOLE_ROLE==='gateway')return Promise.resolve(null); - if(!managedRole())return send(records); + if(process.env.CONSOLE_ROLE==='gateway'){ + // A managed gateway publishes nothing itself, except sending readings it decided while central was + // away to the prover. Central resends them on its schedule anyway, so nothing is recorded here. + return destination==='hook:core/prover'||destination.startsWith('prover:')?send(records):Promise.resolve(null); + } + if(!managedRole()||!recorded(destination))return send(records); if(!publications)return Promise.reject(new Error('Central publication journal is unavailable.')); return publications.publish(destination,records,send); } diff --git a/src/central/hub.ts b/src/central/hub.ts index de28bf2..30b16ee 100644 --- a/src/central/hub.ts +++ b/src/central/hub.ts @@ -54,7 +54,8 @@ export class GatewayHub { if(typeof value==='object'&&value!==null&&'type' in value&&value.type==='operation'){ let message;try{message=parseOperation(data.toString());if(message.type!=='operation')throw new Error();}catch{socket.close(1008,'Invalid operation');return;} inFlight++; - void serveOperation(this.processor(),gateway,message).then(reply=>{if(!reply.error&&message.action==='offer'&&message.event)this.identities.observeDevice(message.event.deviceInfo.devEui,gateway);if(socket.readyState===WebSocket.OPEN)socket.send(JSON.stringify(reply));}).catch(()=>socket.close(1011,'Operation storage unavailable')).finally(()=>inFlight--); + void serveOperation(this.processor(),gateway,message).then(reply=>{// Learn which gateway serves a meter, also from reports it decided while central was away. + if(!reply.error&&(message.action==='offer'||message.action==='reconcile')&&message.event)this.identities.observeDevice(message.event.deviceInfo.devEui,gateway);if(socket.readyState===WebSocket.OPEN)socket.send(JSON.stringify(reply));}).catch(()=>socket.close(1011,'Operation storage unavailable')).finally(()=>inFlight--); return; } try { @@ -75,16 +76,9 @@ export class GatewayHub { socket.on('error',()=>socket.terminate()); }); server.once('close',()=>{for(const gateway of this.rotations.keys())this.clearRotation(gateway);for(const socket of this.sockets.values())socket.terminate();wss.close();}); - app.get('/api/gateways',(req,res)=>res.json({selected:this.selected(req),gateways:this.identities.gateways().map(g=>({...g,online:this.sockets.get(g.id)?.readyState===WebSocket.OPEN,health:this.identities.health(g.id),updates:this.identities.updates(g.id)}))})); + app.get('/api/gateways',(_req,res)=>res.json({gateways:this.identities.gateways().map(g=>({...g,online:this.sockets.get(g.id)?.readyState===WebSocket.OPEN,health:this.identities.health(g.id),updates:this.identities.updates(g.id)}))})); app.get('/api/devices/gateways',(_req,res)=>res.json({devices:this.identities.devices()})); app.post('/api/devices/:devEui/gateway',(req,res)=>{try{if(req.body?.gateway!==null&&typeof req.body?.gateway!=='string')throw new Error();this.identities.assignDevice(req.params.devEui,req.body.gateway,res.locals.centralUser.id);res.json({devices:this.identities.devices()});}catch{res.status(400).json({error:'Choose an observed device and available gateway, or null to restore discovery.'});}}); - app.post('/api/gateways/select',(req,res)=>{ - const secure=this.development?'':'; Secure'; - // null deselects: Terminal and ChirpStack then work on the central server itself. - if(req.body?.id===null){res.setHeader('Set-Cookie',`console_gateway=; Path=/; Max-Age=0; HttpOnly; SameSite=Strict${secure}`);res.json({selected:null});return;} - if(typeof req.body?.id!=='string'||!this.identities.gateways().some(g=>g.id===req.body.id&&!g.revoked)){res.status(400).json({error:'Choose an available gateway.'});return;} - res.setHeader('Set-Cookie',`console_gateway=${req.body.id}; Path=/; HttpOnly; SameSite=Strict${secure}`);res.json({selected:req.body.id}); - }); app.post('/api/gateways',(req,res)=>{ 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.'});} @@ -118,9 +112,16 @@ export class GatewayHub { catch{res.status(409).json({error:'Credential rotation is already pending or unavailable.'});} }); } - selected(req:http.IncomingMessage):string|null { - const id=(req.headers.cookie||'').split(';').map(v=>v.trim()).find(v=>v.startsWith('console_gateway='))?.slice(16); - return id&&this.identities.gateways().some(g=>g.id===id&&!g.revoked)?id:null; + /** Each browser tab names its own target, so tabs never change each other's. + * Empty or "central" means the central server; otherwise an available gateway. + * Returns null for a gateway that is unknown, deleted or revoked. */ + target(raw:string|null|undefined):{gateway:string|null}|null { + if(!raw||raw==='central')return {gateway:null}; + return this.identities.gateways().some(g=>g.id===raw&&!g.revoked)?{gateway:raw}:null; + } + /** The target a WebSocket (such as Terminal) asked for in its address: /terminal?gateway=ID. */ + socketTarget(req:http.IncomingMessage):{gateway:string|null}|null { + return this.target(new URL(req.url||'/','http://console.invalid').searchParams.get('gateway')); } open(gateway:string,purpose:TunnelPurpose,host:string,port:number):Promise{ const tunnels=this.tunnels.get(gateway);if(!tunnels)return Promise.reject(new Error('Gateway is offline.'));return tunnels.open(purpose,host,port); diff --git a/src/central/operations.ts b/src/central/operations.ts index 14341bb..d0e49ee 100644 --- a/src/central/operations.ts +++ b/src/central/operations.ts @@ -12,7 +12,7 @@ export interface Operation { queueBefore: string[]; queueId: string | null; sequence: number; uncertainCounted: boolean; } export interface GatewayCounts { uncertain: number; reconciled: number; superseded: number; pending: number } -export type OperationLog = (event: { gateway: string; id: string; state: OperationState }) => void; +export type OperationLog = (event: { gateway: string; id: string; state: OperationState; owner?: Owner | null }) => void; type Row = Omit & { event: string; decision: string | null; attempted: number; queueBefore: string; uncertainCounted: number; }; @@ -145,6 +145,6 @@ export class OperationJournal { } replays(target:Owner):Outcome[] {return (this.db.prepare('SELECT outcome FROM agent_replays WHERE target=? ORDER BY rowid').all(target) as Array<{outcome:string}>).map(row=>JSON.parse(row.outcome) as Outcome);} replayed(target:Owner,id:string):void {this.db.prepare('DELETE FROM agent_replays WHERE id=? AND target=?').run(id,target);} - private emit(id: string): void { const row=this.get(id); if(row) this.log({gateway:row.gateway,id:row.id,state:row.state}); } + private emit(id: string): void { const row=this.get(id); if(row) this.log({gateway:row.gateway,id:row.id,state:row.state,owner:row.owner}); } close(): void { this.db.close(); } } diff --git a/src/central/processor.ts b/src/central/processor.ts index de919cd..7cc11bf 100644 --- a/src/central/processor.ts +++ b/src/central/processor.ts @@ -4,7 +4,9 @@ import type { MeterRecord, TransactionRecord } from '../types'; import type { UplinkEvent } from './protocol'; import { eventIdentity, eventFingerprint } from './operations'; -export interface BillingSnapshot { meter: MeterRecord | null; transactions: TransactionRecord[] } +/** Older gateways list readings they already sent to a prover at handover; accepted and ignored. */ +export interface PublishedRecord { destination: string; tokenId: number; nonce: number; state: 'submitted' | 'uncertain' } +export interface BillingSnapshot { meter: MeterRecord | null; transactions: TransactionRecord[]; published?: PublishedRecord[] } export interface Outcome { id: string; state: 'offered' | 'processing' | 'decided' | 'uncertain' | 'rejected'; active: boolean; decision: StateDownlink | null; snapshot: BillingSnapshot | null; diff --git a/src/central/replication.ts b/src/central/replication.ts index 12dd5bb..7b5a13a 100644 --- a/src/central/replication.ts +++ b/src/central/replication.ts @@ -13,15 +13,19 @@ export class Replication { async flush(target:Owner):Promise { const decider=target==='central'?this.central:this.local; if(!decider.reconcile)throw new Error('Decider does not support reconciliation.'); - for(const outcome of this.journal.replays(target)){ - const operation=this.journal.get(outcome.id);if(!operation)throw new Error('Reconciliation event is missing.'); - await decider.reconcile(operation.gateway,operation.event,outcome); - this.journal.replayed(target,outcome.id); + for(const stored of this.journal.replays(target)){ + const operation=this.journal.get(stored.id);if(!operation)throw new Error('Reconciliation event is missing.'); + await decider.reconcile(operation.gateway,operation.event,stored); + this.journal.replayed(target,stored.id); } } guarded(owner:Owner):Decider { const target=owner==='central'?this.central:this.local; - return {offer:async(gateway,event)=>{await this.flush(owner);await target.offer(gateway,event);}, + return {offer:async(gateway,event)=>{ + // A decider must first learn the reports the other side decided; until then it cannot take new ones. + try{await this.flush(owner);} + catch(error){throw new Error(`replaying ${this.journal.replays(owner).length} report(s) decided elsewhere failed: ${error instanceof Error?error.message:String(error)}`);} + await target.offer(gateway,event);}, commit:(gateway,id)=>target.commit(gateway,id),outcome:(gateway,id)=>target.outcome(gateway,id),reconcile:target.reconcile?.bind(target)}; } } diff --git a/src/central/rpc.ts b/src/central/rpc.ts index 285d061..e2c1585 100644 --- a/src/central/rpc.ts +++ b/src/central/rpc.ts @@ -3,6 +3,10 @@ import WebSocket from 'ws'; import type { Decider, Outcome, BillingSnapshot } from './processor'; import { parseMessage, UplinkEvent, MAX_MESSAGE_BYTES } from './protocol'; import { eventIdentity } from './operations'; +import { createLogger } from '../utils/logger'; +import { errorText } from '../utils/report-log'; + +const logger=createLogger('[central]'); export type OperationRequest = {v:1;type:'operation';requestId:string;action:'offer'|'commit'|'outcome'|'reconcile';id:string;event?:UplinkEvent;outcome?:Outcome}; export type OperationResponse = {v:1;type:'operation-result';requestId:string;id:string;outcome:Outcome|null;error:boolean}; @@ -15,7 +19,11 @@ function snapshot(value:unknown):value is BillingSnapshot { const m=value.meter; if(!record(m)||typeof m.publicKey!=='string'||!/^0x[a-fA-F0-9]{64}$/.test(m.publicKey)||!int(m.tokenId)||!int(m.latestNonce,-1)|| typeof m.devEui!=='string'||!/^[a-fA-F0-9]{16}$/.test(m.devEui))return false; - return value.transactions.every(t=>record(t)&&int(t.nonce)&&int(t.identifier)&&int(t.receivedAt)&&typeof t.raw==='string'&&/^[a-fA-F0-9]+$/.test(t.raw)&&[148,212,224].includes(t.raw.length)); + if(!value.transactions.every(t=>record(t)&&int(t.nonce)&&int(t.identifier)&&int(t.receivedAt)&&typeof t.raw==='string'&&/^[a-fA-F0-9]+$/.test(t.raw)&&[148,212,224].includes(t.raw.length)))return false; + if(value.published===undefined)return true; + return Array.isArray(value.published)&&value.published.length<=1000&&value.published.every(p=>record(p)&&typeof p.destination==='string'&& + /^prover:https?:\/\/\S{1,300}$/.test(p.destination)&&int(p.tokenId)&&int(p.nonce)&&(p.state==='submitted'||p.state==='uncertain')&& + (value.transactions as Array<{identifier:number;nonce:number}>).some(t=>t.identifier===p.tokenId&&t.nonce===p.nonce)); } export function validOutcome(value:unknown,id:string):value is Outcome { if(!record(value)||value.id!==id||!['offered','processing','decided','uncertain','rejected'].includes(String(value.state))||typeof value.active!=='boolean')return false; @@ -51,7 +59,10 @@ export async function serveOperation(processor:Decider,gateway:string,request:Op else if(request.action==='outcome')outcome=await processor.outcome(gateway,request.id); else if(processor.reconcile)await processor.reconcile(gateway,request.event!,request.outcome!); else throw new Error('Reconciliation is unavailable.'); - } catch {error=true;} + } catch(failure) { + error=true; + logger.warn(`Refused ${request.action} of report ${request.id} from gateway ${gateway}: ${errorText(failure)}`); + } return {v:1,type:'operation-result',requestId:request.requestId,id:request.id,outcome,error}; } diff --git a/src/index.ts b/src/index.ts index 480ce9e..203a522 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,15 +10,19 @@ else { const sessions=new Map(); attachTerminal(server,undefined,{ connection:async request=>{ - const gateway=runtime.hub.selected(request); + const target=runtime.hub.socketTarget(request); + if(!target)throw new Error('That gateway is no longer available.'); + const gateway=target.gateway; // No gateway selected: a shell on the central server itself (the container shares the host network). if(!gateway)return centralSsh(); const sock=await runtime.hub.open(gateway,'ssh','ssh',22); return {sock,hostHash:'sha256',hostVerifier:(hash:string)=>runtime.identities.verifyHostKey(gateway,hash)}; }, + // Only the same signed-in user, working on the same target, may pick a session back up. + owner:request=>`${runtime.auth.session(request)?.id||'none'}|${runtime.hub.socketTarget(request)?.gateway||'central'}`, audit:(request,event,session)=>{ if(event==='start'){ - const user=runtime.auth.session(request),gateway=runtime.hub.selected(request)||undefined; + const user=runtime.auth.session(request),gateway=runtime.hub.socketTarget(request)?.gateway||undefined; if(user)sessions.set(session,{user:user.id,gateway}); } const actor=sessions.get(session); @@ -31,6 +35,7 @@ import { loadExtensionsFromConfig, loadUIExtensionsFromConfig, runHook, notifyHo import setupDatabase from './store/sqlite'; import { createLogger } from './utils/logger'; import { warnRemovedManagerSettings } from './lib/legacy-config'; +import { watchResolver } from './utils/dns-watch'; const logger = createLogger('[index]'); warnRemovedManagerSettings(process.env, message => logger.warn(message)); @@ -76,6 +81,13 @@ warnRemovedManagerSettings(process.env, message => logger.warn(message)); startServer(); + // After a reboot Docker may start this container before the host has DNS (for example slow Wi-Fi). + // Its copy of the DNS settings then stays empty; restart so Docker provides the host's current ones. + watchResolver(() => { + logger.error('This container has no DNS server (Docker copied the host\'s DNS settings before its network was up). Restarting so Docker provides fresh ones.'); + process.exit(1); + }); + await runHook('onAfterInit'); } catch (error) { logger.error('[fatal] Failed to initialize application:', error); diff --git a/src/lib/core/prover/index.ts b/src/lib/core/prover/index.ts index 00cdb06..7ea37cd 100644 --- a/src/lib/core/prover/index.ts +++ b/src/lib/core/prover/index.ts @@ -108,8 +108,12 @@ export default class implements Hooks { } async sendPendingTransactionsToProver(proverURL: string, pendingTransactions: TransactionRecord[]) { - if (managedRole()) return publishManaged(`prover:${proverURL}`, pendingTransactions, - records => this.sendTransactionsToProver(proverURL, buildBatchPayload(records))); + // Resent on every run until the chain confirms the nonces (no "already sent" record). On a managed + // gateway, publishManaged only lets prover sends through; elsewhere it sends straight away. + if (managedRole()) return publishManaged(`prover:${proverURL}`, pendingTransactions, records => { + logger.info('Sending', records.length, 'transactions to prover at', proverURL); + return retry(async () => this.sendTransactionsToProver(proverURL, buildBatchPayload(records))); + }); logger.info('[info] Sending', pendingTransactions.length, 'transactions to prover at', proverURL); const requestPayload = buildBatchPayload(pendingTransactions); diff --git a/src/lib/core/streamr/index.ts b/src/lib/core/streamr/index.ts index aa25d86..2603d88 100644 --- a/src/lib/core/streamr/index.ts +++ b/src/lib/core/streamr/index.ts @@ -6,6 +6,7 @@ import type { Hooks, TransactionRecord } from "../../../types"; import { getSyncedPendingTransactions } from "../../sync"; import { createLogger } from "../../../utils/logger"; import { managedRole, publishManaged } from '../../../central/distribution'; +import { recordPublish } from "./state"; const logger = createLogger("[streamr]"); @@ -44,7 +45,7 @@ export default class implements Hooks { 2000, ); }), - ); + ).then(() => recordPublish('success', pendingTransactions.length), error => { recordPublish('error'); throw error; }); } }, { name: "streamr-publish-pending-transactions", noOverlap: true }, diff --git a/src/lib/core/streamr/state.ts b/src/lib/core/streamr/state.ts new file mode 100644 index 0000000..40e6241 --- /dev/null +++ b/src/lib/core/streamr/state.ts @@ -0,0 +1,5 @@ +/** Shared by the hourly job and the Publish Now button, so the app shows whichever ran last. */ +export const lastPublish: { time: Date | null; status: 'success' | 'error' | null; count: number } = { time: null, status: null, count: 0 }; +export function recordPublish(status: 'success' | 'error', count = 0): void { + lastPublish.time = new Date(); lastPublish.status = status; lastPublish.count = count; +} diff --git a/src/lib/core/streamr/ui.ts b/src/lib/core/streamr/ui.ts index 48d9336..4a67023 100644 --- a/src/lib/core/streamr/ui.ts +++ b/src/lib/core/streamr/ui.ts @@ -1,7 +1,8 @@ -import { getAllTransactionRecords } from "../../../store/sqlite"; +import { getAllTransactionRecords, getMeterByDevEui } from "../../../store/sqlite"; import { buildBatchPayload, retry } from "../../utils"; import { managedRole, publishManaged } from '../../../central/distribution'; -import type { UIHooks, UIAppIcon, UIAppWindow, UIAction, TransactionRecord } from "../../../types"; +import { lastPublish, recordPublish } from './state'; +import type { UIHooks, UIAppIcon, UIAppWindow, UIAction, TransactionRecord, UIStatusContext } from "../../../types"; import { StreamrClient } from "@streamr/sdk"; import { createLogger } from "../../../utils/logger"; @@ -24,9 +25,7 @@ function escapeHtml(value: string): string { */ export default class implements UIHooks { private streamIds: string[] = process.env.STREAMR_STREAM_ID ? process.env.STREAMR_STREAM_ID.split(",") : []; - private cronSchedule: string = process.env.STREAMR_CRONSCHEDULE || "Not configured"; - private lastPublishTime: Date | null = null; - private lastPublishStatus: "success" | "error" | null = null; + private cronSchedule: string = process.env.STREAMR_CRONSCHEDULE || "0 * * * *"; getAppIcon(): UIAppIcon { return { @@ -38,10 +37,9 @@ export default class implements UIHooks { } async getAppWindow(): Promise { - const pendingCount = (await this.getPendingTransactions()).length; - const streamIds = this.streamIds; - const cronSchedule = this.cronSchedule; - + // First paint; /js/streamr.js keeps it current and follows the tab's gateway on central. + const status = await this.status(); + const gateway = status.role === 'gateway'; return { id: "streamr", title: "Streamr Publisher", @@ -50,21 +48,23 @@ export default class implements UIHooks {
STREAMR NETWORK

Keep the data flowing.

Publish your pending meter transactions to connected streams.

- ${pendingCount} pending + ${gateway ? "Central publishes" : `${status.pendingCount} pending`}
+

${escapeHtml(status.scope)}

-
Pending transactions${pendingCount}
-
Last publish${this.lastPublishTime ? this.lastPublishTime.toLocaleString() : "Never"} - ${this.lastPublishStatus ? `${this.lastPublishStatus === "success" ? "Published successfully" : "Publish failed"}` : ""}
+
${gateway ? "Readings kept here" : "Waiting to publish"}${gateway ? status.recordCount : status.pendingCount} + ${status.uncertainCount ? `${status.uncertainCount} with an uncertain outcome (not resent automatically)` : ""}
+
Last publish${status.lastPublishTime ? new Date(status.lastPublishTime).toLocaleString() : "Never"} + ${status.lastPublishStatus ? (status.lastPublishStatus === "success" ? "Published successfully" : "Publish failed") : ""}

Configuration

-
Publish schedule${escapeHtml(cronSchedule)}
+
Publish schedule${escapeHtml(this.cronSchedule)}

Connected streams

-
    ${streamIds.map((id) => `
  • ${escapeHtml(id)}
  • `).join("") || '
  • No streams configured.
  • '}
+
    ${this.streamIds.map((id) => `
  • ${escapeHtml(id)}
  • `).join("") || '
  • No streams configured.
  • '}
- - + +
`, }; @@ -77,7 +77,7 @@ export default class implements UIHooks { label: "Publish Now", buttonClass: "is-warning", handler: async () => { - if (process.env.CONSOLE_ROLE === 'gateway') return { message: 'Publication is owned by the central Console. Local reports are retained for reconciliation.' }; + if (process.env.CONSOLE_ROLE === 'gateway') return { message: 'Central publishes for managed gateways. Readings here are kept for reconciliation.' }; const pendingTransactions = await this.getPendingTransactions(); if (pendingTransactions.length === 0) { @@ -92,15 +92,14 @@ export default class implements UIHooks { }), ); - this.lastPublishTime = new Date(); - this.lastPublishStatus = "success"; + recordPublish("success", pendingTransactions.length); return { - message: managedRole() ? 'Central publication checked. Previously submitted or uncertain transactions are not resubmitted.' : `Published ${pendingTransactions.length} transactions to ${this.streamIds.length} stream(s)`, + message: managedRole() ? `Published ${pendingTransactions.length} reading(s) not yet confirmed on-chain, for every gateway, to ${this.streamIds.length} stream(s)` : `Published ${pendingTransactions.length} transactions to ${this.streamIds.length} stream(s)`, data: { count: pendingTransactions.length }, }; } catch (error: any) { - this.lastPublishStatus = "error"; + recordPublish("error"); throw new Error(`Failed to publish: ${error.message}`); } }, @@ -108,14 +107,32 @@ export default class implements UIHooks { ]; } - async getStatusData(): Promise> { - const pendingTransactions = await this.getPendingTransactions(); + async getStatusData(context?: UIStatusContext): Promise> { + return this.status(context); + } + + /** What the window shows: readings not yet confirmed on-chain, which every publish (re)sends. + * Central: optionally only one gateway's meters. A managed gateway: central publishes. */ + async status(context: UIStatusContext = {}) { + const role = (process.env.CONSOLE_ROLE || "standalone") as "standalone" | "central" | "gateway"; + let records = await this.getPendingTransactions(); + if (context.devEuis) { + const tokens = new Set(context.devEuis.map((devEui) => getMeterByDevEui(devEui)?.tokenId).filter((id) => id !== undefined)); + records = records.filter((record) => tokens.has(record.identifier)); + } + const scope = role === "gateway" ? "This gateway is managed by central, which publishes its readings to Streamr." + : role === "central" ? (context.gateway ? `Showing readings from ${context.gatewayName || "this gateway"}'s meters. Central publishes for every gateway together.` + : "Showing readings from every gateway. Central publishes them on the schedule below.") + : ""; return { - pendingCount: pendingTransactions.length, - streamIds: this.streamIds, - cronSchedule: this.cronSchedule, - lastPublishTime: this.lastPublishTime, - lastPublishStatus: this.lastPublishStatus, + role, scope, gateway: context.gateway || null, + recordCount: records.length, + // Readings stay until the chain confirms their nonce, and each publish sends all of them. + pendingCount: role === "gateway" ? 0 : records.length, + uncertainCount: 0, + canPublish: role !== "gateway" && this.streamIds.length > 0, + streamIds: this.streamIds, cronSchedule: this.cronSchedule, + lastPublishTime: lastPublish.time, lastPublishStatus: lastPublish.status, }; } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 8a44a90..78a5435 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -55,7 +55,7 @@ export async function runHook(hook: K, ...args: Parameter if (fn) functionReturn = await (fn as any).call(ext, ...args); } catch (error) { errors.push(error); - logger.error(`Extension hook ${hook} failed:`, error); + logger.error(`Extension hook ${hook} failed: ${error instanceof Error ? error.message : String(error)}`); } if (typeof functionReturn === "boolean" && hook === "isOnStateCompute") { @@ -76,7 +76,8 @@ export async function notifyHook(hook: K, ...args: Parame /** Managed roles publish centrally with a durable claim per extension and nonce. * Built-in cron/UI publishers additionally share destination-level claims. */ export async function distributeManaged(records: TransactionRecord[]): Promise { - if (process.env.CONSOLE_ROLE !== 'central') return; + // Gateways run this only for their own fallback decisions; publishManaged lets just the prover through. + if (process.env.CONSOLE_ROLE !== 'central' && process.env.CONSOLE_ROLE !== 'gateway') return; for (const [index, extension] of extensions.entries()) { const publish = extension.onTransactionDistribution; if (!publish) continue; diff --git a/src/public/js/actions.js b/src/public/js/actions.js index cc36904..b41fcfc 100644 --- a/src/public/js/actions.js +++ b/src/public/js/actions.js @@ -51,6 +51,8 @@ async function invokeAction(moduleId, actionId, buttonElement) { buttonElement.disabled = false; buttonElement.removeAttribute('aria-busy'); } + // Let the app refresh what it shows (and re-disable the button if nothing is left). + document.dispatchEvent(new CustomEvent('app:action', { detail: { moduleId, actionId } })); } } diff --git a/src/public/js/chirpstack-bridge.js b/src/public/js/chirpstack-bridge.js index 809198a..d298fdb 100644 --- a/src/public/js/chirpstack-bridge.js +++ b/src/public/js/chirpstack-bridge.js @@ -2,10 +2,14 @@ (function () { 'use strict'; if (!location.pathname.startsWith('/chirpstack/')) return; + // Central serves each target under its own path (/chirpstack/~GATEWAY_ID/), so two + // tabs can show two ChirpStacks; standalone uses /chirpstack/. + const scope = /^\/chirpstack\/~([\w-]{1,64})\//.exec(location.pathname); + const base = scope ? `/chirpstack/~${scope[1]}` : '/chirpstack'; function proxied(value) { const url = new URL(String(value), location.href); - if (url.origin === location.origin && !url.pathname.startsWith('/chirpstack/')) { - url.pathname = '/chirpstack' + url.pathname; + if (url.origin === location.origin && !url.pathname.startsWith(base + '/')) { + url.pathname = base + (url.pathname.startsWith('/chirpstack/') ? url.pathname.slice('/chirpstack'.length) : url.pathname); } return url.href; } @@ -35,7 +39,8 @@ for (const name of ['localStorage', 'sessionStorage']) { try { const storage = window[name]; - const prefix = 'm3tering.chirpstack.'; + // Each target keeps its own ChirpStack login: one gateway's token never reaches another. + const prefix = scope ? `m3tering.chirpstack.${scope[1]}.` : 'm3tering.chirpstack.'; const keys = () => Object.keys(storage).filter(key => key.startsWith(prefix)); const scoped = { get length() { return keys().length; }, diff --git a/src/public/js/gateways.js b/src/public/js/gateways.js index 138200b..1be0168 100644 --- a/src/public/js/gateways.js +++ b/src/public/js/gateways.js @@ -2,7 +2,20 @@ 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'],TOKEN_LIFETIME=10*60000,CENTRAL_NAME='Central server'; - let gateways=[],selected=null,busy=false,again=false,editing=null,focusRename=false,tokenTimer=null; + // The target belongs to this tab only (sessionStorage): other tabs keep their own gateway or central. + const TARGET_KEY='m3tering.console.target'; + const loadTarget=()=>{try{return sessionStorage.getItem(TARGET_KEY)||null;}catch{return null;}}; + let gateways=[],selected=loadTarget(),busy=false,again=false,editing=null,focusRename=false,tokenTimer=null; + // Terminal and ChirpStack name this tab's target in their addresses; "central" means the central server. + window.ConsoleTarget=()=>selected||'central'; + function applyTarget(){ + try{if(selected)sessionStorage.setItem(TARGET_KEY,selected);else sessionStorage.removeItem(TARGET_KEY);}catch{/* still works for this page */} + const frame=$('chirpstack-frame'),src=`/chirpstack/~${encodeURIComponent(selected||'central')}/`; + // Unload the previous target's ChirpStack; the next launch loads this one. + if(frame&&frame.dataset.src!==src){frame.dataset.src=src;if(frame.hasAttribute('src')){frame.src='about:blank';frame.removeAttribute('src');}} + // Apps that show per-gateway data (such as Streamr) refresh for the new target. + document.dispatchEvent(new CustomEvent('console:target',{detail:{target:selected||'central'}})); + } const openUpdates=new Set(); const error=message=>{$('gateway-error').textContent=message||'';}; @@ -120,8 +133,7 @@ // null deselects the gateway. if(id===selected){setMenu(false);switcher.focus();return;} for(const app of NETWORK_APPS)Desktop.close(app); - try{await api('/api/gateways/select',{id});await refresh();setMenu(false);switcher.focus();} - catch(e){$('gateway-menu-summary').textContent=e.message;} + selected=id;applyTarget();render();setMenu(false);switcher.focus(); } function stopEditing(){editing=null;if(list.contains(document.activeElement))document.activeElement.blur();} function startEditing(id){editing=id;focusRename=true;render();} @@ -195,7 +207,9 @@ async function refresh(){ if(busy){again=true;return;}busy=true; - try{const result=await api('/api/gateways');gateways=result.gateways;selected=result.selected; + try{const result=await api('/api/gateways');gateways=result.gateways; + // A gateway deleted or revoked elsewhere can't stay this tab's target. + if(selected&&!gateways.some(g=>g.id===selected&&!g.revoked)){for(const app of NETWORK_APPS)Desktop.close(app);selected=null;applyTarget();} 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();}} @@ -262,5 +276,6 @@ 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);}; + applyTarget(); const timer=setInterval(()=>{void refresh().then(retitle);},5000);window.addEventListener('pagehide',()=>clearInterval(timer));refresh(); })(); diff --git a/src/public/js/streamr.js b/src/public/js/streamr.js new file mode 100644 index 0000000..0ba2045 --- /dev/null +++ b/src/public/js/streamr.js @@ -0,0 +1,39 @@ +/* Keeps the Streamr window current: on open, after Publish Now, when this tab's + * gateway changes (central), and every 30 seconds while it is visible. */ +(() => { + const win = document.getElementById('streamr'); + if (!win) return; + const field = name => win.querySelector(`[data-streamr-field="${name}"]`); + const set = (name, text) => { const node = field(name); if (node) node.textContent = text; }; + let busy = false; + async function refresh() { + if (busy) return; busy = true; + try { + const headers = typeof window.ConsoleTarget === 'function' ? { 'X-Console-Target': window.ConsoleTarget() } : {}; + const response = await fetch('/api/apps/streamr/status', { headers }); + if (response.status === 401) { location.assign('/login'); return; } + if (!response.ok) return; + const s = await response.json(), gateway = s.role === 'gateway'; + set('scope', s.scope || ''); + set('badge', gateway ? 'Central publishes' : `${s.pendingCount} pending`); + set('pending-label', gateway ? 'Readings kept here' : 'Waiting to publish'); + set('pending', String(gateway ? s.recordCount : s.pendingCount)); + set('uncertain', s.uncertainCount ? `${s.uncertainCount} with an uncertain outcome (not resent automatically)` : ''); + set('last', s.lastPublishTime ? new Date(s.lastPublishTime).toLocaleString() : 'Never'); + set('last-status', s.lastPublishStatus ? (s.lastPublishStatus === 'success' ? 'Published successfully' : 'Publish failed') : ''); + const publish = field('publish'); + // Leave a running publish alone; actions.js restores the button when it finishes. + if (publish && publish.getAttribute('aria-busy') !== 'true') { + publish.hidden = gateway; + publish.disabled = !s.canPublish || s.pendingCount === 0; + } + } catch { /* keep the last values */ } finally { busy = false; } + } + window.StreamrApp = { refresh }; + window.addEventListener('desktop:open', event => { if (event.detail?.id === 'streamr') void refresh(); }); + document.addEventListener('app:action', event => { if (event.detail?.moduleId === 'streamr') void refresh(); }); + document.addEventListener('console:target', () => void refresh()); + const timer = setInterval(() => { if (!win.hidden) void refresh(); }, 30000); + window.addEventListener('pagehide', () => clearInterval(timer)); + void refresh(); +})(); diff --git a/src/public/js/terminal.js b/src/public/js/terminal.js index 8ad323f..5c4a1dd 100644 --- a/src/public/js/terminal.js +++ b/src/public/js/terminal.js @@ -46,39 +46,54 @@ function fitTerminal() { const rows = Math.max(3, Math.floor((container.clientHeight - 20) / charHeight)); if (terminal.cols !== cols || terminal.rows !== rows) terminal.resize(cols, rows); } -function connectTerminal(event) { - event.preventDefault(); +// A session outlives a dropped connection (phones freeze background pages): the server keeps the +// shell for a while and this page reconnects with a one-time resume key, kept in this tab only. +const RESUME_STORE = 'm3tering.terminal.resume'; +let resumeKey = null, resumeTimer = 0, resumeDelay = 1000, userEnded = false; +const terminalTarget = () => typeof window.ConsoleTarget === 'function' ? window.ConsoleTarget() : 'local'; +function storedResumeKey() { + try { const saved = JSON.parse(sessionStorage.getItem(RESUME_STORE) || 'null'); return saved && saved.target === terminalTarget() ? saved.key : null; } + catch { return null; } +} +function rememberResumeKey(key) { + resumeKey = key; + try { if (key) sessionStorage.setItem(RESUME_STORE, JSON.stringify({ key, target: terminalTarget() })); else sessionStorage.removeItem(RESUME_STORE); } + catch { /* the key still works for this page */ } +} +function showTerminalScreen() { + document.getElementById('terminal-auth').style.display = 'none'; + document.getElementById('terminal-container').style.display = 'flex'; +} +function openTerminalSocket(first, { resuming = false } = {}) { const status = document.getElementById('status-message'); - try { initializeTerminal(); } catch (error) { status.textContent = error.message; return; } - if (socket && (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING)) return; - const username = document.getElementById('username').value; - const password = document.getElementById('password').value; - status.textContent = 'Connecting to terminal…'; const protocol = location.protocol === 'https:' ? 'wss' : 'ws'; - const connection = new WebSocket(`${protocol}://${location.host}/terminal`); + // On central, this tab's own target (a gateway ID or "central"); other tabs keep theirs. + const target = typeof window.ConsoleTarget === 'function' ? `?gateway=${encodeURIComponent(window.ConsoleTarget())}` : ''; + const connection = new WebSocket(`${protocol}://${location.host}/terminal${target}`); socket = connection; - let failure = ''; + let failure = '', ended = false; const timeout = setTimeout(() => { failure = 'Terminal connection timed out. Check that SSH is running on the Console host.'; connection.close(); }, 35000); connection.binaryType = 'arraybuffer'; connection.onopen = () => { if (socket !== connection || !terminal) return; - status.textContent = 'Authenticating with the host…'; - connection.send(JSON.stringify({ type: 'auth', username, password, cols: terminal.cols, rows: terminal.rows })); + if (!resuming) status.textContent = 'Authenticating with the host…'; + connection.send(JSON.stringify({ ...first(), cols: terminal.cols, rows: terminal.rows })); }; connection.onmessage = event => { if (socket !== connection || !terminal) return; if (!isConnected) { let control; try { control = JSON.parse(event.data); } catch { failure = String(event.data); status.textContent = failure; return; } + if (control.type === 'resume-failed') { ended = true; rememberResumeKey(null); failure = 'Your terminal session has ended. Sign in to start a new one.'; return; } if (control.type === 'error') { failure = control.message; status.textContent = failure; return; } if (control.type !== 'ready') return; clearTimeout(timeout); document.getElementById('password').value = ''; - isConnected = true; + isConnected = true; userEnded = false; resumeDelay = 1000; + rememberResumeKey(control.resume || null); status.textContent = 'Terminal session connected'; - document.getElementById('terminal-auth').style.display = 'none'; - document.getElementById('terminal-container').style.display = 'flex'; - terminal.write('Connected to M3tering Console Terminal.\r\n'); + showTerminalScreen(); + terminal.write(control.resumed ? '\r\n[Reconnected to your terminal session.]\r\n' : 'Connected to M3tering Console Terminal.\r\n'); fitTerminal(); connection.send(JSON.stringify({ type: 'resize', cols: terminal.cols, rows: terminal.rows })); terminal.focus(); @@ -89,20 +104,66 @@ function connectTerminal(event) { connection.onclose = event => { clearTimeout(timeout); if (socket !== connection) return; + const wasConnected = isConnected; isConnected = false; socket = null; + if (event.reason === 'Terminal session ended' || event.reason === 'Terminal opened elsewhere') { ended = true; rememberResumeKey(null); } + // A dropped connection (backgrounded page, switching apps, network blip): keep the screen and + // reconnect to the same shell as soon as the page is visible again. + if (!ended && !userEnded && resumeKey && (wasConnected || resuming)) { + status.textContent = 'Connection lost. Reconnecting…'; + terminal?.write(wasConnected ? '\r\n[Connection lost. Reconnecting…]\r\n' : ''); + scheduleResume(); + return; + } document.getElementById('terminal-auth').style.display = 'block'; document.getElementById('terminal-container').style.display = 'none'; status.textContent = failure || event.reason || 'Terminal connection closed. Sign in again to reconnect.'; - terminal?.write('\r\nConnection closed.\r\n'); + if (wasConnected) terminal?.write('\r\nConnection closed.\r\n'); }; connection.onerror = () => { if (socket !== connection) return; - failure = 'Cannot reach the Terminal service. Check the Console connection and WebSocket proxy support.'; - status.textContent = failure; + if (!resuming) { failure = 'Cannot reach the Terminal service. Check the Console connection and WebSocket proxy support.'; status.textContent = failure; } }; } +function connectTerminal(event) { + event.preventDefault(); + const status = document.getElementById('status-message'); + try { initializeTerminal(); } catch (error) { status.textContent = error.message; return; } + if (socket && (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING)) return; + const username = document.getElementById('username').value; + const password = document.getElementById('password').value; + status.textContent = 'Connecting to terminal…'; + userEnded = false; clearTimeout(resumeTimer); + openTerminalSocket(() => ({ type: 'auth', username, password })); +} +function resumeTerminal() { + clearTimeout(resumeTimer); resumeTimer = 0; + const key = resumeKey || storedResumeKey(); + if (!key || socket || userEnded) return false; + try { initializeTerminal(); } catch { return false; } + resumeKey = key; + document.getElementById('status-message').textContent = 'Reconnecting to your terminal session…'; + showTerminalScreen(); + openTerminalSocket(() => ({ type: 'resume', key: resumeKey }), { resuming: true }); + return true; +} +function scheduleResume() { + clearTimeout(resumeTimer); + // Background pages are frozen anyway: wait until the page is visible, then retry with backoff. + if (document.visibilityState !== 'visible') return; + resumeTimer = setTimeout(resumeTerminal, resumeDelay); + resumeDelay = Math.min(resumeDelay * 2, 15000); +} +document.addEventListener('visibilitychange', () => { + if (document.visibilityState === 'visible' && resumeKey && !socket && !userEnded) { resumeDelay = 1000; resumeTerminal(); } +}); +window.addEventListener('online', () => { if (resumeKey && !socket && !userEnded) { resumeDelay = 1000; resumeTerminal(); } }); function disconnectTerminal() { const previous = socket; + userEnded = true; clearTimeout(resumeTimer); + // Ending on purpose: tell the server not to keep the shell waiting for a reconnect. + if (previous?.readyState === WebSocket.OPEN && isConnected) previous.send(JSON.stringify({ type: 'end' })); + rememberResumeKey(null); socket = null; isConnected = false; previous?.close(); document.getElementById('terminal-auth').style.display = 'block'; @@ -111,6 +172,9 @@ function disconnectTerminal() { terminal?.reset(); } function showTerminalAuth() { + // After the browser reloaded a background page, pick the running session back up. + userEnded = false; + if (resumeTerminal()) return; document.getElementById('terminal-auth').style.display = 'block'; document.getElementById('terminal-container').style.display = 'none'; document.getElementById('status-message').textContent = 'Enter your credentials to start a terminal session.'; diff --git a/src/services/chirpstack.ts b/src/services/chirpstack.ts index 7444843..1fb8a04 100644 --- a/src/services/chirpstack.ts +++ b/src/services/chirpstack.ts @@ -21,14 +21,14 @@ function cleanHeaders(headers: IncomingHttpHeaders): IncomingHttpHeaders { return result; } -export function rewriteChirpstackText(body: string, html: boolean): string { +export function rewriteChirpstackText(body: string, html: boolean, base = prefix): string { if (!html) { // Vite / CRA asset references in bundles and stylesheets (including lazy chunks). - return body.replace(/(["'`(])\/(assets|static)\//g, `$1${prefix}/$2/`) - .replace(/(["'`(])\/(?!chirpstack\/)([^"'`()\s]+\.(?:png|svg|ico|jpe?g|webp|gif|woff2?)(?:\?[^"'`()\s]*)?)(?=["'`)])/g, `$1${prefix}/$2`); + return body.replace(/(["'`(])\/(assets|static)\//g, `$1${base}/$2/`) + .replace(/(["'`(])\/(?!chirpstack\/)([^"'`()\s]+\.(?:png|svg|ico|jpe?g|webp|gif|woff2?)(?:\?[^"'`()\s]*)?)(?=["'`)])/g, `$1${base}/$2`); } const rewritten = body.replace(/\b(src|href|action)=(['"])\/(?!\/|chirpstack(?:\/|['"]))([^'"]*)\2/gi, - (_, attr, quote, path) => `${attr}=${quote}${prefix}/${path}${quote}`); + (_, attr, quote, path) => `${attr}=${quote}${base}/${path}${quote}`); // Runs before the upstream application: remaps gRPC-Web and isolates storage. return rewritten.replace(/]*)?>/i, '$&'); } @@ -44,17 +44,19 @@ function unavailable(res: Response, configuration = false): void { export function proxyChirpstack(req: Request, res: Response): void { proxy(req,res); } -export function proxyChirpstackThrough(req:Request,res:Response,target:URL,agent?:http.Agent):void { - proxy(req,res,{target,agent}); +export function proxyChirpstackThrough(req:Request,res:Response,target:URL,agent?:http.Agent,base?:string):void { + proxy(req,res,{target,agent,base}); } -function proxy(req:Request,res:Response,transport?:{target:URL;agent?:http.Agent}):void { +/** `base` is the public path ChirpStack is served under: /chirpstack, or /chirpstack/~TARGET on central. */ +function proxy(req:Request,res:Response,transport?:{target:URL;agent?:http.Agent;base?:string}):void { + const base = transport?.base || prefix; res.setHeader('Cache-Control', 'no-store'); res.setHeader('Referrer-Policy', 'no-referrer'); res.setHeader('X-Frame-Options', 'SAMEORIGIN'); res.setHeader('Content-Security-Policy', "frame-ancestors 'self'"); if (!isSameOrigin(req)) { res.status(403).send('Cross-origin requests are not permitted'); return; } - if (req.originalUrl.split('?')[0] === prefix) { - res.redirect(307, `${prefix}/${req.originalUrl.includes('?') ? req.originalUrl.slice(req.originalUrl.indexOf('?')) : ''}`); + if (req.originalUrl.split('?')[0] === base) { + res.redirect(307, `${base}/${req.originalUrl.includes('?') ? req.originalUrl.slice(req.originalUrl.indexOf('?')) : ''}`); return; } let target: URL; @@ -79,7 +81,7 @@ function proxy(req:Request,res:Response,transport?:{target:URL;agent?:http.Agent if (responseHeaders.location) { try { const location = new URL(responseHeaders.location, new URL(target.origin + req.url)); - if (location.origin === target.origin) responseHeaders.location = prefix + location.pathname + location.search + location.hash; + if (location.origin === target.origin) responseHeaders.location = base + location.pathname + location.search + location.hash; } catch { delete responseHeaders.location; } } const type = String(responseHeaders['content-type'] || ''); @@ -106,7 +108,7 @@ function proxy(req:Request,res:Response,transport?:{target:URL;agent?:http.Agent chunks.push(chunk); }); response.on('end', () => { - if (!res.destroyed && !res.writableEnded) res.end(rewriteChirpstackText(Buffer.concat(chunks).toString('utf8'), html)); + if (!res.destroyed && !res.writableEnded) res.end(rewriteChirpstackText(Buffer.concat(chunks).toString('utf8'), html, base)); }); }); upstream.setTimeout(60_000, () => upstream.destroy(new Error('ChirpStack upstream timeout'))); diff --git a/src/services/grpc.ts b/src/services/grpc.ts index fcfa53c..f00a926 100644 --- a/src/services/grpc.ts +++ b/src/services/grpc.ts @@ -3,6 +3,7 @@ import { DeviceServiceClient } from "@chirpstack/chirpstack-api/api/device_grpc_ import { DeviceQueueItem, EnqueueDeviceQueueItemRequest, GetDeviceQueueItemsRequest } from "@chirpstack/chirpstack-api/api/device_pb"; import type { ChirpstackQueue } from '../central/coordinator'; import { createLogger } from "../utils/logger"; +import { describeReply } from "../utils/report-log"; const logger = createLogger("[grpc]"); @@ -27,7 +28,7 @@ export function enqueue(devEui: string, data: number[]): Promise { return new Promise((resolve, reject) => { client().enqueue(enqueueReq, metadata, (err, resp) => { if (err || !resp) { reject(err || new Error("Missing enqueue response")); return; } - logger.info(`${data} has been enqueued with id: ` + resp.getId()); + logger.info(`Reply enqueued to ChirpStack for ${devEui} (queue id ${resp.getId()}): ${describeReply(data)}`); resolve(); }); }); diff --git a/src/services/mqtt.ts b/src/services/mqtt.ts index c1bd9c5..c685ad8 100644 --- a/src/services/mqtt.ts +++ b/src/services/mqtt.ts @@ -15,6 +15,7 @@ import { uplinkPayload } from '../lib/uplink-payload'; import { runHook, notifyHook, verifyPayloadSignature } from '../lib/utils'; import { getLatestTransactionNonce } from '../lib/sync'; import { createMeterLogger, createLogger } from '../utils/logger'; +import { describeReport, describeReply, errorText } from '../utils/report-log'; import { meterIdFromPublicKeyContractCall } from '../utils/contracts'; const logger = createLogger('[mqtt]'); @@ -84,6 +85,7 @@ export async function handleMessage(blob: Buffer, downlink: DownlinkSink = chirp } devEui = message.deviceInfo.devEui; log = createMeterLogger({ devEui }); + log.info(`Report received: ${describeReport(message)}`); if (deviceLocks.has(devEui!)) { void notifyHook('onMessageDropped', 'locked', devEui!); return; @@ -140,14 +142,19 @@ export async function handleMessage(blob: Buffer, downlink: DownlinkSink = chirp let is_on = false; try { is_on = await runHook('isOnStateCompute', meter.tokenId); } catch (error) { - log.error('Could not determine meter power state:', error); + log.error(`Could not determine meter power state, so the reply is OFF: ${errorText(error)}`); void notifyHook('onIsOnStateComputeError', meter.tokenId, error); } void notifyHook('onIsOnStateComputed', meter.tokenId, is_on); const state = decoded.nonce === 0 ? { nonce: meter.latestNonce, is_on } : { is_on }; + log.info(newTransaction ? `Reading recorded: meter ${meter.tokenId} nonce ${decoded.nonce}` + : decoded.nonce === 0 ? `Meter ${meter.tokenId} asked for its nonce (latest ${meter.latestNonce})` + : `Repeat of meter ${meter.tokenId} nonce ${decoded.nonce}: acknowledged, not billed again`); try { - await downlink({ devEui: devEui!, fPort: 2, confirmed: false, - bytes: encode(state as State, decoded.extensions.latitude ?? 0, decoded.extensions.longitude ?? 0) }); + const bytes = encode(state as State, decoded.extensions.latitude ?? 0, decoded.extensions.longitude ?? 0); + await downlink({ devEui: devEui!, fPort: 2, confirmed: false, bytes }); + // Managed roles hand the reply to the gateway agent, which enqueues it; standalone enqueued it already. + log.info(`${managedBilling.getStore() ? 'Reply decided (the gateway agent enqueues it)' : 'Reply enqueued'}: ${describeReply(bytes)}`); void notifyHook('onStateEnqueued', state, decoded.extensions.latitude ?? 0, decoded.extensions.longitude ?? 0); } finally { // Delivery services run after the downlink attempt and cannot prevent it. @@ -161,7 +168,7 @@ export async function handleMessage(blob: Buffer, downlink: DownlinkSink = chirp } } } catch (error) { - log.error('Error handling MQTT message:', error); + log.error(`Report rejected: ${errorText(error)}`); void notifyHook('onMessageError', error); } finally { if (lockedMeter) meterLocks.delete(lockedMeter); diff --git a/src/services/server.ts b/src/services/server.ts index dfe1c77..67353d4 100644 --- a/src/services/server.ts +++ b/src/services/server.ts @@ -4,7 +4,8 @@ import express, { Express, Request, Response } from 'express'; import { requireSameOrigin } from './auth'; import { proxyChirpstack, proxyChirpstackThrough } from './chirpstack'; import { createLogger } from '../utils/logger'; -import { getUIComponents, invokeUIAction } from '../lib/utils'; +import { getUIComponents, invokeUIAction, getUIExtension } from '../lib/utils'; +import type { UIStatusContext } from '../types'; import { central, config } from '../central/runtime'; import { centralChirpstackUrl } from '../central/config'; import { protectSessionSockets } from './socket-router'; @@ -30,11 +31,19 @@ app.set('views', './src/views'); central?.auth.mount(app,central.hub.machineRoutes); const gatewayHub=central?.hub; app.use('/chirpstack', gatewayHub ? (req,res) => { - const gateway=gatewayHub.selected(req); + // Each tab names its target in the path, /chirpstack/~GATEWAY_ID/ or /chirpstack/~central/, + // so two tabs can show two different ChirpStacks at once. + const match=/^\/~([\w-]{1,64})(?=\/|\?|$)/.exec(req.url); + const name=match?.[1]||'central',base=`/chirpstack/~${name}`; + if(match)req.url=req.url.slice(match[0].length)||'/'; + if(match&&!req.url.startsWith('/'))req.url='/'+req.url; + const target=gatewayHub.target(name); + if(!target){res.status(404).type('text').send('That gateway is no longer available. Choose another in the gateway menu.');return;} + const gateway=target.gateway; if(!gateway){ // No gateway selected: show the ChirpStack dashboard configured for central. let dashboard:URL|undefined,invalid=false;try{dashboard=centralChirpstackUrl();}catch{invalid=true;} - if(dashboard){proxyChirpstackThrough(req,res,dashboard);return;} + if(dashboard){proxyChirpstackThrough(req,res,dashboard,undefined,base);return;} const heading=invalid?'CENTRAL_CHIRPSTACK_URL is not a valid address':'No ChirpStack dashboard set'; res.status(409).type('html').send(`ChirpStack

${heading}

Select a gateway to open its ChirpStack, or set CENTRAL_CHIRPSTACK_URL in central's .env to your online ChirpStack dashboard's address (such as https://chirpstack.example.com, with no path) and run ./auto-compose.sh.

`); return; @@ -43,7 +52,7 @@ app.use('/chirpstack', gatewayHub ? (req,res) => { if(req.destroyed||res.destroyed){stream.destroy();return;} const agent=new http.Agent({keepAlive:false});agent.createConnection=()=>stream; res.once('close',()=>{agent.destroy();stream.destroy();}); - proxyChirpstackThrough(req,res,new URL(stream.upstreamOrigin||'http://chirpstack:8080'),agent); + proxyChirpstackThrough(req,res,new URL(stream.upstreamOrigin||'http://chirpstack:8080'),agent,base); }).catch(()=>{if(!res.headersSent)res.status(502).send('Gateway ChirpStack is unavailable.');else res.destroy();}); } : proxyChirpstack); app.use(requireSameOrigin); @@ -65,6 +74,26 @@ app.get('/', async (req: Request, res: Response, next) => { } catch (error) { next(error); } }); +// Live status for an app window. On central the tab names its target in X-Console-Target, +// so an app can show one gateway's meters (or everything, for the central server). +app.get('/api/apps/:moduleId/status', async (req: Request, res: Response, next) => { + try { + const ext = getUIExtension(req.params.moduleId); + if (!ext?.getStatusData) { res.status(404).json({ error: 'This app has no status.' }); return; } + const context: UIStatusContext = {}; + if (central) { + const target = central.hub.target(String(req.headers['x-console-target'] || '')); + if (!target) { res.status(404).json({ error: 'That gateway is no longer available.' }); return; } + if (target.gateway) { + context.gateway = target.gateway; + context.gatewayName = central.identities.gateways().find(g => g.id === target.gateway)?.name; + context.devEuis = central.identities.devices().filter(d => d.gateway === target.gateway).map(d => d.devEui); + } + } + res.json(await ext.getStatusData(context)); + } catch (error) { next(error); } +}); + // API endpoint to invoke UI actions app.post('/api/actions/:moduleId/:actionId', async (req: Request, res: Response, next) => { try { diff --git a/src/services/socket-router.ts b/src/services/socket-router.ts index 4288025..7342477 100644 --- a/src/services/socket-router.ts +++ b/src/services/socket-router.ts @@ -14,7 +14,8 @@ export function registerSocket(server: http.Server, path: string, wss: WebSocket if (!routes) { routes = new Map(); servers.set(server, routes); server.on('upgrade', (request, socket, head) => { - const target = routes!.get(request.url || ''); + // Match on the path: a socket may carry its target in the query (/terminal?gateway=ID). + const target = routes!.get((request.url || '').split('?')[0]); 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/services/ssh.ts b/src/services/ssh.ts index a7609c1..35dcd81 100644 --- a/src/services/ssh.ts +++ b/src/services/ssh.ts @@ -1,7 +1,7 @@ import http from 'http'; import WebSocket from 'ws'; import { Client as SSHClient, ClientChannel, ConnectConfig } from 'ssh2'; -import { randomUUID } from 'crypto'; +import { randomBytes, randomUUID } from 'crypto'; import { registerSocket } from './socket-router'; import { createLogger } from '../utils/logger'; @@ -10,84 +10,150 @@ const logger = createLogger('[ssh]'); export interface TerminalOptions { connection?:(request:http.IncomingMessage)=>Promise; audit?:(request:http.IncomingMessage,event:'start'|'end',session:string)=>void; + /** Who may resume a session (central: the signed-in user and the tab's target). Standalone: anyone with the key. */ + owner?:(request:http.IncomingMessage)=>string; + /** How long a shell survives without a browser connection (default 10 minutes, TERMINAL_RESUME_MS). */ + resumeMs?:number; } + +/** A shell that outlives its browser connection for a while: phones freeze or drop background + * pages, and switching apps must not end the SSH session. Output is kept (bounded) until the + * page reconnects with the session's one-time resume key. */ +interface Session { + key:string; owner:string; id:string; request:http.IncomingMessage; + ssh:SSHClient; stream:ClientChannel; ws?:WebSocket; + buffer:Buffer[]; size:number; dropped:boolean; timer?:ReturnType; ended:boolean; +} +const BUFFER_LIMIT = 256 * 1024; + export function attachTerminal(httpServer: http.Server, target = { host: '127.0.0.1', port: 22 }, options:TerminalOptions={}): WebSocket.Server { const wss = new WebSocket.Server({ noServer: true, maxPayload: 64 * 1024 }); registerSocket(httpServer, '/terminal', wss); + const sessions = new Map(); + const resumeMs = options.resumeMs ?? (Number(process.env.TERMINAL_RESUME_MS) || 10 * 60000); + const ownerOf = (request:http.IncomingMessage) => options.owner?.(request) ?? 'local'; + const dimension = (value: unknown, fallback: number) => + typeof value === 'number' && Number.isInteger(value) && value >= 1 && value <= 500 ? value : fallback; + const send = (ws:WebSocket|undefined, message: string | Buffer) => { if (ws?.readyState === WebSocket.OPEN) ws.send(message); }; + + function endSession(session:Session, reason?:string) { + if (session.ended) return; + session.ended = true; clearTimeout(session.timer); sessions.delete(session.key); + session.stream.end(); session.ssh.end(); + if (session.ws?.readyState === WebSocket.OPEN) session.ws.close(1000, reason || 'Terminal session ended'); + options.audit?.(session.request, 'end', session.id); + } + function deliver(session:Session, data:Buffer) { + if (session.ws?.readyState === WebSocket.OPEN) { session.ws.send(data); return; } + // Detached: keep the newest output, up to the limit. + session.buffer.push(data); session.size += data.length; + while (session.size > BUFFER_LIMIT && session.buffer.length > 1) { session.size -= session.buffer.shift()!.length; session.dropped = true; } + } + function detach(session:Session, ws:WebSocket) { + if (session.ended || session.ws !== ws) return; + session.ws = undefined; + session.timer = setTimeout(() => endSession(session), resumeMs); session.timer.unref?.(); + } + /** Bind a (new) browser connection; the key changes every time, so an old key can't be replayed. */ + function attach(session:Session, ws:WebSocket, cols:number, rows:number, resumed:boolean) { + clearTimeout(session.timer); session.timer = undefined; + const previous = session.ws; session.ws = ws; + if (previous && previous !== ws && previous.readyState === WebSocket.OPEN) previous.close(1000, 'Terminal opened elsewhere'); + sessions.delete(session.key); session.key = randomBytes(32).toString('base64url'); sessions.set(session.key, session); + ws.send(JSON.stringify({ type: 'ready', resume: session.key, ...(resumed ? { resumed: true } : {}) })); + if (session.dropped) ws.send('\r\n[Some output from while you were away was not kept.]\r\n'); + for (const chunk of session.buffer) ws.send(chunk); + session.buffer = []; session.size = 0; session.dropped = false; + session.stream.setWindow(rows, cols, cols * 8, rows * 16); + } + wss.on('connection', (ws: WebSocket, request:http.IncomingMessage) => { logger.info('Terminal transport connected; waiting for SSH login'); - let ssh: SSHClient | undefined; - let stream: ClientChannel | undefined; - let started = false; - let shellTimer: ReturnType | undefined; - let alive = true; - const session=randomUUID();let audited=false; + let session: Session | undefined; + let pending: { ssh: SSHClient; stream?: ClientChannel } | undefined; + let started = false, audited = false, shellTimer: ReturnType | undefined, alive = true; + const id = randomUUID(); ws.on('pong', () => { alive = true; }); const heartbeat = setInterval(() => { if (!alive) { ws.terminate(); return; } alive = false; if (ws.readyState === WebSocket.OPEN) ws.ping(); }, 30000); heartbeat.unref(); - const send = (message: string | Buffer) => { if (ws.readyState === WebSocket.OPEN) ws.send(message); }; const timer = setTimeout(() => ws.close(1008, 'SSH credentials required'), 15000); - const cleanup = () => { clearInterval(heartbeat); clearTimeout(shellTimer); clearTimeout(timer); stream?.end(); ssh?.end(); - if(audited){audited=false;options.audit?.(request,'end',session);} }; + const cleanup = () => { + clearInterval(heartbeat); clearTimeout(shellTimer); clearTimeout(timer); + // A running shell waits for the page to come back; one still being set up is abandoned. + if (session) { detach(session, ws); return; } + pending?.stream?.end(); pending?.ssh.end(); + if (audited) { audited = false; options.audit?.(request, 'end', id); } + }; ws.on('close', cleanup); ws.on('error', cleanup); - const dimension = (value: unknown, fallback: number) => - typeof value === 'number' && Number.isInteger(value) && value >= 1 && value <= 500 ? value : fallback; ws.on('message', data => { if (!started) { - let auth; - try { auth = JSON.parse(data.toString()); } catch { ws.close(1008, 'Invalid authentication message'); return; } - if (auth?.type !== 'auth' || typeof auth.username !== 'string' || !auth.username || auth.username.length > 256 || - typeof auth.password !== 'string' || !auth.password || auth.password.length > 4096) { + let first; + try { first = JSON.parse(data.toString()); } catch { ws.close(1008, 'Invalid authentication message'); return; } + if (first?.type === 'resume') { + started = true; clearTimeout(timer); + const found = typeof first.key === 'string' ? sessions.get(first.key) : undefined; + if (!found || found.ended || found.owner !== ownerOf(request)) { + send(ws, JSON.stringify({ type: 'resume-failed' })); ws.close(1000, 'Terminal session ended'); return; + } + session = found; attach(found, ws, dimension(first.cols, 80), dimension(first.rows, 24), true); + return; + } + if (first?.type !== 'auth' || typeof first.username !== 'string' || !first.username || first.username.length > 256 || + typeof first.password !== 'string' || !first.password || first.password.length > 4096) { ws.close(1008, 'SSH username and password are required'); return; } started = true; clearTimeout(timer); - const cols = dimension(auth.cols, 80), rows = dimension(auth.rows, 24); - ssh = new SSHClient(); + const cols = dimension(first.cols, 80), rows = dimension(first.rows, 24); + const ssh = new SSHClient(); pending = { ssh }; ssh.on('ready', () => { - if (ws.readyState !== WebSocket.OPEN) { ssh?.end(); return; } - shellTimer = setTimeout(() => { send('SSH authenticated but the host did not open a shell.'); ws.close(); cleanup(); }, 10000); - ssh!.shell({ term: 'xterm', cols, rows }, (error, channel) => { + if (ws.readyState !== WebSocket.OPEN) { ssh.end(); return; } + shellTimer = setTimeout(() => { send(ws, 'SSH authenticated but the host did not open a shell.'); ws.close(); cleanup(); }, 10000); + ssh.shell({ term: 'xterm', cols, rows }, (error, channel) => { clearTimeout(shellTimer); if (error || ws.readyState !== WebSocket.OPEN) { - channel?.end(); send('Unable to open SSH shell.'); ws.close(); ssh?.end(); return; + channel?.end(); send(ws, 'Unable to open SSH shell.'); ws.close(); ssh.end(); return; } - stream = channel; - channel.on('data', send); - channel.stderr.on('data', send); - channel.on('close', () => { ws.close(); ssh?.end(); }); - channel.on('error', () => { send('SSH shell disconnected.'); ws.close(); }); - ws.send(JSON.stringify({ type: 'ready' })); + pending!.stream = channel; + const created: Session = { key: '', owner: ownerOf(request), id, request, ssh, stream: channel, buffer: [], size: 0, dropped: false, ended: false }; + channel.on('data', (chunk: Buffer) => deliver(created, chunk)); + channel.stderr.on('data', (chunk: Buffer) => deliver(created, chunk)); + channel.on('close', () => endSession(created)); + channel.on('error', () => { send(created.ws, 'SSH shell disconnected.'); endSession(created); }); + session = created; audited = false; // the session now reports its own end + attach(created, ws, cols, rows, false); }); }); - ssh.on('error', () => { send('SSH connection failed. Check your credentials and host availability.'); ws.close(); ssh?.end(); }); - ssh.on('close', () => ws.close()); + ssh.on('error', () => { send(ws, 'SSH connection failed. Check your credentials and host availability.'); ws.close(); ssh.end(); }); + ssh.on('close', () => { if (session) endSession(session); else ws.close(); }); const connect=async()=>{ try { const connection:ConnectConfig=options.connection?await options.connection(request):target; if(ws.readyState!==WebSocket.OPEN){connection.sock?.destroy();return;} - if(options.audit){options.audit(request,'start',session);audited=true;} - ssh!.connect({ ...connection, keepaliveInterval: 10000, keepaliveCountMax: 3, username: auth.username, password: auth.password, readyTimeout: 15000 }); - } catch { send(options.connection?'Gateway SSH is unavailable. Select a connected gateway and verify its SSH host key.':'Unable to connect to SSH.');ws.close();cleanup(); } + if(options.audit){options.audit(request,'start',id);audited=true;} + ssh.connect({ ...connection, keepaliveInterval: 10000, keepaliveCountMax: 3, username: first.username, password: first.password, readyTimeout: 15000 }); + } catch { send(ws, options.connection?'Gateway SSH is unavailable. Select a connected gateway and verify its SSH host key.':'Unable to connect to SSH.');ws.close();cleanup(); } }; void connect(); return; } - if (!stream) return; + if (!session || session.ws !== ws) return; try { const control = JSON.parse(data.toString()); if (control.type === 'resize') { const cols = dimension(control.cols, 80), rows = dimension(control.rows, 24); - stream.setWindow(rows, cols, cols * 8, rows * 16); + session.stream.setWindow(rows, cols, cols * 8, rows * 16); return; } + // The user closed Terminal or disconnected: end now instead of waiting to be resumed. + if (control.type === 'end') { endSession(session); return; } } catch { /* Terminal keystrokes are ordinary text, not control messages. */ } - if (stream.writable) stream.write(data.toString()); + if (session.stream.writable) session.stream.write(data.toString()); }); }); - httpServer.once('close', () => { for (const client of wss.clients) client.terminate(); wss.close(); }); + httpServer.once('close', () => { for (const s of [...sessions.values()]) endSession(s); for (const client of wss.clients) client.terminate(); wss.close(); }); return wss; } diff --git a/src/types.ts b/src/types.ts index 72418f0..217f244 100644 --- a/src/types.ts +++ b/src/types.ts @@ -143,6 +143,7 @@ export interface UIAction { * UI Hooks interface for extending the console UI * Modules can implement this to add icons, windows, and actions to the web interface */ +export type UIStatusContext = { gateway?: string; gatewayName?: string; devEuis?: string[] }; export type UIHooks = { /** Return an app icon to display on the desktop */ getAppIcon?: () => UIAppIcon | Promise; @@ -150,6 +151,6 @@ export type UIHooks = { getAppWindow?: () => UIAppWindow | Promise; /** Return available actions for this module */ getActions?: () => UIAction[] | Promise; - /** Return metadata/status data for display */ - getStatusData?: () => Record | Promise>; + /** Return metadata/status data for display. On central, scoped to the tab's gateway when one is selected. */ + getStatusData?: (context?: UIStatusContext) => Record | Promise>; }; diff --git a/src/utils/dns-watch.ts b/src/utils/dns-watch.ts new file mode 100644 index 0000000..3050ed1 --- /dev/null +++ b/src/utils/dns-watch.ts @@ -0,0 +1,27 @@ +import fs from 'fs'; + +/** Host-network containers get a copy of the host's /etc/resolv.conf when they start, and keep it. + * If Docker starts them before the host's network has its DNS server (for example Wi-Fi that comes + * up minutes after boot), the copy is empty and every lookup fails until the container restarts. */ +export function hasNameserver(file = '/etc/resolv.conf'): boolean { + try { return /^\s*nameserver\s+\S+/m.test(fs.readFileSync(file, 'utf8')); } + catch { return true; } // No file to inspect (not in a container): nothing to repair. +} + +export interface ResolverWatchOptions { file?: string; afterMs?: number; everyMs?: number; now?: () => number } + +/** Calls onStuck once when the DNS configuration has been empty for afterMs (default 3 minutes, + * CONSOLE_DNS_RESTART_MS). The caller exits so Docker restarts it with a fresh copy. */ +export function watchResolver(onStuck: () => void, options: ResolverWatchOptions = {}): () => void { + const file = options.file ?? '/etc/resolv.conf', now = options.now ?? Date.now; + const afterMs = options.afterMs ?? (Number(process.env.CONSOLE_DNS_RESTART_MS) || 180000); + let since: number | null = null; + const check = () => { + if (hasNameserver(file)) { since = null; return; } + since ??= now(); + if (now() - since >= afterMs) { clearInterval(timer); onStuck(); } + }; + const timer = setInterval(check, options.everyMs ?? 30000); timer.unref(); + check(); + return () => clearInterval(timer); +} diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 6d20ad3..57b8108 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -16,7 +16,10 @@ function managedLog(value:unknown,depth=0):unknown { if(typeof value==='string'){ let text=value; for(const [name,secret] of Object.entries(process.env)){ - if(secret&&/(?:PASSWORD|SECRET|TOKEN|PRIVATE_KEY|CREDENTIAL)/i.test(name))text=text.split(secret).join('[redacted]'); + // Very short values (such as a template's "...") are not real secrets and would mangle ordinary text. + if(secret&&secret.length>=8&&/(?:PASSWORD|SECRET|TOKEN|PRIVATE_KEY|CREDENTIAL)/i.test(name))text=text.split(secret).join('[redacted]'); + // RPC providers often put an API key in the URL path or query; plain public endpoints stay readable. + else if(secret&&/RPC/i.test(name)&&/^https?:\/\/[^/]+\/./i.test(secret))text=text.split(secret).join('[rpc url redacted]'); } return text.replace(/(?:0x)?[a-fA-F0-9]{64,}/g,'[redacted bytes]') .replace(/[A-Za-z0-9+/]{80,}={0,2}/g,'[redacted payload]') diff --git a/src/utils/report-log.ts b/src/utils/report-log.ts new file mode 100644 index 0000000..adb7f5d --- /dev/null +++ b/src/utils/report-log.ts @@ -0,0 +1,49 @@ +import { uplinkPayload } from '../lib/uplink-payload'; +import { decodePayload } from '../lib/decode'; + +/** One readable line per meter report and per state reply, for `docker compose logs`. + * Only meter readings and reply contents: never keys, tokens or the full signature. */ + +type Uplink = { deviceInfo?: { devEui?: unknown }; fCnt?: unknown; fPort?: unknown; deduplicationId?: unknown; + data?: unknown; dataHex?: unknown; dataEncoding?: unknown }; + +const short = (hex: string) => hex.length > 16 ? `${hex.slice(0, 8)}…${hex.slice(-8)}` : hex; + +export function describeReport(event: Uplink): string { + const parts = [`devEui=${String(event.deviceInfo?.devEui ?? '?')}`]; + if (typeof event.fCnt === 'number') parts.push(`fCnt=${event.fCnt}`); + if (typeof event.fPort === 'number') parts.push(`fPort=${event.fPort}`); + if (typeof event.deduplicationId === 'string') parts.push(`id=${event.deduplicationId}`); + try { + const bytes = uplinkPayload(event).bytes; + // Only fPort 2 carries signed readings; other ports use their own formats. + if (event.fPort !== undefined && event.fPort !== 2) { + parts.push(`data=${short(bytes.toString('hex').toUpperCase())}`, `bytes=${bytes.length}`); + return parts.join(' '); + } + try { + const reading = decodePayload(bytes), ext = reading.extensions; + parts.push(`nonce=${reading.nonce}`, `energy=${reading.energy}kWh`); + if (ext.voltage !== undefined) parts.push(`voltage=${ext.voltage}V`); + if (ext.latitude !== undefined && ext.longitude !== undefined) parts.push(`location=${ext.latitude},${ext.longitude}`); + if (ext.deviceId) parts.push(`meterKey=${short(ext.deviceId)}`); + parts.push(`signature=${short(reading.signature)}`); + } catch { parts.push('reading=undecodable'); } + parts.push(`bytes=${bytes.length}`); + } catch { parts.push('payload=missing or invalid'); } + return parts.join(' '); +} + +/** State reply: is_on(1) [nonce(4)] latitude(f32) longitude(f32). */ +export function describeReply(bytes: number[]): string { + const buf = Buffer.from(bytes), parts = [`state=${buf[0] === 1 ? 'ON' : buf[0] === 0 ? 'OFF' : `?(${buf[0]})`}`]; + if (buf.length === 13) parts.push(`nonce=${buf.readUInt32BE(1)}`); + if (buf.length === 9 || buf.length === 13) { + const at = buf.length - 8; + parts.push(`location=${+buf.readFloatBE(at).toFixed(5)},${+buf.readFloatBE(at + 4).toFixed(5)}`); + } + parts.push(`hex=${buf.toString('hex').toUpperCase()}`); + return parts.join(' '); +} + +export const errorText = (error: unknown) => error instanceof Error ? error.message : String(error); diff --git a/src/views/layouts/main.hbs b/src/views/layouts/main.hbs index 5d175d4..4e1c954 100644 --- a/src/views/layouts/main.hbs +++ b/src/views/layouts/main.hbs @@ -25,6 +25,7 @@ + {{#if central}}{{/if}} diff --git a/tests/services/agent-runtime.test.ts b/tests/services/agent-runtime.test.ts index 502f37e..9470e63 100644 --- a/tests/services/agent-runtime.test.ts +++ b/tests/services/agent-runtime.test.ts @@ -48,7 +48,11 @@ test('agent persists before MQTT ack, filters ports, reconnects its own session client.emit('connect',{});client.emit('reconnect');client.emit('connect',{}); expect(client.subscribe).toHaveBeenCalledTimes(2);expect(client.subscribe).toHaveBeenLastCalledWith(`application/${application}/device/+/event/up`,{qos:1},expect.any(Function)); const packet=(value:unknown)=>({cmd:'publish' as const,qos:1 as const,retain:false,dup:false,messageId:1,topic:`application/${application}/device/${event.deviceInfo.devEui}/event/up`,payload:Buffer.from(JSON.stringify(value))}); - const ignored=jest.fn();client.handleMessage(packet({...event,fPort:4}),ignored);expect(ignored).toHaveBeenCalledWith();expect(compute).not.toHaveBeenCalled(); + // Other ports are acknowledged and dropped without a log line. + const logged=jest.spyOn(console,'log');const before=logged.mock.calls.length; + const ignored=jest.fn();client.handleMessage(packet({...event,fPort:4}),ignored);client.handleMessage(packet({...event,fPort:0,data:undefined}),ignored); + expect(ignored).toHaveBeenCalledTimes(2);expect(ignored).toHaveBeenCalledWith();expect(compute).not.toHaveBeenCalled(); + expect(logged.mock.calls.slice(before).filter(call=>/fPort|skipped|Report/.test(String(call[0])))).toEqual([]); for(const malformed of [null,[],42,{fPort:2,deviceInfo:{devEui:42,applicationId:application}}]){const ack=jest.fn();client.handleMessage(packet(malformed),ack);expect(ack).toHaveBeenCalledWith();} expect(client.stream.destroy).not.toHaveBeenCalled(); let acked=false;client.handleMessage(packet(event),error=>{ diff --git a/tests/services/billing-import.test.ts b/tests/services/billing-import.test.ts new file mode 100644 index 0000000..bab776b --- /dev/null +++ b/tests/services/billing-import.test.ts @@ -0,0 +1,34 @@ +jest.mock('../../src/store/sqlite', () => ({ getMeterByPublicKey: jest.fn(), mergeBillingSnapshot: jest.fn(), readBillingSnapshot: jest.fn() })); +jest.mock('../../src/utils/contracts', () => ({ meterIdFromPublicKeyContractCall: jest.fn(async () => 7), meterLatestNonceContractCall: jest.fn(async () => 100) })); +jest.mock('../../src/lib/utils', () => ({ verifyPayloadSignature: jest.fn(() => true), distributeManaged: jest.fn(async () => {}) })); +jest.mock('../../src/services/mqtt', () => ({ handleMessage: jest.fn() })); +import { importBillingSnapshot } from '../../src/central/billing'; +import { getMeterByPublicKey, mergeBillingSnapshot } from '../../src/store/sqlite'; +import { verifyPayloadSignature } from '../../src/lib/utils'; +import type { BillingSnapshot } from '../../src/central/processor'; + +const publicKey = `0x${'a'.repeat(64)}`; +const record = (nonce: number) => { const raw = Buffer.alloc(74); raw.writeUInt32BE(nonce, 0); return { nonce, identifier: 7, receivedAt: 1, raw: raw.toString('hex') }; }; +const snapshot = (...nonces: number[]): BillingSnapshot => ({ meter: { devEui: 'fadf417b946e25ef', publicKey, tokenId: 7, latestNonce: Math.max(...nonces) }, transactions: nonces.map(record) }); +beforeEach(() => { jest.clearAllMocks(); jest.spyOn(console, 'log').mockImplementation(() => {}); }); +afterEach(() => jest.restoreAllMocks()); + +test('a meter new to central starts its history at the oldest signed record, then gaps are refused again', async () => { + (getMeterByPublicKey as jest.Mock).mockReturnValue(undefined); + // The chain has nonce 100; the gateway, which ran standalone first, replays 1246. + await importBillingSnapshot(snapshot(1246)); + expect(mergeBillingSnapshot).toHaveBeenCalledTimes(1); + // Now central knows the meter at 1246: the next record must follow on. + (getMeterByPublicKey as jest.Mock).mockReturnValue({ publicKey, tokenId: 7, devEui: 'fadf417b946e25ef', latestNonce: 1246 }); + await importBillingSnapshot(snapshot(1247)); + await expect(importBillingSnapshot(snapshot(1250))).rejects.toThrow('gap before nonce 1250 (central can confirm up to 1246)'); + expect(mergeBillingSnapshot).toHaveBeenCalledTimes(2); +}); + +test('first contact still checks signatures and the token', async () => { + (getMeterByPublicKey as jest.Mock).mockReturnValue(undefined); + (verifyPayloadSignature as jest.Mock).mockReturnValueOnce(false); + await expect(importBillingSnapshot(snapshot(1246))).rejects.toThrow('Invalid signed reconciliation record'); + await expect(importBillingSnapshot({ ...snapshot(1246), meter: { ...snapshot(1246).meter!, tokenId: 8 } })).rejects.toThrow('token does not match'); + expect(mergeBillingSnapshot).not.toHaveBeenCalled(); +}); diff --git a/tests/services/central-link.test.ts b/tests/services/central-link.test.ts index 0f064fa..0c78221 100644 --- a/tests/services/central-link.test.ts +++ b/tests/services/central-link.test.ts @@ -76,19 +76,20 @@ test('rename updates the list; delete disconnects the agent and removes the gate }finally{await f.close();} }); -test('selecting null clears the gateway so apps target the central server',async()=>{ +test('each tab names its own target: central, an available gateway, or nothing that has gone',async()=>{ const f=await fixture(); try{ - const post=(body:unknown)=>fetch(f.base+'/api/gateways/select',{method:'POST',headers:{cookie:f.cookie,origin:f.base,'Content-Type':'application/json'},body:JSON.stringify(body)}); - const chosen=await post({id:f.enrolled.gateway}); - expect(chosen.status).toBe(200);expect(chosen.headers.get('set-cookie')).toContain(`console_gateway=${f.enrolled.gateway}`); - const cleared=await post({id:null}); - expect(cleared.status).toBe(200);expect(await cleared.json()).toEqual({selected:null}); - expect(cleared.headers.get('set-cookie')).toMatch(/^console_gateway=; Path=\/; Max-Age=0; HttpOnly; SameSite=Strict/); - const list=await (await fetch(f.base+'/api/gateways',{headers:{cookie:f.cookie}})).json() as {selected:unknown}; - expect(list.selected).toBeNull(); - expect((await post({id:'not-a-gateway'})).status).toBe(400); - expect((await post({})).status).toBe(400); + expect(f.hub.target(null)).toEqual({gateway:null}); + expect(f.hub.target('central')).toEqual({gateway:null}); + expect(f.hub.target(f.enrolled.gateway)).toEqual({gateway:f.enrolled.gateway}); + expect(f.hub.target('not-a-gateway')).toBeNull(); + expect(f.hub.socketTarget({url:`/terminal?gateway=${f.enrolled.gateway}`} as never)).toEqual({gateway:f.enrolled.gateway}); + expect(f.hub.socketTarget({url:'/terminal'} as never)).toEqual({gateway:null}); + // There is no shared, cookie-based selection any more. + const list=await (await fetch(f.base+'/api/gateways',{headers:{cookie:f.cookie}})).json() as Record; + expect(list).not.toHaveProperty('selected'); + expect((await fetch(f.base+'/api/gateways/select',{method:'POST',headers:{cookie:f.cookie,origin:f.base,'Content-Type':'application/json'},body:'{"id":null}'})).status).toBe(404); + f.store.revoke(f.enrolled.gateway,'test');expect(f.hub.target(f.enrolled.gateway)).toBeNull(); }finally{await f.close();} }); diff --git a/tests/services/chirpstack.test.ts b/tests/services/chirpstack.test.ts index 8707818..8e24134 100644 --- a/tests/services/chirpstack.test.ts +++ b/tests/services/chirpstack.test.ts @@ -54,3 +54,11 @@ test('a direct dashboard origin is proxied without a tunnel agent', async () => expect(await response.text()).toBe('dashboard /tenants cookie=none'); } finally { server.close(); upstream.close(); } }); + +test('a per-target base keeps asset and link rewrites under that target on central', () => { + const base = '/chirpstack/~gateway-1'; + const html = rewriteChirpstackText('t', true, base); + expect(html).toContain('src="/chirpstack/~gateway-1/assets/app.js"'); + expect(html).toContain('href="/chirpstack/~gateway-1/tenants"'); + expect(rewriteChirpstackText('import("/assets/chunk.js")', false, base)).toBe('import("/chirpstack/~gateway-1/assets/chunk.js")'); +}); diff --git a/tests/services/deployment.test.ts b/tests/services/deployment.test.ts index fec1a18..b3cdfdf 100644 --- a/tests/services/deployment.test.ts +++ b/tests/services/deployment.test.ts @@ -28,7 +28,9 @@ esac expect(calls).toContain('pull console');expect(calls).toContain('stop console');expect(calls).toContain('rm --force console'); expect(calls).not.toMatch(/down|--volumes|system prune|build console/); expect(calls.indexOf('pull console')).toBeLessThan(calls.indexOf('stop console')); - expect(fs.readFileSync(path.join(dir,'.env'),'utf8')).toBe(environment); + // Only the two role lines are added on success, so plain `docker compose logs -f` follows this role. + expect(fs.readFileSync(path.join(dir,'.env'),'utf8')).toBe(fail?environment:environment+"# Set by ./auto-compose.sh: plain 'docker compose logs -f' follows the role running here.\nCOMPOSE_FILE=compose.yaml\nCOMPOSE_PROJECT_NAME=existing\n"); + if(!fail)expect(run.stdout).toContain("Logs: run 'docker compose logs -f' here"); expect(fs.readFileSync(path.join(dir,'.data','sentinel'),'utf8')).toBe('keep'); if(fail){expect(calls).toContain('logs --tail 80 console');expect(calls).toContain('--force-recreate console');expect(calls).not.toContain('image rm');} else expect(calls).toContain('image rm sha256:old'); @@ -131,7 +133,7 @@ test('gateway role enrolls: secrets are owner-only, standalone stops before Cons 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(env).toBe(environment+"# Set by ./auto-compose.sh: plain 'docker compose logs -f' follows the role running here.\nCOMPOSE_FILE=compose.gateway.yaml\nCOMPOSE_PROJECT_NAME=site\n"); 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', () => { @@ -279,3 +281,19 @@ test('GATEWAY_FALLBACK_PORT in .env moves the check to that port',()=>{ expect(runGateway(gatewayEnv+'GATEWAY_FALLBACK_PORT=3101\n',{credential:true,busyPort:'3011'}).run.status).toBe(0); expect(runGateway(gatewayEnv+'GATEWAY_FALLBACK_PORT=3101\n',{credential:true,busyPort:'3101'}).run.status).toBe(2); }); + +test('the role lines in .env are updated in place, keep the file owner-only, and are not duplicated', () => { + const dir=fs.mkdtempSync(path.join(os.tmpdir(),'console-role-')); + 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'),'CONSOLE_ROLE=central\nCENTRAL_DOMAIN=console.example.test\nCOMPOSE_FILE=compose.yaml\n',{mode:0o600}); + fs.mkdirSync(path.join(dir,'.data'));fs.writeFileSync(path.join(dir,'.data','central.db'),'db'); + fs.writeFileSync(path.join(dir,'docker'),"#!/bin/bash\ncase \"$*\" in *'ps -q console'*) echo new-container ;; esac\n",{mode:0o755}); + const run=()=>spawnSync('bash',['auto-compose.sh','--project-name','central'],{cwd:dir,encoding:'utf8',env:{...process.env,PATH:dir+':'+process.env.PATH}}); + expect(run().status).toBe(0);expect(run().status).toBe(0); + const env=fs.readFileSync(path.join(dir,'.env'),'utf8'); + expect(env.match(/^COMPOSE_FILE=/gm)).toHaveLength(1);expect(env).toContain('COMPOSE_FILE=compose.central.yaml'); + expect(env.match(/^COMPOSE_PROJECT_NAME=central$/gm)).toHaveLength(1); + expect(fs.statSync(path.join(dir,'.env')).mode&0o777).toBe(0o600); + } finally {fs.rmSync(dir,{recursive:true,force:true});} +}); diff --git a/tests/services/dns-watch.test.ts b/tests/services/dns-watch.test.ts new file mode 100644 index 0000000..50f3860 --- /dev/null +++ b/tests/services/dns-watch.test.ts @@ -0,0 +1,22 @@ +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import { hasNameserver, watchResolver } from '../../src/utils/dns-watch'; + +test('an empty DNS configuration triggers a restart only after it stays empty, and recovers if it fills in', () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dns-watch-')), file = path.join(dir, 'resolv.conf'); + jest.useFakeTimers(); + try { + fs.writeFileSync(file, '\n\n'); + expect(hasNameserver(file)).toBe(false); + let clock = 0; const stuck = jest.fn(); + watchResolver(stuck, { file, afterMs: 180000, everyMs: 30000, now: () => clock }); + clock = 120000; jest.advanceTimersByTime(120000); expect(stuck).not.toHaveBeenCalled(); + fs.writeFileSync(file, 'nameserver 10.61.29.144\n'); // the host got DNS and Docker refreshed it + clock = 300000; jest.advanceTimersByTime(180000); expect(stuck).not.toHaveBeenCalled(); + fs.writeFileSync(file, ''); // empty again: the 3 minutes start over + clock = 330000; jest.advanceTimersByTime(30000); + clock = 510000; jest.advanceTimersByTime(180000); expect(stuck).toHaveBeenCalledTimes(1); + expect(hasNameserver(path.join(dir, 'missing'))).toBe(true); // nothing to inspect: never restart + } finally { jest.useRealTimers(); fs.rmSync(dir, { recursive: true, force: true }); } +}); diff --git a/tests/services/publication-policy.test.ts b/tests/services/publication-policy.test.ts new file mode 100644 index 0000000..0fd45b2 --- /dev/null +++ b/tests/services/publication-policy.test.ts @@ -0,0 +1,37 @@ +import { configurePublications, publishManaged } from '../../src/central/distribution'; +import type { TransactionRecord } from '../../src/types'; + +const record = (nonce: number): TransactionRecord => ({ identifier: 7, nonce, receivedAt: 1, raw: '' }); +const role = process.env.CONSOLE_ROLE; +afterEach(() => { if (role === undefined) delete process.env.CONSOLE_ROLE; else process.env.CONSOLE_ROLE = role; }); + +test('central resends to the prover and Streamr every run, but uploads to Arweave and runs each module hook once', async () => { + process.env.CONSOLE_ROLE = 'central'; configurePublications(':memory:'); + const send = jest.fn(async () => 'sent'); + for (const destination of ['prover:https://prover.example', 'streamr:stream/a']) { + expect(await publishManaged(destination, [record(1)], send)).toBe('sent'); + expect(await publishManaged(destination, [record(1)], send)).toBe('sent'); // not confirmed on-chain yet: send again + } + for (const destination of ['arweave', 'hook:core/prover', 'hook:core/arweave']) { + expect(await publishManaged(destination, [record(1)], send)).toBe('sent'); + expect(await publishManaged(destination, [record(1)], send)).toBeNull(); // once per reading + } + expect(send).toHaveBeenCalledTimes(7); +}); + +test('a managed gateway sends only to the prover (including through the module hook), without keeping a record', async () => { + process.env.CONSOLE_ROLE = 'gateway'; + const send = jest.fn(async () => 'sent'); + expect(await publishManaged('hook:core/prover', [record(1)], send)).toBe('sent'); + expect(await publishManaged('hook:core/prover', [record(1)], send)).toBe('sent'); + expect(await publishManaged('prover:https://prover.example', [record(1)], send)).toBe('sent'); + for (const destination of ['arweave', 'hook:core/arweave', 'streamr:stream/a', 'hook:core/streamr']) + expect(await publishManaged(destination, [record(1)], send)).toBeNull(); + expect(send).toHaveBeenCalledTimes(3); +}); + +test('standalone always sends', async () => { + delete process.env.CONSOLE_ROLE; + const send = jest.fn(async () => 'sent'); + for (const destination of ['arweave', 'prover:x', 'streamr:y', 'hook:core/prover']) expect(await publishManaged(destination, [record(1)], send)).toBe('sent'); +}); diff --git a/tests/services/reconciliation.test.ts b/tests/services/reconciliation.test.ts index 5da5fc5..e944379 100644 --- a/tests/services/reconciliation.test.ts +++ b/tests/services/reconciliation.test.ts @@ -67,3 +67,52 @@ test('reconciliation cannot attach another device decision or meter snapshot to expect(await processor.outcome('gateway',report.deduplicationId!)).toBeNull(); }finally{processor.close();} }); + +test('when central cannot take a report the agent logs why, including a failed replay of earlier decisions',async()=>{ + const journal=new OperationJournal(':memory:'); + const central=new DurableProcessor(':memory:',async()=>{},()=>snapshot,async()=>{}); + const local=new DurableProcessor(':memory:',async(_event,sink)=>sink(decision),()=>snapshot); + jest.spyOn(central,'reconcile').mockRejectedValue(new Error('meter token does not match the contract')); + let healthy=false;const messages:string[]=[]; + const replication=new Replication(journal,central,local); + const queue={list:jest.fn(async()=>[]),enqueue:jest.fn(async()=>'queue-1')}; + const coordinator=new UplinkCoordinator(journal,replication.guarded('central'),replication.guarded('local'),queue, + {centralHealthy:()=>healthy,timeoutMs:50,reconcileSnapshot:(owner,outcome)=>replication.observe(owner,outcome),log:message=>messages.push(message)}); + try{ + await coordinator.receive('gateway',report); + expect(messages).toEqual([`Report ${report.deduplicationId}: central link is not ready (offline or connected under 15 s); this gateway decides`]); + healthy=true;messages.length=0; + await coordinator.receive('gateway',{...report,deduplicationId:'replay-2'}); + expect(messages).toEqual(['Report replay-2: central did not take it (Decider unavailable: replaying 1 report(s) decided elsewhere failed: meter token does not match the contract); this gateway decides']); + expect(journal.get('replay-2')?.owner).toBe('local'); + }finally{await coordinator.stop();journal.close();central.close();local.close();} +}); + +test('after central sends a reply and then goes away, the gateway decides the next report; an unfinished one still waits',async()=>{ + const journal=new OperationJournal(':memory:'); + const decision2={...decision}; + const central={offer:jest.fn(async()=>{}),commit:jest.fn(async(_g:string,id:string)=>({id,state:'decided' as const,active:false,decision:decision2,snapshot:null})), + outcome:jest.fn(async(_g:string,id:string)=>{if(down)throw new Error('central unreachable');return {id,state:'decided' as const,active:false,decision:decision2,snapshot:null};}),reconcile:jest.fn(async()=>{})}; + const local={offer:jest.fn(async()=>{}),commit:jest.fn(async(_g:string,id:string)=>({id,state:'decided' as const,active:false,decision:decision2,snapshot:null})), + outcome:jest.fn(async(_g:string,id:string)=>({id,state:'decided' as const,active:false,decision:decision2,snapshot:null})),reconcile:jest.fn(async()=>{})}; + let healthy=true,down=false; + const queue={list:jest.fn(async()=>[]),enqueue:jest.fn(async()=>`q-${queue.enqueue.mock.calls.length}`)}; + const coordinator=new UplinkCoordinator(journal,central,local,queue,{centralHealthy:()=>healthy,timeoutMs:50}); + const event=(id:string)=>({...report,deduplicationId:id}); + try{ + await coordinator.receive('gateway',event('a')); + expect(journal.get('a')).toMatchObject({owner:'central',state:'enqueued'}); + healthy=false;down=true;central.outcome.mockClear(); + await coordinator.receive('gateway',event('b')); + expect(journal.get('b')).toMatchObject({owner:'local',state:'enqueued'}); + expect(central.outcome).not.toHaveBeenCalled(); + // A prior report central owns but has not finished must still be asked about first. + healthy=true;down=false;central.commit.mockRejectedValueOnce(new Error('lost reply')); + await coordinator.receive('gateway',event('c')); + expect(journal.get('c')?.state).toBe('uncertain'); + healthy=false;down=true; + await coordinator.receive('gateway',event('d')); + expect(central.outcome).toHaveBeenCalled(); + expect(journal.get('d')?.owner).toBeNull(); + }finally{await coordinator.stop();journal.close();} +}); diff --git a/tests/services/report-log.test.ts b/tests/services/report-log.test.ts new file mode 100644 index 0000000..03ec7bc --- /dev/null +++ b/tests/services/report-log.test.ts @@ -0,0 +1,59 @@ +import { describeReport, describeReply } from '../../src/utils/report-log'; +import { encodeTransaction, encode } from '../../src/lib/encode'; +import { createLogger } from '../../src/utils/logger'; + +const reading = Buffer.from(encodeTransaction({ nonce: 42, energy: 1.5, signature: 'ab'.repeat(64), voltage: 230.4, + deviceId: 'cd'.repeat(32), longitude: 7.49508, latitude: 9.05785 })); +const event = { deviceInfo: { devEui: '0011223344556677' }, fCnt: 17, fPort: 2, deduplicationId: 'dedup-1', data: reading.toString('base64') }; + +test('a report is described by its reading, with the signature and meter key shortened', () => { + const text = describeReport(event); + expect(text).toContain('devEui=0011223344556677'); + expect(text).toContain('fCnt=17'); + expect(text).toContain('nonce=42'); + expect(text).toContain('energy=1.5kWh'); + expect(text).toContain('voltage=230.4V'); + expect(text).toContain('location=9.05785,7.49508'); + expect(text).toContain('signature=abababab…abababab'); + expect(text).toContain('bytes=112'); + expect(text).not.toContain('ab'.repeat(20)); + expect(describeReport({ ...event, data: 'AAAA' })).toContain('reading=undecodable'); + expect(describeReport({ deviceInfo: { devEui: 'x' } })).toContain('payload=missing or invalid'); + // Other ports have their own formats: shown as raw bytes, never as fake readings. + const other = describeReport({ ...event, fPort: 4 }); + expect(other).toContain('fPort=4');expect(other).toContain('bytes=112');expect(other).toMatch(/data=[0-9A-F]{8}…[0-9A-F]{8}/); + expect(other).not.toContain('nonce=');expect(other).not.toContain('energy='); +}); + +test('replies are described by state, nonce and location', () => { + expect(describeReply(encode({ is_on: true }, 9.5, 7.25))).toBe('state=ON location=9.5,7.25 hex=014118000040E80000'); + expect(describeReply(encode({ is_on: false, nonce: 41 } as never, 0, 0))).toMatch(/^state=OFF nonce=41 location=0,0 hex=/); +}); + +test('managed logs keep these lines readable instead of redacting them', () => { + const role = process.env.CONSOLE_ROLE, spy = jest.spyOn(console, 'log').mockImplementation(() => {}); + try { + process.env.CONSOLE_ROLE = 'gateway'; + createLogger('[agent]').info(`Report received from ChirpStack: ${describeReport(event)}`); + const line = String(spy.mock.calls[0][0]); + expect(line).toContain('nonce=42');expect(line).toContain('energy=1.5kWh');expect(line).not.toContain('[redacted'); + } finally { spy.mockRestore(); if (role === undefined) delete process.env.CONSOLE_ROLE; else process.env.CONSOLE_ROLE = role; } +}); + +test('a template placeholder such as API_TOKEN=... does not redact ordinary text, real secrets still are', () => { + const saved = { ...process.env }, spy = jest.spyOn(console, 'log').mockImplementation(() => {}); + try { + Object.assign(process.env, { CONSOLE_ROLE: 'central', API_TOKEN: '...', CENTRAL_ADMIN_PASSWORD: 'a-real-password-1' }); + createLogger('[index]').info('Starting application initialization... with a-real-password-1'); + expect(String(spy.mock.calls[0][0])).toBe('[info] [index] Starting application initialization... with [redacted]'); + } finally { spy.mockRestore(); process.env = saved; } +}); + +test('RPC addresses with a key in the path are hidden; plain public endpoints stay readable', () => { + const saved = { ...process.env }, spy = jest.spyOn(console, 'log').mockImplementation(() => {}); + try { + Object.assign(process.env, { CONSOLE_ROLE: 'gateway', MAINNET_RPC: 'https://mainnet.example.io/v3/abc123key', ARBITRUM_RPC: 'https://arb.example.org' }); + createLogger('[x]').info('failed https://mainnet.example.io/v3/abc123key and https://arb.example.org'); + expect(String(spy.mock.calls[0][0])).toBe('[info] [x] failed [rpc url redacted] and https://arb.example.org'); + } finally { spy.mockRestore(); process.env = saved; } +}); diff --git a/tests/services/ssh.test.ts b/tests/services/ssh.test.ts index 9c33276..42f4dc1 100644 --- a/tests/services/ssh.test.ts +++ b/tests/services/ssh.test.ts @@ -38,7 +38,65 @@ test('terminal-ready handshake is emitted only after the shell exists', () => { expect(ws.send).not.toHaveBeenCalled(); const channel: any = new EventEmitter(); channel.stderr = new EventEmitter(); channel.end = jest.fn(); channel.setWindow = jest.fn(); ssh.shell.mockImplementation((_, done) => done(null, channel)); - ssh.emit('ready'); expect(ws.send).toHaveBeenCalledWith(JSON.stringify({ type: 'ready' })); + ssh.emit('ready'); expect(JSON.parse(ws.send.mock.calls[0][0])).toEqual({ type: 'ready', resume: expect.stringMatching(/^[\w-]{43}$/) }); ws.emit('message', Buffer.from(JSON.stringify({ type: 'resize', cols: 9000, rows: -1 }))); expect(channel.setWindow).toHaveBeenCalledWith(24, 80, 640, 384); }); + +function socket() { + const next: any = new EventEmitter(); next.readyState = WebSocket.OPEN; next.send = jest.fn(); + next.close = jest.fn(() => { next.readyState = WebSocket.CLOSED; next.emit('close'); }); + bridge.emit('connection', next); return next; +} +function startShell() { + const channel: any = new EventEmitter(); channel.stderr = new EventEmitter(); channel.end = jest.fn(); channel.setWindow = jest.fn(); + channel.write = jest.fn(); channel.writable = true; + ssh.shell.mockImplementation((_: unknown, done: (e: null, c: unknown) => void) => done(null, channel)); + ws.emit('message', Buffer.from(JSON.stringify({ type: 'auth', username: 'user', password: 'test-frame-only' }))); + ssh.emit('ready'); + return { channel, key: JSON.parse(ws.send.mock.calls[0][0]).resume as string }; +} +test('a dropped connection keeps the shell; the page resumes it, gets what it missed, and the key changes', () => { + const { channel, key } = startShell(); + ws.readyState = WebSocket.CLOSED; ws.emit('close'); // phone put the page in the background + expect(ssh.end).not.toHaveBeenCalled(); expect(channel.end).not.toHaveBeenCalled(); + channel.emit('data', Buffer.from('output while away')); + const next = socket(); + next.emit('message', Buffer.from(JSON.stringify({ type: 'resume', key, cols: 100, rows: 30 }))); + const ready = JSON.parse(next.send.mock.calls[0][0]); + expect(ready).toMatchObject({ type: 'ready', resumed: true }); expect(ready.resume).not.toBe(key); + expect(next.send).toHaveBeenCalledWith(Buffer.from('output while away')); + expect(channel.setWindow).toHaveBeenLastCalledWith(30, 100, 800, 480); + next.emit('message', Buffer.from('ls\r')); expect(channel.write).toHaveBeenCalledWith('ls\r'); + // The old key no longer works. + const replay = socket(); replay.emit('message', Buffer.from(JSON.stringify({ type: 'resume', key }))); + expect(replay.send).toHaveBeenCalledWith(JSON.stringify({ type: 'resume-failed' })); + // Ending on purpose closes the shell at once. + next.emit('message', Buffer.from(JSON.stringify({ type: 'end' }))); + expect(channel.end).toHaveBeenCalled(); expect(ssh.end).toHaveBeenCalled(); + expect(next.close).toHaveBeenCalledWith(1000, 'Terminal session ended'); +}); +test('a shell nobody comes back to ends after the grace period', () => { + const { channel, key } = startShell(); + ws.readyState = WebSocket.CLOSED; ws.emit('close'); + jest.advanceTimersByTime(10 * 60000); + expect(channel.end).toHaveBeenCalled(); expect(ssh.end).toHaveBeenCalled(); + const late = socket(); late.emit('message', Buffer.from(JSON.stringify({ type: 'resume', key }))); + expect(late.send).toHaveBeenCalledWith(JSON.stringify({ type: 'resume-failed' })); +}); +test('only the same owner may resume (central: same user and target)', () => { + bridge.close(); + let who = 'user-1|gateway-a'; + bridge = attachTerminal(new http.Server(), undefined, { owner: () => who }); + ws = new EventEmitter(); ws.readyState = WebSocket.OPEN; ws.send = jest.fn(); + ws.close = jest.fn(() => { ws.readyState = WebSocket.CLOSED; ws.emit('close'); }); + bridge.emit('connection', ws); + const { key } = startShell(); + ws.readyState = WebSocket.CLOSED; ws.emit('close'); + who = 'user-2|gateway-a'; + const other = socket(); other.emit('message', Buffer.from(JSON.stringify({ type: 'resume', key }))); + expect(other.send).toHaveBeenCalledWith(JSON.stringify({ type: 'resume-failed' })); + who = 'user-1|gateway-a'; + const same = socket(); same.emit('message', Buffer.from(JSON.stringify({ type: 'resume', key }))); + expect(JSON.parse(same.send.mock.calls[0][0])).toMatchObject({ type: 'ready', resumed: true }); +}); diff --git a/tests/ui/gateways.spec.cjs b/tests/ui/gateways.spec.cjs index a3caa9e..584913f 100644 --- a/tests/ui/gateways.spec.cjs +++ b/tests/ui/gateways.spec.cjs @@ -1,13 +1,15 @@ const { test,expect }=require('@playwright/test'); +// Start a tab on a gateway (main page only, and only before the tab has chosen anything itself). +const pick=(page,id)=>page.addInitScript(target=>{if(window.top!==window||sessionStorage.getItem('picked'))return;sessionStorage.setItem('picked','1');sessionStorage.setItem('m3tering.console.target',target);},id); + 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,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}});}); + await page.route('**/api/gateways',route=>route.fulfill({json:{gateways}})); + await pick(page,'gateway-one'); await page.goto('/?central=1'); // Status lives only in the gateway menu: nothing floats over the desktop. await expect(page.locator('#gateway-status')).toHaveCount(0); @@ -28,6 +30,7 @@ test('central selector identifies the gateway and exposes uncertainty without se await expect(page.locator('#chirpstack')).toBeHidden(); await page.evaluate(()=>Desktop.launch('chirpstack')); await expect(page.locator('#chirpstack .window-title')).toContainText('South gateway'); + await expect(page.locator('#chirpstack-frame')).toHaveAttribute('src','/chirpstack/~gateway-two/'); await page.locator('#gateway-switcher').click(); await page.getByRole('button',{name:'Manage gateways'}).click(); await expect(page.locator('#gateway-dialog')).toBeVisible(); @@ -39,32 +42,60 @@ test('central selector identifies the gateway and exposes uncertainty without se }); test('the gateway can be deselected so apps open on the central server',async({page})=>{ - let selected='gateway-one';const posted=[]; const gateways=[{id:'gateway-one',name:'North gateway',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}]; - await page.route('**/api/gateways',route=>route.fulfill({json:{selected,gateways}})); - await page.route('**/api/gateways/select',route=>{selected=route.request().postDataJSON().id;posted.push(selected);return route.fulfill({json:{selected}});}); + await page.route('**/api/gateways',route=>route.fulfill({json:{gateways}})); + await pick(page,'gateway-one'); await page.goto('/?central=1'); + const target=()=>page.evaluate(()=>sessionStorage.getItem('m3tering.console.target')); await expect(page.locator('#gateway-switcher-name')).toHaveText('North gateway'); await page.evaluate(()=>Desktop.launch('chirpstack')); await page.locator('#gateway-switcher').click(); await page.getByRole('option',{name:/Central server/}).click(); - expect(posted).toEqual([null]); + expect(await target()).toBeNull(); await expect(page.locator('#gateway-switcher-name')).toHaveText('Central server'); await expect(page.locator('#gateway-switcher')).toHaveAttribute('data-state','central'); await expect(page.locator('#chirpstack')).toBeHidden(); await page.evaluate(()=>Desktop.launch('chirpstack')); await expect(page.locator('#chirpstack .window-title')).toContainText('Central server'); + await expect(page.locator('#chirpstack-frame')).toHaveAttribute('src','/chirpstack/~central/'); await page.locator('#gateway-switcher').click(); await expect(page.locator('#gateway-current')).toContainText('open on the central server'); await expect(page.getByRole('option',{name:/Central server/})).toHaveAttribute('aria-selected','true'); await page.getByRole('option',{name:/North gateway/}).click(); - expect(posted).toEqual([null,'gateway-one']); + expect(await target()).toBe('gateway-one'); await expect(page.locator('#gateway-switcher-name')).toHaveText('North gateway'); + // A reload keeps this tab's choice. + await page.reload();await expect(page.locator('#gateway-switcher-name')).toHaveText('North gateway'); +}); + +test('two tabs keep their own targets: switching in one never changes the other',async({context})=>{ + const gateways=[ + {id:'gateway-one',name:'North gateway',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}, + {id:'gateway-two',name:'South gateway',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}, + ]; + await context.route('**/api/gateways',route=>route.fulfill({json:{gateways}})); + const first=await context.newPage(),second=await context.newPage(); + await first.goto('/?central=1');await second.goto('/?central=1'); + await first.locator('#gateway-switcher').click();await first.getByRole('option',{name:/North gateway/}).click(); + await second.locator('#gateway-switcher').click();await second.getByRole('option',{name:/South gateway/}).click(); + // Let the first tab refresh its gateway list: its choice must survive. + await first.waitForTimeout(5500); + await expect(first.locator('#gateway-switcher-name')).toHaveText('North gateway'); + await expect(second.locator('#gateway-switcher-name')).toHaveText('South gateway'); + for(const [page,id] of [[first,'gateway-one'],[second,'gateway-two']]){ + await page.evaluate(()=>Desktop.launch('chirpstack')); + await expect(page.locator('#chirpstack-frame')).toHaveAttribute('src',`/chirpstack/~${id}/`); + expect(await page.evaluate(()=>window.ConsoleTarget())).toBe(id); + } + // A third, new tab starts on the central server rather than inheriting either choice. + const third=await context.newPage();await third.goto('/?central=1'); + await expect(third.locator('#gateway-switcher-name')).toHaveText('Central server'); }); test('on a phone the top bar fits and the gateway menu is a full-width sheet',async({page})=>{ await page.setViewportSize({width:390,height:844}); - await page.route('**/api/gateways',route=>route.fulfill({json:{selected:'g',gateways:[{id:'g',name:'A gateway with a rather long site name',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}]}})); + await page.route('**/api/gateways',route=>route.fulfill({json:{gateways:[{id:'g',name:'A gateway with a rather long site name',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}]}})); + await pick(page,'g'); await page.route('**/api/account',route=>route.fulfill({json:{username:'operator'}})); await page.goto('/?central=1'); const topbar=await page.locator('.topbar').boundingBox(); @@ -101,7 +132,7 @@ test('gateways can be added, renamed and deleted from the management dialog',asy 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}}); + return route.fulfill({json:{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]); @@ -147,7 +178,7 @@ test('gateways can be added, renamed and deleted from the management dialog',asy test('the account dialog changes the password and reports mistakes',async({page})=>{ const posts=[]; - await page.route('**/api/gateways',route=>route.fulfill({json:{selected:null,gateways:[]}})); + await page.route('**/api/gateways',route=>route.fulfill({json:{gateways:[]}})); await page.route('**/api/account',route=>route.fulfill({json:{username:'admin'}})); await page.route('**/api/account/password',route=>{const body=route.request().postDataJSON();posts.push(body); return body.current==='right-pass'?route.fulfill({json:{changed:true}}):route.fulfill({status:400,json:{error:'Your current password is incorrect.'}});}); @@ -177,3 +208,25 @@ test('the account dialog changes the password and reports mistakes',async({page} await page.getByRole('button',{name:'Close',exact:true}).click(); await expect(page.locator('#account-dialog')).toBeHidden(); }); + +test('Streamr follows this tab\'s target on central and refreshes when it changes',async({page})=>{ + const gateways=[{id:'gateway-one',name:'North gateway',online:true,revoked:false,enrolled:true,health:{mqtt:'connected'},updates:[]}]; + const targets=[]; + await page.route('**/api/gateways',route=>route.fulfill({json:{gateways}})); + await page.route('**/api/apps/streamr/status',route=>{ + const target=route.request().headers()['x-console-target'];targets.push(target); + const gateway=target==='gateway-one'; + return route.fulfill({json:{role:'central',scope:gateway?"Showing readings from North gateway's meters. Central publishes for every gateway together.":'Showing readings from every gateway. Central publishes them on the schedule below.', + recordCount:gateway?2:5,pendingCount:gateway?0:3,uncertainCount:gateway?0:1,canPublish:true,lastPublishTime:null,lastPublishStatus:null}}); + }); + await page.goto('/?central=1'); + await page.evaluate(()=>Desktop.launch('streamr')); + await expect(page.locator('[data-streamr-field="scope"]')).toContainText('every gateway'); + await expect(page.locator('[data-streamr-field="pending"]')).toHaveText('3'); + await expect(page.locator('[data-streamr-field="uncertain"]')).toContainText('1 with an uncertain outcome'); + await page.locator('#gateway-switcher').click();await page.getByRole('option',{name:/North gateway/}).click(); + await page.evaluate(()=>Desktop.launch('streamr')); + await expect(page.locator('[data-streamr-field="scope"]')).toContainText("North gateway's meters"); + await expect(page.locator('[data-streamr-field="pending"]')).toHaveText('0'); + expect(targets).toContain('central');expect(targets).toContain('gateway-one'); +}); diff --git a/tests/ui/serve.cjs b/tests/ui/serve.cjs index d9ac864..aa51a44 100644 --- a/tests/ui/serve.cjs +++ b/tests/ui/serve.cjs @@ -14,7 +14,7 @@ app.get('/', (req, res) => { for (let i = 0; i < count; i++) { const id = i ? `extension-${i}` : 'streamr'; icons.push({ id, label: i ? `Extension ${i}` : 'Streamr', iconHtml: '' }); - windows.push({ id, title: i ? `Extension ${i}` : 'Streamr Publisher', contentHtml: '

Configuration

Pending transactions: 2

' }); + windows.push({ id, title: i ? `Extension ${i}` : 'Streamr Publisher', contentHtml: (i ? '' : '

2') + '

Configuration

Pending transactions: 2

' }); } res.render('index', { icons, windows, central:req.query.central==='1' }); }); diff --git a/tests/ui/streamr.test.ts b/tests/ui/streamr.test.ts index 53bbff9..1c73b6a 100644 --- a/tests/ui/streamr.test.ts +++ b/tests/ui/streamr.test.ts @@ -2,7 +2,8 @@ import StreamrUI from '../../src/lib/core/streamr/ui'; import { getAllTransactionRecords } from '../../src/store/sqlite'; jest.mock('@streamr/sdk', () => ({ StreamrClient: jest.fn() })); -jest.mock('../../src/store/sqlite', () => ({ getAllTransactionRecords: jest.fn(() => []) })); +jest.mock('../../src/store/sqlite', () => ({ getAllTransactionRecords: jest.fn(() => []), getMeterByDevEui: jest.fn((devEui: string) => devEui === 'aaaaaaaaaaaaaaaa' ? { tokenId: 1 } : devEui === 'bbbbbbbbbbbbbbbb' ? { tokenId: 2 } : null) })); +import { configurePublications } from '../../src/central/distribution'; describe('Streamr desktop presentation', () => { const originalStream = process.env.STREAMR_STREAM_ID; @@ -31,4 +32,39 @@ describe('Streamr desktop presentation', () => { expect(window.contentHtml).not.toContain('