Bds Maneger Server Modification #248
@ -6,6 +6,9 @@ const { Backup } = require("./BdsBackup");
|
||||
const { CronJob } = require("cron");
|
||||
const BdsSettings = require("../src/lib/BdsSettings");
|
||||
|
||||
const PlayerJson = require("./ManegerServer/Players_json");
|
||||
const BasicCommands = require("./ManegerServer/BasicCommands");
|
||||
|
||||
const ServerSessions = {};
|
||||
module.exports.GetSessions = () => ServerSessions;
|
||||
|
||||
@ -117,10 +120,6 @@ module.exports.StartServer = function start() {
|
||||
}
|
||||
fs.writeFileSync(LatestLog_Path, "");
|
||||
|
||||
// Player JSON File
|
||||
ServerExec.stdout.on("data", data => Player_Json(data, UpdateUserJSON));
|
||||
ServerExec.stderr.on("data", data => Player_Json(data, UpdateUserJSON));
|
||||
|
||||
// Log File
|
||||
ServerExec.stdout.on("data", LogSaveFunction);
|
||||
ServerExec.stderr.on("data", LogSaveFunction);
|
||||
@ -142,7 +141,7 @@ module.exports.StartServer = function start() {
|
||||
if (!(action === "all" || action === "connect" || action === "disconnect")) throw new Error("Use some valid action: all, connect, disconnect");
|
||||
|
||||
// Functions
|
||||
const data = data => Player_Json(data, function (array_status){
|
||||
const data = data => PlayerJson.UpdateUserJSON(data, function (array_status){
|
||||
array_status.filter(On => {if ("all" === action || On.Action === action) return true; else return false;}).forEach(_player => callback(_player))
|
||||
});
|
||||
ServerExec.stdout.on("data", data);
|
||||
@ -152,134 +151,18 @@ module.exports.StartServer = function start() {
|
||||
ServerExec.stdin.write(`${command}\n`);
|
||||
return command;
|
||||
};
|
||||
const stop = function (){
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write("stop\n");
|
||||
return "stop";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
ServerExec.kill("SIGKILL");
|
||||
return "process";
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write("stop\n");
|
||||
return "stop";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write("stop\n");
|
||||
return "stop";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write("stop\n");
|
||||
return "stop";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
const op = function (player = "Steve") {
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`op "${player}"\n`);
|
||||
return "op";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`op ${player}\n`);
|
||||
return "op";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`op ${player}\n`);
|
||||
return "op";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`op ${player}\n`);
|
||||
return "op";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
const deop = function (player = "Steve") {
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`deop "${player}"\n`);
|
||||
return "deop";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`deop ${player}\n`);
|
||||
return "deop";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`deop ${player}\n`);
|
||||
return "deop";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`deop ${player}\n`);
|
||||
return "deop";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
const ban = function (player = "Steve") {
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`kick "${player}"\n`);
|
||||
return "kick";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`ban ${player}\n`);
|
||||
return "ban";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`ban ${player}\n`);
|
||||
return "ban";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`ban ${player}\n`);
|
||||
return "ban";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
const kick = function (player = "Steve", text = "you got kicked") {
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`kick "${player}" ${text}\n`);
|
||||
return "kick";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`kick ${player} ${text}\n`);
|
||||
return "kick";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`kick ${player} ${text}\n`);
|
||||
return "kick";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`kick ${player} ${text}\n`);
|
||||
return "kick";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
const tp = function (player = "Steve", cord = {x: 0, y: 128, z: 0}) {
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`tp ${player} ${cord.x} ${cord.y} ${cord.z}\n`);
|
||||
return "tp";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`tp ${player} ${cord.x} ${cord.y} ${cord.z}\n`);
|
||||
return "tp";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`tp ${player} ${cord.x} ${cord.y} ${cord.z}\n`);
|
||||
return "tp";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`tp ${player} ${cord.x} ${cord.y} ${cord.z}\n`);
|
||||
return "tp";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
function say(text = ""){
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`say ${text}\n`);
|
||||
return "say";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`say ${text}\n`);
|
||||
return "say";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`say ${text}\n`);
|
||||
return "say";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`say ${text}\n`);
|
||||
return "say";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
}
|
||||
|
||||
const BasicCo = BasicCommands.BasicCommands(ServerExec);
|
||||
|
||||
// Mount commands to Return
|
||||
const returnFuntion = {
|
||||
uuid: randomUUID(),
|
||||
LogPath: LogFile,
|
||||
pid: ServerExec.pid,
|
||||
uptime: 0,
|
||||
StartTime: (new Date()),
|
||||
command, log, exit, on, stop, op, deop, ban, kick, tp, say
|
||||
...BasicCo,
|
||||
command, log, exit, on
|
||||
}
|
||||
|
||||
// Uptime Server
|
||||
@ -291,15 +174,15 @@ module.exports.StartServer = function start() {
|
||||
|
||||
// Socket.io
|
||||
io.on("connection", socket => {
|
||||
socket.on("ServerCommand", (data, callback) => {
|
||||
if (typeof data === "string") return returnFuntion.command(data);
|
||||
else if (typeof data === "object") {
|
||||
if (typeof data.uuid === "string") {
|
||||
if (data.uuid === returnFuntion.uuid) return returnFuntion.command(data.command);
|
||||
socket.on("ServerCommand", (data) => {
|
||||
if (typeof data === "string") return returnFuntion.command(data);
|
||||
else if (typeof data === "object") {
|
||||
if (typeof data.uuid === "string") {
|
||||
if (data.uuid === returnFuntion.uuid) return returnFuntion.command(data.command);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
});
|
||||
return;
|
||||
});
|
||||
});
|
||||
ServerExec.on("exit", code => io.emit("ServerExit", {
|
||||
UUID: returnFuntion.uuid,
|
||||
@ -320,86 +203,22 @@ module.exports.StartServer = function start() {
|
||||
});
|
||||
});
|
||||
|
||||
// Player JSON File
|
||||
ServerExec.stdout.on("data", data => PlayerJson.CreatePlayerJson(data, Actions => {
|
||||
PlayerJson.UpdateUserJSON(Actions);
|
||||
io.emit("PlayerAction", Actions);
|
||||
}));
|
||||
ServerExec.stderr.on("data", data => PlayerJson.CreatePlayerJson(data, Actions => {
|
||||
PlayerJson.UpdateUserJSON(Actions);
|
||||
io.emit("PlayerAction", Actions);
|
||||
}));
|
||||
|
||||
// Return
|
||||
ServerSessions[returnFuntion.uuid] = returnFuntion;
|
||||
module.exports.BdsRun = returnFuntion;
|
||||
return returnFuntion;
|
||||
}
|
||||
|
||||
function Player_Json(data = "", callback = () => {}){
|
||||
const Current_platorm = BdsSettings.GetPlatform();
|
||||
// Bedrock
|
||||
if (Current_platorm === "bedrock") {
|
||||
// "[INFO] Player connected: Sirherobrine, xuid: 2535413418839840",
|
||||
// "[INFO] Player disconnected: Sirherobrine, xuid: 2535413418839840",
|
||||
const BedrockMap = data.split(/\n|\r/gi).map(line => {
|
||||
if (line.includes("connected") || line.includes("disconnected")) {
|
||||
let SplitLine = line.replace(/\[.+\]\s+Player/gi, "").trim().split(/\s+/gi);
|
||||
|
||||
// player
|
||||
let Player = line.trim().replace(/\[.+\]\s+Player/gi, "").trim().replace(/disconnected:|connected:/, "").trim().split(/,\s+xuid:/).filter(a=>a).map(a=>a.trim()).filter(a=>a);
|
||||
|
||||
//
|
||||
let Actions = null;
|
||||
if (/^disconnected/.test(SplitLine[0].trim())) Actions = "disconnect";
|
||||
else if (/^connected/.test(SplitLine[0].trim())) Actions = "connect";
|
||||
|
||||
// Object Map
|
||||
const ObjectReturn = {
|
||||
Player: Player[0],
|
||||
Action: Actions,
|
||||
xuid: Player[1] || null,
|
||||
Date: new Date(),
|
||||
}
|
||||
|
||||
// Return
|
||||
return ObjectReturn
|
||||
} else return false;
|
||||
}).filter(a=>a);
|
||||
callback(BedrockMap);
|
||||
}
|
||||
// Java and Pocketmine-MP
|
||||
else if (Current_platorm === "java" || Current_platorm === "pocketmine") {
|
||||
const JavaMap = data.split(/\n|\r/gi).map(line => {
|
||||
if (line.trim().includes("joined the game") || line.includes("left the game")) {
|
||||
line = line.replace(/^\[.+\] \[.+\/.+\]:/, "").trim();
|
||||
let Actions = null;
|
||||
if (/joined/.test(line)) Actions = "connect";
|
||||
else if (/left/.test(line)) Actions = "disconnect";
|
||||
|
||||
// Player Object
|
||||
const JavaObject = {
|
||||
Player: line.replace(/joined the game|left the game/gi, "").trim(),
|
||||
Action: Actions,
|
||||
Date: new Date(),
|
||||
}
|
||||
|
||||
// Return JSON
|
||||
return JavaObject
|
||||
} else return false;
|
||||
}).filter(a=>a);
|
||||
callback(JavaMap);
|
||||
}
|
||||
}
|
||||
|
||||
const UpdateUserJSON = function (New_Object = []){
|
||||
const Player_Json_path = BdsSettings.GetPaths("player");
|
||||
const Current_platorm = BdsSettings.GetPlatform();
|
||||
let Players_Json = {
|
||||
bedrock: [],
|
||||
java: [],
|
||||
pocketmine: [],
|
||||
jsprismarine: [],
|
||||
}
|
||||
if (fs.existsSync(Player_Json_path)) Players_Json = JSON.parse(fs.readFileSync(Player_Json_path, "utf8"));
|
||||
|
||||
// Array
|
||||
Players_Json[Current_platorm] = Players_Json[Current_platorm].concat(New_Object)
|
||||
|
||||
fs.writeFileSync(Player_Json_path, JSON.stringify(Players_Json, null, 2));
|
||||
return Players_Json
|
||||
}
|
||||
|
||||
// Search player in JSON
|
||||
module.exports.Player_Search = function Player_Search(player = "dontSteve") {
|
||||
const Player_Json_path = BdsSettings.GetPaths("player"), Current_platorm = BdsSettings.GetPlatform();
|
||||
|
127
src/ManegerServer/BasicCommands.js
Normal file
127
src/ManegerServer/BasicCommands.js
Normal file
@ -0,0 +1,127 @@
|
||||
const BdsSettings = require("../lib/BdsSettings");
|
||||
const child_process = require("child_process");
|
||||
|
||||
module.exports.BasicCommands = function BasicCommands(ServerExec = child_process.exec("exit 0")) {
|
||||
const CurrentBdsPlatform = BdsSettings.GetPlatform();
|
||||
const stop = function (){
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write("stop\n");
|
||||
return "stop";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
ServerExec.kill("SIGKILL");
|
||||
return "process";
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write("stop\n");
|
||||
return "stop";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write("stop\n");
|
||||
return "stop";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write("stop\n");
|
||||
return "stop";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
const op = function (player = "Steve") {
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`op "${player}"\n`);
|
||||
return "op";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`op ${player}\n`);
|
||||
return "op";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`op ${player}\n`);
|
||||
return "op";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`op ${player}\n`);
|
||||
return "op";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
const deop = function (player = "Steve") {
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`deop "${player}"\n`);
|
||||
return "deop";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`deop ${player}\n`);
|
||||
return "deop";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`deop ${player}\n`);
|
||||
return "deop";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`deop ${player}\n`);
|
||||
return "deop";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
const ban = function (player = "Steve") {
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`kick "${player}"\n`);
|
||||
return "kick";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`ban ${player}\n`);
|
||||
return "ban";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`ban ${player}\n`);
|
||||
return "ban";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`ban ${player}\n`);
|
||||
return "ban";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
const kick = function (player = "Steve", text = "you got kicked") {
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`kick "${player}" ${text}\n`);
|
||||
return "kick";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`kick ${player} ${text}\n`);
|
||||
return "kick";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`kick ${player} ${text}\n`);
|
||||
return "kick";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`kick ${player} ${text}\n`);
|
||||
return "kick";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
const tp = function (player = "Steve", cord = {x: 0, y: 128, z: 0}) {
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`tp ${player} ${cord.x} ${cord.y} ${cord.z}\n`);
|
||||
return "tp";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`tp ${player} ${cord.x} ${cord.y} ${cord.z}\n`);
|
||||
return "tp";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`tp ${player} ${cord.x} ${cord.y} ${cord.z}\n`);
|
||||
return "tp";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`tp ${player} ${cord.x} ${cord.y} ${cord.z}\n`);
|
||||
return "tp";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
};
|
||||
function say(text = ""){
|
||||
if (CurrentBdsPlatform === "bedrock") {
|
||||
ServerExec.stdin.write(`say ${text}\n`);
|
||||
return "say";
|
||||
} else if (CurrentBdsPlatform === "dragonfly") {
|
||||
throw new Error("Dragonfly does not support commands");
|
||||
} else if (CurrentBdsPlatform === "java") {
|
||||
ServerExec.stdin.write(`say ${text}\n`);
|
||||
return "say";
|
||||
} else if (CurrentBdsPlatform === "pocketmine") {
|
||||
ServerExec.stdin.write(`say ${text}\n`);
|
||||
return "say";
|
||||
} else if (CurrentBdsPlatform === "spigot") {
|
||||
ServerExec.stdin.write(`say ${text}\n`);
|
||||
return "say";
|
||||
} else throw new Error("Bds Core Bad Config Error");
|
||||
}
|
||||
return { stop, op, deop, ban, kick, tp, say };
|
||||
}
|
114
src/ManegerServer/Players_json.js
Normal file
114
src/ManegerServer/Players_json.js
Normal file
@ -0,0 +1,114 @@
|
||||
const fs = require("fs");
|
||||
const BdsSettings = require("../lib/BdsSettings");
|
||||
|
||||
function CreatePlayerJson(data = "", callback = (d = [{Player: "", Action: "connect", Platform: "", xuid: "", Date: ""},{Player: "", Action: "disconnect", Platform: "", xuid: "", Date: ""}]) => {d}){
|
||||
const Current_platorm = BdsSettings.GetPlatform();
|
||||
// Bedrock
|
||||
if (Current_platorm === "bedrock") {
|
||||
// "[INFO] Player connected: Sirherobrine, xuid: 2535413418839840",
|
||||
// "[INFO] Player disconnected: Sirherobrine, xuid: 2535413418839840",
|
||||
const BedrockMap = data.split(/\n|\r/gi).map(line => {
|
||||
if (line.includes("connected")) {
|
||||
let SplitLine = line.replace(/\[.+\]\s+Player/gi, "").trim().split(/\s+/gi);
|
||||
let Actions = "";
|
||||
if (/^disconnected/.test(SplitLine[0].trim())) Actions = "disconnect"; else Actions = "connect";
|
||||
|
||||
// Object Map
|
||||
const ObjectReturn = {
|
||||
Player: line.replace(/^.*connected:/gi, "").replace(/, xuid:.*$/gi, "").trim(),
|
||||
Action: Actions,
|
||||
Platform: Current_platorm,
|
||||
xuid: line.replace(/^.*,.*xuid:/gi, "").trim(),
|
||||
Date: `${new Date()}`,
|
||||
}
|
||||
|
||||
// Return
|
||||
return ObjectReturn
|
||||
} else return false;
|
||||
}).filter(a=>a);
|
||||
return callback(BedrockMap);
|
||||
}
|
||||
// Java and Pocketmine-MP
|
||||
else if (Current_platorm === "java" || Current_platorm === "pocketmine") {
|
||||
const JavaMap = data.split(/\n|\r/gi).map(line => {
|
||||
if (line.trim().includes("joined the game") || line.includes("left the game")) {
|
||||
line = line.replace(/^\[.+\] \[.+\/.+\]:/, "").trim();
|
||||
let Actions = "";
|
||||
if (/joined/.test(line)) Actions = "connect";
|
||||
else if (/left/.test(line)) Actions = "disconnect";
|
||||
else Actions = null;
|
||||
|
||||
// Player Object
|
||||
const JavaObject = {
|
||||
Player: line.replace(/joined the game|left the game/gi, "").trim(),
|
||||
Action: Actions,
|
||||
Platform: Current_platorm,
|
||||
Date: `${new Date()}`,
|
||||
}
|
||||
|
||||
// Return JSON
|
||||
return JavaObject
|
||||
} else return false;
|
||||
}).filter(a=>a);
|
||||
return callback(JavaMap);
|
||||
}
|
||||
}
|
||||
module.exports.CreatePlayerJson = CreatePlayerJson;
|
||||
|
||||
function UpdateUserJSON(New_Object = []){
|
||||
const Player_Json_path = BdsSettings.GetPaths("player");
|
||||
const Current_platorm = BdsSettings.GetPlatform();
|
||||
let Players_Json = [
|
||||
{
|
||||
Player: "Steve",
|
||||
Action: "connect",
|
||||
Platform: Current_platorm,
|
||||
Date: `${new Date()}`
|
||||
}
|
||||
];
|
||||
Players_Json = [];
|
||||
if (fs.existsSync(Player_Json_path)) Players_Json = JSON.parse(fs.readFileSync(Player_Json_path, "utf8"));
|
||||
if (Players_Json.version === undefined) {
|
||||
Players_Json.version = 2;
|
||||
let OldPlayers_Json = {
|
||||
bedrock: [],
|
||||
java: [],
|
||||
pocketmine: [],
|
||||
jsprismarine: [],
|
||||
}
|
||||
OldPlayers_Json.bedrock.forEach(a=>Players_Json.push({
|
||||
Player: a.Player,
|
||||
Action: a.Action,
|
||||
Platform: "bedrock",
|
||||
Date: a.Date
|
||||
}));
|
||||
OldPlayers_Json.java.forEach(a=>Players_Json.push({
|
||||
Player: a.Player,
|
||||
Action: a.Action,
|
||||
Platform: "java",
|
||||
Date: a.Date
|
||||
}));
|
||||
OldPlayers_Json.pocketmine.forEach(a=>Players_Json.push({
|
||||
Player: a.Player,
|
||||
Action: a.Action,
|
||||
Platform: "pocketmine",
|
||||
Date: a.Date
|
||||
}));
|
||||
}
|
||||
// Array
|
||||
Players_Json = Players_Json.concat(New_Object)
|
||||
|
||||
fs.writeFileSync(Player_Json_path, JSON.stringify(Players_Json, null, 2));
|
||||
return Players_Json;
|
||||
}
|
||||
module.exports.UpdateUserJSON = UpdateUserJSON;
|
||||
|
||||
// Search player in JSON
|
||||
module.exports.Player_Search = function Player_Search(player = "dontSteve") {
|
||||
const Player_Json_path = BdsSettings.GetPaths("player");
|
||||
const Players_Json = JSON.parse(fs.readFileSync(Player_Json_path, "utf8"))
|
||||
for (let Player of Players_Json) {
|
||||
if (Player.Player === player.trim()) return Player;
|
||||
}
|
||||
return {};
|
||||
}
|
266
src/api.js
266
src/api.js
@ -55,6 +55,59 @@ app.all(["/v2", "/v2/*"], ({res}) => res.status(401).json({
|
||||
Error: "v2 route moved to root routes"
|
||||
}));
|
||||
|
||||
// Check Token
|
||||
function CheckToken (req, res, next) {
|
||||
if (req.method === "GET") {
|
||||
if (req.query.token) {
|
||||
if (BdsChecks.token_verify(req.query.token)) {
|
||||
req.token = req.query.token;
|
||||
return next();
|
||||
}
|
||||
} else if (req.headers.token) {
|
||||
if (BdsChecks.token_verify(req.headers.token)) {
|
||||
req.token = req.headers.token;
|
||||
return next();
|
||||
}
|
||||
} else if (req.query.Token) {
|
||||
if (BdsChecks.token_verify(req.query.Token)) {
|
||||
req.token = req.query.Token;
|
||||
return next();
|
||||
}
|
||||
} else if (req.headers.Token) {
|
||||
if (BdsChecks.token_verify(req.headers.Token)) {
|
||||
req.token = req.headers.token;
|
||||
return next();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (req.body.token) {
|
||||
if (BdsChecks.token_verify(req.body.token)) {
|
||||
req.token = req.body.token;
|
||||
return next();
|
||||
}
|
||||
} else if (req.headers.token) {
|
||||
if (BdsChecks.token_verify(req.headers.token)) {
|
||||
req.token = req.headers.token;
|
||||
return next();
|
||||
}
|
||||
} else if (req.body.Token) {
|
||||
if (BdsChecks.token_verify(req.body.Token)) {
|
||||
req.token = req.body.Token;
|
||||
return next();
|
||||
}
|
||||
} else if (req.headers.Token) {
|
||||
if (BdsChecks.token_verify(req.headers.Token)) {
|
||||
req.token = req.headers.Token;
|
||||
return next();
|
||||
}
|
||||
}
|
||||
}
|
||||
return res.status(401).json({
|
||||
error: "Unauthorized",
|
||||
message: "Token is not valid"
|
||||
});
|
||||
}
|
||||
|
||||
// ? /bds/
|
||||
app.get(["/bds/info", "/bds", "/"], ({res}) => {
|
||||
try {
|
||||
@ -100,6 +153,24 @@ app.get(["/bds/info", "/bds", "/"], ({res}) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Get Server Log
|
||||
app.get(["/bds/log", "/log"], CheckToken, (req, res) => {
|
||||
const Sessions = BdsManegerCore.BdsManegerServer.GetSessions();
|
||||
return res.json(Object.keys(Sessions).map(session => {
|
||||
try {
|
||||
return {
|
||||
UUID: session,
|
||||
data: fs.readFileSync(Sessions[session].LogPath, "utf8").replaceAll("\r\n", "\n").split("\n")
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
UUID: session,
|
||||
Error: String(err)
|
||||
};
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
// Server Info
|
||||
app.get("/bds/info/server", ({res}) => {
|
||||
let ServerRunner = require("./BdsManegerServer").BdsRun;
|
||||
@ -132,128 +203,9 @@ app.get("/bds/info/server", ({res}) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Check Token
|
||||
// app.all("*", (req, res, next) => {
|
||||
// if (req.method === "GET") {
|
||||
// if (req.query.token) {
|
||||
// if (BdsChecks.token_verify(req.query.token)) {
|
||||
// req.token = req.query.token;
|
||||
// return next();
|
||||
// }
|
||||
// } else if (req.headers.token) {
|
||||
// if (BdsChecks.token_verify(req.headers.token)) {
|
||||
// req.token = req.headers.token;
|
||||
// return next();
|
||||
// }
|
||||
// } else if (req.query.Token) {
|
||||
// if (BdsChecks.token_verify(req.query.Token)) {
|
||||
// req.token = req.query.Token;
|
||||
// return next();
|
||||
// }
|
||||
// } else if (req.headers.Token) {
|
||||
// if (BdsChecks.token_verify(req.headers.Token)) {
|
||||
// req.token = req.headers.token;
|
||||
// return next();
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// if (req.body.token) {
|
||||
// if (BdsChecks.token_verify(req.body.token)) {
|
||||
// req.token = req.body.token;
|
||||
// return next();
|
||||
// }
|
||||
// } else if (req.headers.token) {
|
||||
// if (BdsChecks.token_verify(req.headers.token)) {
|
||||
// req.token = req.headers.token;
|
||||
// return next();
|
||||
// }
|
||||
// } else if (req.body.Token) {
|
||||
// if (BdsChecks.token_verify(req.body.Token)) {
|
||||
// req.token = req.body.Token;
|
||||
// return next();
|
||||
// }
|
||||
// } else if (req.headers.Token) {
|
||||
// if (BdsChecks.token_verify(req.headers.Token)) {
|
||||
// req.token = req.headers.Token;
|
||||
// return next();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return res.status(401).json({
|
||||
// error: "Unauthorized",
|
||||
// message: "Token is not valid"
|
||||
// });
|
||||
// });
|
||||
|
||||
// Whitelist
|
||||
app.get("/bds/info/server/whitelist", (req, res) => {
|
||||
const ServerConfig = BdsManegerCore.BdsSettings.GetJsonConfig();
|
||||
if (ServerConfig.whitelist) {
|
||||
const { Token = null , Action = null } = req.query;
|
||||
const WgiteList = BdsSettings.get_whitelist();
|
||||
if (Action) {
|
||||
if (Action === "add") {
|
||||
if (WgiteList.findIndex(WL => WL.Token === Token) === -1) {
|
||||
WgiteList.push({
|
||||
Token: Token,
|
||||
Time: Date.now()
|
||||
});
|
||||
fs.writeFileSync(BdsManegerCore.BdsSettings.GetPaths("whitelist"), JSON.stringify(WgiteList));
|
||||
res.json({
|
||||
success: true,
|
||||
message: "Whitelist Added"
|
||||
});
|
||||
} else {
|
||||
res.json({
|
||||
success: false,
|
||||
message: "Whitelist Already Exist"
|
||||
});
|
||||
}
|
||||
} else if (Action === "remove") {
|
||||
if (WgiteList.findIndex(WL => WL.Token === Token) !== -1) {
|
||||
WgiteList.splice(WgiteList.findIndex(WL => WL.Token === Token), 1);
|
||||
fs.writeFileSync(BdsManegerCore.BdsSettings.GetPaths("whitelist"), JSON.stringify(WgiteList));
|
||||
res.json({
|
||||
success: true,
|
||||
message: "Whitelist Removed"
|
||||
});
|
||||
} else {
|
||||
res.json({
|
||||
success: false,
|
||||
message: "Whitelist Not Found"
|
||||
});
|
||||
}
|
||||
} else {
|
||||
res.json({
|
||||
success: false,
|
||||
message: "Invalid Action"
|
||||
});
|
||||
}
|
||||
} else {
|
||||
res.json(WgiteList);
|
||||
}
|
||||
} else {
|
||||
res.status(400).json({
|
||||
error: "Whitelist Not Enabled"
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Download Server
|
||||
app.get("/bds/download_server", (req, res) => {
|
||||
const { Token = null, Version = "latest" } = req.query;
|
||||
|
||||
// Check is Token is String
|
||||
if (!Token) return res.status(400).json({
|
||||
error: "Bad Request",
|
||||
message: "Token is required"
|
||||
});
|
||||
|
||||
// Check Token
|
||||
if (!(BdsChecks.token_verify(Token))) return res.status(400).json({
|
||||
error: "Bad Request",
|
||||
message: "Token is invalid"
|
||||
});
|
||||
app.get("/bds/download_server", CheckToken, (req, res) => {
|
||||
const { Version = "latest" } = req.query;
|
||||
|
||||
// Download Server
|
||||
BdsManegerCore.download(Version, true).then(() => {
|
||||
@ -269,8 +221,8 @@ app.get("/bds/download_server", (req, res) => {
|
||||
});
|
||||
|
||||
// Update/Set Server Settings
|
||||
app.post("/bds/save_settings", (req, res) => {
|
||||
const { Token = null,
|
||||
app.post("/bds/save_settings", CheckToken, (req, res) => {
|
||||
const {
|
||||
WorldName = "Bds Maneger",
|
||||
ServerDescription = "The Bds Maneger",
|
||||
DefaultGamemode = "creative",
|
||||
@ -284,18 +236,6 @@ app.post("/bds/save_settings", (req, res) => {
|
||||
port_v6 = "19133",
|
||||
} = req.body;
|
||||
|
||||
// Check is Token is String
|
||||
if (!Token) return res.status(400).json({
|
||||
error: "Bad Request",
|
||||
message: "Token is required"
|
||||
});
|
||||
|
||||
// Check Token
|
||||
if (!(BdsChecks.token_verify(Token))) return res.status(400).json({
|
||||
error: "Bad Request",
|
||||
message: "Token is invalid"
|
||||
});
|
||||
|
||||
// Save Settings
|
||||
try {
|
||||
BdsManegerCore.set_config({
|
||||
@ -347,7 +287,7 @@ app.get("/bds/bridge", (req, res) => {
|
||||
|
||||
// ? /player
|
||||
const GetPlayerJson = (Platform = BdsManegerCore.BdsSettings.GetJsonConfig().server.platform) => ([...{...JSON.parse(fs.readFileSync(BdsManegerCore.BdsSettings.GetPaths("player"), "utf8"))}[Platform]]);
|
||||
app.get("/players", (req, res) => {
|
||||
app.get("/players", CheckToken, (req, res) => {
|
||||
const { Platform = BdsSettings.GetPlatform(), Player = null, Action = null } = req.query;
|
||||
let PlayerList = GetPlayerJson(Platform);
|
||||
if (Player) PlayerList = PlayerList.filter(PLS => PLS.Player === Player);
|
||||
@ -367,10 +307,8 @@ app.get("/players", (req, res) => {
|
||||
|
||||
// Players Actions in Backside Manager
|
||||
// kick player
|
||||
app.get("/players/kick", (req, res) => {
|
||||
const { Token = null, Player = "Sirherobrine", Text = "You have been removed from the Server" } = req.query;
|
||||
if (!Token) return res.status(400).json({ error: "Token is required" });
|
||||
if (!BdsChecks.token_verify(Token)) return res.status(400).json({ error: "Token is invalid" });
|
||||
app.get("/players/kick", CheckToken, (req, res) => {
|
||||
const { Player = "Sirherobrine", Text = "You have been removed from the Server" } = req.query;
|
||||
|
||||
// Kick player
|
||||
const RunnerServer = require("./BdsManegerServer").BdsRun;
|
||||
@ -386,10 +324,8 @@ app.get("/players/kick", (req, res) => {
|
||||
});
|
||||
|
||||
// Ban player
|
||||
app.get("/players/ban", (req, res) => {
|
||||
const { Token = null, Player = "Sirherobrine" } = req.query;
|
||||
if (!Token) return res.status(400).json({ error: "Token is required" });
|
||||
if (!BdsChecks.token_verify(Token)) return res.status(400).json({ error: "Token is invalid" });
|
||||
app.get("/players/ban", CheckToken, (req, res) => {
|
||||
const { Player = "Sirherobrine" } = req.query;
|
||||
|
||||
// Ban player
|
||||
const RunnerServer = require("./BdsManegerServer").BdsRun;
|
||||
@ -405,10 +341,8 @@ app.get("/players/ban", (req, res) => {
|
||||
});
|
||||
|
||||
// Op player
|
||||
app.get("/players/op", (req, res) => {
|
||||
const { Token = null, Player = "Sirherobrine" } = req.query;
|
||||
if (!Token) return res.status(400).json({ error: "Token is required" });
|
||||
if (!BdsChecks.token_verify(Token)) return res.status(400).json({ error: "Token is invalid" });
|
||||
app.get("/players/op", CheckToken, CheckToken, (req, res) => {
|
||||
const { Player = "Sirherobrine" } = req.query;
|
||||
|
||||
// Op player
|
||||
const RunnerServer = require("./BdsManegerServer").BdsRun;
|
||||
@ -425,9 +359,7 @@ app.get("/players/op", (req, res) => {
|
||||
|
||||
// Deop player
|
||||
app.get("/players/deop", (req, res) => {
|
||||
const { Token = null, Player = "Sirherobrine" } = req.query;
|
||||
if (!Token) return res.status(400).json({ error: "Token is required" });
|
||||
if (!BdsChecks.token_verify(Token)) return res.status(400).json({ error: "Token is invalid" });
|
||||
const { Player = "Sirherobrine" } = req.query;
|
||||
|
||||
// Deop player
|
||||
const RunnerServer = require("./BdsManegerServer").BdsRun;
|
||||
@ -443,10 +375,8 @@ app.get("/players/deop", (req, res) => {
|
||||
});
|
||||
|
||||
// Say to Server
|
||||
app.get("/players/say", (req, res) => {
|
||||
const { Token = null, Text = "Hello Server" } = req.query;
|
||||
if (!Token) return res.status(400).json({ error: "Token is required" });
|
||||
if (!BdsChecks.token_verify(Token)) return res.status(400).json({ error: "Token is invalid" });
|
||||
app.get("/players/say", CheckToken, (req, res) => {
|
||||
const { Text = "Hello Server" } = req.query;
|
||||
|
||||
// Say to Server
|
||||
const RunnerServer = require("./BdsManegerServer").BdsRun;
|
||||
@ -463,9 +393,7 @@ app.get("/players/say", (req, res) => {
|
||||
|
||||
// Tp player
|
||||
app.get("/players/tp", (req, res) => {
|
||||
const { Token = null, Player = "Sirherobrine", X = 0, Y = 0, Z = 0 } = req.query;
|
||||
if (!Token) return res.status(400).json({ error: "Token is required" });
|
||||
if (!BdsChecks.token_verify(Token)) return res.status(400).json({ error: "Token is invalid" });
|
||||
const { Player = "Sirherobrine", X = 0, Y = 0, Z = 0 } = req.query;
|
||||
|
||||
// Tp player
|
||||
const RunnerServer = require("./BdsManegerServer").BdsRun;
|
||||
@ -485,6 +413,12 @@ app.get("/players/tp", (req, res) => {
|
||||
});
|
||||
|
||||
// Export API Routes
|
||||
/**
|
||||
* @param {Number} port_api - Port of API, default is 1932
|
||||
* @callback {Function} callback - Callback function when API is ready
|
||||
*
|
||||
* Launch an API To manage the server Remotely, some features are limited.
|
||||
*/
|
||||
function API(port_api = 1932, callback = port => {console.log("Bds Maneger Core REST API, http port", port)}){
|
||||
const MapRoutes = app._router.stack.map(d => {if (d.route) {if (d.route.path) return d.route.path;else return d.route.regexp.source;} else return null;}).filter(d => d);
|
||||
app.all("*", (req, res) => {
|
||||
@ -503,6 +437,13 @@ function API(port_api = 1932, callback = port => {console.log("Bds Maneger Core
|
||||
|
||||
// Bds Maneger Core API token Register
|
||||
const path_tokens = path.join(BdsSettings.bds_dir, "bds_tokens.json");
|
||||
/**
|
||||
*
|
||||
* Register new Token to API and more features in the Bds Maneger Core.
|
||||
*
|
||||
* @param {Array} Admin_Scoper - Array of Admin Scoper (Soon will be implemented)
|
||||
* @returns {String} Token - Token of the API
|
||||
*/
|
||||
function token_register(Admin_Scoper = ["web_admin", "admin"]) {
|
||||
Admin_Scoper = Array.from(Admin_Scoper).filter(scoper => /admin/.test(scoper));
|
||||
let tokens = [];
|
||||
@ -520,7 +461,14 @@ function token_register(Admin_Scoper = ["web_admin", "admin"]) {
|
||||
return bdsuid;
|
||||
}
|
||||
|
||||
// Bds Maneger Core API Delet token
|
||||
// Bds Maneger Core API Delete token
|
||||
/**
|
||||
*
|
||||
* Delete Token of the API
|
||||
*
|
||||
* @param {String} Token - Token of the API to delete
|
||||
* @returns {Boolean} - True if the token is deleted
|
||||
*/
|
||||
function delete_token(Token = "") {
|
||||
if (!Token) return false;
|
||||
if (typeof Token !== "string") return false;
|
||||
|
Reference in New Issue
Block a user