From 0e44138f5168c06a05cecfd3bdd97d5ad7fbb6d6 Mon Sep 17 00:00:00 2001 From: Anonymous275 Date: Fri, 1 May 2020 18:44:29 +0300 Subject: [PATCH] Discord integration,fixed game not launching with the mod, faster connection --- Compressor.cpp | 5 +- CoreNetwork.cpp | 28 +++++----- Discord.cpp | 59 +++++++++++---------- Resources.cpp | 23 ++++---- Security.cpp | 19 ++++--- VehicleNetwork.cpp | 50 +++++++++++------- include/enet.h | 2 +- main.cpp | 128 +++++++++++++++++++++++++++++++++------------ proxy.cpp | 112 +++++++++++++++++++++++++-------------- 9 files changed, 271 insertions(+), 155 deletions(-) diff --git a/Compressor.cpp b/Compressor.cpp index 779578b..5173ad2 100644 --- a/Compressor.cpp +++ b/Compressor.cpp @@ -11,10 +11,9 @@ void Print(const std::string&MSG){ //std::cout << MSG << std::endl; } - std::string Compress(const std::string&Data){ std::string b; - b.resize(Data.size()); + b.resize(Data.length()); z_stream defstream; defstream.zalloc = Z_NULL; defstream.zfree = Z_NULL; @@ -52,7 +51,7 @@ std::string Decompress(const std::string&Data) inflateEnd(&infstream); for(int i = int(c.length())-1;i >= 0;i--){ if(c.at(i) != '\0'){ - c.resize(i+2); + c.resize(i+1); break; } } diff --git a/CoreNetwork.cpp b/CoreNetwork.cpp index cac6512..90b6e12 100644 --- a/CoreNetwork.cpp +++ b/CoreNetwork.cpp @@ -19,9 +19,10 @@ extern std::string MStatus; extern int ping; extern bool Terminate; extern bool TCPTerminate; +extern bool MPDEV; void StartSync(const std::string &Data){ - std::thread t1(SyncResources,Data.substr(1,Data.find(':')-1),std::stoi(Data.substr(Data.find(':')+1))); - //std::thread t1(SyncResources,"127.0.0.1",30814); + //std::thread t1(SyncResources,Data.substr(1,Data.find(':')-1),std::stoi(Data.substr(Data.find(':')+1))); + std::thread t1(SyncResources,"127.0.0.1",30814); t1.detach(); } @@ -58,8 +59,9 @@ std::string Parse(const std::string& Data){ [[noreturn]] void CoreNetworkThread(){ + std::cout << "Ready!" << std::endl; do{ - std::cout << "Core Network on start!" << std::endl; + if(MPDEV)std::cout << "Core Network on start!" << std::endl; WSADATA wsaData; int iResult; SOCKET ListenSocket = INVALID_SOCKET; @@ -75,7 +77,7 @@ std::string Parse(const std::string& Data){ // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { - std::cout <<"WSAStartup failed with error: " << iResult << std::endl; + if(MPDEV)std::cout <<"WSAStartup failed with error: " << iResult << std::endl; } ZeroMemory(&hints, sizeof(hints)); @@ -87,14 +89,14 @@ std::string Parse(const std::string& Data){ // Resolve the server address and port iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); if ( iResult != 0 ) { - std::cout << "(Core) getaddrinfo failed with error: " << iResult << std::endl; + if(MPDEV)std::cout << "(Core) getaddrinfo failed with error: " << iResult << std::endl; WSACleanup(); } // Create a socket for connecting to server ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { - std::cout << "(Core) socket failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Core) socket failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); WSACleanup(); } @@ -102,7 +104,7 @@ std::string Parse(const std::string& Data){ // Setup the TCP listening socket iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); if (iResult == SOCKET_ERROR) { - std::cout << "(Core) bind failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Core) bind failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); @@ -112,13 +114,13 @@ std::string Parse(const std::string& Data){ iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - std::cout << "(Core) listen failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Core) listen failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); } ClientSocket = accept(ListenSocket, nullptr, nullptr); if (ClientSocket == INVALID_SOCKET) { - std::cout << "(Core) accept failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Core) accept failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); } @@ -135,16 +137,16 @@ std::string Parse(const std::string& Data){ } else if (iResult == 0) - std::cout << "(Core) Connection closing...\n"; + if(MPDEV)std::cout << "(Core) Connection closing...\n"; else { - std::cout << "(Core) recv failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Core) recv failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); } if(!Response.empty()){ iSendResult = send( ClientSocket, Response.c_str(), Response.length(), 0); if (iSendResult == SOCKET_ERROR) { - std::cout << "send failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "send failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); }else{ @@ -155,7 +157,7 @@ std::string Parse(const std::string& Data){ iResult = shutdown(ClientSocket, SD_SEND); if (iResult == SOCKET_ERROR) { - std::cout << "(Core) shutdown failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Core) shutdown failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); Sleep(500); diff --git a/Discord.cpp b/Discord.cpp index d202f9a..d751816 100644 --- a/Discord.cpp +++ b/Discord.cpp @@ -11,33 +11,37 @@ #include #include #include - -static const char* APPLICATION_ID = "345229890980937739"; +extern bool MPDEV; +static const char* APPLICATION_ID = "629743237988352010"; static int FrustrationLevel = 0; static int64_t StartTime; static int SendPresence = 1; static std::vector LocalInfo; +std::vector GetDiscordInfo(){ + return LocalInfo; +} + static void updateDiscordPresence() { if (SendPresence) { char buffer[256]; DiscordRichPresence discordPresence; memset(&discordPresence, 0, sizeof(discordPresence)); - discordPresence.state = "West of House"; - sprintf(buffer, "Frustration level: %d", FrustrationLevel); - discordPresence.details = buffer; + discordPresence.state = "Playing with friends!"; + //sprintf(buffer, "Frustration level: %d", FrustrationLevel); + //discordPresence.details = buffer; discordPresence.startTimestamp = StartTime; - discordPresence.endTimestamp = time(0) + 5 * 60; - discordPresence.largeImageKey = "canary-large"; - discordPresence.smallImageKey = "ptb-small"; - discordPresence.partyId = "party1234"; - discordPresence.partySize = 1; - discordPresence.partyMax = 6; - discordPresence.matchSecret = "xyzzy"; - discordPresence.joinSecret = "join"; - discordPresence.spectateSecret = "look"; - discordPresence.instance = 0; + //discordPresence.endTimestamp = time(0) + 5 * 60; + discordPresence.largeImageKey = "mainlogo"; + //discordPresence.smallImageKey = "logo"; + //discordPresence.partyId = "party1234"; + //discordPresence.partySize = 1; + //discordPresence.partyMax = 6; + //discordPresence.matchSecret = "xyzzy"; + //discordPresence.joinSecret = "join"; + //discordPresence.spectateSecret = "look"; + //discordPresence.instance = 0; Discord_UpdatePresence(&discordPresence); } else { @@ -58,22 +62,22 @@ static void handleDiscordReady(const DiscordUser* connectedUser) static void handleDiscordDisconnected(int errcode, const char* message) { - printf("\nDiscord: disconnected (%d: %s)\n", errcode, message); + if(MPDEV)printf("\nDiscord: disconnected (%d: %s)\n", errcode, message); } static void handleDiscordError(int errcode, const char* message) { - printf("\nDiscord: error (%d: %s)\n", errcode, message); + if(MPDEV)printf("\nDiscord: error (%d: %s)\n", errcode, message); } static void handleDiscordJoin(const char* secret) { - printf("\nDiscord: join (%s)\n", secret); + if(MPDEV)printf("\nDiscord: join (%s)\n", secret); } static void handleDiscordSpectate(const char* secret) { - printf("\nDiscord: spectate (%s)\n", secret); + if(MPDEV)printf("\nDiscord: spectate (%s)\n", secret); } static void handleDiscordJoinRequest(const DiscordUser* request) @@ -122,29 +126,30 @@ static void discordInit() Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL); } -static std::vector Loop() +static void Loop() { char line[512]; char* space; StartTime = time(0); - while (LocalInfo.size() < 3) { - //updateDiscordPresence(); + while (true) { + updateDiscordPresence(); #ifdef DISCORD_DISABLE_IO_THREAD Discord_UpdateConnection(); #endif Discord_RunCallbacks(); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); + if(LocalInfo.empty()){ + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + }else std::this_thread::sleep_for(std::chrono::milliseconds(2000)); } - return LocalInfo; } -std::vector Discord_Main() +void Discord_Main() { discordInit(); - return Loop(); - //Discord_Shutdown(); + Loop(); + Discord_Shutdown(); } diff --git a/Resources.cpp b/Resources.cpp index 67c1147..0a06a7b 100644 --- a/Resources.cpp +++ b/Resources.cpp @@ -12,7 +12,8 @@ #include namespace fs = std::experimental::filesystem; - +void Exit(const std::string& Msg); +extern bool MPDEV; std::string UlStatus = "Ulstart"; std::string MStatus = " "; @@ -30,7 +31,7 @@ std::vector Split(const std::string& String,const std::string& deli } void ProxyThread(const std::string& IP, int port); void SyncResources(const std::string&IP,int Port){ - std::cout << "Called" << std::endl; + if(MPDEV)std::cout << "Called" << std::endl; std::string FileList; struct stat info{}; if(stat( "Resources", &info) != 0){ @@ -50,7 +51,7 @@ void SyncResources(const std::string&IP,int Port){ if(SendingSocket == INVALID_SOCKET) { - printf("Client: socket() failed! Error code: %d\n", WSAGetLastError()); + if(MPDEV)printf("Client: socket() failed! Error code: %d\n", WSAGetLastError()); WSACleanup(); return; } @@ -65,7 +66,7 @@ void SyncResources(const std::string&IP,int Port){ if(RetCode != 0) { - printf("Client: connect() failed! Error code: %ld\n", WSAGetLastError()); + if(MPDEV)printf("Client: connect() failed! Error code: %ld\n", WSAGetLastError()); closesocket(SendingSocket); WSACleanup(); return; @@ -132,30 +133,30 @@ void SyncResources(const std::string&IP,int Port){ break; } else if (iResult == 0) - printf("Connection closing...\n"); + if(MPDEV)printf("Connection closing...\n"); else { - printf("(Resources) recv failed with error: %d\n", WSAGetLastError()); + if(MPDEV)printf("(Resources) recv failed with error: %d\n", WSAGetLastError()); closesocket(SendingSocket); break; } }while (iResult > 0); if(BytesSent == SOCKET_ERROR) - printf("Client: send() error %d.\n", WSAGetLastError()); + if(MPDEV)printf("Client: send() error %d.\n", WSAGetLastError()); if( shutdown(SendingSocket, SD_SEND) != 0) - printf("Client: Well, there is something wrong with the shutdown() The error code: %d\n", WSAGetLastError()); + if(MPDEV)printf("Client: Well, there is something wrong with the shutdown() The error code: %d\n", WSAGetLastError()); if(closesocket(SendingSocket) != 0) - printf("Client: Cannot close \"SendingSocket\" socket. Error code: %d\n", WSAGetLastError()); + if(MPDEV)printf("Client: Cannot close \"SendingSocket\" socket. Error code: %d\n", WSAGetLastError()); if(WSACleanup() != 0) - printf("Client: WSACleanup() failed!...\n"); + if(MPDEV)printf("Client: WSACleanup() failed!...\n"); UlStatus = "Uldone"; std::cout << "Done!" << std::endl; - ProxyThread(IP,Port); + ProxyThread(IP,Port+1); } \ No newline at end of file diff --git a/Security.cpp b/Security.cpp index 2960d27..5623faf 100644 --- a/Security.cpp +++ b/Security.cpp @@ -6,6 +6,8 @@ #include #include #include +#include + #define MAX_KEY_LENGTH 255 #define MAX_VALUE_NAME 16383 @@ -110,9 +112,10 @@ std::string QueryKey(HKEY hKey,int ID) std::string data = reinterpret_cast(buffer); std::string key = achValue; switch (ID){ - case 1: if(data.find(HTA("737465616d")) != std::string::npos) {return data;} break; - case 2: if(key == HTA("4e616d65") && data == HTA("4265616d4e472e6472697665")) {return data;} break; - case 3: return data.substr(0,data.length()-2); + case 1: if(data.find(HTA("737465616d")) != std::string::npos)return data; break; + case 2: if(key == HTA("4e616d65") && data == HTA("4265616d4e472e6472697665"))return data; break; + case 3: return data.substr(0,data.length()-2); break; + case 4: if(key == HTA("75736572706174685f6f76657272696465"))return data; default: break; } } @@ -156,10 +159,11 @@ std::vector Check(){ while (fgets(Buffer.data(), Buffer.size(), pipe.get()) != nullptr) { result += Buffer.data(); } - if(result.size() > 100 && result.find_last_of("Byte") != std::string::npos){ - int pos = result.find_last_of("Byte"); - while(result.substr(pos,4) != "File"){pos--;} - while(!isdigit(result.at(pos))){pos--;} + std::string File = HTA("3238343136302e6a736f6e"); + if(result.size() > 100 && result.find(File) != std::string::npos){ + int pos = int(result.find(File)) + 9; + while(pos != result.length() && !isdigit(result.at(pos))){pos++;} + if(pos - result.length() < 5)Exit(MSG2 + " Code 2"); if((result.substr(pos,1).at(0) - 48) == 0) Exit(MSG1 + " Code 2"); }else Exit(MSG2 + " Code 2"); result.clear(); @@ -167,7 +171,6 @@ std::vector Check(){ Result.clear(); TraceBack++; }else{Exit(MSG2 + ". Code: 4");} - K1.clear(); RegCloseKey(hKey); dwRegOPenKey = OpenKey(HKEY_CURRENT_USER, K2.c_str(), &hKey); diff --git a/VehicleNetwork.cpp b/VehicleNetwork.cpp index e88da50..7607300 100644 --- a/VehicleNetwork.cpp +++ b/VehicleNetwork.cpp @@ -10,9 +10,10 @@ #include extern bool TCPTerminate; +extern bool MPDEV; void Print(const std::string&MSG); std::queue VNTCPQueue; -extern std::queue RUDPToSend; +void RUDPSEND(const std::string&Data); #define DEFAULT_PORT "4446" void Responder(const SOCKET *CS){ @@ -23,12 +24,12 @@ void Responder(const SOCKET *CS){ VNTCPQueue.front() += "\n"; iSendResult = send(ClientSocket, VNTCPQueue.front().c_str(), VNTCPQueue.front().length(), 0); if (iSendResult == SOCKET_ERROR) { - std::cout << "(VN) send failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(VN) send failed with error: " << WSAGetLastError() << std::endl; break; } else { if(iSendResult > 1000){ - std::cout << "(Launcher->Game VN) Bytes sent: " << iSendResult << " : " << VNTCPQueue.front().substr(0,10) - << VNTCPQueue.front().substr(VNTCPQueue.front().length()-10) << std::endl; + if(MPDEV){std::cout << "(Launcher->Game VN) Bytes sent: " << iSendResult << " : " << VNTCPQueue.front().substr(0,10) + << VNTCPQueue.front().substr(VNTCPQueue.front().length()-10) << std::endl;} } VNTCPQueue.pop(); } @@ -36,10 +37,11 @@ void Responder(const SOCKET *CS){ std::this_thread::sleep_for(std::chrono::nanoseconds(1)); } } - +std::string Compress(const std::string&Data); +std::string Decompress(const std::string&Data); void VehicleNetworkStart(){ do { - std::cout << "VN on Start" << std::endl; + if(MPDEV)std::cout << "VN on Start" << std::endl; WSADATA wsaData; int iResult; SOCKET ListenSocket = INVALID_SOCKET; @@ -55,7 +57,7 @@ void VehicleNetworkStart(){ // Initialize Winsock iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { - std::cout << "(VN) WSAStartup failed with error: " << iResult << std::endl; + if(MPDEV)std::cout << "(VN) WSAStartup failed with error: " << iResult << std::endl; std::cin.get(); exit(-1); } @@ -69,7 +71,7 @@ void VehicleNetworkStart(){ // Resolve the server address and port iResult = getaddrinfo(nullptr, DEFAULT_PORT, &hints, &result); if (iResult != 0) { - std::cout << "(VN) getaddrinfo failed with error: " << iResult << std::endl; + if(MPDEV)std::cout << "(VN) getaddrinfo failed with error: " << iResult << std::endl; WSACleanup(); break; } @@ -77,7 +79,7 @@ void VehicleNetworkStart(){ // Create a socket for connecting to server ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { - std::cout << "(VN) socket failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(VN) socket failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); WSACleanup(); break; @@ -86,7 +88,7 @@ void VehicleNetworkStart(){ // Setup the TCP listening socket iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen); if (iResult == SOCKET_ERROR) { - std::cout << "(VN) bind failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(VN) bind failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); @@ -97,20 +99,20 @@ void VehicleNetworkStart(){ iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - std::cout << "(VN) listen failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(VN) listen failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); continue; } ClientSocket = accept(ListenSocket, NULL, NULL); if (ClientSocket == INVALID_SOCKET) { - std::cout << "(VN) accept failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(VN) accept failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); continue; } closesocket(ListenSocket); - std::cout << "(VN) Game Connected!" << std::endl; + if(MPDEV)std::cout << "(VN) Game Connected!" << std::endl; std::thread TCPSend(Responder,&ClientSocket); TCPSend.detach(); @@ -123,18 +125,26 @@ void VehicleNetworkStart(){ memcpy(&buff[0],recvbuf,iResult); buff.resize(iResult); //Print(buff); - if(buff.length() > 3)RUDPToSend.push(buff); - - - std::cout << "(Game->Launcher VN) Data : " << buff.length() << std::endl; + if(MPDEV) { + std::string cmp = Compress(buff), dcm = Decompress(cmp); + std::cout << "Compressed Size : " << cmp.length() << std::endl; + std::cout << "Decompressed Size : " << dcm.length() << std::endl; + if (cmp == dcm) { + std::cout << "Success!" << std::endl; + } else { + std::cout << "Fail!" << std::endl; + } + } + RUDPSEND(buff); + //std::cout << "(Game->Launcher VN) Data : " << buff.length() << std::endl; } else if (iResult == 0) { - std::cout << "(VN) Connection closing...\n"; + if(MPDEV)std::cout << "(VN) Connection closing...\n"; closesocket(ClientSocket); WSACleanup(); continue; } else { - std::cout << "(VN) recv failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(VN) recv failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); continue; @@ -143,7 +153,7 @@ void VehicleNetworkStart(){ iResult = shutdown(ClientSocket, SD_SEND); if (iResult == SOCKET_ERROR) { - std::cout << "(VN) shutdown failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(VN) shutdown failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); continue; diff --git a/include/enet.h b/include/enet.h index 2bcfdbb..6734822 100644 --- a/include/enet.h +++ b/include/enet.h @@ -601,7 +601,7 @@ enum { ENET_PEER_WINDOW_SIZE_SCALE = 64 * 1024, ENET_PEER_TIMEOUT_LIMIT = 32, ENET_PEER_TIMEOUT_MINIMUM = 5000, - ENET_PEER_TIMEOUT_MAXIMUM = 30000, + ENET_PEER_TIMEOUT_MAXIMUM = 6000, ENET_PEER_PING_INTERVAL = 500, ENET_PEER_UNSEQUENCED_WINDOWS = 64, ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 1024, diff --git a/main.cpp b/main.cpp index 38ab6b7..72cf014 100644 --- a/main.cpp +++ b/main.cpp @@ -3,20 +3,25 @@ //// #include -#include -#include +#include #include #include -#include +#include +#include +#include #pragma comment(lib, "urlmon.lib") std::string HTTP_REQUEST(const std::string&url,int port); -std::vector Discord_Main(); +void CheckForUpdates(const std::string& CV); +std::vector GetDiscordInfo(); +std::string QueryKey(HKEY hKey,int ID); std::vector GlobalInfo; std::vector Check(); std::string getHardwareID(); -void CheckForUpdates(const std::string& CV); +extern int DEFAULT_PORT; +void Discord_Main(); +bool MPDEV = false; void ProxyStart(); void Download(const std::string& URL,const std::string& path){ @@ -54,8 +59,9 @@ std::string CheckDir(char*dir, const std::string& ver){ SetCurrentDirectoryA(AD.c_str()); SystemExec("rename *.exe " + DN + ">nul"); SystemExec(R"(powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Desktop\BeamMP-Launcher.lnk');$s.TargetPath=')"+AD+"\\"+DN+"';$s.Save()\""); - CreateDirectoryA("BeamNG",nullptr); - CreateDirectoryA("BeamNG\\mods",nullptr); + if(stat("BeamNG",&info))SystemExec("mkdir BeamNG>nul"); + if(stat("BeamNG\\mods",&info))SystemExec("mkdir BeamNG\\mods>nul"); + if(stat("BeamNG\\settings",&info))SystemExec("mkdir BeamNG\\settings>nul"); SetFileAttributesA("BeamNG",2|4); return AD + "\\BeamNG"; } @@ -76,43 +82,97 @@ std::string CheckVer(const std::string &path){ return temp; } void SyncResources(const std::string&IP,int Port); + +std::string Write(const std::string&Path){ + HKEY hKey; + LPCTSTR sk = TEXT("Software\\BeamNG\\BeamNG.drive"); + + LONG openRes = RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS, &hKey); + + if (openRes != ERROR_SUCCESS) { + Exit("Error! Please launch the game at least once"); + } + std::string Query = QueryKey(hKey,4); + LPCTSTR value = TEXT("userpath_override"); + LONG setRes = RegSetValueEx(hKey, value, 0, REG_SZ, (LPBYTE)Path.c_str(), Path.size()); + + if (setRes != ERROR_SUCCESS) { + Exit("Error! Failed to launch the game code 1"); + } + RegCloseKey(hKey); + return Query; +} +void RollBack(const std::string&Val){ + std::this_thread::sleep_for(std::chrono::seconds(5)); + Write(Val); +} +void StartGame(const std::string&ExeDir,const std::string&Current){ + std::cout << "Game Launched!\n"; + std::thread RB(RollBack,Current); + RB.detach(); + SystemExec(ExeDir + " -nocrashreport"); + Exit("Game Closed!"); +} + int main(int argc, char* argv[]) { - std::string ver = "0.735", Path = CheckDir(argv[0],ver),HTTP_Result; - CheckForUpdates(ver); //Update Check + const unsigned long long NPos = std::string::npos; + struct stat info{}; + if(argc > 1){ + std::string Port = argv[1]; + if(Port.find_first_not_of("0123456789") == NPos){ + DEFAULT_PORT = std::stoi(Port); + std::cout << "Running on custom port : " << DEFAULT_PORT << std::endl; + } + } + std::string ver = "0.89", Path = CheckDir(argv[0],ver),HTTP_Result; + CheckForUpdates(ver); + std::thread t1(Discord_Main); + t1.detach(); + + + std::cout << "Connecting to discord client..." << std::endl; + while(GlobalInfo.empty()){ + GlobalInfo = GetDiscordInfo(); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } + + + HTTP_Result = HTTP_REQUEST("https://beamng-mp.com/entitlement?did="+GlobalInfo.at(2),443); + if(HTTP_Result.find("\"MDEV\"") == NPos){ + if (HTTP_Result.find("\"MOD\"") == NPos && HTTP_Result.find("\"EA\"") == NPos){ + if (HTTP_Result.find("\"SUPPORT\"") == NPos && HTTP_Result.find("\"YT\"") == NPos){ + exit(-1); + } + } + }else MPDEV = true; //Security std::vector Data = Check(); std::string GamePath = Data.at(2); - std::cout << "You own BeamNG on this machine!" << std::endl; - std::cout << "Game Ver : " << CheckVer(GamePath) << std::endl; + if(MPDEV)std::cout << "You own BeamNG on this machine!" << std::endl; + std::cout << "Game Version : " << CheckVer(GamePath) << std::endl; + std::string ExeDir = "\""+GamePath.substr(0,GamePath.find_last_of('\\')) + R"(\Bin64\BeamNG.drive.x64.exe")"; + std::string Settings = Path + "\\settings\\uiapps-layouts.json"; + if(stat(Settings.c_str(),&info)!=0){ + Download("https://beamng-mp.com/client-data",Settings); + std::cout << "Downloaded default config!" << std::endl; + } - GlobalInfo = Discord_Main(); - std::cout << "Name : " << GlobalInfo.at(0) << std::endl; - std::cout << "Discriminator : " << GlobalInfo.at(1) << std::endl; - std::cout << "Unique ID : " << GlobalInfo.at(2) << std::endl; + Download("https://beamng-mp.com/builds/client?did="+GlobalInfo.at(2),Path + R"(\mods\BeamMP.zip)"); - std::cout << "HWID : " << getHardwareID() << std::endl; - - HTTP_Result = HTTP_REQUEST("https://beamng-mp.com/entitlement?did="+Discord_Main().at(2),443); - std::cout << "you have : " << HTTP_Result << std::endl; - if(HTTP_Result.find("[\"MDEV\"]") == std::string::npos && HTTP_Result.find("[\"MOD\"]") == std::string::npos)exit(-1); ///Remove later - - - std::string ExeDir = GamePath.substr(0,GamePath.find_last_of('\\')) + "\\Bin64\\BeamNG.drive.x64.exe"; - Download("https://beamng-mp.com/builds/client?did="+Discord_Main().at(2),Path + R"(\mods\BeamMP.zip)"); - - - /*if(HTTP_Result.find("[\"MDEV\"]") != std::string::npos){ - WinExec(ExeDir + " -cefdev -console -nocrashreport -userpath " + Path); + if(!MPDEV){ + std::thread Game(StartGame,ExeDir,Write(Path + "\\")); + Game.detach(); }else{ - WinExec(ExeDir + " -nocrashreport -userpath " + Path); - }*/ - //std::cout << "Game Launched!\n"; - - - ProxyStart(); //Proxy main start + std::cout << "Name : " << GlobalInfo.at(0) << std::endl; + std::cout << "Discriminator : " << GlobalInfo.at(1) << std::endl; + std::cout << "Unique ID : " << GlobalInfo.at(2) << std::endl; + std::cout << "HWID : " << getHardwareID() << std::endl; + std::cout << "you have : " << HTTP_Result << std::endl; + } + ProxyStart(); Exit(""); return 0; } \ No newline at end of file diff --git a/proxy.cpp b/proxy.cpp index 53f2d6c..4efef0b 100644 --- a/proxy.cpp +++ b/proxy.cpp @@ -12,7 +12,8 @@ #include #include -#define DEFAULT_PORT "4445" +int DEFAULT_PORT = 4445; + typedef struct { ENetHost *host; ENetPeer *peer; @@ -20,15 +21,25 @@ typedef struct { std::vector Split(const std::string& String,const std::string& delimiter); std::chrono::time_point PingStart,PingEnd; extern std::vector GlobalInfo; +std::condition_variable RUDPLOCK,TCPLOCK; std::queue RUDPToSend; std::queue RUDPData; bool TCPTerminate = false; bool Terminate = false; bool CServer = true; int ping = 0; +extern bool MPDEV; [[noreturn]] void CoreNetworkThread(); +void TCPSEND(const std::string&Data){ + RUDPData.push(Data); + TCPLOCK.notify_all(); +} +void RUDPSEND(const std::string&Data){ + RUDPToSend.push(Data); + RUDPLOCK.notify_all(); +} void AutoPing(ENetPeer*peer){ while(!Terminate && peer != nullptr){ enet_peer_send(peer, 0, enet_packet_create("p", 2, ENET_PACKET_FLAG_RELIABLE)); @@ -54,13 +65,13 @@ void RUDPParser(const std::string& Data,ENetPeer*peer){ return; } ///std::cout << "Received: " << Data << std::endl; - RUDPData.push(Data); + TCPSEND(Data); } void HandleEvent(ENetEvent event,Client client){ switch (event.type){ case ENET_EVENT_TYPE_CONNECT: - std::cout << "(Launcher->Server) Client Connected!" << std::endl; + std::cout << "Connected to server!" << std::endl; //printf("Client Connected port : %u.\n",event.peer->address.port); event.peer->data = (void*)"Connected Server"; break; @@ -70,6 +81,8 @@ void HandleEvent(ENetEvent event,Client client){ break; case ENET_EVENT_TYPE_DISCONNECT: printf ("%s disconnected.\n", (char *)event.peer->data); + CServer = true; + Terminate = true; event.peer->data = nullptr; break; @@ -77,28 +90,29 @@ void HandleEvent(ENetEvent event,Client client){ printf ("%s timeout.\n", (char *)event.peer->data); CServer = true; Terminate = true; + TCPSEND("TTimeout"); event.peer->data = nullptr; break; - case ENET_EVENT_TYPE_NONE: break; } } void RUDPClientThread(const std::string& IP, int Port){ if (enet_initialize() != 0) { - std::cout << "An error occurred while initializing RUDP.\n"; + std::cout << "An error occurred while initializing!\n"; } + std::mutex Lock; Client client; ENetAddress address = {0}; address.host = ENET_HOST_ANY; address.port = Port; - std::cout << "(Launcher->Server) Connecting...\n"; + if(MPDEV)std::cout << "(Launcher->Server) Connecting...\n"; enet_address_set_host(&address, IP.c_str()); client.host = enet_host_create(nullptr, 1, 2, 0, 0); client.peer = enet_host_connect(client.host, &address, 2, 0); if (client.peer == nullptr) { - std::cout << "could not connect\n"; + if(MPDEV)std::cout << "could not connect\n"; } std::thread Ping(AutoPing,client.peer); Ping.detach(); @@ -107,56 +121,67 @@ void RUDPClientThread(const std::string& IP, int Port){ enet_host_service(client.host, &event, 0); HandleEvent(event,client); while (!RUDPToSend.empty()){ + if(RUDPToSend.front().length() > 3) { int Rel = 8; char C = RUDPToSend.front().at(0); - if (C == 's' || C == 'd' || C == 'm' || C == 'r')Rel = 1; + if (C == 'O' || C == 'T')Rel = 1; ENetPacket *packet = enet_packet_create(RUDPToSend.front().c_str(), RUDPToSend.front().length() + 1, Rel); enet_peer_send(client.peer, 0, packet); if (RUDPToSend.front().length() > 1000) { - std::cout << "(Launcher->Server) Bytes sent: " << RUDPToSend.front().length() << " : " + if(MPDEV){std::cout << "(Launcher->Server) Bytes sent: " << RUDPToSend.front().length() << " : " << RUDPToSend.front().substr(0, 10) - << RUDPToSend.front().substr(RUDPToSend.front().length() - 10) << std::endl; + << RUDPToSend.front().substr(RUDPToSend.front().length() - 10) << std::endl;} } + } RUDPToSend.pop(); } - std::this_thread::sleep_for(std::chrono::nanoseconds(1)); + std::unique_lock lk(Lock); + std::chrono::high_resolution_clock::time_point tp = std::chrono::high_resolution_clock::now() + std::chrono::nanoseconds (1); + RUDPLOCK.wait_until(lk, tp, [](){return !RUDPToSend.empty();}); + } while (!Terminate); enet_peer_disconnect(client.peer,0); enet_host_service(client.host, &event, 0); HandleEvent(event,client); CServer = true; - std::cout << "(Launcher->Server) Terminated!" << std::endl; + std::cout << "Connection Terminated!" << std::endl; } void TCPRespond(const SOCKET *CS){ SOCKET ClientSocket = *CS; int iSendResult; - std::mutex m; + std::mutex Lock; while(!TCPTerminate){ while (!RUDPData.empty()) { std::string Data = RUDPData.front() + "\n"; iSendResult = send(ClientSocket, Data.c_str(), Data.length(), 0); if (iSendResult == SOCKET_ERROR) { - std::cout << "(Proxy) send failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Proxy) send failed with error: " << WSAGetLastError() << std::endl; + TCPTerminate = true; break; } else { if(iSendResult > 1000){ std::cout << "(Launcher->Game) Bytes sent: " << iSendResult << " : " << RUDPData.front().substr(0,10) << RUDPData.front().substr(RUDPData.front().length()-10) << std::endl; } + //std::cout << "(Launcher->Game) Bytes sent: " << iSendResult << " : " << RUDPData.front()<< std::endl; RUDPData.pop(); } } - std::this_thread::sleep_for(std::chrono::nanoseconds(1)); + std::unique_lock lk(Lock); + std::chrono::high_resolution_clock::time_point tp = std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(1); + TCPLOCK.wait_until(lk, tp, [](){return !RUDPData.empty();}); } } +std::string Compress(const std::string&Data); +std::string Decompress(const std::string&Data); void TCPServerThread(const std::string& IP, int Port){ - std::cout << "Proxy Started! " << IP << ":" << Port << std::endl; + if(MPDEV)std::cout << "Proxy Started! " << IP << ":" << Port << std::endl; do { - std::cout << "Proxy on Start" << std::endl; + if(MPDEV)std::cout << "Proxy on Start" << std::endl; WSADATA wsaData; int iResult; SOCKET ListenSocket = INVALID_SOCKET; @@ -170,7 +195,7 @@ void TCPServerThread(const std::string& IP, int Port){ // Initialize Winsock iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { - std::cout << "(Proxy) WSAStartup failed with error: " << iResult << std::endl; + if(MPDEV)std::cout << "(Proxy) WSAStartup failed with error: " << iResult << std::endl; std::cin.get(); exit(-1); } @@ -180,11 +205,10 @@ void TCPServerThread(const std::string& IP, int Port){ hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_PASSIVE; - // Resolve the server address and port - iResult = getaddrinfo(nullptr, DEFAULT_PORT, &hints, &result); + iResult = getaddrinfo(nullptr, std::to_string(DEFAULT_PORT).c_str(), &hints, &result); if (iResult != 0) { - std::cout << "(Proxy) getaddrinfo failed with error: " << iResult << std::endl; + if(MPDEV)std::cout << "(Proxy) getaddrinfo failed with error: " << iResult << std::endl; WSACleanup(); break; } @@ -192,7 +216,7 @@ void TCPServerThread(const std::string& IP, int Port){ // Create a socket for connecting to server ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); if (ListenSocket == INVALID_SOCKET) { - std::cout << "(Proxy) socket failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Proxy) socket failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); WSACleanup(); break; @@ -201,7 +225,7 @@ void TCPServerThread(const std::string& IP, int Port){ // Setup the TCP listening socket iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen); if (iResult == SOCKET_ERROR) { - std::cout << "(Proxy) bind failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Proxy) bind failed with error: " << WSAGetLastError() << std::endl; freeaddrinfo(result); closesocket(ListenSocket); WSACleanup(); @@ -212,20 +236,20 @@ void TCPServerThread(const std::string& IP, int Port){ iResult = listen(ListenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { - std::cout << "(Proxy) listen failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Proxy) listen failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); continue; } ClientSocket = accept(ListenSocket, NULL, NULL); if (ClientSocket == INVALID_SOCKET) { - std::cout << "(Proxy) accept failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Proxy) accept failed with error: " << WSAGetLastError() << std::endl; closesocket(ListenSocket); WSACleanup(); continue; } closesocket(ListenSocket); - std::cout << "(Proxy) Game Connected!" << std::endl; + if(MPDEV)std::cout << "(Proxy) Game Connected!" << std::endl; if(CServer){ std::thread t1(RUDPClientThread, IP, Port); t1.detach(); @@ -241,18 +265,26 @@ void TCPServerThread(const std::string& IP, int Port){ buff.resize(iResult*2); memcpy(&buff[0],recvbuf,iResult); buff.resize(iResult); - - if(buff.length() > 3)RUDPToSend.push(buff); - + if(MPDEV && buff.length() > 1000) { + std::string cmp = Compress(buff), dcm = Decompress(cmp); + std::cout << "Compressed Size : " << cmp.length() << std::endl; + std::cout << "Decompressed Size : " << dcm.length() << std::endl; + if (cmp == dcm) { + std::cout << "Success!" << std::endl; + } else { + std::cout << "Fail!" << std::endl; + } + } + RUDPSEND(buff); //std::cout << "(Game->Launcher) Data : " << buff.length() << std::endl; } else if (iResult == 0) { - std::cout << "(Proxy) Connection closing...\n"; + if(MPDEV)std::cout << "(Proxy) Connection closing...\n"; closesocket(ClientSocket); WSACleanup(); continue; } else { - std::cout << "(Proxy) recv failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Proxy) recv failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); continue; @@ -261,7 +293,7 @@ void TCPServerThread(const std::string& IP, int Port){ iResult = shutdown(ClientSocket, SD_SEND); if (iResult == SOCKET_ERROR) { - std::cout << "(Proxy) shutdown failed with error: " << WSAGetLastError() << std::endl; + if(MPDEV)std::cout << "(Proxy) shutdown failed with error: " << WSAGetLastError() << std::endl; closesocket(ClientSocket); WSACleanup(); continue; @@ -271,19 +303,23 @@ void TCPServerThread(const std::string& IP, int Port){ }while (!TCPTerminate); } - +void VehicleNetworkStart(); void ProxyStart(){ std::thread t1(CoreNetworkThread); - std::cout << "Core Network Started!\n"; + if(MPDEV)std::cout << "Core Network Started!\n"; t1.join(); } -void VehicleNetworkStart(); -void ProxyThread(const std::string& IP, int Port){ +void Reset(){ Terminate = false; TCPTerminate = false; + while(!RUDPToSend.empty()) RUDPToSend.pop(); + while(!RUDPData.empty()) RUDPData.pop(); +} +void ProxyThread(const std::string& IP, int Port){ + Reset(); std::thread t1(TCPServerThread,IP,Port); t1.detach(); - std::thread t2(VehicleNetworkStart); - t2.detach(); + /*std::thread t2(VehicleNetworkStart); + t2.detach();*/ } \ No newline at end of file