Add amber phosphor Telnet terminal with isolated BPQ sessions

This commit is contained in:
alberto committed 2026-10-16 18:55:15 +02:00
1 parent b5ee05f257
commit 3f6d35690b
8 files changed
+96 -5

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
import net from 'node:net';
import {StringDecoder} from 'node:string_decoder';
// Telnet commands may be split across TCP packets, including subnegotiations.
export class TelnetText {
constructor(reply){this.reply=reply;this.state='text';this.command=0;}
read(chunk){const out=[];for(const b of chunk){switch(this.state){
case 'text':if(b===255)this.state='iac';else out.push(b);break;
case 'iac':if(b===255){out.push(b);this.state='text';}else if(b===250)this.state='sub';else if([251,252,253,254].includes(b)){this.command=b;this.state='option';}else this.state='text';break;
case 'option':if(this.command===251)this.reply(Buffer.from([255,254,b]));if(this.command===253)this.reply(Buffer.from([255,252,b]));this.state='text';break;
case 'sub':if(b===255)this.state='sub-iac';break;
case 'sub-iac':this.state=b===240?'text':'sub';break;
}}return Buffer.from(out);}
}
export class Terminal {
constructor(credentials,options={}){this.credentials=credentials;this.options=options;this.state='offline';this.events=[];this.seq=0;this.lastAccess=Date.now();this.lastInput=Date.now();this.timer=setInterval(()=>{if(this.socket&&!this.socket.destroyed&&(Date.now()-this.lastAccess>90000||Date.now()-this.lastInput>15*60000))this.disconnect('Sessione chiusa per inattività');},10000);this.timer.unref();}
event(text){if(!text)return;this.events.push({id:++this.seq,text});let size=this.events.reduce((n,e)=>n+e.text.length,0);while(this.events.length>500||size>200000)size-=this.events.shift().text.length;}
snapshot(after=0){this.lastAccess=Date.now();return {state:this.state,events:this.events.filter(e=>e.id>after),latest:this.seq,port:this.options.port,error:this.error||null};}
async connect(){if(this.state==='connecting'||this.socket&&!this.socket.destroyed)return this.snapshot();this.state='connecting';let credentials;try{credentials=await this.credentials();}catch{this.state='error';throw new Error('Credenziali Telnet BPQ non disponibili');}if(this.state!=='connecting')return this.snapshot();this.error=null;this.events=[];this.stage=0;this.auth='';this.escape='';this.pendingCR=false;this.decoder=new StringDecoder('utf8');this.lastAccess=this.lastInput=Date.now();const socket=net.createConnection({host:'127.0.0.1',port:this.options.port});this.socket=socket;const telnet=new TelnetText(b=>socket.write(b));socket.setTimeout(15000);
socket.on('timeout',()=>{if(this.state==='connecting')socket.destroy(new Error('Accesso Telnet non confermato'));});
socket.on('error',()=>{if(this.socket!==socket)return;this.state='error';this.error='Connessione Telnet BPQ non disponibile';this.event('\n[Connessione Telnet non disponibile]\n');});
socket.on('close',()=>{if(this.socket!==socket)return;if(this.pendingCR){this.event('\n');this.pendingCR=false;}if(this.state!=='error')this.state='offline';this.event('\n[Sessione terminata]\n');});
socket.on('data',chunk=>{if(this.socket!==socket)return;const text=this.decoder.write(telnet.read(chunk));if(this.stage<3){this.auth=(this.auth+text).slice(-12000);
if(this.stage===0&&/Username:/i.test(this.auth)){socket.write(credentials.user+'\r');this.stage=1;this.auth='';return;}
if(this.stage===1&&/Password:/i.test(this.auth)){socket.write(credentials.password+'\r');this.stage=2;this.auth='';return;}
if(this.stage===2){if(/invalid|incorrect|denied|Username:/i.test(this.auth)){socket.destroy(new Error('Accesso rifiutato'));return;}const welcome=this.auth.search(/Welcome|[A-Z0-9]{1,6}:[A-Z0-9-]+>/i);if(welcome>=0){this.stage=3;this.state='online';socket.setTimeout(0);this.output(this.auth.slice(welcome));this.auth='';}}return;
}this.output(text);});return this.snapshot();}
output(text){let out='';for(const c of text){if(this.pendingCR){out+='\n';this.pendingCR=false;if(c==='\n'||c==='\0')continue;}
if(this.escape){this.escape+=c;if(this.escape.length>128||/^\x1b\[[0-?]*[ -/]*[@-~]$/.test(this.escape)||this.escape.length===2&&c!=='[')this.escape='';continue;}
if(c==='\x1b'){this.escape=c;continue;}if(c==='\r'){this.pendingCR=true;continue;}if(c==='\b'){out=out.slice(0,-1);continue;}if(c==='\n'||c==='\t'||c.charCodeAt(0)>=32)out+=c;
}this.event(out);}
send(text){if(this.state!=='online'||!this.socket||this.socket.destroyed)throw new Error('Collega prima il terminale');if(typeof text!=='string'||text.length>1000||/[\x00-\x1f\x7f]/.test(text))throw new Error('Invia un solo comando, massimo 1000 caratteri');this.lastInput=this.lastAccess=Date.now();this.event(text+'\n');this.socket.write(text+'\r');}
disconnect(reason='Disconnessione richiesta'){this.lastInput=Date.now();if(this.socket&&!this.socket.destroyed)this.socket.destroy();this.state='offline';this.event('\n['+reason+']\n');}
close(){this.disconnect();clearInterval(this.timer);}
}