rewrite
This commit is contained in:
Anonymous275
2020-08-21 20:58:10 +03:00
parent 232c4d7b28
commit 31c96cee94
59 changed files with 4247 additions and 1212 deletions

149
src/Init/Config.cpp Normal file
View File

@@ -0,0 +1,149 @@
///
/// Created by Anonymous275 on 7/28/2020
///
#include "Security/Enc.h"
#include "Logger.h"
#include <fstream>
#include <string>
#include <thread>
std::string ServerName;
std::string ServerDesc;
std::string Resource;
std::string MapName;
std::string Key;
int MaxPlayers;
bool Private;
int MaxCars;
bool Debug;
int Port;
void SetValues(const std::string& Line, int Index) {
int state = 0;
std::string Data;
bool Switch = false;
if (Index > 5)Switch = true;
for (char c : Line) {
if (Switch){
if (c == '\"')state++;
if (state > 0 && state < 2)Data += c;
}else{
if (c == ' ')state++;
if (state > 1)Data += c;
}
}
Data = Data.substr(1);
std::string::size_type sz;
bool Boolean = std::string(Data).find("true") != -1;//searches for "true"
switch (Index) {
case 1 :
Debug = Boolean;//checks and sets the Debug Value
break;
case 2 :
Private = Boolean;//checks and sets the Private Value
break;
case 3 :
Port = std::stoi(Data, &sz);//sets the Port
break;
case 4 :
MaxCars = std::stoi(Data, &sz);//sets the Max Car amount
break;
case 5 :
MaxPlayers = std::stoi(Data, &sz); //sets the Max Amount of player
break;
case 6 :
MapName = Data; //Map
break;
case 7 :
ServerName = Data; //Name
break;
case 8 :
ServerDesc = Data; //desc
break;
case 9 :
Resource = Data; //File name
break;
case 10 :
Key = Data; //File name
default:
break;
}
}
std::string RemoveComments(const std::string& Line){
std::string Return;
for(char c : Line) {
if(c == '#')break;
Return += c;
}
return Return;
}
void LoadConfig(std::ifstream& IFS){
std::string line;
int index = 1;
while (getline(IFS, line)) {
index++;
}
if(index-1 < 11){
error(Sec("Outdated/Incorrect config please remove it server will close in 5 secs"));
std::this_thread::sleep_for(std::chrono::seconds(3));
exit(0);
}
IFS.close();
IFS.open(Sec("Server.cfg"));
info(Sec("Config found updating values"));
index = 1;
while (getline(IFS, line)) {
if(line.rfind('#', 0) != 0 && line.rfind(' ', 0) != 0){ //Checks if it starts as Comment
std::string CleanLine = RemoveComments(line); //Cleans it from the Comments
SetValues(CleanLine,index); //sets the values
index++;
}
}
}
void GenerateConfig(){
std::ofstream FileStream;
FileStream.open (Sec("Server.cfg"));
FileStream << Sec("# This is the BeamMP Server Configuration File v0.60\n"
"Debug = false # true or false to enable debug console output\n"
"Private = false # Private?\n"
"Port = 30814 # Port to run the server on UDP and TCP\n"
"Cars = 1 # Max cars for every player\n"
"MaxPlayers = 10 # Maximum Amount of Clients\n"
"Map = \"/levels/gridmap/info.json\" # Default Map\n"
"Name = \"BeamMP New Server\" # Server Name\n"
"Desc = \"BeamMP Default Description\" # Server Description\n"
"use = \"Resources\" # Resource file name\n"
"AuthKey = \"\" # Auth Key");
FileStream.close();
}
void Default(){
info(Sec("Config not found generating default"));
GenerateConfig();
warn(Sec("You are required to input the AuthKey"));
std::this_thread::sleep_for(std::chrono::seconds(3));
exit(0);
}
void DebugData(){
debug(std::string(Sec("Debug : ")) + (Debug?"true":"false"));
debug(std::string(Sec("Private : ")) + (Private?"true":"false"));
debug(Sec("Port : ") + std::to_string(Port));
debug(Sec("Max Cars : ") + std::to_string(MaxCars));
debug(Sec("MaxPlayers : ") + std::to_string(MaxPlayers));
debug(Sec("MapName : ") + MapName);
debug(Sec("ServerName : ") + ServerName);
debug(Sec("ServerDesc : ") + ServerDesc);
debug(Sec("File : ") + Resource);
debug(Sec("Key length: ") + std::to_string(Key.length()));
}
void InitConfig(){
std::ifstream IFS;
IFS.open(Sec("Server.cfg"));
if(IFS.good())LoadConfig(IFS);
else Default();
if(IFS.is_open())IFS.close();
if(Key.empty()){
error(Sec("No AuthKey was found"));
std::this_thread::sleep_for(std::chrono::seconds(3));
exit(0);
}
if(Debug)DebugData();
}

59
src/Init/Heartbeat.cpp Normal file
View File

@@ -0,0 +1,59 @@
///
/// Created by Anonymous275 on 7/28/2020
///
#include "Security/Enc.h"
#include "Curl/Http.h"
#include "Client.hpp"
#include "Settings.h"
#include "Logger.h"
#include <thread>
#include <chrono>
std::string GetPlayers(){
std::string Return;
for(Client* c : CI->Clients){
if(c != nullptr){
Return += c->GetName() + ";";
}
}
return Return;
}
std::string GenerateCall(){
std::string State = Private ? Sec("true") : Sec("false");
std::string ret = Sec("uuid=");
ret += Key+Sec("&players=")+std::to_string(CI->Size())+Sec("&maxplayers=")+std::to_string(MaxPlayers)+Sec("&port=")
+ std::to_string(Port) + Sec("&map=") + MapName + Sec("&private=")+State+Sec("&version=")+GetSVer()+
Sec("&clientversion=")+GetCVer()+Sec("&name=")+ServerName+Sec("&pps=")+StatReport+Sec("&modlist=")+FileList+
Sec("&modstotalsize=")+std::to_string(MaxModSize)+Sec("&modstotal=")+std::to_string(ModsLoaded)
+Sec("&playerslist=")+GetPlayers()+Sec("&desc=")+ServerDesc;
return ret;
}
void Heartbeat(){
std::string R,T;
while(true){
R = GenerateCall();
if(!CustomIP.empty())R+=Sec("&ip=")+CustomIP;
//https://beamng-mp.com/heartbeatv2
std::string link = Sec("https://beamng-mp.com/heartbeatv2");
T = PostHTTP(link,R);
if(T.find_first_not_of(Sec("20")) != std::string::npos){
//Backend system refused server startup!
std::this_thread::sleep_for(std::chrono::seconds(10));
T = PostHTTP(link,R);
if(T.find_first_not_of(Sec("20")) != std::string::npos){
error(Sec("Backend system refused server! Check your AuthKey"));
std::this_thread::sleep_for(std::chrono::seconds(3));
exit(-1);
}
}
//Server Authenticated
if(T.length() == 4)info(Sec("Server authenticated"));
R.clear();
T.clear();
std::this_thread::sleep_for(std::chrono::seconds(3));
}
}
void HBInit(){
std::thread HB(Heartbeat);
HB.detach();
}

33
src/Init/Resources.cpp Normal file
View File

@@ -0,0 +1,33 @@
///
/// Created by Anonymous275 on 7/28/2020
///
#include "Security/Enc.h"
#include <filesystem>
#include "Settings.h"
#include <algorithm>
#include "Logger.h"
namespace fs = std::experimental::filesystem;
uint64_t MaxModSize = 0;
std::string FileSizes;
std::string FileList;
int ModsLoaded = 0;
void InitRes(){
std::string Path = Resource + Sec("/Client");
if(!fs::exists(Path))fs::create_directory(Path);
for (const auto & entry : fs::directory_iterator(Path)){
auto pos = entry.path().string().find(Sec(".zip"));
if(pos != std::string::npos){
if(entry.path().string().length() - pos == 4){
FileList += entry.path().string() + ";";
FileSizes += std::to_string(fs::file_size(entry.path()))+";";
MaxModSize += fs::file_size(entry.path());
ModsLoaded++;
}
}
}
std::replace(FileList.begin(),FileList.end(),'\\','/');
if(ModsLoaded){
info(Sec("Loaded ")+std::to_string(ModsLoaded)+Sec(" Mods"));
}
}

32
src/Init/Startup.cpp Normal file
View File

@@ -0,0 +1,32 @@
///
/// Created by Anonymous275 on 7/28/2020
///
#include "Security/Enc.h"
#include "Client.hpp"
#include "Logger.h"
#include <string>
std::string CustomIP;
std::string GetSVer(){
return Sec("0.60");
}
std::string GetCVer(){
return Sec("1.60");
}
void Args(int argc, char* argv[]){
info(Sec("BeamMP Server Running version ") + GetSVer());
if(argc > 1){
CustomIP = argv[1];
size_t n = std::count(CustomIP.begin(), CustomIP.end(), '.');
auto p = CustomIP.find_first_not_of(Sec(".0123456789"));
if(p != std::string::npos || n != 3 || CustomIP.substr(0,3) == Sec("127")){
CustomIP.clear();
warn(Sec("IP Specified is invalid! Ignoring"));
}else info(Sec("Server started with custom IP"));
}
}
void InitServer(int argc, char* argv[]){
InitLog();
Args(argc,argv);
CI = new ClientInterface;
}