Implement Discord CnC Server into Emmelia
This commit is contained in:
129
controllers/adminController.js
Normal file
129
controllers/adminController.js
Normal file
@@ -0,0 +1,129 @@
|
||||
// Config
|
||||
const discordConfig = require("../config/discordConfig");
|
||||
// Debug
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
||||
const log = new DebugBuilder("server", "adminController");
|
||||
// Utilities
|
||||
const mysqlHandler = require("../utilities/mysqlHandler");
|
||||
const utils = require("../utilities/utils");
|
||||
const requests = require("../utilities/httpRequests");
|
||||
|
||||
/** Get the presets of all online nodes, can be used for functions
|
||||
*
|
||||
* @param callback Callback function
|
||||
* @returns {*} A list of the systems online
|
||||
*/
|
||||
async function getPresetsOfOnlineNodes(callback) {
|
||||
mysqlHandler.getOnlineNodes((onlineNodes) => {
|
||||
let systems = {};
|
||||
onlineNodes.forEach(onlineNode => {
|
||||
systems[onlineNode.id] = utils.BufferToJson(onlineNode.nearbySystems);
|
||||
});
|
||||
|
||||
callback(systems);
|
||||
});
|
||||
}
|
||||
|
||||
async function requestNodeListenToPreset(preset, nodeId, callback) {
|
||||
mysqlHandler.getNodeInfoFromId(nodeId, (nodeObject) =>{
|
||||
reqOptions = new requests.requestOptions("/bot/join", "POST", nodeObject.ip, nodeObject.port);
|
||||
requests.sendHttpRequest(reqOptions, JSON.stringify({
|
||||
"channelID": discordConfig.channelID,
|
||||
"presetName": preset
|
||||
}), (responseObject) => {
|
||||
callback(responseObject)
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
async function getNodeBotStatus(nodeId, callback) {
|
||||
mysqlHandler.getNodeInfoFromId(nodeId, (nodeObject) =>{
|
||||
reqOptions = new requests.requestOptions("/bot/status", "GET", nodeObject.ip, nodeObject.port, undefined, 5);
|
||||
requests.sendHttpRequest(reqOptions, JSON.stringify({}), (responseObject) => {
|
||||
if (responseObject === false) {
|
||||
// Bot is joined
|
||||
}
|
||||
else {
|
||||
// Bot is free
|
||||
}
|
||||
callback(responseObject);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function requestNodeLeaveServer(nodeId, callback) {
|
||||
getNodeBotStatus(nodeId, (responseObject) => {
|
||||
if (responseObject === false) {
|
||||
// Bot is joined
|
||||
mysqlHandler.getNodeInfoFromId(nodeId, (nodeObject) =>{
|
||||
reqOptions = new requests.requestOptions("/bot/leave", "POST", nodeObject.ip, nodeObject.port);
|
||||
requests.sendHttpRequest(reqOptions, JSON.stringify({}), (responseObject) => {
|
||||
callback(responseObject);
|
||||
});
|
||||
});
|
||||
}
|
||||
else {
|
||||
// Bot is free
|
||||
callback(false);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/** Return to requests for the presets of all online nodes, cannot be used in functions
|
||||
*
|
||||
* @param {*} req Express request parameter
|
||||
* @param {*} res Express response parameter
|
||||
*/
|
||||
exports.getAvailablePresets = async (req, res) => {
|
||||
await getPresetsOfOnlineNodes((systems) => {
|
||||
res.status(200).json({
|
||||
"systemsOnline": systems
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
/** Request a node to join the server listening to a specific preset
|
||||
*
|
||||
* @param {*} req Express request parameter
|
||||
* @var {*} req.body.preset The preset to join (REQ)
|
||||
* @var {*} req.body.nodeId The specific node to join (OPT/REQ if more than one node has the preset)
|
||||
* @param {*} res Express response parameter
|
||||
*/
|
||||
exports.joinPreset = async (req, res) => {
|
||||
if (!req.body.preset) return res.status(400).json("No preset specified");
|
||||
await getPresetsOfOnlineNodes((systems) => {
|
||||
const systemsWithSelectedPreset = Object.values(systems).filter(nodePresets => nodePresets.includes(req.body.preset)).length
|
||||
if (!systemsWithSelectedPreset) return res.status(400).json("No system online with that preset");
|
||||
if (systemsWithSelectedPreset > 1) {
|
||||
if (!req.body.nodeId) return res.status(175).json("Multiple locations with the selected channel, please specify a nodeID (nodeId)")
|
||||
requestNodeListenToPreset(req.body.preset, req.body.nodeId, (responseObject) => {
|
||||
if (responseObject === false) return res.status(400).json("Timeout reached");
|
||||
return res.sendStatus(responseObject.statusCode);
|
||||
});
|
||||
}
|
||||
else {
|
||||
let nodeId;
|
||||
if (!req.body.nodeId) nodeId = utils.getKeyByArrayValue(systems, req.body.preset);
|
||||
else nodeId = req.body.nodeId;
|
||||
requestNodeListenToPreset(req.body.preset, nodeId, (responseObject) => {
|
||||
if (responseObject === false) return res.status(400).json("Timeout reached");
|
||||
return res.sendStatus(responseObject.statusCode);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Request a node to join the server listening to a specific preset
|
||||
*
|
||||
* @param {*} req Express request parameter
|
||||
* @param {*} res Express response parameter
|
||||
*/
|
||||
exports.leaveServer = async (req, res) => {
|
||||
if (!req.body.nodeId) return res.status(400).json("No nodeID specified");
|
||||
|
||||
requestNodeLeaveServer(req.body.nodeId, (responseObject) => {
|
||||
if (responseObject === false) return res.status(400).json("Bot not joined to server");
|
||||
return res.sendStatus(responseObject.statusCode);
|
||||
});
|
||||
}
|
||||
80
controllers/nodesController.js
Normal file
80
controllers/nodesController.js
Normal file
@@ -0,0 +1,80 @@
|
||||
// Debug
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder.js");
|
||||
const log = new DebugBuilder("server", "nodesController");
|
||||
// Utilities
|
||||
const mysqlHander = require("../utilities/mysqlHandler");
|
||||
const utils = require("../utilities/utils");
|
||||
|
||||
exports.listAllNodes = async (req, res) => {
|
||||
mysqlHander.getAllNodes((allNodes) => {
|
||||
res.status(200).json({
|
||||
"nodes_online": allNodes
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Add a new node to the
|
||||
exports.newNode = async (req, res) => {
|
||||
if (!req.body.name) return res.send(400)
|
||||
|
||||
try {
|
||||
// Try to add the new user with defaults if missing options
|
||||
mysqlHander.addNewNode({
|
||||
'name': req.body.name,
|
||||
'ip': req.body.ip ?? null,
|
||||
'port': req.body.port ?? null,
|
||||
'location': req.body.location ?? null,
|
||||
'nearbySystems': req.body.nearbySystems ?? null,
|
||||
'online': req.body.online ?? 0
|
||||
}, (queryResults) => {
|
||||
// Send back a success if the user has been added and the ID for the client to keep track of
|
||||
res.status(202).json({"nodeId": queryResults.insertId});
|
||||
})
|
||||
}
|
||||
catch (err) {
|
||||
// Catch any errors
|
||||
if (err === "No name provided") {
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
else log.ERROR(err)
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
}
|
||||
|
||||
// Get the known info for the node specified
|
||||
exports.getNodeInfo = async (req, res) => {
|
||||
if (!req.query.id) return res.status(400).json("No id specified");
|
||||
mysqlHander.getNodeInfoFromId(req.query.id, (nodeInfo) => {
|
||||
res.status(200).json(nodeInfo);
|
||||
})
|
||||
}
|
||||
|
||||
// Updates the information received from the client based on ID
|
||||
exports.nodeCheckIn = async (req, res) => {
|
||||
if (!req.body.id) return res.status(400).json("No id specified");
|
||||
mysqlHander.getNodeInfoFromId(req.body.id, (nodeInfo) => {
|
||||
let nodeObject = {};
|
||||
// Convert the DB systems buffer to a JSON object to be worked with
|
||||
nodeInfo.nearbySystems = utils.BufferToJson(nodeInfo.nearbySystems)
|
||||
// Convert the online status to a boolean to be worked with
|
||||
nodeInfo.online = nodeInfo.online !== 0;
|
||||
|
||||
if (req.body.name && req.body.name !== nodeInfo.name) nodeObject.name = req.body.name
|
||||
if (req.body.ip && req.body.ip !== nodeInfo.ip) nodeObject.ip = req.body.ip
|
||||
if (req.body.port && req.body.port !== nodeInfo.port) nodeObject.port = req.body.port
|
||||
if (req.body.location && req.body.location !== nodeInfo.location) nodeObject.location = req.body.location
|
||||
if (req.body.nearbySystems && JSON.stringify(req.body.nearbySystems) !== JSON.stringify(nodeInfo.nearbySystems)) nodeObject.nearbySystems = req.body.nearbySystems
|
||||
if (req.body.online && req.body.online !== nodeInfo.online) nodeObject.online = req.body.online
|
||||
|
||||
// If no changes are made tell the client
|
||||
if (Object.keys(nodeObject).length === 0) return res.status(200).json("No keys updated");
|
||||
|
||||
log.INFO("Updating the following keys for ID: ", req.body.id, nodeObject);
|
||||
// Adding the ID key to the body so that the client can double-check their ID
|
||||
nodeObject.id = req.body.id;
|
||||
mysqlHander.updateNodeInfo(nodeObject, () => {
|
||||
return res.status(202).json({"updatedKeys": nodeObject});
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user