From 527381f4d82c3631324cd8e33a2cc2c45f55e10f Mon Sep 17 00:00:00 2001 From: Anonymous275 Date: Fri, 10 Apr 2020 03:00:06 +0300 Subject: [PATCH] quick fixes --- CMakeLists.txt | 4 +- CoreNetwork.cpp | 140 ++++++++++++++++++++++++ UpdateCheck.cpp | 11 +- main.cpp | 32 +++--- proxy.cpp | 278 ++++++++++++++++++++++++++---------------------- 5 files changed, 314 insertions(+), 151 deletions(-) create mode 100644 CoreNetwork.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f522e46..a49ae20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.15) +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2") + include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/curl) @@ -7,6 +9,6 @@ project(BeamMP-Launcher) set(CMAKE_CXX_STANDARD 14) -add_executable(BeamMP-Launcher main.cpp proxy.cpp Security.cpp http.cpp Discord.cpp UpdateCheck.cpp) +add_executable(BeamMP-Launcher main.cpp proxy.cpp Security.cpp http.cpp Discord.cpp UpdateCheck.cpp CoreNetwork.cpp) target_link_libraries(BeamMP-Launcher discord-rpc libcurl_a) \ No newline at end of file diff --git a/CoreNetwork.cpp b/CoreNetwork.cpp new file mode 100644 index 0000000..1299d69 --- /dev/null +++ b/CoreNetwork.cpp @@ -0,0 +1,140 @@ +/// +/// Created by Anonymous275 on 4/3/2020 +/// +#include +#include +#include +#include + +#define DEFAULT_BUFLEN 64000 +#define DEFAULT_PORT "4444" + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wmissing-noreturn" + +std::string HTTP_REQUEST(const std::string&url,int port); +void ProxyThread(const std::string& IP, int port); + +std::string Parse(const std::string& Data){ + char Code = Data.substr(0,1).at(0); + std::cout << "Code : " << Code << std::endl; + std::cout << "Data : " << Data.substr(1) << std::endl; + switch (Code){ + case 'A': + return Data.substr(0,1); + case 'B': + return Code + HTTP_REQUEST("s1.yourthought.co.uk/servers-info",3599); + case 'C': + ProxyThread(Data.substr(1,Data.find(':')-1),std::stoi(Data.substr(Data.find(':')+1))); + return ""; + default: + return ""; + } +} + + +void CoreNetworkThread(){ + do{ + WSADATA wsaData; + int iResult; + SOCKET ListenSocket = INVALID_SOCKET; + SOCKET ClientSocket = INVALID_SOCKET; + + struct addrinfo *result = nullptr; + struct addrinfo hints{}; + + int iSendResult; + char recvbuf[DEFAULT_BUFLEN]; + int recvbuflen = DEFAULT_BUFLEN; + + // Initialize Winsock + iResult = WSAStartup(MAKEWORD(2,2), &wsaData); + if (iResult != 0) { + std::cout <<"WSAStartup failed with error: " << iResult << std::endl; + } + + ZeroMemory(&hints, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_flags = AI_PASSIVE; + + // Resolve the server address and port + iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); + if ( iResult != 0 ) { + std::cout << "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 << "socket failed with error: " << WSAGetLastError() << std::endl; + freeaddrinfo(result); + WSACleanup(); + } + + // Setup the TCP listening socket + iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); + if (iResult == SOCKET_ERROR) { + std::cout << "bind failed with error: " << WSAGetLastError() << std::endl; + freeaddrinfo(result); + closesocket(ListenSocket); + WSACleanup(); + } + + freeaddrinfo(result); + + iResult = listen(ListenSocket, SOMAXCONN); + if (iResult == SOCKET_ERROR) { + std::cout << "listen failed with error: " << WSAGetLastError() << std::endl; + closesocket(ListenSocket); + WSACleanup(); + } + ClientSocket = accept(ListenSocket, nullptr, nullptr); + if (ClientSocket == INVALID_SOCKET) { + std::cout << "accept failed with error: " << WSAGetLastError() << std::endl; + closesocket(ListenSocket); + WSACleanup(); + } + closesocket(ListenSocket); + + do { + std::string Response; + + iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); + if (iResult > 0) { + std::string data = recvbuf; + data.resize(iResult); + Response = Parse(data) + "\n"; + } + + else if (iResult == 0) + std::cout << "Connection closing...\n"; + else { + std::cout << "recv failed with error: " << WSAGetLastError() << std::endl; + closesocket(ClientSocket); + WSACleanup(); + } + if(!Response.empty()){ + iSendResult = send( ClientSocket, Response.c_str(), int(Response.length()), 0); + if (iSendResult == SOCKET_ERROR) { + std::cout << "send failed with error: " << WSAGetLastError() << std::endl; + closesocket(ClientSocket); + WSACleanup(); + }else{ + std::cout << "Bytes sent: " << iSendResult << std::endl; + } + } + } while (iResult > 0); + + iResult = shutdown(ClientSocket, SD_SEND); + if (iResult == SOCKET_ERROR) { + std::cout << "shutdown failed with error: " << WSAGetLastError() << std::endl; + closesocket(ClientSocket); + WSACleanup(); + } + closesocket(ClientSocket); + WSACleanup(); + }while (true); +} \ No newline at end of file diff --git a/UpdateCheck.cpp b/UpdateCheck.cpp index 29ea23f..e193480 100644 --- a/UpdateCheck.cpp +++ b/UpdateCheck.cpp @@ -3,8 +3,6 @@ /// #include -#include - void Download(const std::string& URL,const std::string& path); std::string HTTP_REQUEST(const std::string&url,int port); @@ -13,15 +11,16 @@ void WinExec(const std::string& cmd); void CheckForUpdates(const std::string& CV){ system ("cls"); - std::string HTTP = HTTP_REQUEST("https://beamng-mp.com/builds/launcher/latest?version=true",443); + std::string HTTP = HTTP_REQUEST("https://beamng-mp.com/builds/launcher?version=true",443); HTTP = HTTP.substr(HTTP.find_last_of("ver=")+1); + + struct stat buffer{}; + if(stat ("BeamMP-Launcher.back", &buffer) == 0)remove("BeamMP-Launcher.back"); if(HTTP > CV){ - struct stat buffer{}; - if(stat ("BeamMP-Launcher.back", &buffer) == 0)remove("BeamMP-Launcher.back"); std::cout << "Update found!" << std::endl; std::cout << "Updating..." << std::endl; SystemExec("rename BeamMP-Launcher.exe BeamMP-Launcher.back>nul"); - Download("https://beamng-mp.com/builds/launcher/latest?download=true", "BeamMP-Launcher.exe"); + Download("https://beamng-mp.com/builds/launcher?download=true", "BeamMP-Launcher.exe"); WinExec("BeamMP-Launcher.exe"); exit(1); }else{ diff --git a/main.cpp b/main.cpp index 3e5d119..2c099dd 100644 --- a/main.cpp +++ b/main.cpp @@ -34,6 +34,7 @@ void Exit(const std::string& Msg){ std::cin.ignore(); exit(-1); } + std::string CheckDir(char*dir, std::string ver){ system(("title BeamMP Launcher v" + ver).c_str()); char*temp;size_t len; @@ -61,7 +62,7 @@ std::string CheckDir(char*dir, std::string ver){ } std::string CheckVer(const std::string &path){ - std::string vec,Path = path.substr(0,path.find_last_of('\\')) + "\\integrity.json"; + std::string vec,temp,Path = path.substr(0,path.find_last_of('\\')) + "\\integrity.json"; std::ifstream f(Path.c_str(), std::ios::binary); f.seekg(0, std::ios_base::end); std::streampos fileSize = f.tellg(); @@ -69,13 +70,16 @@ std::string CheckVer(const std::string &path){ f.seekg(0, std::ios_base::beg); f.read(&vec[0], fileSize); f.close(); - vec = vec.substr(vec.find_last_of("version"),vec.length()); - return vec.substr(vec.find(" \"")+2,vec.find_last_of('"')-6); + vec = vec.substr(vec.find_last_of("version"),vec.find_last_of('"')); + for(const char &a : vec){ + if(isdigit(a) || a == '.')temp+=a; + } + return temp; } int main(int argc, char* argv[]) { - std::string ver = "0.16",Path = CheckDir(argv[0],ver),HTTP_Result; + std::string ver = "0.21", Path = CheckDir(argv[0],ver),HTTP_Result; CheckForUpdates(ver); //Update Check //Security @@ -92,21 +96,21 @@ int main(int argc, char* argv[]) std::cout << "HWID : " << getHardwareID() << std::endl; std::string ExeDir = GamePath.substr(0,GamePath.find_last_of('\\')) + "\\Bin64\\BeamNG.drive.x64.exe"; - Download("https://beamng-mp.com/builds/latest",Path + R"(\mods\BeamMP.zip)"); - + Download("https://beamng-mp.com/builds/client?did="+Discord_Main().at(2),Path + R"(\mods\BeamMP.zip)"); HTTP_Result = HTTP_REQUEST("https://beamng-mp.com/entitlement?did="+Discord_Main().at(2),443); - std::cout << "you are : " << HTTP_Result << std::endl; + std::cout << "you have : " << HTTP_Result << std::endl; - /*WinExec(ExeDir + " -userpath " + Path); - std::cout << "Game Launched!\n";*/ + /*if(HTTP_Result.find("[\"MDEV\"]") != std::string::npos){ + WinExec(ExeDir + " -cefdev -console -nocrashreport -userpath " + Path); + }else{ + WinExec(ExeDir + " -nocrashreport -userpath " + Path); + }*/ + //std::cout << "Game Launched!\n"; ///HTTP REQUEST FOR SERVER LIST - /*HTTP_Result = HTTP_REQUEST("s1.yourthought.co.uk/servers-info",3599); - std::cout << HTTP_Result;*/ - - ///Mods - //Start(); //Proxy main start + + ProxyStart(); //Proxy main start Exit(""); return 0; diff --git a/proxy.cpp b/proxy.cpp index d5371da..8dfad15 100644 --- a/proxy.cpp +++ b/proxy.cpp @@ -8,129 +8,25 @@ #include #include #include +#include #include +#include + #define DEFAULT_BUFLEN 64000 -#define DEFAULT_PORT "4444" +#define DEFAULT_PORT "4445" typedef struct { ENetHost *host; ENetPeer *peer; } Client; -std::string RUDPData; -std::string RUDPToSend; +std::queue RUDPData; +std::queue RUDPToSend; +bool Terminate = false; + +void CoreNetworkThread(); -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wmissing-noreturn" -void TCPServerThread(){ - do{ - WSADATA wsaData; - int iResult; - SOCKET ListenSocket = INVALID_SOCKET; - SOCKET ClientSocket = INVALID_SOCKET; - - struct addrinfo *result = NULL; - struct addrinfo hints; - - int iSendResult; - char recvbuf[DEFAULT_BUFLEN]; - int recvbuflen = DEFAULT_BUFLEN; - - // Initialize Winsock - iResult = WSAStartup(MAKEWORD(2,2), &wsaData); - if (iResult != 0) { - std::cout <<"WSAStartup failed with error: " << iResult << std::endl; - } - - ZeroMemory(&hints, sizeof(hints)); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - hints.ai_flags = AI_PASSIVE; - - // Resolve the server address and port - iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); - if ( iResult != 0 ) { - std::cout << "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 << "socket failed with error: " << WSAGetLastError() << std::endl; - freeaddrinfo(result); - WSACleanup(); - } - - // Setup the TCP listening socket - iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); - if (iResult == SOCKET_ERROR) { - std::cout << "bind failed with error: " << WSAGetLastError() << std::endl; - freeaddrinfo(result); - closesocket(ListenSocket); - WSACleanup(); - } - - freeaddrinfo(result); - - iResult = listen(ListenSocket, SOMAXCONN); - if (iResult == SOCKET_ERROR) { - std::cout << "listen failed with error: " << WSAGetLastError() << std::endl; - closesocket(ListenSocket); - WSACleanup(); - } - ClientSocket = accept(ListenSocket, NULL, NULL); - if (ClientSocket == INVALID_SOCKET) { - std::cout << "accept failed with error: " << WSAGetLastError() << std::endl; - closesocket(ListenSocket); - WSACleanup(); - } - closesocket(ListenSocket); - - do { - if(!RUDPData.empty()){ - iSendResult = send( ClientSocket, RUDPData.c_str(), int(RUDPData.length())+1, 0); - if (iSendResult == SOCKET_ERROR) { - std::cout << "send failed with error: " << WSAGetLastError() << std::endl; - closesocket(ClientSocket); - WSACleanup(); - }else{ - RUDPData.clear(); - std::cout << "Bytes sent: " << iSendResult << std::endl; - } - } - - iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); - if (iResult > 0) { - RUDPToSend = recvbuf; - RUDPToSend.resize(iResult-1); - std::cout << "size : " << RUDPToSend.size() << std::endl; - std::cout << "Data : " << RUDPToSend.c_str() << std::endl; - } - - else if (iResult == 0) - std::cout << "Connection closing...\n"; - else { - std::cout << "recv failed with error: " << WSAGetLastError() << std::endl; - closesocket(ClientSocket); - WSACleanup(); - } - - } while (iResult > 0); - - iResult = shutdown(ClientSocket, SD_SEND); - if (iResult == SOCKET_ERROR) { - std::cout << "shutdown failed with error: " << WSAGetLastError() << std::endl; - closesocket(ClientSocket); - WSACleanup(); - } - - closesocket(ClientSocket); - WSACleanup(); - }while (true); -} #pragma clang diagnostic pop #pragma clang diagnostic pop void HandleEvent(ENetEvent event,Client client){ @@ -142,7 +38,7 @@ void HandleEvent(ENetEvent event,Client client){ break; case ENET_EVENT_TYPE_RECEIVE: printf("Received: %s\n",event.packet->data); - RUDPData = reinterpret_cast(event.packet->data); + RUDPData.push(reinterpret_cast(event.packet->data)); enet_packet_destroy (event.packet); break; case ENET_EVENT_TYPE_DISCONNECT: @@ -159,48 +55,170 @@ void HandleEvent(ENetEvent event,Client client){ case ENET_EVENT_TYPE_NONE: break; } } -void RUDPClientThread(){ +void RUDPClientThread(const std::string& IP, int Port){ if (enet_initialize() != 0) { std::cout << "An error occurred while initializing ENet.\n"; } - Client client; ENetAddress address = {0}; address.host = ENET_HOST_ANY; - address.port = 30814; + address.port = Port; std::cout << "starting client...\n"; - enet_address_set_host(&address, "localhost"); - client.host = enet_host_create(NULL, 1, 2, 0, 0); + 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 == NULL) { + if (client.peer == nullptr) { std::cout << "could not connect\n"; } do { - ENetEvent event; enet_host_service(client.host, &event, 0); HandleEvent(event,client); //Handles the Events - if(!RUDPToSend.empty()){ - ENetPacket* packet = enet_packet_create (RUDPToSend.c_str(), - RUDPToSend.size()+1, + while (!RUDPToSend.empty()){ + ENetPacket* packet = enet_packet_create (RUDPToSend.front().c_str(), + RUDPToSend.front().size()+1, ENET_PACKET_FLAG_RELIABLE); //Create A reliable packet using the data enet_peer_send(client.peer, 0, packet); - RUDPToSend.clear(); + std::cout << "sending : " << RUDPToSend.front() << std::endl; + RUDPToSend.pop(); } - Sleep(50); - } while (true); + Sleep(10); + } while (!Terminate); +} +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wmissing-noreturn" +void TCPServerThread(const std::string& IP, int Port){ + Terminate = false; + WSADATA wsaData; + int iResult; + SOCKET ListenSocket = INVALID_SOCKET; + SOCKET ClientSocket = INVALID_SOCKET; + + struct addrinfo *result = NULL; + struct addrinfo hints; + + int iSendResult; + char recvbuf[DEFAULT_BUFLEN]; + int recvbuflen = DEFAULT_BUFLEN; + + // Initialize Winsock + iResult = WSAStartup(MAKEWORD(2,2), &wsaData); + if (iResult != 0) { + std::cout <<"WSAStartup failed with error: " << iResult << std::endl; + } + + ZeroMemory(&hints, sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + hints.ai_flags = AI_PASSIVE; + + // Resolve the server address and port + iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); + if ( iResult != 0 ) { + std::cout << "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 << "socket failed with error: " << WSAGetLastError() << std::endl; + freeaddrinfo(result); + WSACleanup(); + } + + // Setup the TCP listening socket + iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); + if (iResult == SOCKET_ERROR) { + std::cout << "bind failed with error: " << WSAGetLastError() << std::endl; + freeaddrinfo(result); + closesocket(ListenSocket); + WSACleanup(); + } + + freeaddrinfo(result); + + iResult = listen(ListenSocket, SOMAXCONN); + if (iResult == SOCKET_ERROR) { + std::cout << "listen failed with error: " << WSAGetLastError() << std::endl; + closesocket(ListenSocket); + WSACleanup(); + } + ClientSocket = accept(ListenSocket, NULL, NULL); + if (ClientSocket == INVALID_SOCKET) { + std::cout << "accept failed with error: " << WSAGetLastError() << std::endl; + closesocket(ListenSocket); + WSACleanup(); + } + closesocket(ListenSocket); + + std::thread t1(RUDPClientThread,IP,Port); + t1.detach(); + + do { + while(!RUDPData.empty()){ + iSendResult = send( ClientSocket, RUDPData.front().c_str(), int(RUDPData.front().length())+1, 0); + if (iSendResult == SOCKET_ERROR) { + std::cout << "send failed with error: " << WSAGetLastError() << std::endl; + closesocket(ClientSocket); + WSACleanup(); + }else{ + RUDPData.pop(); + std::cout << "Bytes sent: " << iSendResult << std::endl; + } + } + + iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); + if (iResult > 0) { + std::string buff = recvbuf; + buff.resize(iResult); + RUDPToSend.push(buff); + std::cout << "size : " << buff.size() << std::endl; + std::cout << "Data : " << buff.c_str() << std::endl; + } + + else if (iResult == 0) { + std::cout << "Connection closing...\n"; + + } + else { + std::cout << "recv failed with error: " << WSAGetLastError() << std::endl; + closesocket(ClientSocket); + WSACleanup(); + } + + } while (iResult > 0); + + iResult = shutdown(ClientSocket, SD_SEND); + if (iResult == SOCKET_ERROR) { + std::cout << "shutdown failed with error: " << WSAGetLastError() << std::endl; + closesocket(ClientSocket); + WSACleanup(); + } + + closesocket(ClientSocket); + WSACleanup(); + Terminate = true; } + void ProxyStart(){ - std::thread t1(TCPServerThread); - std::thread t2(RUDPClientThread); - t2.join(); + std::thread t1(CoreNetworkThread); + std::cout << "Core Network Started!\n"; + t1.join(); +} + +void ProxyThread(const std::string& IP, int port){ + Terminate = false; + std::thread t1(TCPServerThread,IP,port); + t1.detach(); } \ No newline at end of file