Files
DRB-CnC/Client/controllers/clientController.js
Logan Cusano 851a9c55fa Fix for #32
- Add the response check when updating client info
2023-06-23 21:43:05 -04:00

225 lines
9.2 KiB
JavaScript

// Debug
const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("client", "clientController");
// Configs
require('dotenv').config();
const modes = require("../config/modes");
// Modules
const { executeAsyncConsoleCommand, nodeObject, BufferToJson } = require("../utilities/utilities");
// Utilities
const { updateId, updateConfig } = require("../utilities/updateConfig");
const { updatePreset, addNewPreset, getPresets, removePreset } = require("../utilities/updatePresets");
const { onHttpError, requestOptions, sendHttpRequest } = require("../utilities/httpRequests");
var runningClientConfig = new nodeObject({_id: process.env.CLIENT_ID, _ip: process.env.CLIENT_IP, _name: process.env.CLIENT_NAME, _port: process.env.CLIENT_PORT, _location: process.env.CLIENT_LOCATION, _nearbySystems: getPresets(), _online: process.env.CLIENT_ONLINE});
/**
* Check the body for the required fields to update or add a preset
* @param req Express req from the endpoint controller
* @param res Express res from the endpoint controller
* @param callback The callback function to call when this function completes
* @returns {*}
*/
function checkBodyForPresetFields(req, res, callback) {
if (!req.body?.systemName) return res.status(403).json({"message": "No system in the request"});
if (!req.body?.frequencies && Array.isArray(req.body.frequencies)) return res.status(403).json({"message": "No frequencies in the request or type is not an array"});
if (!req.body?.mode && typeof req.body.mode === "string") return res.status(403).json({"message": "No mode in the request"});
if (!req.body?.trunkFile) {
if (modes.digitalModes.includes(req.body.mode)) return res.status(403).json({"message": "No trunk file in the request but digital mode specified. If you are not using a trunk file for this frequency make sure to specify 'none' for trunk file in the request"})
// If there is a value keep it but if not, add nothing so the system can update that key (if needed)
req.body.trunkFile = req.body.trunkFile ?? "none";
}
return callback();
}
async function checkLocalIP() {
if (process.platform === "win32") {
// Windows
var networkConfig = await executeAsyncConsoleCommand("ipconfig");
log.DEBUG('Network Config: ', networkConfig);
var networkConfigLines = await networkConfig.split("\n").filter(line => {
if (!line.includes(":")) return false;
line = line.split(":");
if (!line.length === 2) return false;
return true;
}).map(line => {
line = String(line).split(':', 2);
line[0] = String(line[0]).replace(/[.]|[\s]/g, "").trim();
line[1] = String(line[1]).replace(/(\\r|\\n)/g, "").trim();
return line;
});
networkConfig = Object.fromEntries(networkConfigLines);
log.DEBUG("Parsed IP Config Results: ", networkConfig);
log.DEBUG("Local IP address: ", networkConfig['IPv4Address']);
return networkConfig['IPv4Address'];
}
else {
// Linux
var networkConfig = await executeAsyncConsoleCommand("ip addr");
}
}
/**
* Checks the config file for all required fields or gets and updates the required fields
*/
exports.checkConfig = async function checkConfig() {
if (!runningClientConfig.id || runningClientConfig.id == 0 || runningClientConfig.id == '0') {
await updateId(0);
runningClientConfig.id = 0;
}
if (!runningClientConfig.ip) {
const ipAddr = await checkLocalIP();
await updateConfig('CLIENT_IP', ipAddr);
runningClientConfig.ip = ipAddr;
}
if(!runningClientConfig.name) {
const lastOctet = await String(checkLocalIP()).spit('.')[-1];
const name = `Radio-Node-${lastOctet}`;
await updateConfig('CLIENT_NAME', name);
runningClientConfig.name = name;
}
if(!runningClientConfig.port) {
const port = 3010;
await updateConfig('CLIENT_PORT', port);
runningClientConfig.port = port;
}
}
/** 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
*/
exports.checkIn = async () => {
let reqOptions;
await this.checkConfig();
runningClientConfig.online = true;
// Check if there is an ID found, if not add the node to the server. If there was an ID, check in with the server to make sure it has the correct information
try {
if (!runningClientConfig?.id || runningClientConfig.id == 0) {
// ID was not found in the config, creating a new node
reqOptions = new requestOptions("/nodes/newNode", "POST");
sendHttpRequest(reqOptions, JSON.stringify(runningClientConfig), async (responseObject) => {
// Check if the server responded
if (!responseObject) {
log.WARN("Server did not respond to checkIn. Will wait 60 seconds then try again");
setTimeout(() => {
// Run itself again to see if the server is up now
this.checkIn();
}, 60000);
return
}
// Update the client's ID if the server accepted its
if (responseObject.statusCode === 202) {
runningClientConfig.id = responseObject.body.nodeId;
log.DEBUG("Response object from new node: ", responseObject, runningClientConfig);
await updateId(runningClientConfig.id);
}
if (responseObject.statusCode >= 300) {
// Server threw an error
log.DEBUG("HTTP Error: ", responseObject, await BufferToJson(responseObject.body));
await onHttpError(responseObject.statusCode);
}
});
}
else {
// ID is in the config, checking in with the server
reqOptions = new requestOptions("/nodes/nodeCheckIn", "POST");
sendHttpRequest(reqOptions, JSON.stringify(runningClientConfig), (responseObject) => {
log.DEBUG("Check In Respose: ", responseObject);
// Check if the server responded
if (!responseObject) {
log.WARN("Server did not respond to checkIn. Will wait 60 seconds then try again");
setTimeout(() => {
// Run itself again to see if the server is up now
this.checkIn();
}, 60000);
return
}
if (responseObject.statusCode === 202) {
log.DEBUG("Updated keys: ", responseObject.body.updatedKeys)
// Server accepted an update
}
if (responseObject.statusCode === 200) {
// Server accepted the response but there were no keys to be updated
}
if (responseObject.statusCode >= 300) {
// Server threw an error
onHttpError(responseObject.statusCode);
}
});
}
}
catch (err) {
log.ERROR("Error checking in: ", err);
}
}
/** Controller for the /client/requestCheckIn endpoint
* This is the endpoint wrapper to queue a check in
*/
exports.requestCheckIn = async (req, res) => {
this.checkIn();
return res.sendStatus(200);
}
/** Controller for the /client/presets endpoint
* This is the endpoint wrapper to get the presets object
*/
exports.getPresets = async (req, res) => {
return res.status(200).json(getPresets());
}
/** Controller for the /client/updatePreset endpoint
* This is the endpoint wrapper to update the selected preset (must include the whole object for that preset otherwise it will be rejected)
*/
exports.updatePreset = async (req, res) => {
checkBodyForPresetFields(req, res, () => {
updatePreset(req.body.systemName, () => {
return res.sendStatus(200);
}, {frequencies: req.body.frequencies, mode: req.body.mode, trunkFile: req.body.trunkFile});
})
}
/**
* Adds a new preset to the client
*/
exports.addNewPreset = async (req, res) => {
checkBodyForPresetFields(req, res, () => {
addNewPreset(req.body.systemName, req.body.frequencies, req.body.mode, () => {
return res.sendStatus(200);
}, req.body.trunkFile);
});
}
/**
* Removes a preset from the client
*/
exports.removePreset = async (req, res) => {
checkBodyForPresetFields(req, res, () => {
if (!req.body.systemName) return res.status("500").json({"message": "You must specify a system name to delete, this must match exactly to how the system name is saved."})
removePreset(req.body.systemName, () => {
return res.sendStatus(200);
}, req.body.trunkFile);
});
}
/**
* Runs the updater service
*/
exports.updateClient = async (req, res) => {
await executeAsyncConsoleCommand("systemctl start RadioNodeUpdater.service");
return res.sendStatus(200);
}