- AbitaregliIblei.it

- Server aggiornamenti agli script.
- Editor HTML corretto un po'.
- Record Mysql per server (appena iniziato)
This commit is contained in:
Surya Paolo
2024-09-06 19:57:09 +02:00
parent 45f601bd26
commit fe4a67c9f1
28 changed files with 638 additions and 567 deletions

View File

@@ -17,8 +17,6 @@ var http = require('http');
const WebSocket = require('ws');
const { spawn } = require('child_process');
const pty = require('node-pty');
const NUOVO_METODO_TEST = true;
const METODO_MULTI_CORS = false;
@@ -283,6 +281,9 @@ async function mystart() {
// await estraiTutteLeImmagini();
console.log('Versione Server: ' + await tools.getVersServer());
await tools.getApps();
if (process.env.PROD !== 1) {
@@ -716,11 +717,6 @@ async function faitest() {
}
}
/*const domains = [
{ hostname: 'piuchebuono.app', port: 3000 },
{ hostname: 'gruppomacro.app', port: 3010 },
];*/
function getCredentials(hostname) {
if (NUOVO_METODO_TEST) {
@@ -801,6 +797,8 @@ function startServer(app, port) {
let httpsServer = null;
let httpServer = null;
console.log('isProduction', isProduction);
if (isProduction) {
for (let i = 0; i < domains.length; i++) {
const credentials = getCredentials(domains[i].hostname);
@@ -859,82 +857,98 @@ function startServer(app, port) {
// process.exit(1);
}
wss.on('connection', (ws) => {
// console.log('Client connected');
let scriptProcess = null;
if (wss) {
ws.on('message', (message) => {
const parsedMessage = JSON.parse(message);
wss.on('connection', (ws) => {
console.log('Client socket connected...');
if (parsedMessage.type === 'start_script') {
if (scriptProcess) {
scriptProcess.kill();
}
const { User } = require('./models/user');
const scriptPath = path.join(__dirname, '..', '..', '', parsedMessage.scriptName);
let scriptProcess = null;
// Verifica che lo script esista e sia all'interno della directory consentita
if (fs.existsSync(scriptPath)) {
scriptProcess = pty.spawn('bash', [scriptPath], {
name: 'xterm-color',
cols: 80,
rows: 40,
cwd: process.cwd(),
env: process.env
});
try {
let buffer = '';
scriptProcess.on('data', (data) => {
buffer += data;
const pty = require('node-pty');
// Invia l'output al client
ws.send(JSON.stringify({ type: 'output', data: data }));
ws.on('message', (message) => {
const parsedMessage = JSON.parse(message);
// Controlla se c'è una richiesta di input
if (buffer.endsWith(': ') || buffer.includes('? ') ||
buffer.toLowerCase().includes('password')
|| buffer.includes('Inserisci')
|| buffer.includes('Inserted')
|| buffer.includes('(Y')
) {
ws.send(JSON.stringify({ type: 'input_required', prompt: data.trim() }));
buffer = '';
if ((parsedMessage.type === 'start_script') && (User.isAdminById(parsedMessage.user_id))) {
if (scriptProcess) {
scriptProcess.kill();
}
// Pulisci il buffer se diventa troppo grande
if (buffer.length > 5024) {
buffer = buffer.slice(-500);
}
});
const scriptPath = path.join(__dirname, '..', '..', '', parsedMessage.scriptName);
scriptProcess.on('exit', (code) => {
if (code === 0) {
ws.send(JSON.stringify({ type: 'close', data: `*** FINE SCRIPT ***` }));
// Verifica che lo script esista e sia all'interno della directory consentita
if (fs.existsSync(scriptPath)) {
scriptProcess = pty.spawn('bash', [scriptPath], {
name: 'xterm-color',
cols: 80,
rows: 40,
cwd: process.cwd(),
env: process.env
});
let buffer = '';
scriptProcess.on('data', (data) => {
buffer += data;
// Invia l'output al client
ws.send(JSON.stringify({ type: 'output', data: data }));
// Controlla se c'è una richiesta di input
if (buffer.endsWith(': ') || buffer.includes('? ') ||
buffer.toLowerCase().includes('password')
|| buffer.includes('Inserisci')
|| buffer.includes('Inserted')
|| buffer.includes('(Y')
) {
ws.send(JSON.stringify({ type: 'input_required', prompt: data.trim() }));
buffer = '';
}
// Pulisci il buffer se diventa troppo grande
if (buffer.length > 5024) {
buffer = buffer.slice(-500);
}
});
scriptProcess.on('exit', (code) => {
if (code === 0) {
ws.send(JSON.stringify({ type: 'close', data: `*** FINE SCRIPT ***` }));
} else {
ws.send(JSON.stringify({ type: 'close', data: `Script terminato con codice ${code}` }));
}
});
} else {
ws.send(JSON.stringify({ type: 'close', data: `Script terminato con codice ${code}` }));
ws.send(JSON.stringify({ type: 'error', data: 'Script non trovato o non autorizzato' }));
}
});
} else {
ws.send(JSON.stringify({ type: 'error', data: 'Script non trovato o non autorizzato' }));
}
} else if (parsedMessage.type === 'input') {
if (scriptProcess) {
scriptProcess.write(parsedMessage.data + '\n');
}
} else if (parsedMessage.type === 'input') {
if (scriptProcess) {
scriptProcess.write(parsedMessage.data + '\n');
}
}
});
ws.on('close', () => {
console.log('*** Client socket disconnected');
if (scriptProcess) {
scriptProcess.kill();
}
});
} catch (error) {
console.error('connection: Errore durante l\'inizializzazione del WebSocket, error:', error.message);
}
});
ws.on('close', () => {
console.log('Client disconnected');
if (scriptProcess) {
scriptProcess.kill();
}
});
});
} else {
console.error('Nessuna Socket Aperta con WebSocket !!');
}
} catch (e) {
console.log('error ' + e);
console.log('error startServer: ' + e);
}
}