2020-12-04 11:33:24 -03:00
function date ( fu ) {
var today = new Date ( ) ;
2021-01-08 15:12:42 -03:00
if ( fu == 'year' )
2020-12-04 11:33:24 -03:00
return ` ${ today . getFullYear ( ) } `
2021-01-08 15:12:42 -03:00
else if ( fu == 'day' )
2020-12-04 11:33:24 -03:00
return ` ${ String ( today . getDate ( ) ) . padStart ( 2 , '0' ) } `
2021-01-08 15:12:42 -03:00
else if ( fu == 'month' )
2020-12-04 11:33:24 -03:00
return ` ${ String ( today . getMonth ( ) + 1 ) . padStart ( 2 , '0' ) } `
2021-01-08 15:12:42 -03:00
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
2020-12-30 11:57:54 -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
}
2020-12-12 00:37:02 -03:00
if ( process . argv [ 0 ] . includes ( 'electron' ) ) {
var electron _de = true ;
} else if ( process . argv [ 0 ] . includes ( 'node' ) ) {
var electron _de = undefined ;
} else {
var electron _de = false ;
}
2021-01-13 17:38:21 -03:00
const arch = process . arch
if ( arch == 'x64' ) {
var archi = 'amd64'
} else if ( arch == 'arm64' ) {
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. \! \! ` )
var archi = 'arm'
} else {
console . warn ( ` Unsupported processor, ${ arch } will not be supported by The Bds Maneger ` )
}
2020-12-25 19:22:51 -03:00
const path = require ( 'path' )
2021-01-09 00:00:04 -03:00
const fs = require ( 'fs' ) ;
2021-01-12 21:46:22 -03:00
const package _root = path . join ( process . cwd ( ) , 'package.json' )
2020-12-12 00:37:02 -03:00
if ( process . platform == 'win32' ) {
2020-12-25 19:22:51 -03:00
var home = process . env . USERPROFILE ;
2021-01-13 17:38:21 -03:00
var desktop = path . join ( home , 'Desktop' )
2020-12-25 19:22:51 -03:00
var server _dir = path . join ( home , ` bds_Server ` ) ;
2021-01-12 21:46:22 -03:00
if ( fs . existsSync ( package _root ) ) {
var cache _dir = path . join ( home , 'AppData' , 'Roaming' , require ( package _root ) . name )
} 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-25 19:22:51 -03:00
var log _dir = path . join ( server _dir , 'log' )
var log _file = path . join ( log _dir , ` ${ date ( ) } _Bds_log.log ` )
2020-12-12 00:37:02 -03:00
var log _date = ` ${ date ( ) } `
2020-12-28 17:52:31 -03:00
var tmp = process . env . TMP
2020-12-12 00:37:02 -03:00
var system = ` windows ` ;
} else if ( process . platform == 'linux' ) {
var home = process . env . HOME ;
2020-12-25 19:22:51 -03:00
var server _dir = path . join ( home , 'bds_Server' ) ;
2021-01-12 21:46:22 -03:00
if ( fs . existsSync ( package _root ) ) {
var cache _dir = path . join ( home , '.config' , require ( package _root ) . name ) ;
} else {
console . warn ( ` Temporary Storages, some functions will be lost after restarting the system ` )
var cache _dir = ` /tmp/bds_tmp_configs ` ;
}
2021-01-13 17:38:21 -03:00
let file = path . join ( home , ".config" , "user-dirs.dirs" )
let data = { }
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 ) { }
}
}
}
// one day will be in the documents XDG_DOCUMENTS_DIR
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-12 21:46:22 -03:00
var log _dir = path . join ( server _dir , 'log' )
var log _file = path . join ( log _dir , ` ${ date ( ) } _Bds_log.log ` )
var log _date = ` ${ date ( ) } `
var tmp = ` /tmp `
var system = ` linux ` ;
} else if ( process . platform == 'android' ) {
2021-01-13 17:38:21 -03:00
if ( process . env . ANDROID _IGNORE !== undefined ) {
var home = ` /data/data/com.temux/files/home ` ;
var server _dir = path . join ( home , 'bds_Server' ) ;
if ( fs . existsSync ( package _root ) ) {
var cache _dir = path . join ( home , '.config' , require ( package _root ) . name ) ;
} else {
console . warn ( ` Temporary Storages, some functions will be lost after restarting the system ` )
var cache _dir = ` /tmp/bds_tmp_configs ` ;
}
var log _dir = path . join ( server _dir , 'log' )
var log _file = path . join ( log _dir , ` ${ date ( ) } _Bds_log.log ` )
var log _date = ` ${ date ( ) } `
var tmp = ` /tmp `
var system = ` linux ` ;
} else {
require ( "open" ) ( "https://github.com/Bds-Maneger/Bds_Maneger/wiki/systems-support#a-message-for-android-users" )
console . error ( 'Android is not yet supported by bds manager' )
process . exit ( 2007 )
}
2020-12-12 00:37:02 -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" )
2020-12-25 12:12:05 -03:00
console . error ( 'Please use Windows or Linux MacOS Not yet supported' )
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-12 21:46:22 -03:00
var shell = require ( 'shelljs' ) ;
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-11 17:35:44 -03:00
shell . mkdir ( '-p' , cache _dir ) ;
}
2021-01-08 15:12:42 -03:00
if ( ! ( fs . existsSync ( server _dir ) ) ) {
console . log ( 'Creating the bds directory' )
2021-01-12 21:46:22 -03:00
shell . mkdir ( '-p' , server _dir ) ;
2021-01-08 15:12:42 -03:00
}
2021-01-12 21:46:22 -03:00
if ( fs . existsSync ( server _dir ) ) {
if ( ! fs . existsSync ( log _dir ) ) {
console . log ( 'Creating the bds log dir' )
shell . mkdir ( '-p' , log _dir ) ;
} ;
2020-12-13 00:26:42 -03:00
} ;
2021-01-12 21:46:22 -03:00
if ( require ( "fs" ) . existsSync ( ` ${ server _dir } /telegram_token.txt ` ) ) {
module . exports . token = require ( "fs" ) . readFileSync ( ` ${ server _dir } /telegram_token.txt ` , "utf8" ) . replaceAll ( '\n' , '' ) ;
} else {
module . exports . token = undefined ;
}
// Depacretd function
module . exports . Storage = ( ) => {
2021-01-13 21:33:29 -03:00
var localStorage = require ( 'node-localstorage' ) . localStorage ;
return new localStorage ( ` ${ cache _dir } /Local_Storage ` ) ( ) ;
2021-01-11 17:35:44 -03:00
} ;
2021-01-12 21:46:22 -03:00
2021-01-11 17:35:44 -03:00
module . exports . telegram _token _save = ( token ) => {
fs . writeFileSync ( ` ${ server _dir } /telegram_token.txt ` , token )
return 'OK'
}
2021-01-12 21:46:22 -03:00
// Set commands
2020-12-26 18:44:25 -03:00
if ( typeof fetch === 'undefined' ) {
2021-01-09 00:00:04 -03:00
global . fetch = require ( 'node-fetch' )
2021-01-08 15:12:42 -03:00
}
2021-01-12 21:46:22 -03:00
2021-01-13 21:33:29 -03:00
if ( typeof localStorage === 'undefined' ) {
var localStorage = require ( 'node-localstorage' ) . LocalStorage ;
global . localStorage = new localStorage ( ` ${ cache _dir } /Local_Storage ` ) ;
2021-01-11 17:35:44 -03:00
}
2021-01-12 23:43:10 -03:00
let blanks ;
if ( process . env . BDS _MONI == blanks ) {
process . env . BDS _MONI = 'false'
}
// process.env.BDS_MONI
if ( process . env . ENABLE _BDS _API == blanks ) {
process . env . ENABLE _BDS _API = 'false'
}
// process.env.ENABLE_BDS_API
2021-01-12 21:46:22 -03:00
// Fetchs
2021-01-08 15:12:42 -03:00
fetch ( 'https://raw.githubusercontent.com/Bds-Maneger/Raw_files/main/credentials.json' ) . then ( response => response . text ( ) ) . then ( gd _cre => {
module . exports . google _drive _credential = gd _cre
module . exports . drive _backup = require ( './Services/drive/auth' ) . drive _backup
module . exports . mcpe _file = require ( './Services/drive/auth' ) . mcpe
2021-01-10 20:39:56 -03:00
} ) ;
2021-01-08 15:12:42 -03:00
fetch ( 'https://raw.githubusercontent.com/Bds-Maneger/Raw_files/main/Server.json' ) . then ( response => response . json ( ) ) . then ( rawOUT => {
for ( let v in rawOUT . Versions ) {
var html = ` ${ rawOUT . Versions [ v ] } ` ;
var out = ` ${ out } \n <option value= \" ${ html } \" > ${ html } </option> ` ;
v ++ ;
} ;
module . exports . version _select = out . replaceAll ( undefined , '' ) ;
module . exports . version _raw = rawOUT . Versions ;
module . exports . bds _latest = rawOUT . latest ;
2021-01-12 23:43:10 -03:00
const enable _api = process . env . ENABLE _BDS _API . includes ( 'true' )
if ( enable _api ) {
if ( typeof bds _api _start === 'undefined' ) {
module . exports . api = require ( './API/api' ) ( )
} else {
console . log ( ` API already started ` )
}
2021-01-09 00:00:04 -03:00
} else {
2021-01-13 17:38:21 -03:00
console . warn ( ` The API via http is disabled, for more information, visit https://docs.srherobrine23.com/enable_bds_requests.html ` )
2021-01-09 00:00:04 -03:00
}
2021-01-12 23:43:10 -03:00
2021-01-08 15:12:42 -03:00
module . exports . get _version = ( type ) => {
if ( type == 'raw' )
return rawOUT . Versions ;
else
return out . replaceAll ( undefined , '' ) ;
}
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-01-12 21:46:22 -03:00
2021-01-11 17:35:44 -03:00
module . exports . home = home
2021-01-13 17:38:21 -03:00
module . exports . desktop = desktop
2021-01-11 17:35:44 -03:00
module . exports . system = system
module . exports . server _dir = server _dir
module . exports . world _dir = path . join ( server _dir , 'worlds' )
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
2020-12-13 00:26:42 -03:00
/* Commands server */
2020-12-25 12:12:05 -03:00
module . exports . detect = require ( "./Services/detect_bds" ) . bds _detect
2020-12-13 00:26:42 -03:00
module . exports . telegram = require ( "./Services/telegram/telegram_bot" )
2020-12-15 16:32:51 -03:00
module . exports . start = require ( './Services/start' ) . Server _start
module . exports . stop = require ( './Services/stop' ) . Server _stop
2020-12-13 00:26:42 -03:00
module . exports . date = date
2020-12-25 12:12:05 -03:00
module . exports . command = require ( './Services/command' ) . command
2020-12-15 16:32:51 -03:00
module . exports . backup = require ( "./Services/backup" ) . World _BAckup
module . exports . kill = require ( "./Services/kill" ) . bds _kill
2020-12-18 14:42:22 -03:00
module . exports . version _Download = require ( "./Services/download" ) . DownloadBDS
2020-12-20 20:48:15 -03:00
module . exports . set _config = require ( "./Services/bds_settings" ) . config
module . exports . get _config = require ( "./Services/bds_settings" ) . get _config
2021-01-08 15:12:42 -03:00
module . exports . config _example = require ( "./Services/bds_settings" ) . config _example
2021-01-10 21:58:01 -03:00
module . exports . token _register = ( ) => {
if ( ! ( fs . existsSync ( path . join ( server _dir , "bds_tokens.json" ) ) ) ) {
fs . writeFileSync ( path . join ( server _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 ( server _dir , "bds_tokens.json" ) , 'utf8' , function ( err , data ) {
if ( err ) {
console . log ( err ) ;
} else {
obj = JSON . parse ( data ) ; //now it an object
2021-01-11 17:35:44 -03:00
var count = Object . keys ( obj ) . length
var teste = { count , token }
obj . push ( teste ) ; //add some data
2021-01-10 21:58:01 -03:00
json = JSON . stringify ( obj ) ; //convert it back to json
fs . writeFileSync ( path . join ( server _dir , "bds_tokens.json" ) , json , 'utf8' ) ; // write it back
} } ) ;
} )
} ) ;
2021-01-11 17:35:44 -03:00
}
2021-01-12 23:43:10 -03:00
const bds _monitor = process . env . BDS _MONI . includes ( 'true' )
if ( bds _monitor ) {
const si = require ( 'systeminformation' ) ;
setInterval ( ( ) => {
// si.cpu().then(data => {module.exports.cpu_speed = Math.trunc(data.speed)})
si . mem ( ) . then ( data => {
module . exports . ram _free = Math . trunc ( data . free / 1024 / 1024 / 1024 ) ;
module . exports . ram _total = Math . trunc ( data . total / 1024 / 1024 / 1024 ) ;
} )
si . currentLoad ( ) . then ( data => {
module . exports . current _cpu = Math . trunc ( data . currentload )
} )
} , 1000 ) ;
si . processes ( ) . then ( data => {
const list = data . list
for ( let pid in list ) {
var pids = list [ pid ] . command
if ( pids . includes ( 'bedrock_server' ) ) {
module . exports . bds _cpu = Math . trunc ( list [ pid ] . pcpu )
} else {
pid ++
}
}
} )
setInterval ( ( ) => {
si . processes ( ) . then ( data => {
const list = data . list
for ( let pid in list ) {
var pids = list [ pid ] . command
if ( pids . includes ( 'bedrock_server' ) ) {
module . exports . bds _cpu = Math . trunc ( list [ pid ] . pcpu )
} else {
pid ++
}
}
} )
} , 3000 ) ;
} else {
2021-01-13 17:38:21 -03:00
console . warn ( ` the use of cpu is disabled, for more information, visit https://docs.srherobrine23.com/enable_bds_requests.html ` )
2021-01-12 23:43:10 -03:00
}