Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Social Engine & Remote Navigator8 * File : services/control-api/src/cli.ts9 * Module : services10 * Repository : https://git.spboucher.ai/maclustr-social11 * Project Website : https://social.spboucher.ai12 * Created : 2026-12-0113 * Last Modified : 2026-12-0114 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Operator CLI, run on the node. No command prints or accepts a Facebook credential.43import { loadConfig } from './config.ts';44import { Db } from './db.ts';45import { createSetupLink, h, token } from './auth.ts';46import { verifyAuditChain, audit } from './audit.ts';47import { seed } from './seed.ts';48import { engageEmergencyStop } from './core/sessions.ts';49const cfg = loadConfig(); const db = new Db(cfg.databaseUrl, 2);50const [cmd, ...a] = process.argv.slice(2);51const SYS = { kind: 'system' as const, id: 'cli' };52try {53 switch (cmd) {54 case 'seed': await seed(db, cfg); console.log('ok'); break;55 case 'owner-invite': { // owner-invite <handle> "<Nom>" [social|navigator]56 if (!a[0] || !a[1]) throw new Error('usage: owner-invite <handle> "<Nom>" [social|navigator]');57 const u = (await db.one("INSERT INTO users(handle, display_name, role_id) VALUES ($1,$2,'owner') ON CONFLICT (handle) DO UPDATE SET display_name=excluded.display_name RETURNING id", [a[0], a[1]]))!;58 await db.tx((tx) => audit(tx, { actor: SYS, action: 'user.owner_invite', targetKind: 'user', targetId: u.id, result: 'ok' }));59 console.log(await createSetupLink(db, cfg, u.id, a[2] === 'navigator' ? cfg.navigatorOrigin : cfg.socialOrigin)); break;60 }61 case 'worker-create': { // worker-create <name> [host] → prints the token once62 if (!a[0]) throw new Error('usage: worker-create <name> [host]');63 const t = token(40);64 await db.query('INSERT INTO workers(name, token_hash, host) VALUES ($1,$2,$3) ON CONFLICT (name) DO UPDATE SET token_hash=excluded.token_hash, status=\'offline\'', [a[0], h(t), a[1] ?? '']);65 await db.tx((tx) => audit(tx, { actor: SYS, action: 'worker.token_issue', targetKind: 'worker', targetId: a[0], result: 'ok' }));66 console.log(t); break;67 }68 case 'session-create': { // session-create <label> <worker-name> [start-url] — same checks as the API (policy allow-list)69 const w = await db.one("SELECT id FROM workers WHERE name=$1 AND status <> 'disabled'", [a[1] ?? '']); if (!w) throw new Error('worker inconnu');70 if (a[2]) { const { isAddressBarUrlAllowed } = await import('@mls/policy-engine'); const { activePolicy } = await import('./core/policy.ts'); const d = isAddressBarUrlAllowed(await activePolicy(db), a[2]); if (!d.allow) throw new Error(`URL refusée : ${d.reason}`); }71 const id = await db.tx(async (tx) => { const p = (await tx.query('INSERT INTO navigator_profiles(worker_id, label) VALUES ($1,$2) RETURNING id', [w.id, a[0]])).rows[0]; const r = (await tx.query("INSERT INTO navigator_sessions(profile_id, start_url, wanted) VALUES ($1,$2,'running') RETURNING id", [p.id, a[2] ?? null])).rows[0]; await audit(tx, { actor: SYS, action: 'session.create', targetKind: 'session', targetId: r.id, result: 'ok' }); return r.id; });72 console.log(id); break;73 }74 case 'emergency-stop': await db.tx((tx) => engageEmergencyStop(tx, a[0] !== 'release', a.slice(1).join(' ') || 'CLI', SYS, 'cli')); console.log(a[0] === 'release' ? 'levé' : 'engagé'); break;75 case 'revoke-web-sessions': console.log((await db.query('DELETE FROM auth_sessions')).rowCount, 'session(s) fermée(s)'); break;76 case 'backup-mark': await db.tx((tx) => audit(tx, { actor: SYS, action: a[0] === 'fail' ? 'backup.failed' : 'backup.ok', targetKind: 'system', result: a[0] === 'fail' ? 'failed' : 'ok', detail: { file: a[1] ?? '', bytes: Number(a[2] ?? 0) } })); break;77 case 'audit-verify': console.log(JSON.stringify(await verifyAuditChain(db))); break;78 case 'status': console.log(JSON.stringify({ stop: (await db.one('SELECT emergency_stop, reason FROM system_state')), sessions: (await db.query('SELECT state, count(*)::int n FROM navigator_sessions GROUP BY state')).rows, workers: (await db.query('SELECT name, status, last_heartbeat_at FROM workers')).rows, jobs: (await db.query('SELECT state, count(*)::int n FROM publication_jobs GROUP BY state')).rows, users: (await db.one('SELECT count(*)::int n FROM users'))!.n, passkeys: (await db.one('SELECT count(*)::int n FROM passkeys'))!.n }, null, 1)); break;79 default: console.log('commandes : seed | owner-invite | worker-create | session-create | emergency-stop [release] <raison> | revoke-web-sessions | audit-verify | status'); process.exitCode = cmd ? 2 : 0;80 }81} catch (e) { console.error(String((e as Error).message)); process.exitCode = 1; } finally { await db.end(); }82