Init Commit
Server
- Working init discord bot
- Modular events and commands
- Has access to the socket server
- Working init socket server
- Need to work on getting access to discord bot
- Working init web server
Currently working on breaking out the init of the socket server
Client
- Working init socket client
Currently working on the discord bot to join voice channels
This commit is contained in:
49
client/modules/baseUtils.mjs
Normal file
49
client/modules/baseUtils.mjs
Normal file
@@ -0,0 +1,49 @@
|
||||
import { networkInterfaces } from 'os';
|
||||
import { createHash, randomBytes } from 'crypto';
|
||||
|
||||
/**
|
||||
* Check to see if the input is a valid JSON string
|
||||
*
|
||||
* @param {*} str The string to check for valud JSON
|
||||
* @returns {true|false}
|
||||
*/
|
||||
export function isJsonString (str) {
|
||||
try {
|
||||
JSON.parse(str);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate a unique ID for a given device, this will also be unique on the same device at a different time.
|
||||
* @returns {string} A generated unique ID
|
||||
*/
|
||||
export function generateUniqueID () {
|
||||
const netInterfaces = networkInterfaces();
|
||||
let macAddress = '';
|
||||
|
||||
// Find the first non-internal MAC address
|
||||
for (const key in netInterfaces) {
|
||||
const iface = netInterfaces[key][0];
|
||||
if (!iface.internal) {
|
||||
macAddress = iface.mac;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If no non-internal MAC address is found, fallback to a random value
|
||||
if (!macAddress) {
|
||||
macAddress = randomBytes(6).toString('hex').toUpperCase();
|
||||
}
|
||||
|
||||
// Use MAC address and current timestamp to create a unique ID
|
||||
const timestamp = Date.now();
|
||||
const uniqueID = createHash('sha256')
|
||||
.update(macAddress + timestamp)
|
||||
.digest('hex');
|
||||
|
||||
return uniqueID;
|
||||
}
|
||||
40
client/modules/clientObjectDefinitions.mjs
Normal file
40
client/modules/clientObjectDefinitions.mjs
Normal file
@@ -0,0 +1,40 @@
|
||||
import dotenv from 'dotenv';
|
||||
dotenv.config()
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
export class ClientNodeObject {
|
||||
/**
|
||||
*
|
||||
* @param {string} param0._id The ID of the node (Assigned by the client on first boot)
|
||||
* @param {string} param0._name The name of the node (Assigned by the user)
|
||||
* @param {string} param0._location The physical location of the node (Assigned by the user)
|
||||
* @param {object} param0._nearbySystems An object array of nearby systems (Assigned by the user)
|
||||
*/
|
||||
constructor({ _id = undefined, _name = undefined, _location = undefined, _nearbySystems = undefined}) {
|
||||
this.id = _id;
|
||||
this.name = _name;
|
||||
this.location = _location;
|
||||
this.nearbySystems = _nearbySystems;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** The configuration object for the node */
|
||||
export class ClientNodeConfig {
|
||||
constructor({
|
||||
_id = process.env.CLIENT_ID,
|
||||
_name = process.env.CLIENT_NAME,
|
||||
_location = process.env.CLIENT_LOCATION,
|
||||
_nearbySystems = undefined, // TODO - Get the presets when loading the config instead of having to do it after the fact
|
||||
_serverIp = process.env.SERVER_IP,
|
||||
_serverPort = process.env.SERVER_PORT,
|
||||
}) {
|
||||
this.node = new ClientNodeObject({
|
||||
_id:_id, _name:_name, _location:_location, _nearbySystems:_nearbySystems
|
||||
});
|
||||
this.serverIp = _serverIp;
|
||||
this.serverPort = _serverPort;
|
||||
}
|
||||
}
|
||||
37
client/modules/socketClient.mjs
Normal file
37
client/modules/socketClient.mjs
Normal file
@@ -0,0 +1,37 @@
|
||||
import { io } from "socket.io-client";
|
||||
|
||||
export function initSocketConnection() {
|
||||
const serverEndpoint = `http://${localNodeConfig.serverIp}:${localNodeConfig.serverPort}` || 'http://localhost:3000'; // Adjust the server endpoint
|
||||
|
||||
const socket = io.connect(serverEndpoint);
|
||||
|
||||
return socket;
|
||||
}
|
||||
|
||||
export function initSocketListeners(socket){
|
||||
socket.on('connect', () => {
|
||||
console.log('Connected to the server');
|
||||
logIntoServer(socket);
|
||||
});
|
||||
|
||||
socket.on('node-join', (joinData) => {
|
||||
console.log("Join requested: ", joinData)
|
||||
});
|
||||
|
||||
socket.on('node-leave', () => {
|
||||
console.log("Leave requested");
|
||||
});
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
console.log('Disconnected from the server');
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
export function logIntoServer(socket, nodeData) {
|
||||
socket.emit("node-login", nodeData);
|
||||
}
|
||||
|
||||
export function sendNodeUpdate(socket, nodeData) {
|
||||
socket.emit('node-update', nodeData);
|
||||
}
|
||||
107
client/modules/updateConfig.mjs
Normal file
107
client/modules/updateConfig.mjs
Normal file
@@ -0,0 +1,107 @@
|
||||
// Modules
|
||||
import replace from 'replace-in-file';
|
||||
|
||||
class Options {
|
||||
constructor(key, updatedValue) {
|
||||
this.files = "./.env";
|
||||
// A regex of the line containing the key in the config file
|
||||
this.from = new RegExp(`${key}="?(.+)"?`, "g");
|
||||
// Check to see if the value is a string and needs to be wrapped in double quotes
|
||||
if (Array(["string", "number"]).includes(typeof updatedValue)) this.to = `${key}="${updatedValue}",`;
|
||||
else this.to = `${key}=${updatedValue}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper to update the client's saved ID
|
||||
* @param updatedId The updated ID assigned to the node
|
||||
*/
|
||||
export function updateId (updatedId) {
|
||||
updateConfig('CLIENT_ID', updatedId);
|
||||
process.env.CLIENT_ID = updatedId;
|
||||
console.log("Updated ID to: ", updatedId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper to update any or all keys in the client config
|
||||
*
|
||||
* @param {Object} runningConfig Running config object
|
||||
* @param {Object} newConfigObject Object with what keys you wish to update (node object format, will be converted)
|
||||
* @param {number} newConfigObject.id The ID given to the node to update
|
||||
* @param {string} newConfigObject.name The name of the node
|
||||
* @param {string} newConfigObject.ip The IP the server can contact the node on
|
||||
* @param {number} newConfigObject.port The port the server can contact the node on
|
||||
* @param {string} newConfigObject.location The physical location of the node
|
||||
* @returns
|
||||
*/
|
||||
export function updateClientConfig (runningConfig, newConfigObject) {
|
||||
var updatedKeys = []
|
||||
const configKeys = Object.keys(newConfigObject);
|
||||
|
||||
if (configKeys.includes("id")) {
|
||||
if (runningConfig.id != newConfigObject.id) {
|
||||
this.updateId(newConfigObject.id);
|
||||
updatedKeys.push({ 'CLIENT_ID': newConfigObject.id });
|
||||
}
|
||||
}
|
||||
if (configKeys.includes("name")) {
|
||||
if (runningConfig.name != newConfigObject.name) {
|
||||
this.updateConfig('CLIENT_NAME', newConfigObject.name);
|
||||
updatedKeys.push({ 'CLIENT_NAME': newConfigObject.name });
|
||||
process.env.CLIENT_NAME = newConfigObject.name;
|
||||
console.log("Updated name to: ", newConfigObject.name);
|
||||
}
|
||||
}
|
||||
if (configKeys.includes("ip")) {
|
||||
if (runningConfig.ip != newConfigObject.ip) {
|
||||
this.updateConfig('CLIENT_IP', newConfigObject.ip);
|
||||
updatedKeys.push({ 'CLIENT_IP': newConfigObject.ip });
|
||||
process.env.CLIENT_IP = newConfigObject.ip;
|
||||
console.log("Updated ip to: ", newConfigObject.ip);
|
||||
}
|
||||
}
|
||||
if (configKeys.includes("port")) {
|
||||
if (runningConfig.port != newConfigObject.port) {
|
||||
this.updateConfig('CLIENT_PORT', newConfigObject.port);
|
||||
updatedKeys.push({ 'CLIENT_PORT': newConfigObject.port });
|
||||
process.env.CLIENT_PORT = newConfigObject.port;
|
||||
console.log("Updated port to: ", newConfigObject.port);
|
||||
}
|
||||
}
|
||||
if (configKeys.includes("location")) {
|
||||
if (runningConfig.location != newConfigObject.location) {
|
||||
this.updateConfig('CLIENT_LOCATION', newConfigObject.location);
|
||||
updatedKeys.push({ 'CLIENT_LOCATION': newConfigObject.location });
|
||||
process.env.CLIENT_LOCATION = newConfigObject.location;
|
||||
console.log("Updated location to: ", newConfigObject.location);
|
||||
}
|
||||
}
|
||||
|
||||
return updatedKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} key The config file key to update with the value
|
||||
* @param {string} value The value to update the key with
|
||||
*/
|
||||
export function updateConfig (key, value) {
|
||||
const options = new Options(key, value);
|
||||
|
||||
updateConfigFile(options, (updatedFiles) => {
|
||||
// Do Something
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper to write changes to the file
|
||||
* @param options An instance of the Objects class specified to the key being updated
|
||||
* @param callback Callback when the files have been modified
|
||||
*/
|
||||
function updateConfigFile(options, callback) {
|
||||
replace(options, (error, changedFiles) => {
|
||||
if (error) return console.error('Error occurred:', error);
|
||||
console.log('Updated config file: ', changedFiles);
|
||||
callback(changedFiles);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user