From 4e24561790b45be04d16244e99d933d511163470 Mon Sep 17 00:00:00 2001 From: Anonymous275 Date: Sun, 12 Apr 2020 01:18:44 +0300 Subject: [PATCH] 2h of work pls no kill --- CMakeLists.txt | 2 +- CoreNetwork.cpp | 4 +- Resources.cpp | 183 ++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 6 +- 4 files changed, 190 insertions(+), 5 deletions(-) create mode 100644 Resources.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a49ae20..7f3b8a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,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 CoreNetwork.cpp) +add_executable(BeamMP-Launcher main.cpp proxy.cpp Security.cpp http.cpp Discord.cpp UpdateCheck.cpp CoreNetwork.cpp Resources.cpp) target_link_libraries(BeamMP-Launcher discord-rpc libcurl_a) \ No newline at end of file diff --git a/CoreNetwork.cpp b/CoreNetwork.cpp index 1299d69..095604d 100644 --- a/CoreNetwork.cpp +++ b/CoreNetwork.cpp @@ -14,6 +14,7 @@ std::string HTTP_REQUEST(const std::string&url,int port); void ProxyThread(const std::string& IP, int port); +void SyncResources(const std::string& IP, int port); std::string Parse(const std::string& Data){ char Code = Data.substr(0,1).at(0); @@ -25,7 +26,8 @@ std::string Parse(const std::string& Data){ 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))); + //ProxyThread(Data.substr(1,Data.find(':')-1),std::stoi(Data.substr(Data.find(':')+1))); + SyncResources(Data.substr(1,Data.find(':')-1),std::stoi(Data.substr(Data.find(':')+1))); return ""; default: return ""; diff --git a/Resources.cpp b/Resources.cpp new file mode 100644 index 0000000..4b0e25e --- /dev/null +++ b/Resources.cpp @@ -0,0 +1,183 @@ +/// +/// Created by Anonymous275 on 4/11/2020 +/// + +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include +#include +#include + +std::vector Split(const std::string& String,const std::string& delimiter){ + std::vector Val; + size_t pos = 0; + std::string token,s = String; + while ((pos = s.find(delimiter)) != std::string::npos) { + token = s.substr(0, pos); + Val.push_back(token); + s.erase(0, pos + delimiter.length()); + } + Val.push_back(s); + return Val; +} + +void SyncResources(const std::string&IP,int Port){ + std::cout << "Called" << std::endl; + std::string FileList; + struct stat info{}; + if(stat( "Resources", &info) != 0){ + _wmkdir(L"Resources"); + } + + WSADATA wsaData; + SOCKET SendingSocket; + SOCKADDR_IN ServerAddr, ThisSenderInfo; + int RetCode; + int BytesSent, nlen; + + WSAStartup(MAKEWORD(2,2), &wsaData); + + + SendingSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + + if(SendingSocket == INVALID_SOCKET) + { + printf("Client: socket() failed! Error code: %d\n", WSAGetLastError()); + WSACleanup(); + return; + } + + // Set up a SOCKADDR_IN structure that will be used to connect + + // to a listening server on port 5150. For demonstration + + // purposes, let's assume our server's IP address is 127.0.0.1 or localhost + + // IPv4 + + ServerAddr.sin_family = AF_INET; + + // Port no. + + ServerAddr.sin_port = htons(Port); + + // The IP address + + ServerAddr.sin_addr.s_addr = inet_addr(IP.c_str()); + + // Make a connection to the server with socket SendingSocket. + + RetCode = connect(SendingSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr)); + + if(RetCode != 0) + { + printf("Client: connect() failed! Error code: %ld\n", WSAGetLastError()); + // Close the socket + closesocket(SendingSocket); + // Do the clean up + WSACleanup(); + // Exit with error + return; + } + + // At this point you can start sending or receiving data on + // the socket SendingSocket. + + // Some info on the receiver side... + + getsockname(SendingSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr)); + + /*printf("Client: Receiver IP(s) used: %s\n", inet_ntoa(ServerAddr.sin_addr)); + + printf("Client: Receiver port used: %d\n", htons(ServerAddr.sin_port));*/ + + BytesSent = send(SendingSocket, "a", 1, 0); + + + int iResult; + char recvbuf[65535]; + int recvbuflen = 65535; + + do { + iResult = recv(SendingSocket, recvbuf, recvbuflen, 0); + if (iResult > 0) { + //printf("Bytes received: %d\n", iResult); + std::string Data = recvbuf,Response,toSend; + Data.resize(iResult); + std::vector list = Split(Data,";"); + Data.clear(); + Data.resize(recvbuflen); + for(const std::string& a : list){ + if(!a.empty() && stat(a.c_str(), &info) != 0){ + std::ofstream LFS; + LFS.open(a.c_str()); + LFS.close(); + toSend = "b"+a; + std::cout << a << std::endl; + send(SendingSocket, toSend.c_str(), toSend.length(), 0); + do{ + iResult = recv(SendingSocket, &Data[0], recvbuflen, 0); + if(iResult < 0)break; + Data.resize(iResult); + if(Data.find("End of file") != std::string::npos){ + if(Data.length() > 11){ + LFS.open (a.c_str(), std::ios_base::app | std::ios::binary); + LFS << Data.substr(0,Data.length()-11); + LFS.close(); + } + std::cout << "File Done\n"; + break; + } + LFS.open (a.c_str(), std::ios_base::app | std::ios::binary); + LFS << Data; + LFS.close(); + }while(Data != "End of file"); + } + } + break; + // Echo the buffer back to the sender + /*iSendResult = send(Client, Response.c_str(), Response.length(), 0); + if (iSendResult == SOCKET_ERROR) { + printf("send failed with error: %d\n", WSAGetLastError()); + closesocket(Client); + return; + } + printf("Bytes sent: %d\n", iSendResult);*/ + } + else if (iResult == 0) + printf("Connection closing...\n"); + else { + printf("recv failed with error: %d\n", WSAGetLastError()); + closesocket(SendingSocket); + break; + } + } while (iResult > 0); + + + if(BytesSent == SOCKET_ERROR) + printf("Client: send() error %ld.\n", WSAGetLastError()); + + + + if( shutdown(SendingSocket, SD_SEND) != 0) + printf("Client: Well, there is something wrong with the shutdown() The error code: %ld\n", WSAGetLastError()); + + // When you are finished sending and receiving data on socket SendingSocket, + + // you should close the socket using the closesocket API. We will + + // describe socket closure later in the chapter. + + if(closesocket(SendingSocket) != 0) + printf("Client: Cannot close \"SendingSocket\" socket. Error code: %ld\n", WSAGetLastError()); + + + + + // When your application is finished handling the connection, call WSACleanup. + + if(WSACleanup() != 0) + printf("Client: WSACleanup() failed!...\n"); +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index 2c099dd..216f990 100644 --- a/main.cpp +++ b/main.cpp @@ -76,7 +76,7 @@ std::string CheckVer(const std::string &path){ } return temp; } - +void SyncResources(const std::string&IP,int Port); int main(int argc, char* argv[]) { std::string ver = "0.21", Path = CheckDir(argv[0],ver),HTTP_Result; @@ -110,8 +110,8 @@ int main(int argc, char* argv[]) ///HTTP REQUEST FOR SERVER LIST ///Mods - ProxyStart(); //Proxy main start - + //ProxyStart(); //Proxy main start + SyncResources("127.0.0.1",30814); Exit(""); return 0; } \ No newline at end of file