nodemanager-server.js v1.0.1
NodeManager-Server.js
NodeManager is an application which allows you to register a computer as a node and control it using a web-server.
This is an API wrapper for NodeJS.
Main Website: https://nodemanager.tk/
Creating an example instance powered by ExpressJS
const express = require("express");
const app = express();
const nodemanager_server = require("nodemanager-server.js");
let server = null;
app.use(express.json());
async function connect(n) {
if (server === null || server === undefined) {
if (n !== null && n !== undefined) {
if ((n.id !== null && n.id !== undefined) && (n.key !== null && n.key !== undefined)) {
server = new nodemanager_server({ id: n !== null && n !== undefined ? n.id !== null && n.id !== undefined ? n.id : "" : "", key: n !== null && n !== undefined ? n.key !== null && n.key !== undefined ? n.key : "" : "" })
server.onOpen = async () => {
console.log("Connected to PC.");
server.sendCommand(`main`, `cmd`, `echo Hello World!`);
};
server.onClose = async (exitCode) => {
console.log(`Connection to the PC has been closed.`);
};
server.onHeartbeat = async () => {
console.log("Heartbeat.");
};
server.onClientError = async (error) => {
console.log(error);
};
server.connect();
}
}
}
}
app.post("/create", async (req, res, next) => {
res.setHeader("Content-Type", "application/json");
try {
const body = req.body;
if ((body !== null && body !== undefined)) {
if ((body.node !== null && body.node !== undefined)) {
if ((body.node.id !== null && body.node.id !== undefined) && (body.node.key !== null && body.node.key !== undefined)) {
try {
if (server === null || server === undefined) {
await connect(body.node);
return res.status(204).end();
} else {
return res.status(400).end(JSON.stringify({ "success": false, "errors": ["node already connected"] }));
}
} catch (e) {
console.log(e);
return res.status(400).end(JSON.stringify({ "success": false, "errors": ["an error has occured"] }));
}
} else {
return res.status(400).end(JSON.stringify({ "success": false, "errors": ["no id or key found"] }));
}
} else {
return res.status(400).end(JSON.stringify({ "success": false, "errors": ["no node found"] }));
}
} else {
return res.status(400).end(JSON.stringify({ "success": false, "errors": ["no body found"] }));
}
} catch (e) {
return res.status(400).end(JSON.stingify({ "success": false, "errors": ["an error has occured"] }));
}
});
const listener = app.listen("80", () => {
console.log(`Site listening on port ${listener.address().port} `);
});
Options
id
: The ID you put on the config, also sent with the key incase you forgot
key
: The key given by NodeManager
removePromptWatermark
: Removes the watermark and copyrights from the prompts (OPTIONAL)
Methods of an instance
isConnected()
Returns whenever there's an open connection
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
console.log(server.isConnected());
connect()
Makes a connection to the computer
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
server.connect();
disconnect()
Cancels the current connection to the computer
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
server.connect();
server.disconect();
sendCommand(prompt_id
, prompt_type
, cmd
)
Sends a command to the computer
This method takes 3 arguments,
prompt_id
: An ID which will be used to get the output of a command
prompt_type
: Console to run the command, either cmd
or powershell
cmd
: The command to run
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
server.sendCommand("main" , "cmd" , "echo Hello World!")
getOutputs()
Gets the outputs of all prompts (commands ran), in JSON format
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
console.log(server.getOutputs());
getOutput(prompt_id
)
Gets the output of the given prompt id
This method takes 1 argument,
prompt_id
: The ID of the prompt
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
console.log(server.getOutput("main"));
clearOutput(prompt_id
)
Clears the given output of the given prompt id
This method takes 1 argument,
prompt_id
: The ID of the prompt
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
server.clearOutput("main");
restartClient()
Tells the node to restart itself
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
server.restartClient();
Utils
getIP()
Get's the node's IP
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
server.onOpen = async () => {
console.log(await server.getIP());
// Any commands to run when the computer is connected
};
Events
onOpen()
Runs when a connection to the computer is open
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
server.onOpen = async () => {
console.log("Connected to PC.");
// Any commands to run when the computer is connected
};
onClose(exitCode
)
Runs when the connection to the computer is closed
This event takes 1 argument,
exitCode
: The code which identifies the reason the connection was closed
Exit codes:
0:
Unknown reason
1
: Computer disconnected, likely shutdown or manually disconnected
2
: Heartbeat failed
3
: Connection was forcefully terminated
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
server.onClose = async (exitCode) => {
console.log(`Connection to the PC has been closed`);
};
onHeartbeat()
Runs when a heartbeat is received by the server
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
server.onHeartbeat = async () => {
console.log("Heartbeat.");
};
onClientError(error
)
Runs when the node catches an error
This event takes 1 argument,
error
: The error sent from the app
const nodemanager_server = require("nodemanager-server.js");
const server = new nodemanager_server({
id: "ID from example"
key: "Key from example"
});
server.onClientError = async (error) => {
console.log(error);
};