Adding Server Checkin

- Update client handler to check IP
- Add checkin to startup
- Add acceptable commands
- Needs linux command
- Needs testing
This commit is contained in:
Logan Cusano
2023-04-30 04:42:04 -04:00
parent 95c99971a2
commit 0cefdba00f
4 changed files with 39 additions and 4 deletions

View File

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

View File

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

View File

@@ -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) {

View File

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