Command: cat /usr/bin/connections | Container Owner: bastothemax #!/usr/bin/node const fs = require('fs'); const readline = require('readline'); const { execSync } = require('child_process'); const { argv } = require('process'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const filename = '/var/connections.json'; let connections = loadConnections(); function loadConnections() { try { const data = fs.readFileSync(filename, 'utf8'); if (!data.trim()) { console.log(`${filename} is empty. Creating the file...`); fs.writeFileSync(filename, '[]'); return []; } return JSON.parse(data); } catch (err) { console.error(`Error reading ${filename}: ${err}`); return []; } } function saveConnections() { fs.writeFileSync(filename, JSON.stringify(connections, null, 2)); } function generateRandomString(length) { const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; let connector = ''; for (let i = 0; i < length; i++) { connector += chars.charAt(Math.floor(Math.random() * chars.length)); } return connector; } function displayMenu() { console.log("\n1. Browse Connections"); console.log("2. Create Connection"); console.log("3. Remove Connection"); console.log("4. Edit Connection"); console.log("5. Start all connections using PM2"); console.log("6. List PM2 Processes"); console.log("7. Exit"); rl.question("Enter your choice: ", (choice) => { switch (choice.trim()) { case '1': browseConnections(); break; case '2': createConnectionMenu(); break; case '3': removeConnectionMenu(); break; case '4': editConnection(); break; case '5': startAllConnections(); break; case '6': listPM2Processes(); break; case '7': saveConnections(); rl.close(); break; default: console.log("Invalid option"); displayMenu(); } }); } function browseConnections() { console.log("\nConnections:"); connections.forEach((conn, index) => { console.log(`${index + 1}. ${conn.name} - Port: ${conn.port}, Connector: ${conn.connector}`); }); displayMenu(); } function browseConnectionsNoMenu() { console.log("\nConnections:"); connections.forEach((conn, index) => { console.log(`${index + 1}. ${conn.name} - Port: ${conn.port}, Connector: ${conn.connector}`); }); } function createConnectionMenu() { rl.question("Enter port number: ", (port) => { rl.question("Enter connection name: ", (name) => { createConnection(port, name); displayMenu(); }); }); } function createConnection(port, name) { const connector = generateRandomString(128); connections.push({ name, port, connector }); saveConnections(); // Save after adding console.log(`Connection '${name}' created successfully on port ${port}.`); // Start PM2 process for the new connection const pm2cmd = `pm2 start holesail --name ${name} -- --live ${port} --connector "${connector}"`; try { execSync(pm2cmd, { stdio: 'inherit' }); console.log(`Started '${name}' using PM2 on port ${port}.`); return connector; // Return the generated connector } catch (err) { console.error(`Error starting '${name}' using PM2: ${err.message}`); return null; // Return null if there was an error } } function removeConnectionMenu() { rl.question("Enter connection name to remove: ", (name) => { deleteConnection(name); displayMenu(); }); } function deleteConnection(name) { const idx = connections.findIndex((conn) => conn.name === name); if (idx !== -1) { connections.splice(idx, 1); saveConnections(); // Save after removing console.log(`Connection '${name}' removed successfully.`); // Remove from PM2 const pm2cmd = `pm2 delete ${name}`; try { execSync(pm2cmd, { stdio: 'inherit' }); console.log(`Removed '${name}' from PM2.`); } catch (err) { console.error(`Error removing '${name}' from PM2: ${err.message}`); } } else { console.log(`Connection '${name}' not found.`); } } function editConnection() { rl.question("Enter connection index to edit: ", (index) => { const idx = parseInt(index.trim()) - 1; if (idx >= 0 && idx < connections.length) { rl.question("Enter new port number: ", (port) => { connections[idx].port = port; saveConnections(); // Save after editing console.log("Connection edited successfully."); displayMenu(); }); } else { console.log("Invalid index."); displayMenu(); } }); } function startAllConnections() { console.log("Starting all connections using PM2..."); connections.forEach((conn) => { const cmd = `pm2 start holesail --name ${conn.name} -- --live ${conn.port} --connector "${conn.connector}"`; try { execSync(cmd, { stdio: 'inherit' }); console.log(`Started connection ${conn.name} on port ${conn.port}`); } catch (err) { console.error(`Error starting connection ${conn.name}: ${err.message}`); } }); displayMenu(); } function listPM2Processes() { console.log("Listing PM2 processes..."); try { const pm2ListCmd = `pm2 list`; const output = execSync(pm2ListCmd, { encoding: 'utf8' }); console.log(output); } catch (err) { console.error(`Error listing PM2 processes: ${err.message}`); } displayMenu(); } // Process command line arguments // Process command line arguments if (argv.includes('--new')) { const index = argv.indexOf('--new'); if (index !== -1 && index + 2 < argv.length) { const port = argv[index + 1]; const name = argv[index + 2]; const connector = createConnection(port, name); if (connector) { console.log(`Connection '${name}' created successfully with connector: ${connector}`); } else { console.log(`Failed to create connection '${name}'.`); } process.exit(); } else { console.log("Please provide a port number and connection name."); } } else if (argv.includes('--delete')) { const index = argv.indexOf('--delete'); if (index !== -1 && index + 1 < argv.length) { const nameToDelete = argv[index + 1]; deleteConnection(nameToDelete); process.exit() } else { console.log("Please provide a connection name to delete."); } } else if (argv.includes('--start')) { // Start all connections using PM2 startAllConnections(); process.exit() } else if (argv.includes('--list')) { // List all connections browseConnectionsNoMenu(); process.exit(); } else { // Start the program if no command-line arguments are provided displayMenu(); }