diff --git a/include/Logger.h b/include/Logger.h index 5c3a52f..c0a6188 100644 --- a/include/Logger.h +++ b/include/Logger.h @@ -6,6 +6,7 @@ #include void InitLog(); void except(const std::string& toPrint); +void fatal(const std::string& toPrint); void debug(const std::string& toPrint); void error(const std::string& toPrint); void info(const std::string& toPrint); diff --git a/src/GameStart.cpp b/src/GameStart.cpp index fdc1554..e854240 100644 --- a/src/GameStart.cpp +++ b/src/GameStart.cpp @@ -20,16 +20,12 @@ std::string Write(const std::string&Path){ LPCTSTR sk = Sec("Software\\BeamNG\\BeamNG.drive"); LONG openRes = RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS, &hKey); if (openRes != ERROR_SUCCESS){ - error(Sec("Please launch the game at least once")); - std::this_thread::sleep_for(std::chrono::seconds(5)); - exit(5); + fatal(Sec("Please launch the game at least once")); } std::string Query = QueryKey(hKey,4); LONG setRes = RegSetValueEx(hKey, Sec("userpath_override"), 0, REG_SZ, (LPBYTE)Path.c_str(), DWORD(Path.size())); if(setRes != ERROR_SUCCESS){ - error(Sec("Failed to launch the game")); - std::this_thread::sleep_for(std::chrono::seconds(5)); - exit(5); + fatal(Sec("Failed to launch the game")); //not fatal later } RegCloseKey(hKey); return Query; diff --git a/src/Logger.cpp b/src/Logger.cpp index 0aff62e..3d36dcc 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -7,6 +7,8 @@ #include #include #include +#include + std::string getDate() { typedef std::chrono::duration>::type> days; std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); @@ -70,6 +72,13 @@ void error(const std::string& toPrint) { std::cout << Print; addToLog(Print); } +void fatal(const std::string& toPrint) { + std::string Print = getDate() + Sec("[FATAL] ") + toPrint + "\n"; + std::cout << Print; + addToLog(Print); + std::this_thread::sleep_for(std::chrono::seconds(5)); + _Exit(-1); +} void except(const std::string& toPrint) { std::string Print = getDate() + Sec("[EXCEP] ") + toPrint + "\n"; std::cout << Print; diff --git a/src/Security/Login.cpp b/src/Security/Login.cpp index 2430224..df4c872 100644 --- a/src/Security/Login.cpp +++ b/src/Security/Login.cpp @@ -3,24 +3,64 @@ /// #include "Curl/http.h" -#include +#include "Logger.h" +#include #include +#include //check file if not present flag for login to the core network //to then get user and pass //if present use to contact the backend to refresh and get a public key for servers //public keys are one time use for a random server +using namespace std::filesystem; + +void UpdateKey(const char* newKey){ + if(newKey){ + std::ofstream Key("key"); + if(Key.is_open()){ + Key << newKey; + Key.close(); + }else fatal("Cannot write to disk!"); + }else if(exists("key")){ + remove("key"); + } +} + +void AskUser(){ + //Flag Core Network Update to have a login screen +} + /// "username":"password" /// "Guest":"Name" /// "pk":"private_key" -///TODO: test with no internet connection void CheckLocalKey(){ - for(int C = 1; C <= 10; C++) { - std::cout << PostHTTP("https://auth.beammp.com/userlogin", R"({"username":"Anonymous275", "password":"SimonAS1482001"})") << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(2)); + if(exists("key") && file_size("key") < 100){ + std::ifstream Key("key"); + if(Key.is_open()) { + auto Size = file_size("key"); + std::string Buffer(Size, 0); + Key.read(&Buffer[0], Size); + Key.close(); + + std::cout << "Key : " << Buffer << std::endl; + Buffer = PostHTTP("https://auth.beammp.com/userlogin", R"({"username":"Anonymous275", "password":""})"); + std::cout << "Ret : " << Buffer << std::endl; + if (Buffer == "-1" || Buffer.find('{') == -1) { + fatal("Cannot connect to authentication servers please try again later!"); + } + }else{ + warn("Could not open saved key!"); + UpdateKey(nullptr); + AskUser(); + } + }else{ + UpdateKey(nullptr); + AskUser(); + std::cout << "No valid Key i am sad now" << std::endl; } system("pause"); + } diff --git a/src/Startup.cpp b/src/Startup.cpp index 6e965ae..f3e2f0f 100644 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -63,9 +63,7 @@ void RequestRole(){ if(HTTP_Result == "-1"){ HTTP_Result = HTTP_REQUEST(Sec("https://backup1.beammp.com/entitlement?did=")+GetDID()+Sec("&t=l"),443); if(HTTP_Result == "-1") { - error(Sec("Sorry Backend System Outage! Don't worry it will back on soon!")); - std::this_thread::sleep_for(std::chrono::seconds(3)); - exit(-1); + fatal(Sec("Sorry Backend System Outage! Don't worry it will back on soon!")); } } if(HTTP_Result.find(Sec("\"MDEV\"")) != NPos || HTTP_Result.find(Sec("\"CON\"")) != NPos){ @@ -74,9 +72,7 @@ void RequestRole(){ } } if(HTTP_Result.find(Sec("Error")) != NPos){ - error(Sec("Sorry You need to be in the official BeamMP Discord to proceed! https://discord.gg/beammp")); - std::this_thread::sleep_for(std::chrono::seconds(3)); - exit(-1); + fatal(Sec("Sorry You need to be in the official BeamMP Discord to proceed! https://discord.gg/beammp")); } info(Sec("Client Connected!")); } @@ -90,9 +86,7 @@ void CheckForUpdates(int argc,char*args[],const std::string& CV){ HTTP = HTTP_REQUEST(link,443); fallback = true; if(HTTP.find_first_of("0123456789") == std::string::npos) { - error(Sec("Primary Servers Offline! sorry for the inconvenience!")); - std::this_thread::sleep_for(std::chrono::seconds(4)); - exit(-1); + fatal(Sec("Primary Servers Offline! sorry for the inconvenience!")); } } if(fallback){