diff --git a/Client/app.js b/Client/app.js index b813471..13d4c3a 100644 --- a/Client/app.js +++ b/Client/app.js @@ -8,6 +8,7 @@ require('dotenv').config(); const fs = require('fs'); const { DebugBuilder } = require("./utilities/debugBuilder"); const deployCommands = require('./utilities/deployCommands'); +const { checkIn } = require("./controllers/clientController"); var indexRouter = require('./routes/index'); var botRouter = require('./routes/bot'); @@ -143,6 +144,9 @@ discordClient.on('ready', () => { log.DEBUG(`Starting HTTP Server`); runHTTPServer(); + + log.DEBUG("Checking in with the master server") + checkIn(); }); // Setup any additional event handlers diff --git a/Client/config/clientConfig.js b/Client/config/clientConfig.js index a27cc52..aa8894f 100644 --- a/Client/config/clientConfig.js +++ b/Client/config/clientConfig.js @@ -12,7 +12,7 @@ exports.clientConfig = { // Configuration for the connection to the server exports.serverConfig = { - "ip": "127.0.0.1", + "ip": "172.16.100.108", "hostname": "localhost", "port": 3000 } diff --git a/Client/controllers/clientController.js b/Client/controllers/clientController.js index 03cab7f..bbf26a1 100644 --- a/Client/controllers/clientController.js +++ b/Client/controllers/clientController.js @@ -1,9 +1,11 @@ // Debug -// const { DebugBuilder } = require("../utilities/debugBuilder.js"); -// const log = new DebugBuilder("client", "clientController"); +const { DebugBuilder } = require("../utilities/debugBuilder.js"); +const log = new DebugBuilder("client", "clientController"); // Configs const config = require("../config/clientConfig"); const modes = require("../config/modes"); +// Modules +const { executeAsyncConsoleCommand } = require("../utilities/executeConsoleCommands.js"); // Utilities const updateConfig = require("../utilities/updateConfig"); const updatePreset = require("../utilities/updatePresets"); @@ -29,6 +31,34 @@ function checkBodyForPresetFields(req, res, callback) { return callback(); } +async function checkLocalIP() { + let ipAddr; + if (process.platform === "win32") { + // Windows + var networkConfig = executeAsyncConsoleCommand("ipconfig"); + log.DEBUG('Network Config: ', networkConfig); + var networkConfigLines = networkConfig.split("\n").filter(line => { + if (!line.includes(":")) return false; + + line = line.split(":"); + + if (!line.length === 2) return false; + + return true; + }).map(line => { + line = line.split(':'); + line[0] = line[0].replace(".", ""); + return line; + }); + log.DEBUG("Parsed IP Config Results: ", networkConfigLines); + return networkConfigLines['IPv4 Address']; + } + else { + // Linux + var networkConfig = executeAsyncConsoleCommand("ip addr"); + } +} + /** Check in with the server * If the bot has a saved ID, check in with the server to update any information or just check back in * If the bot does not have a saved ID, it will attempt to request a new ID from the server @@ -40,6 +70,7 @@ exports.checkIn = async () => { // ID was not found in the config, creating a new node reqOptions = new requests.requestOptions("/nodes/newNode", "POST"); delete config.clientConfig.id; + client.clientConfig.ip = checkLocalIP(); requests.sendHttpRequest(reqOptions, JSON.stringify(config.clientConfig), (responseObject) => { // Update the client's ID if the server accepted it if (responseObject.statusCode === 202) { diff --git a/Client/utilities/executeConsoleCommands.js b/Client/utilities/executeConsoleCommands.js index d601ff1..05b35fc 100644 --- a/Client/utilities/executeConsoleCommands.js +++ b/Client/utilities/executeConsoleCommands.js @@ -11,7 +11,7 @@ const execCommand = promisify(exec); async function executeAsyncConsoleCommand(consoleCommand) { // Check to see if the command is a real command // TODO needs to be improved - const acceptableCommands = [ "arecord -L" ]; + const acceptableCommands = [ "arecord -L", 'ipconfig', 'ip addr' ]; if (!acceptableCommands.includes(consoleCommand)) { log.WARN("Console command is not acceptable: ", consoleCommand); return undefined;