Allow native SYSOP accounts to sign in independently, with an optional sysopUsers allow-list and isolated browser sessions. Keep station services on the configured service account. Add public bulletin comments that preserve category and distribution, alongside private replies to the author. Complete APRS and dynamic status translations and handle decorated button labels correctly. Document configuration and reply behavior. Add authentication, installer, bulletin reply and translation regression coverage.
56 lines
6.2 KiB
JavaScript
56 lines
6.2 KiB
JavaScript
import {readFile} from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import os from 'node:os';
|
|
import {fileURLToPath} from 'node:url';
|
|
import {timingSafeEqual} from 'node:crypto';
|
|
|
|
export function validateSysopUsers(value,users){
|
|
if(value===undefined)return undefined;
|
|
if(!Array.isArray(value)||value.some(name=>typeof name!=='string'||!name.trim()))throw new Error('sysopUsers deve essere una lista di nomi utente SYSOP');
|
|
const known=new Set(users.map(u=>u.user.toLowerCase()));
|
|
const names=value.map(name=>name.trim());
|
|
if(names.some(name=>!known.has(name.toLowerCase())))throw new Error('sysopUsers contiene un utente SYSOP non rilevato in BPQ32.cfg');
|
|
return names.filter((name,index)=>names.findIndex(other=>other.toLowerCase()===name.toLowerCase())===index);
|
|
}
|
|
export function authenticateSysop(users,user,password,allowedUsers){
|
|
const equal=(a,b)=>{const aa=Buffer.from(a),bb=Buffer.from(b);return aa.length===bb.length&&timingSafeEqual(aa,bb);};
|
|
const found=users.find(u=>equal(u.user.toLowerCase(),user.toLowerCase())&&equal(u.password,password));
|
|
if(!found||allowedUsers!==undefined&&!allowedUsers.some(name=>name.toLowerCase()===found.user.toLowerCase()))return null;
|
|
return {call:found.call,name:found.user};
|
|
}
|
|
|
|
export function parseConfig(text){
|
|
let inText=false,inComment=false;const lines=[];
|
|
for(let line of text.split(/\r?\n/)){line=line.trim();if(inText){if(line.startsWith('***'))inText=false;continue;}if(inComment){if(line.startsWith('*/'))inComment=false;continue;}if(line.startsWith('/*')){inComment=!line.includes('*/');continue;}if(/^(?:IDMSG|INFOMSG|BTEXT|CTEXT):/i.test(line)){inText=true;continue;}line=line.split(';')[0].trim();if(line)lines.push(line);}
|
|
const value=(name)=>lines.find(l=>new RegExp('^'+name+'\\s*=','i').test(l))?.split('=').slice(1).join('=').trim()||'';
|
|
const users=lines.flatMap(l=>{const m=l.match(/^USER\s*=\s*([^,]+),([^,]+),([^,]+),(.*)$/i);return m&&/\bsysop\b/i.test(m[4])?[{user:m[1].trim(),password:m[2],call:m[3].trim().toUpperCase()}]:[]});
|
|
const apps=lines.flatMap(l=>{const m=l.match(/^APPLICATION\s+\d+\s*,(.*)$/i);if(!m)return [];const fields=m[1].split(',').map(s=>s.trim());return fields[0]?[{command:fields[0].split('/')[0].toUpperCase(),call:(fields[2]||'').toUpperCase(),alias:fields[3]||''}]:[]});
|
|
let ports=[],current=null;
|
|
for(const l of lines){if(/^PORT$/i.test(l)){current={id:ports.length+1,name:'',driver:'',settings:{}};continue;}if(/^ENDPORT$/i.test(l)){if(current){if(!current.driver)current.driver=current.protocol||current.type||'';ports.push(current)};current=null;continue;}if(current){const m=l.match(/^(PORTNUM|ID|DRIVER|TYPE|PROTOCOL)\s*=\s*(.*)$/i);if(m)current[({PORTNUM:'id',ID:'name',DRIVER:'driver',TYPE:'type',PROTOCOL:'protocol'})[m[1].toUpperCase()]]=m[1].toUpperCase()==='PORTNUM'?Number(m[2].trim()):m[2].trim();const s=l.match(/^(HTTPPORT|TCPPORT|FBBPORT|CMS|LOGINPROMPT|PASSWORDPROMPT)\s*=\s*(.*)$/i);if(s)current.settings[s[1].toUpperCase()]=s[2].trim();}}
|
|
const telnet=ports.find(p=>/^TELNET(?:\.DLL)?$/i.test(p.driver)),settings=telnet?.settings||{};
|
|
for(const p of ports)delete p.settings;
|
|
const radioPorts=ports.filter(p=>!/TELNET|AXIP|LOOPBACK/i.test(p.driver)&&p.driver);
|
|
const nodeCall=value('NODECALL'),sysop=users[0];
|
|
return {nodeCall,call:sysop?.call||nodeCall.replace(/-\d+$/,''),alias:value('NODEALIAS'),locator:value('LOCATOR'),apps,ports,radioPorts,users,httpPort:Number(settings.HTTPPORT)||null,telnetPort:Number(settings.TCPPORT)||null,monitorPort:Number(settings.FBBPORT)||null,telnetPrompts:{loginPrompt:settings.LOGINPROMPT||'',passwordPrompt:settings.PASSWORDPROMPT||''},winlink:settings.CMS==='1'};
|
|
}
|
|
export async function loadConfig(){
|
|
const dir=path.dirname(fileURLToPath(import.meta.url));let saved={};
|
|
try{saved=JSON.parse(await readFile(process.env.BPQ_CONSOLE_CONFIG||path.join(dir,'console.config.json'),'utf8'));}catch(e){if(e.code!=='ENOENT')throw e;}
|
|
if(!saved||typeof saved!=='object'||Array.isArray(saved))throw new Error('La configurazione locale deve essere un oggetto JSON');
|
|
const root=process.env.BPQ_ROOT||saved.bpqRoot||path.join(process.env.APPDATA||path.join(os.homedir(),'AppData','Roaming'),'BPQ32');
|
|
if(saved.language!=null&&!['it','en'].includes(saved.language))throw new Error('language deve essere it oppure en');
|
|
const detected=parseConfig(await readFile(path.join(root,'BPQ32.cfg'),'utf8'));
|
|
if(!detected.users.length)throw new Error('Nessun utente Telnet sysop in BPQ32.cfg');
|
|
const sysopUsers=validateSysopUsers(saved.sysopUsers,detected.users);
|
|
if(saved.radioPort!=null&&!detected.radioPorts.some(p=>p.id===Number(saved.radioPort)))throw new Error('Porta radio configurata non rilevata: rieseguire Install.ps1');
|
|
if(saved.sysopUser&&!detected.users.some(u=>u.user===saved.sysopUser))throw new Error('Utente sysop configurato non rilevato: rieseguire Install.ps1');
|
|
const radio=detected.radioPorts.find(p=>p.id===Number(saved.radioPort))||detected.radioPorts[0]||null;
|
|
const httpPort=Number(saved.httpPort||detected.httpPort);if(!validPort(httpPort))throw new Error('HTTPPORT BPQ non valida o assente: eseguire Install.ps1');
|
|
const port=Number(process.env.BPQ_CONSOLE_PORT||saved.port||8090);if(!validPort(port,1024)||port===httpPort)throw new Error('Porta pannello non valida o in conflitto con HTTPPORT BPQ');
|
|
const features={terminal:!!detected.telnetPort,bbs:detected.apps.some(a=>a.command==='BBS'),chat:detected.apps.some(a=>a.command==='CHAT')&&!!detected.telnetPort,monitor:!!detected.monitorPort,winlink:detected.winlink&&detected.apps.some(a=>a.command==='BBS'),vara:!!radio&&/^VARA/i.test(radio.driver),radio:!!radio};
|
|
return {...detected,...saved,...(sysopUsers===undefined?{}:{sysopUsers}),root,httpPort,radio,features,port,sysop:detected.users.find(u=>u.user===saved.sysopUser)||detected.users[0]};
|
|
}
|
|
export const validPort=(n,min=1)=>Number.isInteger(n)&&n>=min&&n<=65535;
|
|
export function publicConfig(c){return {call:c.sysop.call,nodeCall:c.nodeCall,alias:c.alias,locator:c.locator,apps:c.apps,radio:c.radio,ports:c.ports,features:c.features,forwardPeer:c.forwardPeer||'',cmsPeer:c.cmsPeer||'RMS'};}
|
|
export function ownCall(sender,call){return !!call&&String(sender||'').replace(/^RMS[/:]/i,'').toUpperCase().split('@')[0].replace(/-\d+$/,'')===String(call).toUpperCase().split('@')[0].replace(/-\d+$/,'');}
|