Add forwarding partner dashboard with schedules, rules, queues and manual sync

This commit is contained in:
alberto committed 2026-10-16 17:27:29 +02:00
1 parent 94649dde33
commit 8f18ea81f7
9 files changed
+134 -11

No files matched your search

+7 -3
View File
@@ -1,5 +1,6 @@
import http from 'node:http';
import {Journal} from './journal.mjs';
import {Forwarding} from './forwarding.mjs';
import {SevenPlus} from './sevenplus.mjs';
import {loadConfig,parseConfig,publicConfig,ownCall} from './config.mjs';
import {readFile, stat} from 'node:fs/promises';
@@ -63,7 +64,8 @@ async function dashboard(){
return {updatedAt:new Date().toISOString(),radio:val(0)?parseRadio(val(0)):null,routes:val(1)?parseRoutes(val(1)):[],nodes:[...new Set(nodes)],counts:{messages:list.length,bulletins:list.filter(m=>m.type==='B').length,personal:list.filter(m=>m.type==='P').length},recent:list.slice(0,5),activity,sync,cms,errors:jobs.flatMap((r,i)=>r.status==='rejected'?[['Radio','Rete','Nodi','BBS'][i]+': '+r.reason.message]:[])};
}
let lastCmsRequest=0;
async function syncNow(peer=config.forwardPeer){if(!config.features.bbs||!peer||!/^[A-Z0-9-]{1,12}$/i.test(peer))throw new Error('Seleziona un profilo di forwarding nella configurazione');const status=await bpq('/Mail/Status');const link=status.match(/href=(\/Mail\/FWD\?[^\s>]+)/)?.[1];if(!link)throw new Error('Forwarding non disponibile');const html=await bpq(link);const key=html.match(/var Key\s*=\s*"([^"]+)"/)?.[1];if(!key)throw new Error('Sessione forwarding non disponibile');await bpq('/Mail/FwdDetails?'+key,{method:'POST',body:peer});await bpq('/Mail/FWDSave?'+key,{method:'POST',body:'StartForward'});}
const forwarding=new Forwarding(config,bpq,()=>telemetry.current);
async function syncNow(peer=config.forwardPeer){if(!config.features.bbs)throw new Error('BBS non configurata');return forwarding.start(peer);}
const timestampCache=new Map();
async function messageTimes(id){if(timestampCache.has(id))return timestampCache.get(id);const pending=(async()=>{const f=await messageFields(id);let reference=new Date();try{reference=(await stat(path.join(root,'BPQMailChat','Mail','m_'+String(id).padStart(6,'0')+'.mes'))).mtime;}catch{}const receivedAt=bpqTimestamp(f.RX,reference);return {receivedAt,createdAt:bpqTimestamp(f.Sent,receivedAt?new Date(receivedAt):reference)};})().catch(()=>{timestampCache.delete(id);return {receivedAt:null,createdAt:null};});timestampCache.set(id,pending);return pending;}
const metadataCache=new Map();let metadataKey=null,metadataKeyAt=0;
@@ -99,7 +101,7 @@ const server=http.createServer(async(req,res)=>{
}
if(url.pathname.startsWith('/api/')){
const user=session(req);if(!user){json(res,401,{error:'Accedi con le credenziali BPQ'});return;}
const capability=url.pathname.startsWith('/api/chat')?'chat':url.pathname.startsWith('/api/monitor')?'monitor':url.pathname.startsWith('/api/winlink')?'winlink':url.pathname.startsWith('/api/messages')||url.pathname==='/api/sync'?'bbs':null;if(capability&&!config.features[capability]){json(res,409,{error:'Servizio non configurato in BPQ'});return;}
const capability=url.pathname.startsWith('/api/chat')?'chat':url.pathname.startsWith('/api/monitor')?'monitor':url.pathname.startsWith('/api/winlink')?'winlink':url.pathname.startsWith('/api/forwarding')||url.pathname.startsWith('/api/messages')||url.pathname==='/api/sync'?'bbs':null;if(capability&&!config.features[capability]){json(res,409,{error:'Servizio non configurato in BPQ'});return;}
if(req.method==='POST'&&!equal(req.headers['x-csrf-token']||'',user.csrf)){json(res,403,{error:'Sessione non valida. Ricarica la pagina.'});return;}
if(url.pathname==='/api/session'){json(res,200,{call:user.call,name:user.name,csrf:user.csrf,config:publicConfig(config)});return;}
if(url.pathname==='/api/monitor'&&req.method==='GET'){const after=Number(url.searchParams.get('after')||0);if(!Number.isSafeInteger(after)||after<0)throw new Error('Cursore monitor non valido');json(res,200,monitor.snapshot(after));return;}
@@ -113,6 +115,8 @@ const server=http.createServer(async(req,res)=>{
if(url.pathname==='/api/logout'&&req.method==='POST'){const token=req.headers.cookie.match(/alnode=([a-f0-9]+)/)?.[1];sessions.delete(token);res.setHeader('Set-Cookie','alnode=; HttpOnly; SameSite=Strict; Path=/; Max-Age=0');json(res,200,{ok:true});return;}
if(url.pathname==='/api/journal'&&req.method==='GET'){json(res,200,await journal.snapshot());return;}
if(url.pathname==='/api/stations/location'&&req.method==='POST'){const data=await body(req);json(res,200,await journal.saveLocation(String(data.call||''),String(data.locator||'')));return;}
if(url.pathname==='/api/forwarding'&&req.method==='GET'){json(res,200,await forwarding.snapshot());return;}
const fwdStart=url.pathname.match(/^\/api\/forwarding\/([A-Z0-9-]{1,12})\/sync$/);if(fwdStart&&req.method==='POST'){json(res,200,await forwarding.start(fwdStart[1]));return;}
if(url.pathname==='/api/dashboard'){json(res,200,await dashboard());return;}
if(url.pathname==='/api/telemetry'&&req.method==='GET'){json(res,200,telemetry);return;}
if(url.pathname==='/api/messages'&&req.method==='GET'){json(res,200,await messages());return;}
@@ -129,7 +133,7 @@ const server=http.createServer(async(req,res)=>{
}
if(req.method!=='GET'){json(res,405,{error:'Metodo non disponibile'});return;}
const vendor=url.pathname.match(/^\/vendor\/pdfjs\/(pdf\.min\.mjs|pdf\.worker\.min\.mjs|(?:standard_fonts|cmaps)\/[A-Za-z0-9_.-]+)$/);if(vendor&&!vendor[1].includes('..')){const bytes=await readFile(path.join(dir,'public','vendor','pdfjs',vendor[1]));res.writeHead(200,{'Content-Type':vendor[1].endsWith('.mjs')?'text/javascript':'application/octet-stream','Cache-Control':'public, max-age=86400'});res.end(bytes);return;}
const files={'/journal.js':'journal.js','/journal.css':'journal.css','/world-land.json':'world-land.json','/monitor.js':'monitor.js','/monitor.css':'monitor.css','/':'index.html','/app.js':'app.js','/enhancements.js':'enhancements.js','/notifications.js':'notifications.js','/winlink.js':'winlink.js','/mission.js':'mission.js','/attachments.js':'attachments.js','/mission.css':'mission.css','/style.css':'style.css','/dark.css':'dark.css'};const file=files[url.pathname];if(!file){json(res,404,{error:'Pagina non trovata'});return;}const bytes=await readFile(path.join(dir,'public',file));res.writeHead(200,{'Content-Type':file.endsWith('.json')?'application/json':file.endsWith('.css')?'text/css; charset=utf-8':file.endsWith('.js')?'text/javascript; charset=utf-8':'text/html; charset=utf-8','Cache-Control':'no-cache'});res.end(bytes);
const files={'/forwarding.js':'forwarding.js','/forwarding.css':'forwarding.css','/journal.js':'journal.js','/journal.css':'journal.css','/world-land.json':'world-land.json','/monitor.js':'monitor.js','/monitor.css':'monitor.css','/':'index.html','/app.js':'app.js','/enhancements.js':'enhancements.js','/notifications.js':'notifications.js','/winlink.js':'winlink.js','/mission.js':'mission.js','/attachments.js':'attachments.js','/mission.css':'mission.css','/style.css':'style.css','/dark.css':'dark.css'};const file=files[url.pathname];if(!file){json(res,404,{error:'Pagina non trovata'});return;}const bytes=await readFile(path.join(dir,'public',file));res.writeHead(200,{'Content-Type':file.endsWith('.json')?'application/json':file.endsWith('.css')?'text/css; charset=utf-8':file.endsWith('.js')?'text/javascript; charset=utf-8':'text/html; charset=utf-8','Cache-Control':'no-cache'});res.end(bytes);
}catch(e){json(res,502,{error:e.message||'BPQ temporaneamente non disponibile'});}
});
server.listen(port,'0.0.0.0',()=>console.log(`BPQ Console: http://127.0.0.1:${port}`));