- attivita

- gestione degli script sul server
 - creato websocket per interagire con gli script del server.
This commit is contained in:
Surya Paolo
2024-08-29 23:30:58 +02:00
parent d527f49c5e
commit 45f601bd26
25 changed files with 534 additions and 45 deletions

View File

@@ -1,5 +1,4 @@
require('./config/config');
require('./config/config');
// console.log(" lodash");
@@ -15,6 +14,11 @@ const fs = require('fs');
var https = require('https');
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;
@@ -49,6 +53,9 @@ const Site = require('./models/site');
const i18n = require('i18n');
const readline = require('readline');
let credentials = null;
// OBTAIN
@@ -791,11 +798,14 @@ function startServer(app, port) {
console.log('domains', domains);
let httpsServer = null;
let httpServer = null;
if (isProduction) {
for (let i = 0; i < domains.length; i++) {
const credentials = getCredentials(domains[i].hostname);
// console.log('credentials: ', credentials);
const httpsServer = https.createServer(credentials, app);
httpsServer = https.createServer(credentials, app);
console.log('⭐️⭐️⭐️⭐️⭐️ HTTPS server: ' + domains[i].hostname + ' Port:', domains[i].port);
httpsServer.listen(domains[i].port);
}
@@ -816,7 +826,7 @@ function startServer(app, port) {
}
if (credentials) {
const httpsServer = https.createServer(credentials, app);
httpsServer = https.createServer(credentials, app);
console.log('⭐️⭐️⭐️ HTTPS server IN LOCALE : port', port);
httpsServer.listen(port);
} else {
@@ -837,6 +847,92 @@ function startServer(app, port) {
}
}
}
let wss = null;
if (httpsServer) {
wss = new WebSocket.Server({ server: httpsServer });
} else if (httpServer) {
wss = new WebSocket.Server({ server: httpServer });
} else {
// console.error('Nessun server HTTP o HTTPS disponibile per WebSocket');
// process.exit(1);
}
wss.on('connection', (ws) => {
// console.log('Client connected');
let scriptProcess = null;
ws.on('message', (message) => {
const parsedMessage = JSON.parse(message);
if (parsedMessage.type === 'start_script') {
if (scriptProcess) {
scriptProcess.kill();
}
const scriptPath = path.join(__dirname, '..', '..', '', parsedMessage.scriptName);
// 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: 'error', data: 'Script non trovato o non autorizzato' }));
}
} else if (parsedMessage.type === 'input') {
if (scriptProcess) {
scriptProcess.write(parsedMessage.data + '\n');
}
}
});
ws.on('close', () => {
console.log('Client disconnected');
if (scriptProcess) {
scriptProcess.kill();
}
});
});
} catch (e) {
console.log('error ' + e);
}