Add amber phosphor Telnet terminal with isolated BPQ sessions
This commit is contained in:
1 parent
b5ee05f257
commit
3f6d35690b
8 files changed
+96
-5
No files matched your search
+14
-3
@@ -1,3 +1,4 @@
|
||||
import {Terminal} from './terminal.mjs';
|
||||
import http from 'node:http';
|
||||
import {Journal} from './journal.mjs';
|
||||
import {GatewayDirectory,modemFamily} from './winlink-gateways.mjs';
|
||||
@@ -52,7 +53,7 @@ function equal(a,b){const aa=Buffer.from(a),bb=Buffer.from(b);return aa.length==
|
||||
async function authenticate(user,password){const users=parseConfig(await readFile(path.join(root,'BPQ32.cfg'),'utf8')).users;const found=users.find(u=>equal(u.user.toLowerCase(),user.toLowerCase())&&equal(u.password,password)&&u.user===config.sysop.user);return found?{call:found.call,name:found.user,config:publicConfig(config)}:null;}
|
||||
async function body(req){let chunks=[],size=0;for await(const chunk of req){size+=chunk.length;if(size>70000)throw new Error('Messaggio troppo grande');chunks.push(chunk)}return JSON.parse(Buffer.concat(chunks).toString()||'{}');}
|
||||
function json(res,status,data){res.writeHead(status,{'Content-Type':'application/json; charset=utf-8','Cache-Control':'no-store'});res.end(JSON.stringify(data));}
|
||||
function session(req){const token=req.headers.cookie?.match(/(?:^|;\s*)alnode=([a-f0-9]+)/)?.[1];const s=sessions.get(token);if(s&&s.expires>Date.now()){s.expires=Date.now()+8*3600000;return s;}if(token)sessions.delete(token);return null;}
|
||||
function session(req){const token=req.headers.cookie?.match(/(?:^|;\s*)alnode=([a-f0-9]+)/)?.[1];const s=sessions.get(token);if(s&&s.expires>Date.now()){s.expires=Date.now()+8*3600000;return s;}if(token){s?.terminal?.close();sessions.delete(token);}return null;}
|
||||
async function dashboard(){
|
||||
const jobs=await Promise.allSettled([config.radio?bpq('/Node/Port?'+config.radio.id):Promise.resolve(null),bpq('/Node/Routes.html'),bpq('/Node/Nodes.html'),config.features.bbs?messages():Promise.resolve([])]);
|
||||
const val=i=>jobs[i].status==='fulfilled'?jobs[i].value:null;
|
||||
@@ -93,6 +94,7 @@ async function sendMessage(data){
|
||||
if(!created)throw new Error('BPQ non ha confermato il salvataggio. Controlla la lista prima di riprovare.');
|
||||
return {id:created.id};
|
||||
}
|
||||
setInterval(()=>{for(const [token,s] of sessions)if(s.expires<Date.now()){s.terminal?.close();sessions.delete(token);}},30000).unref();
|
||||
const server=http.createServer(async(req,res)=>{
|
||||
try{
|
||||
const host=req.headers.host||'';
|
||||
@@ -109,6 +111,15 @@ const server=http.createServer(async(req,res)=>{
|
||||
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/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.startsWith('/api/terminal')){
|
||||
if(!config.telnetPort)throw new Error('Porta Telnet BPQ non configurata');
|
||||
if(!user.terminal)user.terminal=new Terminal(async()=>{const c=await loadConfig();return c.sysop;},{port:config.telnetPort});
|
||||
const terminal=user.terminal;
|
||||
if(url.pathname==='/api/terminal'&&req.method==='GET'){const after=Number(url.searchParams.get('after')||0);if(!Number.isSafeInteger(after)||after<0)throw new Error('Cursore non valido');json(res,200,terminal.snapshot(after));return;}
|
||||
if(url.pathname==='/api/terminal/connect'&&req.method==='POST'){json(res,200,await terminal.connect());return;}
|
||||
if(url.pathname==='/api/terminal/disconnect'&&req.method==='POST'){terminal.disconnect();json(res,200,terminal.snapshot());return;}
|
||||
if(url.pathname==='/api/terminal/send'&&req.method==='POST'){terminal.send((await body(req)).text);json(res,200,{ok:true});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;}
|
||||
if(url.pathname==='/api/monitor/connect'&&req.method==='POST'){json(res,200,await monitor.connect());return;}
|
||||
@@ -118,7 +129,7 @@ const server=http.createServer(async(req,res)=>{
|
||||
if(url.pathname==='/api/chat/disconnect'&&req.method==='POST'){chat.disconnect();json(res,200,{ok:true});return;}
|
||||
if(url.pathname==='/api/chat/send'&&req.method==='POST'){const data=await body(req);chat.send(data.text,data.to);json(res,200,{ok:true});return;}
|
||||
if(url.pathname==='/api/chat/topic'&&req.method==='POST'){chat.changeTopic((await body(req)).topic);json(res,200,{ok:true});return;}
|
||||
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/logout'&&req.method==='POST'){const token=req.headers.cookie.match(/alnode=([a-f0-9]+)/)?.[1];user.terminal?.close();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;}
|
||||
@@ -142,7 +153,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={'/winlink-radio.js':'winlink-radio.js','/winlink-radio.css':'winlink-radio.css','/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);
|
||||
const files={'/terminal.js':'terminal.js','/terminal.css':'terminal.css','/winlink-radio.js':'winlink-radio.js','/winlink-radio.css':'winlink-radio.css','/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}`));
|
||||
Reference in new issue
Block a user