2021-01-17 21:49:59 -03:00
console . log ( ` Running the Bds Maneger API in version ${ require ( _ _dirname + "/package.json" ) . version } ` )
2021-01-30 23:10:11 -03:00
var shell = require ( "shelljs" ) ;
2021-01-17 21:49:59 -03:00
let blanks ;
2020-12-04 11:33:24 -03:00
function date ( fu ) {
var today = new Date ( ) ;
2021-01-15 15:41:16 -03:00
if ( fu == "year" )
2020-12-04 11:33:24 -03:00
return ` ${ today . getFullYear ( ) } `
2021-01-15 15:41:16 -03:00
else if ( fu == "day" )
return ` ${ String ( today . getDate ( ) ) . padStart ( 2 , "0" ) } `
else if ( fu == "month" )
return ` ${ String ( today . getMonth ( ) + 1 ) . padStart ( 2 , "0" ) } `
else if ( fu == "hour" )
2020-12-04 11:33:24 -03:00
return ` ${ today . getHours ( ) } _ ${ today . getMinutes ( ) } `
2021-01-08 15:12:42 -03:00
else
2021-01-15 15:41:16 -03:00
return ` ${ String ( today . getDate ( ) ) . padStart ( 2 , "0" ) } - ${ String ( today . getMonth ( ) + 1 ) . padStart ( 2 , "0" ) } - ${ today . getFullYear ( ) } _ ${ today . getHours ( ) } - ${ today . getSeconds ( ) } `
2020-12-04 11:33:24 -03:00
}
2021-01-15 15:41:16 -03:00
if ( process . argv [ 0 ] . includes ( "electron" ) ) {
2020-12-12 00:37:02 -03:00
var electron _de = true ;
2021-01-15 15:41:16 -03:00
} else if ( process . argv [ 0 ] . includes ( "node" ) ) {
2020-12-12 00:37:02 -03:00
var electron _de = undefined ;
2021-01-17 21:49:59 -03:00
if ( process . env . BDS _MONI == blanks ) {
process . env . BDS _MONI = true
}
// process.env.BDS_MONI
if ( process . env . ENABLE _BDS _API == blanks ) {
process . env . ENABLE _BDS _API = true
}
// process.env.ENABLE_BDS_API
2020-12-12 00:37:02 -03:00
} else {
var electron _de = false ;
}
2021-01-13 17:38:21 -03:00
const arch = process . arch
2021-01-15 15:41:16 -03:00
if ( arch == "x64" ) {
var archi = "amd64"
} else if ( arch == "arm64" ) {
2021-01-13 17:38:21 -03:00
console . warn ( ` It is not recommended to use platforms that are not amd64 (x64), please inform you that you will need to manually configure some things. \! \! ` )
2021-01-15 15:41:16 -03:00
var archi = "arm"
2021-01-13 17:38:21 -03:00
} else {
console . warn ( ` Unsupported processor, ${ arch } will not be supported by The Bds Maneger ` )
}
2021-01-15 15:41:16 -03:00
const path = require ( "path" )
const fs = require ( "fs" ) ;
2021-02-09 01:29:08 -03:00
const { error } = require ( "console" ) ;
2021-01-15 15:41:16 -03:00
const package _root = path . join ( process . cwd ( ) , "package.json" )
2021-02-07 02:29:54 +00:00
const package _root _builder = path . resolve ( "." , "resources" , "app" , "package.json" )
2021-01-15 15:41:16 -03:00
if ( process . platform == "win32" ) {
2020-12-25 19:22:51 -03:00
var home = process . env . USERPROFILE ;
2021-01-15 15:41:16 -03:00
var desktop = path . join ( home , "Desktop" )
2021-01-29 23:49:31 -03:00
if ( fs . existsSync ( package _root ) ) {
var cache _dir = path . join ( home , "AppData" , "Roaming" , require ( package _root ) . name )
2021-02-07 02:29:54 +00:00
} else if ( package _root _builder ) {
var cache _dir = path . join ( home , "AppData" , "Roaming" , require ( package _root _builder ) . name )
2021-01-29 23:49:31 -03:00
} else {
console . warn ( ` Temporary Storages, some functions will be lost after restarting the system ` ) ;
var cache _dir = path . join ( process . env . TMP , ` bds_tmp_configs ` ) ;
}
2020-12-28 17:52:31 -03:00
var tmp = process . env . TMP
2020-12-12 00:37:02 -03:00
var system = ` windows ` ;
2021-01-15 15:41:16 -03:00
} else if ( process . platform == "linux" ) {
2020-12-12 00:37:02 -03:00
var home = process . env . HOME ;
2021-01-29 23:49:31 -03:00
if ( fs . existsSync ( package _root ) ) {
var cache _dir = path . join ( home , ".config" , require ( package _root ) . name ) ;
2021-02-07 02:29:54 +00:00
} else if ( fs . existsSync ( package _root _builder ) ) {
var cache _dir = path . join ( home , ".config" , require ( package _root _builder ) . name ) ;
2021-01-29 23:49:31 -03:00
} else {
console . warn ( ` Temporary Storages, some functions will be lost after restarting the system ` ) ;
var cache _dir = ` /tmp/bds_tmp_configs ` ;
}
2021-01-18 14:31:55 -03:00
var file = path . join ( home , ".config" , "user-dirs.dirs" ) ; var data = { } ;
2021-01-18 17:31:01 -03:00
if ( fs . existsSync ( file ) ) { let content = fs . readFileSync ( file , "utf8" ) ; let lines = content . split ( /\r?\n/g ) . filter ( ( a ) => ! a . startsWith ( "#" ) ) ; for ( let line of lines ) { let i = line . indexOf ( "=" ) ; if ( i >= 0 ) { try { data [ line . substring ( 0 , i ) ] = JSON . parse ( line . substring ( i + 1 ) ) } catch ( e ) { } } } } ; if ( data [ "XDG_DESKTOP_DIR" ] ) { var desktop = data [ "XDG_DESKTOP_DIR" ] ; desktop = desktop . replace ( /\$([A-Za-z\-\_]+)|\$\{([^\{^\}]+)\}/g , ( _ , a , b ) => ( process . env [ a || b ] || "" ) ) } else { var desktop = "/tmp" }
2021-01-29 23:49:31 -03:00
var tmp = ` /tmp ` ;
var system = ` linux ` ;
2021-01-15 15:41:16 -03:00
} else if ( process . platform == "darwin" ) {
2020-12-25 19:47:45 -03:00
require ( "open" ) ( "https://github.com/Bds-Maneger/Bds_Maneger/wiki/systems-support#a-message-for-mac-os-users" )
2021-01-15 15:41:16 -03:00
console . error ( "Please use Windows or Linux MacOS Not yet supported" )
2020-12-25 12:12:05 -03:00
process . exit ( 1984 )
2020-12-12 00:37:02 -03:00
} else {
2020-12-25 12:12:05 -03:00
console . log ( ` Please use an operating system (OS) compatible with Minecraft Bedrock Server ${ process . platform } is not supported ` ) ;
2020-12-25 19:22:51 -03:00
process . exit ( 2021 )
} ;
2021-01-29 23:49:31 -03:00
// ---------
// ---------
2021-02-09 01:29:08 -03:00
if ( typeof fetch === "undefined" ) {
global . fetch = require ( "node-fetch" )
}
if ( typeof localStorage === "undefined" ) {
var localStorageS = require ( "node-localstorage" ) . LocalStorage ;
global . localStorage = new localStorageS ( path . join ( cache _dir , ` Local_Storage ` ) ) ;
}
2021-01-30 22:24:05 -03:00
var bds _dir = path . join ( home , "bds_Server" ) ;
var bds _dir _bedrock = path . join ( bds _dir , 'bedrock' ) ;
var bds _dir _java = path . join ( bds _dir , 'java' ) ;
var bds _dir _backup = path . join ( bds _dir , 'backups' ) ;
module . exports . backup _folder = bds _dir _backup
2021-01-30 23:10:11 -03:00
if ( ! ( fs . existsSync ( bds _dir ) ) ) {
console . log ( "Creating the bds directory" )
fs . mkdirSync ( bds _dir )
if ( ! ( fs . existsSync ( bds _dir ) ) ) shell . mkdir ( "-p" , bds _dir ) ;
}
2021-01-30 22:24:05 -03:00
// Configs
const bds _config _file = path . join ( bds _dir , "bds_config.json" )
if ( fs . existsSync ( bds _config _file ) ) {
var bds _config = JSON . parse ( fs . readFileSync ( bds _config _file , "utf-8" ) )
} else {
2021-02-09 01:29:08 -03:00
const bds _config = {
2021-01-30 22:24:05 -03:00
"bds_platform" : "bedrock" ,
2021-02-09 01:29:08 -03:00
"telegram_token" : "not User defined"
}
fs . writeFileSync ( bds _config _file , JSON . stringify ( bds _config ) )
2021-01-30 22:24:05 -03:00
}
module . exports . platform = bds _config . bds _platform
console . log ( ` Running on the \" ${ bds _config . bds _platform } \" platform ` )
// Configs
2021-01-29 23:49:31 -03:00
var log _dir = path . join ( bds _dir , "log" ) ;
2021-01-30 22:24:05 -03:00
var log _file = path . join ( log _dir , ` ${ date ( ) } _ ${ bds _config . bds _platform } _Bds_log.log ` ) ;
var log _date = date ( ) ;
// ---------
// ---------
2021-01-30 23:10:11 -03:00
2021-01-29 23:49:31 -03:00
2021-01-11 17:35:44 -03:00
if ( ! ( fs . existsSync ( cache _dir ) ) ) {
2021-01-12 21:46:22 -03:00
console . log ( ` Creating a folder for Storage in ${ cache _dir } ` ) ;
2021-01-30 22:24:05 -03:00
fs . mkdirSync ( cache _dir )
2021-01-30 23:00:19 -03:00
if ( ! ( fs . existsSync ( cache _dir ) ) ) shell . mkdir ( "-p" , cache _dir ) ;
2021-01-11 17:35:44 -03:00
}
2021-01-16 20:08:27 -03:00
// e
if ( ! ( fs . existsSync ( bds _dir _java ) ) ) {
console . log ( "Creating the bds directory to Java" )
2021-01-30 22:24:05 -03:00
fs . mkdirSync ( bds _dir _java )
2021-01-30 23:00:19 -03:00
if ( ! ( fs . existsSync ( bds _dir _java ) ) ) shell . mkdir ( "-p" , bds _dir _java ) ;
2021-01-16 20:08:27 -03:00
}
if ( ! ( fs . existsSync ( bds _dir _bedrock ) ) ) {
console . log ( "Creating the bds directory to Bedrock" )
2021-01-30 22:24:05 -03:00
fs . mkdirSync ( bds _dir _bedrock )
2021-01-30 23:00:19 -03:00
if ( ! ( fs . existsSync ( bds _dir _bedrock ) ) ) shell . mkdir ( "-p" , bds _dir _bedrock ) ;
2021-01-08 15:12:42 -03:00
}
2021-01-16 20:08:27 -03:00
// e
2021-01-30 23:00:19 -03:00
if ( ! ( fs . existsSync ( log _dir ) ) ) {
2021-01-12 21:46:22 -03:00
if ( ! fs . existsSync ( log _dir ) ) {
2021-01-29 23:49:31 -03:00
console . log ( ` Creating the bds log dir ( ${ log _dir } ) ` )
fs . mkdirSync ( log _dir )
2021-01-30 23:00:19 -03:00
if ( ! ( fs . existsSync ( log _dir ) ) ) shell . mkdir ( "-p" , log _dir )
2021-01-12 21:46:22 -03:00
} ;
2020-12-13 00:26:42 -03:00
} ;
2021-01-16 20:08:27 -03:00
// e
2021-01-12 21:46:22 -03:00
2021-02-09 01:29:08 -03:00
/ * *
* with this command we can change the platform with this script
*
* bedrock change _platform ( "bedrock" )
*
* java change _platform ( "java" )
* @ example change _platform ( "bedrock" )
* /
function platform _update ( plate ) {
if ( plate === "java" ) complet _ = true
else if ( plate === "bedrock" ) complet _ = true
else throw new console . error ( ` platform not identified or does not exist, ${ plate } informed platform ` ) ;
const bds _config = path . join ( bds _dir , "bds_config.json" )
try {
const config _load = JSON . parse ( fs . readFileSync ( bds _config ) )
config _load . bds _platform = plate
fs . writeFileSync ( bds _config , JSON . stringify ( config _load ) )
console . log ( ` upgrading the bedrock ${ plate } ` )
} catch ( error ) {
throw new console . error ( ` Something happened error code: ${ error } ` ) ;
}
}
if ( ( process . env . SERVER || "bedrock" ) . includes ( "java" , "JAVA" ) ) {
platform _update ( "java" )
} else {
platform _update ( "bedrock" )
2021-01-30 22:24:05 -03:00
}
2021-01-12 21:46:22 -03:00
2021-02-09 01:29:08 -03:00
module . exports . telegram _token _save = ( token ) => {
try {
const bds _config = path . join ( bds _dir , "bds_config.json" )
const config _load = JSON . parse ( fs . readFileSync ( bds _config ) )
config _load . telegram _token = token
fs . writeFileSync ( bds _config , JSON . stringify ( config _load ) )
return true
} catch {
return false
}
2021-01-30 22:24:05 -03:00
}
2021-02-09 01:29:08 -03:00
if ( require ( "fs" ) . existsSync ( path . join ( bds _dir , "telegram_token.txt" ) ) ) {
console . log ( ` We identified the old telegram token file \( ${ path . join ( bds _dir , "telegram_token.txt" ) } \) , starting the immigration process ` )
try {
const token = fs . readFileSync ( path . join ( bds _dir , "telegram_token.txt" ) , "utf-8" ) . split ( "\n" ) . join ( "" )
require ( "./index" ) . telegram _token _save ( token )
fs . rmSync ( path . join ( bds _dir , "telegram_token.txt" ) )
console . log ( ` We finished migrating the old telegram token file ` )
} catch {
throw new error ( ` It was not possible to move the old telegram token file to the new bds maneger api file ` )
}
2021-01-30 22:24:05 -03:00
}
2021-01-16 20:08:27 -03:00
2021-01-18 14:31:55 -03:00
if ( process . env . BDS _MONI == blanks ) { process . env . BDS _MONI = "false" }
if ( process . env . ENABLE _BDS _API == blanks ) { process . env . ENABLE _BDS _API = "false" }
2021-01-12 23:43:10 -03:00
2021-01-12 21:46:22 -03:00
// Fetchs
2021-01-15 15:41:16 -03:00
fetch ( "https://raw.githubusercontent.com/Bds-Maneger/Raw_files/main/credentials.json" ) . then ( response => response . text ( ) ) . then ( gd _cre => {
2021-02-09 01:29:08 -03:00
/ * *
* backup credentials and an interesting design feature , plus privacy is important
* /
2021-01-08 15:12:42 -03:00
module . exports . google _drive _credential = gd _cre
2021-02-09 01:29:08 -03:00
/ * *
* download the latest version of minecraft bedrock for android available , remember to use if you want ✌
*
* you are taking responsibility for that
* /
2021-01-18 14:31:55 -03:00
module . exports . mcpe _file = require ( "./global/auth" ) . mcpe
2021-02-09 01:29:08 -03:00
/ * *
* perform a backup of the map , some resources are still under construction in the code more works
*
* on the bedrock platform , all maps will be backed up into the "worlds" folder
*
* on the java platform the map selected in the server configuration will be backed up , any other map will have to change in the server settings to perform the backup
* /
2021-01-30 22:24:05 -03:00
module . exports . drive _backup = require ( "./global/auth" ) . drive _backup
2021-01-10 20:39:56 -03:00
} ) ;
2021-01-30 22:24:05 -03:00
fetch ( "https://raw.githubusercontent.com/Bds-Maneger/Raw_files/main/Server.json" ) . then ( response => response . json ( ) ) . then ( rawOUT => {
const versions = Object . getOwnPropertyNames ( rawOUT . bedrock ) ;
for ( let v in versions ) {
var html = ` ${ versions [ v ] } ` ;
module . exports . version _select += ` <option value= \" ${ html } \" > ${ html } </option> \n ` ;
v ++ ;
} ;
module . exports . bedrock _all _versions = Object . getOwnPropertyNames ( rawOUT . bedrock ) ;
module . exports . java _all _versions = Object . getOwnPropertyNames ( rawOUT . java ) ;
module . exports . bds _latest = rawOUT . bedrock _lateste ;
module . exports . bedrock _latest = rawOUT . bedrock _lateste ;
module . exports . java _latest = rawOUT . java _lateste ;
if ( process . env . ENABLE _BDS _API . includes ( "true" ) ) {
if ( typeof bds _api _start === "undefined" ) {
require ( "./API/api" ) ( ) ;
require ( "./API/log" ) ( ) ;
}
} else {
console . warn ( ` The API via http is disabled, for more information, visit https://docs.srherobrine23.com/enable_bds_requests.html ` )
}
module . exports . get _version = ( type ) => {
if ( type == "raw" )
return rawOUT . Versions ;
else
return require ( "./" ) . version _select ;
}
2021-01-10 20:39:56 -03:00
} )
2021-01-12 21:46:22 -03:00
// Fetchs
2021-01-11 17:35:44 -03:00
// Module export
/* Variaveis */
2021-02-09 01:29:08 -03:00
/ * *
* this variable makes available the location of the user profile directory as
*
* Linux : / h o m e / U S E R /
*
* Windows : C : \ \ Users \ \ USER \ \
*
* MacOS : not supported
* /
2021-01-11 17:35:44 -03:00
module . exports . home = home
2021-02-09 01:29:08 -03:00
/ * *
* With different languages and systems we want to find the user ' s desktop for some link in the directory or even a nice shortcut
* /
2021-01-13 17:38:21 -03:00
module . exports . desktop = desktop
2021-02-09 01:29:08 -03:00
/ * *
* Identifying a system in the script can be simple with this variable
* /
2021-01-11 17:35:44 -03:00
module . exports . system = system
2021-02-09 01:29:08 -03:00
/ * *
* The most important directory of this project , here are saved some important things like :
*
* The server software
*
* configuration of the Bds Manager API
*
* Backups etc ...
* /
2021-01-16 20:08:27 -03:00
module . exports . bds _dir = bds _dir
2021-02-09 01:29:08 -03:00
2021-01-16 20:08:27 -03:00
module . exports . bds _dir _bedrock = bds _dir _bedrock
module . exports . bds _dir _java = bds _dir _java
2021-01-11 17:35:44 -03:00
module . exports . tmp _dir = tmp
module . exports . electron = electron _de
module . exports . api _dir = cache _dir
module . exports . log _file = log _file
module . exports . log _date = log _date
2021-01-13 17:38:21 -03:00
module . exports . arch = archi
2021-01-11 17:35:44 -03:00
2021-02-09 01:29:08 -03:00
// module.exports.token = JSON.parse(fs.readFileSync(path.join(bds_dir, "bds_config.json"))).telegram_token
module . exports . telegram _token = JSON . parse ( fs . readFileSync ( path . join ( bds _dir , "bds_config.json" ) ) ) . telegram _token
2021-01-18 14:31:55 -03:00
// Global commands
module . exports . telegram = require ( "./global/telegram_bot" )
2021-02-09 01:29:08 -03:00
module . exports . change _platform = platform _update
module . exports . token _register = ( ) => {
if ( ! ( fs . existsSync ( path . join ( bds _dir , "bds_tokens.json" ) ) ) ) {
fs . writeFileSync ( path . join ( bds _dir , "bds_tokens.json" ) , "[]" ) } ;
require ( "crypto" ) . randomBytes ( 10 , function ( err , buffer ) {
var token = buffer . toString ( "hex" ) ;
console . log ( token ) ;
var QRCode = require ( "qrcode" ) ;
QRCode . toString ( token , function ( err , url ) {
fs . readFile ( path . join ( bds _dir , "bds_tokens.json" ) , "utf8" , function ( err , data ) {
if ( err ) { console . log ( err ) ; }
else {
objeto = JSON . parse ( data ) ;
var count = Object . keys ( obj ) . length ;
var teste = { count , token } ;
objeto . push ( teste ) ;
json _ = JSON . stringify ( objeto ) ;
fs . writeFileSync ( path . join ( bds _dir , "bds_tokens.json" ) , json _ , "utf8" ) ; }
} ) ;
} )
} )
}
2020-12-13 00:26:42 -03:00
module . exports . date = date
2021-02-09 01:29:08 -03:00
/ * *
* sending commands more simply to the server
*
* @ example bds . command ( "say hello from Bds Maneger" )
* /
2021-01-18 14:31:55 -03:00
module . exports . command = require ( "./global/command" ) . command
2021-01-30 22:24:05 -03:00
// New management method
2021-02-09 01:29:08 -03:00
/ * *
* 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
*
* @ example const server = bds . start ( ) ;
* server . on . stdout ( "date" , function ( log ) { console . log ( log ) } )
* /
module . exports . start = require ( "./scripts/basic_server" ) . start
/ * *
* use this command for the server , that ' s all
* /
module . exports . stop = require ( "./scripts/basic_server" ) . stop
/ * *
* backup your map locally
* /
module . exports . backup = require ( "./scripts/backups" ) . World _BAckup
/ * *
* identify if there are any servers running in the background
*
* @ example bds . detect ( )
* // true: if the server is running
* // false: if not already
* /
module . exports . detect = require ( "./scripts/detect" )
module . exports . bds _detect = require ( "./scripts/detect" )
/ * *
* @ deprecated
* /
module . exports . version _Download = require ( "./scripts/bds_download" )
/ * *
* download some version of the java and Bedrock servers in the highest possible form
*
* @ example
* bedrock : bds . download ( "1.16.201.02" )
*
* java : bds . download ( "1.16.5" )
* /
module . exports . download = require ( "./scripts/bds_download" )
/ * *
* this function will be used to kill the server in the background
* /
module . exports . kill = require ( "./scripts/kill_server" )
module . exports . config _example = require ( "./scripts/bds_settings" ) . config _example
/ * *
* use this command to modify server settings
*
* @ example
*
* bds . set _config ( {
name : "Bedrock our Java" ,
description : "BDS Maneger" ,
gamemode : "survival" ,
difficulty : "normal" ,
player _permission : "member" ,
xbox : true ,
white _list : false ,
cheats : false ,
players : 100 ,
port : 19132 ,
port6 : 19133
} )
* /
module . exports . set _config = require ( "./scripts/bds_settings" ) . config
/ * *
* takes the server settings in JSON format
* /
module . exports . get _config = require ( "./scripts/bds_settings" ) . get _config