update server to save static key to .env

This commit is contained in:
Raven Scott 2024-11-29 18:00:45 -05:00
parent 01e5973ebc
commit 182460f091
3 changed files with 32 additions and 3 deletions

View File

@ -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"

1
server/.env Normal file
View File

@ -0,0 +1 @@
SERVER_KEY=1288060fad7d4f88928a704fa03e48efc04c1ebb4614093ea720b934f184361b

View File

@ -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 });