diff --git a/package.json b/package.json index 671938a..52ab8df 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,8 @@ }, "dependencies": { "dockernode": "^0.1.0", + "dockerode": "^4.0.2", + "dotenv": "^16.4.5", "hyperswarm": "^4.8.4", "xterm": "^5.3.0", "xterm-addon-fit": "^0.8.0" diff --git a/server/.env b/server/.env new file mode 100644 index 0000000..5dff82d --- /dev/null +++ b/server/.env @@ -0,0 +1 @@ +SERVER_KEY=1288060fad7d4f88928a704fa03e48efc04c1ebb4614093ea720b934f184361b diff --git a/server/server.js b/server/server.js index 7ea085e..1e0d274 100644 --- a/server/server.js +++ b/server/server.js @@ -5,16 +5,42 @@ import Docker from 'dockerode'; import crypto from 'hypercore-crypto'; import { PassThrough } from 'stream'; import os from "os"; +import fs from 'fs'; +import dotenv from 'dotenv'; -const docker = new Docker({ socketPath: os.platform() === "win32" ? '//./pipe/dockerDesktopLinuxEngine' : '/var/run/docker.sock' }); +// Load environment variables from .env file +dotenv.config(); + +const docker = new Docker({ + socketPath: os.platform() === "win32" ? '//./pipe/dockerDesktopLinuxEngine' : '/var/run/docker.sock', +}); const swarm = new Hyperswarm(); const connectedPeers = new Set(); const terminalSessions = new Map(); // Map to track terminal sessions per peer -// Generate a topic for the server -const topic = crypto.randomBytes(32); +// Function to generate a new key +function generateNewKey() { + const newKey = crypto.randomBytes(32); + fs.appendFileSync('.env', `SERVER_KEY=${newKey.toString('hex')}\n`, { flag: 'a' }); + return newKey; +} + +// Load or generate the topic key +let keyHex = process.env.SERVER_KEY; +if (!keyHex) { + console.log('[INFO] No SERVER_KEY found in .env. Generating a new one...'); + const newKey = generateNewKey(); + keyHex = newKey.toString('hex'); +} else { + console.log('[INFO] SERVER_KEY loaded from .env.'); +} + +// Convert the keyHex to a Buffer +const topic = Buffer.from(keyHex, 'hex'); + console.log(`[INFO] Server started with topic: ${topic.toString('hex')}`); +// Start listening or further implementation logic here // Join the swarm with the generated topic swarm.join(topic, { server: true, client: false });