Telegram bot #150

Merged
Sirherobrine23 merged 14 commits from Telegram_bot into main 2021-08-11 02:47:35 +00:00
23 changed files with 726 additions and 502 deletions
Showing only changes of commit 4d8ee61e4a - Show all commits

View File

@ -8,16 +8,21 @@ Any contribution is welcome, but before a look at [CONTRIBUTING.md](CONTRIBUTING
In Version 1.11.0 there was a big change in the way to get the new settings and that left a good part of the program broken, so for those who are going to upgrade to the latest versions of Bds maneger Core will have to change the settings manually.
## Documentation
We have a separate repository for all Bds Maneger Project documentation, <a href="https://docs.bdsmaneger.com/Bds Maneger core" target="_blank">link here from the main page</a>, <a href="https://github.com/The-Bds-Maneger/Bds-Manager-Project-Documentation" target="_blank">Repository link</a>
## Badges
[![Github CodeQL and OSSAR](https://github.com/The-Bds-Maneger/Bds-Maneger-Core/actions/workflows/codeql%20and%20ossar%20analysis.yml/badge.svg)](https://github.com/The-Bds-Maneger/Bds-Maneger-Core/actions/workflows/codeql%20and%20ossar%20analysis.yml)
[![Total alerts](https://img.shields.io/lgtm/alerts/g/Bds-Maneger/bds_maneger_api.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Bds-Maneger/bds_maneger_api/alerts/)
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/Bds-Maneger/bds_maneger_api.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Bds-Maneger/bds_maneger_api/context:javascript)
[![DeepScan grade](https://deepscan.io/api/teams/13683/projects/16691/branches/363172/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=13683&pid=16691&bid=363172)
[![DeepScan grade](https://deepscan.io/api/teams/13683/projects/16691/branches/363172/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=13683&pid=16691&bid=363172)
## Start our Docker image, making everything easier.
## Start our Docker image, making everything easier
Windows:
```cmd
$ docker run --rm -d --name BdsManegerCore -v BdsCore:/home/bds/bds_core ^
-p 19132:19132/udp -p 19133:19133/udp -p 1932:1932/tcp ^
@ -34,6 +39,7 @@ bdsmaneger/core:latest
```
Linux/MacOS:
```bash
$ docker run --rm -d --name BdsManegerCore -v BdsCore/:/home/bds/bds_core \
-p 19132:19132/udp -p 19133:19133/udp -p 1932:1932/tcp \
@ -51,13 +57,7 @@ bdsmaneger/core:latest
## We also have some Implementation Models for Azure
#### Microsoft Azure Container
The Azure container is a special machine for Docker Within Azure, it is fully managed by Azure, it only depends on the Docker image, but it has its limitations.
[![Deploy to Azure](https://aka.ms/deploytoazurebutton)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FBds-Maneger%2FThe-Bds-Maneger-Docker%2Fmain%2Fazure%2FBdsMangerCore_docker.json)
#### Microsoft Azure Virtual machine
### Microsoft Azure Virtual machine
Here we have a virtual machine totally dedicated to Bds Maneger Core, it still uses Docker to deploy Docker images.

View File

@ -1,14 +1,14 @@
#!/usr/bin/env node
if (process.platform === "win32") process.title = "Bds Maneger CLI"; else process.title = "Bds_Manger_CLI";
const readline = require("readline");
if (process.platform === "win32") process.title = "Bds Maneger CLI"; else process.title = "Bds-Manger-CLI";
process.env.IS_BDS_CLI = process.env.IS_BIN_BDS = true;
// Inports
const readline = require("readline");
const bds = require("../index");
const { valid_platform } = require("../lib/BdsSystemInfo");
const { bds_dir, GetServerVersion, GetPlatform, UpdatePlatform, GetServerPaths, GetPaths } = require("../lib/BdsSettings");
const commandExits = require("../lib/commandExist");
const download = require("../src/download");
const download = require("../src/BdsServersDownload");
// Bds Maneger ArgV
const argv = require("minimist")(process.argv.slice(2));

View File

@ -1,10 +1,11 @@
const fs = require("fs");
const { Telegraf, Markup } = require("telegraf");
const bds = require("../index");
const { GetPlatform, GetPaths } = require("../lib/BdsSettings");
const { GetPlatform, GetPaths, GetTelegramToken } = require("../lib/BdsSettings");
const { GetKernel, arch, system } = require("../lib/BdsSystemInfo");
const { Detect } = require("../src/CheckKill");
const { Servers } = require("../lib/ServerURL");
const { CheckTelegramUser } = require("../src/UsersAndtokenChecks")
// Bot Start And Help messages
const HelpAndStart = [
@ -24,7 +25,7 @@ const HelpAndStart = [
]
// Set Telegram Bot
const bot = new Telegraf(bds.telegram_token);
const bot = new Telegraf(GetTelegramToken());
// Start and Help Command
bot.start((ctx)=>ctx.reply(HelpAndStart.join("\n")));
@ -32,6 +33,9 @@ bot.help((ctx)=>ctx.reply(HelpAndStart.join("\n")));
// User
bot.command("player", ctx => {
// Check admin Username
if (!(CheckTelegramUser(ctx.from.username))) return ctx.reply("you are not an administrator");
const Server = global.ServerExec;
const CtxOption = ctx.message.text.replace("/player", "").trim();
const CtxContext = CtxOption.replace(/^kick|^deop|^ban|^op/, "").trim();
@ -106,6 +110,9 @@ bot.command("player", ctx => {
// Basic server
bot.command("basic", async ctx => {
// Check admin Username
if (!(CheckTelegramUser(ctx.from.username))) return ctx.reply("you are not an administrator");
const text = ctx.message.text.replace("/basic", "").trim();
if (text) {
// Start Server
@ -161,6 +168,9 @@ bot.command("basic", async ctx => {
// Select Platform
bot.command("platform", async ctx => {
// Check admin Username
if (!(CheckTelegramUser(ctx.from.username))) return ctx.reply("you are not an administrator");
const text = ctx.message.text.replace("/platform", "").trim();
if (text) {
try {
@ -184,6 +194,9 @@ bot.command("platform", async ctx => {
// Download Server
bot.command("download", async ctx => {
// Check admin Username
if (!(CheckTelegramUser(ctx.from.username))) return ctx.reply("you are not an administrator");
const version = ctx.message.text.replace(/\/download|[a-zA-Z]/gi, "").trim();
if (version) {
await bds.download(version, true);
@ -201,6 +214,9 @@ bot.command("download", async ctx => {
// Command
bot.command("command", async ctx => {
// Check admin Username
if (!(CheckTelegramUser(ctx.from.username))) return ctx.reply("you are not an administrator");
const text = ctx.message.text.replace("/command", "").trim();
if (!(Detect())) return ctx.reply("Your server is stopped");
if (text) {
@ -238,6 +254,9 @@ bot.command("info", ctx => {
// Live Log User
global.LiveLog = [];
bot.command("live_log", async ctx => {
// Check admin Username
if (!(CheckTelegramUser(ctx.from.username))) return ctx.reply("you are not an administrator");
const option = ctx.message.text.replace("/live_log", "").trim();
if (option) {
if (/enable/.test(option)) {

View File

@ -3,6 +3,9 @@ const { resolve } = require("path");
const path = require("path")
const fs = require("fs");
const randomUUID = require("uuid").v4;
const { bds_dir } = require("./lib/BdsSettings");
if (typeof fetch === "undefined") global.fetch = require("node-fetch");
function date(format) {
const today = new Date(),
@ -26,26 +29,42 @@ module.exports.package_path = bds_core_package
module.exports.package_json = require("./package.json");
module.exports.extra_json = require("./BdsManegerInfo.json");
const { bds_dir } = require("./lib/BdsSettings");
// Inport and Export Arch
const { arch } = require("./lib/BdsSystemInfo");
module.exports.arch = arch
const { GetJsonConfig, UpdatePlatform, UpdateTelegramToken, GetTelegramToken } = require("./lib/BdsSettings");
if (typeof fetch === "undefined") global.fetch = require("node-fetch");
const { GetJsonConfig, UpdatePlatform, UpdateTelegramToken } = require("./lib/BdsSettings");
const maneger_ips = require("./src/external_ip")
// Bds Maneger Core Network
const maneger_ips = require("./src/BdsNetwork")
module.exports.internal_ip = maneger_ips.internal_ip
module.exports.external_ip = maneger_ips.external_ip
module.exports.save_google_id = require("./lib/BdsSettings").CloudConfig.Driver
module.exports.getBdsConfig = GetJsonConfig
module.exports.change_platform = module.exports.platform_update = UpdatePlatform
module.exports.telegram_token_save = UpdateTelegramToken
module.exports.api = require("./src/rest/api");
module.exports.tmphost = {
host: maneger_ips.host,
Response: maneger_ips.HostResponse
}
// Telegram
module.exports.telegram_token = GetTelegramToken();
// Get Old Method Config
module.exports.getBdsConfig = GetJsonConfig;
/**
* Update Current Platform
*/
module.exports.change_platform = module.exports.platform_update = UpdatePlatform;
/**
* Save Telegram token in Settings File
*/
module.exports.telegram_token_save = UpdateTelegramToken
/**
* The Bds Maneger Core Internal API REST
*
* @param {number} port - The port number, default is 1932
*
* @param {function} callback - The callback function after start API
*/
module.exports.api = require("./src/rest/api");
function token_register() {
const bds_token_path = path.join(bds_dir, "bds_tokens.json");
@ -67,17 +86,6 @@ function token_register() {
return bdsuid;
}
/**
* Update, Get and more to Modifications Bds Settings File
*/
module.exports.BdsSettigs = require("./lib/BdsSettings");
// Requires
const { World_BAckup } = require("./src/backups");
const { config, get_config, config_example } = require("./src/ServerSettings");
const download = require("./src/download");
const { start, stop, BdsCommand, CronBackups } = require("./src/basic_server")
/**
* Register tokens to use in Bds Maneger REST and other supported applications
*
@ -85,6 +93,17 @@ const { start, stop, BdsCommand, CronBackups } = require("./src/basic_server")
*/
module.exports.token_register = token_register
/**
* Update, Get and more to Modifications Bds Settings File
*/
module.exports.BdsSettigs = require("./lib/BdsSettings");
// Requires
const { World_BAckup } = require("./src/BdsBackup");
const { config, get_config } = require("./src/ServerSettings");
const download = require("./src/BdsServersDownload");
const { start, stop, BdsCommand, CronBackups } = require("./src/BdsManegerServer")
/**
* Take the current date
*/
@ -98,6 +117,7 @@ module.exports.BdsDate = module.exports.date = date
module.exports.command = BdsCommand
// New management method
// Start Server
/**
* to start the server here in the sera script with child_process, then you will have to use the return function for your log custumization or anything else
*
@ -105,10 +125,14 @@ module.exports.command = BdsCommand
* server.log(function (log){console.log(log)})
*/
module.exports.start = start
// Stop Server
/**
* use this command for the server, that's all
*/
module.exports.stop = stop
// Create Backup of Bds Maneger Core and Servers along with your maps and settings
/**
* backup your map locally
*/
@ -137,14 +161,13 @@ module.exports.kill = Kill
* use download( version, boolean ) // the boolean is for if you want to force the installation of the server
*
* @example
* bedrock: bds.download("1.16.201.02")
* bedrock: download("1.16.201.02")
*
* java: bds.download("1.16.5")
* java: download("1.16.5")
*
* any platform: bds.download("latest") // It will download the latest version available for download
* any platform: download("latest") // It will download the latest version available for download
*/
module.exports.download = download
module.exports.config_example = config_example
/**
* use this command to modify server settings
@ -164,19 +187,12 @@ module.exports.config_example = config_example
});
*/
module.exports.set_config = config
/**
* takes the server settings in JSON format
*/
module.exports.get_config = get_config
/**
* Get temp domain to Server and API (The Domain is even temporary).
*/
module.exports.tmphost = require("./lib/tempHost")
// Core Applications
/**
* Load Crontab Backup
*/

View File

@ -1,37 +0,0 @@
const fetchSync = require("@the-bds-maneger/fetchsync");
const { external_ip, Interfaces } = require("../src/external_ip");
const { GetTempHost } = require("./BdsSettings")
if (GetTempHost()){
// Get HOST
const HostResponse = fetchSync("https://bds-core-back-end.vercel.app/Gethost", {
method: "POST",
mode: "cors",
body: JSON.stringify({
mac: Interfaces[0].MAC,
ip: external_ip.ipv4,
}),
headers: {
"Content-Type": "application/json"
}
}).json();
global.BdsTempHost = HostResponse.user.host.host
module.exports.host = HostResponse.user.host.host
module.exports.Response = HostResponse
console.log(`Bds Maneger Core Temp Host ID: ${HostResponse.user.ID}`)
// Delete Host
process.on("exit", function () {
const deleted_host = fetchSync("https://bds-core-back-end.vercel.app/DeleteHost", {
method: "post",
body: JSON.stringify({
"ID": HostResponse.user.ID
}),
headers: {
"Content-Type": "application/json"
}
}).json()
if (deleted_host.error) console.log(deleted_host.error)
})
} else module.exports.host = null

View File

@ -9,14 +9,14 @@
"main": "index.js",
"scripts": {
"start": "node bin/bds_maneger.js -s",
"start:telegram": "node bin/telegram_bot.js -e js",
"stop": "node -p 'console.log(require(\"./\").kill());'",
"test": "node .Build/test/ci.js",
"ci": "node .Build/test/ci.js",
"eslint": "eslint --debug .",
"eslint:fix": "eslint --debug --fix .",
"nexe": "node .Build/nexe_build.js",
"Docker": "node .Build/DockerImage.js",
"dev:telegram": "node bin/telegram_bot.js -e js"
"Docker": "node .Build/DockerImage.js"
},
"bin": {
"bds_maneger": "./bin/bds_maneger.js",

View File

@ -44,7 +44,7 @@ function Backup() {
for (let index of Object.getOwnPropertyNames(GetPaths("all")).filter(path => !/servers|backups/.test(path)).map(name => GetPaths(name))) {
if (existsSync(index)) {
const _S = statSync(resolve(index));
if (_S.isFile() || _S.isSymbolicLink()) zip.addLocalFile(index); else zip.addLocalFolder(index)
if (_S.isFile() || _S.isSymbolicLink()) zip.addLocalFile(index, "/BdsManegerCore"); else zip.addLocalFolder(index, join("/BdsManegerCore", index.replace(bds_dir, "")));
}
}

View File

@ -5,7 +5,7 @@ const { resolve, join } = require("path");
const randomUUID = require("uuid").v4;
const { CronJob } = require("cron");
const { GetCronBackup } = require("../lib/BdsSettings");
const { Backup } = require("./backups");
const { Backup } = require("./BdsBackup");
// Bds Maneger Inports
const commandExists = require("../lib/commandExist");

89
src/BdsNetwork.js Normal file
View File

@ -0,0 +1,89 @@
// External User ip
const fetchSync = require("@the-bds-maneger/fetchsync");
const os = require("os");
const { GetTempHost } = require("../lib/BdsSettings");
const externalIP = {
ipv4: fetchSync("https://api.ipify.org/").text().replace("\n", ""),
ipv6: fetchSync("https://api64.ipify.org/").text().replace("\n", "")
}
// Internal ip user
const interfaces = os.networkInterfaces();
const internal_ip = [];
for (let inter of Object.getOwnPropertyNames(interfaces).map(index => interfaces[index])){
for (let ind in inter){
if (inter[ind].address.includes("::")) internal_ip.push(`[${inter[ind].address}]`)
else internal_ip.push(inter[ind].address)
}
}
// Network Interfaces
const Interfaces = Object.getOwnPropertyNames(interfaces).map(inter => {
inter = interfaces[inter]
if (inter[0].mac !== "00:00:00:00:00:00") {
try {
return {
MAC: inter[0].mac,
Interna_IP: {
ipv4: inter[0].address,
ipv6: inter[1].address,
}
}
} catch (err) {
return {
MAC: inter[0].mac,
Interna_IP: {
ipv4: inter[0].address,
ipv6: null,
}
}
}
}
}).filter(a=>a);
// Temp Host
var host = null,
HostResponse = null;
if (GetTempHost()){
// Get HOST
HostResponse = fetchSync("https://bds-core-back-end.vercel.app/Gethost", {
method: "POST",
mode: "cors",
body: JSON.stringify({
mac: Interfaces[0].MAC,
ip: external_ip.ipv4,
}),
headers: {
"Content-Type": "application/json"
}
}).json();
global.BdsTempHost = HostResponse.user.host.host
host = HostResponse.user.host.host
console.log(`Bds Maneger Core Temp Host ID: ${HostResponse.user.ID}`)
// Delete Host
process.on("exit", function () {
const deleted_host = fetchSync("https://bds-core-back-end.vercel.app/DeleteHost", {
method: "post",
body: JSON.stringify({
"ID": HostResponse.user.ID
}),
headers: {
"Content-Type": "application/json"
}
}).json()
if (deleted_host.error) console.log(deleted_host.error)
})
} else module.exports.host = null
module.exports = {
externalIP,
ip: externalIP,
internal_ip,
Interfaces,
HostResponse,
host,
}

View File

@ -1,5 +1,5 @@
const bds = require("../index")
const { CheckBan } = require("./check");
const { CheckBan } = require("./UsersAndtokenChecks");
const { GetPlatform, GetPaths } = require("../lib/BdsSettings");
const fs = require("fs");

View File

@ -2,21 +2,19 @@ const { bds_dir, GetServerBan, GetTelegramAdmins, GetPlatform, GetPaths } = requ
const { existsSync, readFileSync } = require("fs")
const { join } = require("path")
module.exports.checkUser = function (admin_name){
var adm = GetTelegramAdmins();
for(let check_ in adm){
const admin_check = adm[check_]
function CheckTelegramUser(admin_name){
for(let admin_check of GetTelegramAdmins()){
if (admin_name === admin_check || admin_check === "all_users") return true;
}
return false
}
module.exports.CheckPlayer = function (player = "null"){
function CheckPlayer(player = "null"){
const json = require(GetPaths("player"))[GetPlatform()];
if (json[player]) return true; else return false
}
module.exports.token_verify = function (token){
function token_verify(token){
const path_tokens = join(bds_dir, "bds_tokens.json")
if (existsSync(path_tokens)) var tokens = JSON.parse(readFileSync(path_tokens, "utf8")); else return false
for (let token_verify of tokens) {
@ -26,7 +24,7 @@ module.exports.token_verify = function (token){
return false
}
module.exports.CheckBan = function (player){
function CheckBan(player){
var players = GetServerBan();
for(let check_ in players){
const admin_check = players[check_]
@ -35,4 +33,11 @@ module.exports.CheckBan = function (player){
}
}
return false
}
module.exports = {
CheckTelegramUser,
CheckPlayer,
CheckBan,
token_verify
}

View File

@ -4,7 +4,7 @@ const { randomUUID } = require("crypto");
const express = require("express");
const { google } = require("googleapis");
const ip_andress = require("../../external_ip");
const ip_andress = require("../../BdsNetwork");
const { bds_dir } = require("../../../lib/BdsSettings");
const PathToToken = join(bds_dir, "google_user_token.json");

View File

@ -1,46 +0,0 @@
// External User ip
const fetchSync = require("@the-bds-maneger/fetchsync");
const os = require("os")
const externalIP = {
ipv4: fetchSync("https://api.ipify.org/").text().replace("\n", ""),
ipv6: fetchSync("https://api64.ipify.org/").text().replace("\n", "")
}
module.exports.external_ip = externalIP
module.exports.ip = externalIP
// Internal ip user
const interfaces = os.networkInterfaces();
const internal_ip = []
for (let index of Object.getOwnPropertyNames(require("os").networkInterfaces())){
const inter = interfaces[index]
for (let ind in inter){
if (inter[ind].address.includes("::")) internal_ip.push(`[${inter[ind].address}]`)
else internal_ip.push(inter[ind].address)
}
}
module.exports.internal_ip = internal_ip
// Network Interfaces
const a = os.networkInterfaces();
module.exports.Interfaces = Object.getOwnPropertyNames(a).map(inter => {
inter = a[inter]
if (inter[0].mac !== "00:00:00:00:00:00") {
try {
return {
MAC: inter[0].mac,
Interna_IP: {
ipv4: inter[0].address,
ipv6: inter[1].address,
}
}
} catch (err) {
return {
MAC: inter[0].mac,
Interna_IP: {
ipv4: inter[0].address,
ipv6: null,
}
}
}
}
}).filter(a=>a)

View File

@ -62,7 +62,7 @@ function api(port_api = 1932, callback = function (port){console.log("Bds Manege
}
module.exports = function (apiConfig = {api_port: 1932}, callback = function (port){console.log("Bds Maneger Core REST API, http port", port)}){
var port_rest = 1932;
if (typeof apiConfig == "object" && apiConfig.api_port !== undefined) port_rest = apiConfig.api_port;
if (typeof apiConfig === "object" && apiConfig.api_port !== undefined) port_rest = apiConfig.api_port;
else if (typeof apiConfig === "number") port_rest = apiConfig;
return api(port_rest, callback);
}

View File

@ -5,7 +5,7 @@ const commandExist = require("../../../lib/commandExist");
const { GetPlatform, GetServerVersion, UpdatePlatform, bds_dir } = require("../../../lib/BdsSettings");
const admzip = require("adm-zip");
const bds = require("../../../index");
const { token_verify } = require("../../check");
const { token_verify } = require("../../UsersAndtokenChecks");
// Backup
app.get("/backup", (req, res) => {

View File

@ -2,7 +2,7 @@ const express = require("express");
const app = express.Router();
const { GetPlatform } = require("../../../lib/BdsSettings")
const bds = require("../../../index");
const { token_verify, CheckPlayer } = require("../../check");
const { token_verify, CheckPlayer } = require("../../UsersAndtokenChecks");
const { readFileSync } = require("fs");
const docs = require("../../../BdsManegerInfo.json").docs;