From fbc5e28d2511cb5bbede777445e116b0bc7ec8ee Mon Sep 17 00:00:00 2001 From: Anonymous-275 Date: Mon, 29 Mar 2021 11:43:24 +0300 Subject: [PATCH] Mingw Compatible --- CMakeLists.txt | 28 +- README.md | 20 +- include/Curl/http.h | 0 include/Discord/discord_info.h | 0 include/Discord/discord_rpc.h | 2 +- include/Json.h | 22 +- include/Logger.h | 0 include/Network/network.h | 4 +- include/Security/Game.h | 0 include/Security/Init.h | 0 include/Startup.h | 1 + include/Zlib/Compressor.h | 0 include/rapidjson/allocators.h | 0 include/rapidjson/cursorstreamwrapper.h | 0 include/rapidjson/document.h | 0 include/rapidjson/encodedstream.h | 0 include/rapidjson/encodings.h | 0 include/rapidjson/error/en.h | 0 include/rapidjson/error/error.h | 0 include/rapidjson/filereadstream.h | 0 include/rapidjson/filewritestream.h | 0 include/rapidjson/fwd.h | 0 include/rapidjson/internal/biginteger.h | 0 include/rapidjson/internal/clzll.h | 0 include/rapidjson/internal/diyfp.h | 0 include/rapidjson/internal/dtoa.h | 0 include/rapidjson/internal/ieee754.h | 0 include/rapidjson/internal/itoa.h | 0 include/rapidjson/internal/meta.h | 0 include/rapidjson/internal/pow10.h | 0 include/rapidjson/internal/regex.h | 0 include/rapidjson/internal/stack.h | 0 include/rapidjson/internal/strfunc.h | 0 include/rapidjson/internal/strtod.h | 0 include/rapidjson/internal/swap.h | 0 include/rapidjson/istreamwrapper.h | 0 include/rapidjson/memorybuffer.h | 0 include/rapidjson/memorystream.h | 0 include/rapidjson/msinttypes/inttypes.h | 0 include/rapidjson/msinttypes/stdint.h | 0 include/rapidjson/ostreamwrapper.h | 0 include/rapidjson/pointer.h | 0 include/rapidjson/prettywriter.h | 0 include/rapidjson/rapidjson.h | 0 include/rapidjson/reader.h | 0 include/rapidjson/schema.h | 0 include/rapidjson/stream.h | 0 include/rapidjson/stringbuffer.h | 0 include/rapidjson/writer.h | 0 src/Compressor.cpp | 0 src/Config.cpp | 60 +++ src/Discord.cpp | 2 +- src/GameStart.cpp | 2 +- src/Logger.cpp | 0 src/Network/Core.cpp | 8 +- src/Network/DNS.cpp | 0 src/Network/GlobalHandler.cpp | 6 +- src/Network/Http.cpp | 282 +++++------ src/Network/Resources.cpp | 7 +- src/Network/VehicleData.cpp | 2 +- src/Network/VehicleEvent.cpp | 4 +- src/Security/BeamNG.cpp | 635 ++++++++++++------------ src/Security/Login.cpp | 223 +++++---- src/Startup.cpp | 397 +++++++-------- src/main.cpp | 0 65 files changed, 883 insertions(+), 822 deletions(-) mode change 100644 => 100755 README.md mode change 100644 => 100755 include/Curl/http.h mode change 100644 => 100755 include/Discord/discord_info.h mode change 100644 => 100755 include/Json.h mode change 100644 => 100755 include/Logger.h mode change 100644 => 100755 include/Network/network.h mode change 100644 => 100755 include/Security/Game.h mode change 100644 => 100755 include/Security/Init.h mode change 100644 => 100755 include/Startup.h mode change 100644 => 100755 include/Zlib/Compressor.h mode change 100644 => 100755 include/rapidjson/allocators.h mode change 100644 => 100755 include/rapidjson/cursorstreamwrapper.h mode change 100644 => 100755 include/rapidjson/document.h mode change 100644 => 100755 include/rapidjson/encodedstream.h mode change 100644 => 100755 include/rapidjson/encodings.h mode change 100644 => 100755 include/rapidjson/error/en.h mode change 100644 => 100755 include/rapidjson/error/error.h mode change 100644 => 100755 include/rapidjson/filereadstream.h mode change 100644 => 100755 include/rapidjson/filewritestream.h mode change 100644 => 100755 include/rapidjson/fwd.h mode change 100644 => 100755 include/rapidjson/internal/biginteger.h mode change 100644 => 100755 include/rapidjson/internal/clzll.h mode change 100644 => 100755 include/rapidjson/internal/diyfp.h mode change 100644 => 100755 include/rapidjson/internal/dtoa.h mode change 100644 => 100755 include/rapidjson/internal/ieee754.h mode change 100644 => 100755 include/rapidjson/internal/itoa.h mode change 100644 => 100755 include/rapidjson/internal/meta.h mode change 100644 => 100755 include/rapidjson/internal/pow10.h mode change 100644 => 100755 include/rapidjson/internal/regex.h mode change 100644 => 100755 include/rapidjson/internal/stack.h mode change 100644 => 100755 include/rapidjson/internal/strfunc.h mode change 100644 => 100755 include/rapidjson/internal/strtod.h mode change 100644 => 100755 include/rapidjson/internal/swap.h mode change 100644 => 100755 include/rapidjson/istreamwrapper.h mode change 100644 => 100755 include/rapidjson/memorybuffer.h mode change 100644 => 100755 include/rapidjson/memorystream.h mode change 100644 => 100755 include/rapidjson/msinttypes/inttypes.h mode change 100644 => 100755 include/rapidjson/msinttypes/stdint.h mode change 100644 => 100755 include/rapidjson/ostreamwrapper.h mode change 100644 => 100755 include/rapidjson/pointer.h mode change 100644 => 100755 include/rapidjson/prettywriter.h mode change 100644 => 100755 include/rapidjson/rapidjson.h mode change 100644 => 100755 include/rapidjson/reader.h mode change 100644 => 100755 include/rapidjson/schema.h mode change 100644 => 100755 include/rapidjson/stream.h mode change 100644 => 100755 include/rapidjson/stringbuffer.h mode change 100644 => 100755 include/rapidjson/writer.h mode change 100644 => 100755 src/Compressor.cpp create mode 100755 src/Config.cpp mode change 100644 => 100755 src/Discord.cpp mode change 100644 => 100755 src/GameStart.cpp mode change 100644 => 100755 src/Logger.cpp mode change 100644 => 100755 src/Network/Core.cpp mode change 100644 => 100755 src/Network/DNS.cpp mode change 100644 => 100755 src/Network/GlobalHandler.cpp mode change 100644 => 100755 src/Network/Http.cpp mode change 100644 => 100755 src/Network/Resources.cpp mode change 100644 => 100755 src/Network/VehicleData.cpp mode change 100644 => 100755 src/Network/VehicleEvent.cpp mode change 100644 => 100755 src/Security/BeamNG.cpp mode change 100644 => 100755 src/Security/Login.cpp mode change 100644 => 100755 src/Startup.cpp mode change 100644 => 100755 src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 70d0880..8fede8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,18 +6,22 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG") file(GLOB source_files "src/*.cpp" "src/*/*.cpp" "src/*/*.hpp" "include/*.h" "include/*/*.h" "include/*/*/*.h") add_executable(${PROJECT_NAME} ${source_files}) set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "BeamMP-Launcher") -find_package(ZLIB REQUIRED) -find_package(CURL CONFIG REQUIRED) -# This might cause issues with old windows headers, but it's worth the trouble to keep the code -# completely cross platform. For fixes to common issues arising from /permissive- visit: -# https://docs.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance +if (WIN32) + find_package(ZLIB REQUIRED) + find_package(CURL REQUIRED) + message(STATUS "MSVC -> forcing use of statically-linked runtime.") + STRING(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) + STRING(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) + #-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static + set(VcpkgRoot ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}) + include_directories(${VcpkgRoot}/include) + link_directories(${VcpkgRoot}/lib) + target_link_libraries(${PROJECT_NAME} PRIVATE discord-rpc CURL::libcurl ZLIB::ZLIB) +else(WIN32) #MINGW + add_definitions("-D_WIN32_WINNT=0x0600") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s -static-libgcc -static-libstdc++ --static") + target_link_libraries(${PROJECT_NAME} discord-rpc.a ssp z ws2_32) +endif(WIN32) -#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /permissive-") -message(STATUS "MSVC -> forcing use of statically-linked runtime.") -STRING(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) -STRING(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) -#-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static - -target_link_libraries(${PROJECT_NAME} PRIVATE rstrtmgr discord-rpc CURL::libcurl ZLIB::ZLIB) target_include_directories(${PROJECT_NAME} PUBLIC $) \ No newline at end of file diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 421f729..6dd998d --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# BeamMP-Launcher - -The launcher is the way we communitcate to outside the game, it does a few automated actions such as but not limited to: downloading the mod, launching the game, and create a connection to a server. - - -Copyright (c) 2019-present Anonymous275. -BeamMP Launcher code is not in the public domain and is not free software. -One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries, -the only permission that has been granted is to use the software in its compiled form as distributed from the BeamMP.com website. -Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. +# BeamMP-Launcher + +The launcher is the way we communitcate to outside the game, it does a few automated actions such as but not limited to: downloading the mod, launching the game, and create a connection to a server. + + +Copyright (c) 2019-present Anonymous275. +BeamMP Launcher code is not in the public domain and is not free software. +One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries, +the only permission that has been granted is to use the software in its compiled form as distributed from the BeamMP.com website. +Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. diff --git a/include/Curl/http.h b/include/Curl/http.h old mode 100644 new mode 100755 diff --git a/include/Discord/discord_info.h b/include/Discord/discord_info.h old mode 100644 new mode 100755 diff --git a/include/Discord/discord_rpc.h b/include/Discord/discord_rpc.h index 3e1441e..8609bf7 100644 --- a/include/Discord/discord_rpc.h +++ b/include/Discord/discord_rpc.h @@ -1,5 +1,5 @@ #pragma once -#include +#include // clang-format off diff --git a/include/Json.h b/include/Json.h old mode 100644 new mode 100755 index cd828ee..f3d4050 --- a/include/Json.h +++ b/include/Json.h @@ -1,12 +1,12 @@ -// Copyright (c) 2019-present Anonymous275. -// BeamMP Launcher code is not in the public domain and is not free software. -// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. -// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. -/// -/// Created by Anonymous275 on 11/27/2020 -/// -#pragma once -#include "rapidjson/stringbuffer.h" -#include "rapidjson/document.h" -#include "rapidjson/writer.h" +// Copyright (c) 2019-present Anonymous275. +// BeamMP Launcher code is not in the public domain and is not free software. +// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. +// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. +/// +/// Created by Anonymous275 on 11/27/2020 +/// +#pragma once +#include "rapidjson/stringbuffer.h" +#include "rapidjson/document.h" +#include "rapidjson/writer.h" namespace json = rapidjson; \ No newline at end of file diff --git a/include/Logger.h b/include/Logger.h old mode 100644 new mode 100755 diff --git a/include/Network/network.h b/include/Network/network.h old mode 100644 new mode 100755 index 84ffcab..617125e --- a/include/Network/network.h +++ b/include/Network/network.h @@ -5,6 +5,8 @@ /// /// Created by Anonymous275 on 7/18/2020 /// + + #pragma once #include void NetReset(); @@ -18,7 +20,7 @@ extern bool Terminate; extern int DEFAULT_PORT; extern uint64_t UDPSock; extern uint64_t TCPSock; -extern std::string Role; +extern std::string Branch; extern bool TCPTerminate; extern std::string LastIP; extern std::string MStatus; diff --git a/include/Security/Game.h b/include/Security/Game.h old mode 100644 new mode 100755 diff --git a/include/Security/Init.h b/include/Security/Init.h old mode 100644 new mode 100755 diff --git a/include/Startup.h b/include/Startup.h old mode 100644 new mode 100755 index 4b64e4f..24c63e2 --- a/include/Startup.h +++ b/include/Startup.h @@ -12,4 +12,5 @@ std::string GetEP(char*P = nullptr); std::string GetGamePath(); std::string GetVer(); std::string GetEN(); +void ConfigInit(); extern bool Dev; \ No newline at end of file diff --git a/include/Zlib/Compressor.h b/include/Zlib/Compressor.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/cursorstreamwrapper.h b/include/rapidjson/cursorstreamwrapper.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/encodedstream.h b/include/rapidjson/encodedstream.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/encodings.h b/include/rapidjson/encodings.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/error/en.h b/include/rapidjson/error/en.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/error/error.h b/include/rapidjson/error/error.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/filereadstream.h b/include/rapidjson/filereadstream.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/filewritestream.h b/include/rapidjson/filewritestream.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/fwd.h b/include/rapidjson/fwd.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/biginteger.h b/include/rapidjson/internal/biginteger.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/clzll.h b/include/rapidjson/internal/clzll.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/diyfp.h b/include/rapidjson/internal/diyfp.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/dtoa.h b/include/rapidjson/internal/dtoa.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/ieee754.h b/include/rapidjson/internal/ieee754.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/itoa.h b/include/rapidjson/internal/itoa.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/meta.h b/include/rapidjson/internal/meta.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/pow10.h b/include/rapidjson/internal/pow10.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/strfunc.h b/include/rapidjson/internal/strfunc.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/strtod.h b/include/rapidjson/internal/strtod.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/internal/swap.h b/include/rapidjson/internal/swap.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/istreamwrapper.h b/include/rapidjson/istreamwrapper.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/memorybuffer.h b/include/rapidjson/memorybuffer.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/memorystream.h b/include/rapidjson/memorystream.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/msinttypes/inttypes.h b/include/rapidjson/msinttypes/inttypes.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/msinttypes/stdint.h b/include/rapidjson/msinttypes/stdint.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/ostreamwrapper.h b/include/rapidjson/ostreamwrapper.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/stream.h b/include/rapidjson/stream.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/stringbuffer.h b/include/rapidjson/stringbuffer.h old mode 100644 new mode 100755 diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h old mode 100644 new mode 100755 diff --git a/src/Compressor.cpp b/src/Compressor.cpp old mode 100644 new mode 100755 diff --git a/src/Config.cpp b/src/Config.cpp new file mode 100755 index 0000000..e1db598 --- /dev/null +++ b/src/Config.cpp @@ -0,0 +1,60 @@ +/// +/// Created by Anonymous275 on 2/23/2021 +/// + +#include "Network/network.h" +#include +#include "Logger.h" +#include +#include "Json.h" +#include +namespace fs = std::filesystem; + +std::string Branch; +void ParseConfig(const json::Document& d){ + if(d["Port"].IsInt()){ + DEFAULT_PORT = d["Port"].GetInt(); + } + + //Default -1 + //Release 1 + //EA 2 + //Dev 3 + //Custom 3 + + if(d["Build"].IsString()){ + Branch = d["Build"].GetString(); + for(char& c : Branch)c = char(tolower(c)); + } +} + +void ConfigInit(){ + if(fs::exists("Launcher.cfg")){ + std::ifstream cfg("Launcher.cfg"); + if(cfg.is_open()){ + auto Size = fs::file_size("Launcher.cfg"); + std::string Buffer(Size, 0); + cfg.read(&Buffer[0], Size); + cfg.close(); + json::Document d; + d.Parse(Buffer.c_str()); + if(d.HasParseError()){ + fatal("Config failed to parse make sure it's valid JSON! Code : " + std::to_string(d.GetParseError())); + } + ParseConfig(d); + }else fatal("Failed to open Launcher.cfg!"); + }else{ + std::ofstream cfg("Launcher.cfg"); + if(cfg.is_open()){ + cfg << + R"({ +"Port": 4444, +"Build": "Default" +})"; + cfg.close(); + }else{ + fatal("Failed to write config on disk!"); + } + } +} + diff --git a/src/Discord.cpp b/src/Discord.cpp old mode 100644 new mode 100755 index b254c64..c050d4e --- a/src/Discord.cpp +++ b/src/Discord.cpp @@ -7,7 +7,7 @@ /// #include "Discord/discord_rpc.h" #include "Logger.h" -#include +#include #include #include diff --git a/src/GameStart.cpp b/src/GameStart.cpp old mode 100644 new mode 100755 index 5e37be7..11bb8dc --- a/src/GameStart.cpp +++ b/src/GameStart.cpp @@ -6,7 +6,7 @@ /// Created by Anonymous275 on 7/19/2020 /// -#include +#include #include "Startup.h" #include "Logger.h" #include diff --git a/src/Logger.cpp b/src/Logger.cpp old mode 100644 new mode 100755 diff --git a/src/Network/Core.cpp b/src/Network/Core.cpp old mode 100644 new mode 100755 index 6175aec..ba55b90 --- a/src/Network/Core.cpp +++ b/src/Network/Core.cpp @@ -9,8 +9,8 @@ #include "Security/Init.h" #include "Curl/http.h" -#include -#include +#include +#include #include "Startup.h" #include "Logger.h" #include @@ -242,9 +242,13 @@ int Handle(EXCEPTION_POINTERS *ep){ void CoreNetwork(){ while(TraceBack >= 4){ +#ifndef __MINGW32__ __try{ +#endif CoreMain(); +#ifndef __MINGW32__ }__except(Handle(GetExceptionInformation())){} +#endif std::this_thread::sleep_for(std::chrono::seconds(1)); } } diff --git a/src/Network/DNS.cpp b/src/Network/DNS.cpp old mode 100644 new mode 100755 diff --git a/src/Network/GlobalHandler.cpp b/src/Network/GlobalHandler.cpp old mode 100644 new mode 100755 index d0f60c1..2e3c405 --- a/src/Network/GlobalHandler.cpp +++ b/src/Network/GlobalHandler.cpp @@ -6,15 +6,15 @@ /// Created by Anonymous275 on 7/25/2020 /// #include "Network/network.h" -#include -#include +#include +#include #include "Logger.h" #include #include #include #include -std::chrono::time_point PingStart,PingEnd; +std::chrono::time_point PingStart,PingEnd; bool GConnected = false; bool CServer = true; SOCKET CSocket = -1; diff --git a/src/Network/Http.cpp b/src/Network/Http.cpp old mode 100644 new mode 100755 index 42f6ad6..b23108c --- a/src/Network/Http.cpp +++ b/src/Network/Http.cpp @@ -1,140 +1,144 @@ -// Copyright (c) 2019-present Anonymous275. -// BeamMP Launcher code is not in the public domain and is not free software. -// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. -// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. -/// -/// Created by Anonymous275 on 7/18/2020 -/// - -#include -#include -#include -#include - -class CurlManager{ -public: - CurlManager(){ - curl = curl_easy_init(); - } - ~CurlManager(){ - curl_easy_cleanup(curl); - } - inline CURL* Get(){ - return curl; - } -private: - CURL *curl; -}; - -static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){ - ((std::string*)userp)->append((char*)contents, size * nmemb); - return size * nmemb; -} - -std::string HTTP_REQUEST(const std::string& IP,int port){ - static thread_local CurlManager M; - static std::mutex Lock; - std::scoped_lock Guard(Lock); - CURL *curl = M.Get(); - CURLcode res; - std::string readBuffer; - if(curl) { - curl_easy_setopt(curl, CURLOPT_URL, IP.c_str()); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); - curl_easy_setopt(curl, CURLOPT_PORT, port); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); - curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); - res = curl_easy_perform(curl); - if(res != CURLE_OK)return "-1"; - } - return readBuffer; -} - -int nb_bar; -double last_progress, progress_bar_adv; -bool Downloaded; -int progress_bar (void *bar, double t, double d){ - if(t > 0 && last_progress != round(d/t*100)){ - nb_bar = 25; - progress_bar_adv = round(d/t*nb_bar); - std::cout<<"\r"; - std::cout<< "Progress : [ "; - std::cout<stream) { - fopen_s(&out->stream,out->filename,"wb"); - if(!out->stream)return -1; - } - return fwrite(buffer, size, nmemb, out->stream); -} -int Download(const std::string& URL,const std::string& Path,bool close){ - static thread_local CurlManager M; - CURL *curl = M.Get(); - CURLcode res; - struct File file = {Path.c_str(),nullptr}; - Downloaded = false; - if(curl){ - curl_easy_setopt(curl, CURLOPT_URL,URL.c_str()); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &file); - curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); - curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_bar); - curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); - res = curl_easy_perform(curl); - if(res != CURLE_OK){ - return res; - } - } - if(file.stream)fclose(file.stream); - if(!Downloaded){ - remove(Path.c_str()); - fatal("Failed to download please try again later!"); - } - std::cout << std::endl; - return -1; -} -std::string PostHTTP(const std::string& IP, const std::string& Fields) { - static auto *header = new curl_slist{(char*)"Content-Type: application/json"}; - static thread_local CurlManager M; - static std::mutex Lock; - std::scoped_lock Guard(Lock); - CURL* curl = M.Get(); - CURLcode res; - std::string readBuffer; - - if (curl) { - curl_easy_setopt(curl, CURLOPT_URL, IP.c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); - curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size()); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); - curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); - curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); - res = curl_easy_perform(curl); - if (res != CURLE_OK) - return "-1"; - } - return readBuffer; +// Copyright (c) 2019-present Anonymous275. +// BeamMP Launcher code is not in the public domain and is not free software. +// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. +// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. +/// +/// Created by Anonymous275 on 7/18/2020 +/// + +#include +#include +#include +#include +#include + +class CurlManager{ +public: + CurlManager(){ + curl = curl_easy_init(); + } + ~CurlManager(){ + curl_easy_cleanup(curl); + } + inline CURL* Get(){ + return curl; + } +private: + CURL *curl; +}; + +static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){ + ((std::string*)userp)->append((char*)contents, size * nmemb); + return size * nmemb; +} + +std::string HTTP_REQUEST(const std::string& IP,int port){ + static thread_local CurlManager M; + static std::mutex Lock; + std::scoped_lock Guard(Lock); + CURL *curl = M.Get(); + CURLcode res; + std::string readBuffer; + if(curl) { + curl_easy_setopt(curl, CURLOPT_URL, IP.c_str()); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); + curl_easy_setopt(curl, CURLOPT_PORT, port); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); + res = curl_easy_perform(curl); + if(res != CURLE_OK)return "-1"; + } + return readBuffer; +} + +int nb_bar; +double last_progress, progress_bar_adv; +bool Downloaded; +int progress_bar (void *bar, double t, double d){ + if(t > 0 && last_progress != round(d/t*100)){ + nb_bar = 25; + progress_bar_adv = round(d/t*nb_bar); + std::cout<<"\r"; + std::cout<< "Progress : [ "; + std::cout<stream) { + fopen_s(&out->stream,out->filename,"wb"); + if(!out->stream)return -1; + } + return fwrite(buffer, size, nmemb, out->stream); +} +int Download(const std::string& URL,const std::string& Path,bool close){ + static thread_local CurlManager M; + CURL *curl = M.Get(); + CURLcode res; + struct File file = {Path.c_str(),nullptr}; + Downloaded = false; + if(curl){ + curl_easy_setopt(curl, CURLOPT_URL,URL.c_str()); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &file); + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE); + curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_bar); + curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL); + res = curl_easy_perform(curl); + if(res != CURLE_OK){ + return res; + } + } + if(file.stream)fclose(file.stream); + if(!Downloaded){ + remove(Path.c_str()); + fatal("Failed to download please try again later!"); + } + std::cout << std::endl; + return -1; +} +std::string PostHTTP(const std::string& IP, const std::string& Fields) { + static auto *header = new curl_slist{(char*)"Content-Type: application/json"}; + static thread_local CurlManager M; + static std::mutex Lock; + std::scoped_lock Guard(Lock); + CURL* curl = M.Get(); + CURLcode res; + std::string readBuffer; + + + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, IP.c_str()); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header); + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size()); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); + //curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); + + res = curl_easy_perform(curl); + + if (res != CURLE_OK) + return "-1"; + } + return readBuffer; } \ No newline at end of file diff --git a/src/Network/Resources.cpp b/src/Network/Resources.cpp old mode 100644 new mode 100755 index 32ee0b0..dd9da8e --- a/src/Network/Resources.cpp +++ b/src/Network/Resources.cpp @@ -7,8 +7,7 @@ /// #include "Network/network.h" - -#include +#include #include #include "Startup.h" #include "Logger.h" @@ -20,6 +19,7 @@ #include #include #include +#include namespace fs = std::filesystem; std::string ListOfMods; @@ -37,8 +37,7 @@ std::vector Split(const std::string& String,const std::string& deli } void CheckForDir(){ - struct stat info{}; - if(stat( "Resources", &info) != 0){ + if(!fs::exists("Resources")){ _wmkdir(L"Resources"); } } diff --git a/src/Network/VehicleData.cpp b/src/Network/VehicleData.cpp old mode 100644 new mode 100755 index 2e17b20..d1c399d --- a/src/Network/VehicleData.cpp +++ b/src/Network/VehicleData.cpp @@ -8,7 +8,7 @@ #include "Zlib/Compressor.h" #include "Network/network.h" -#include +#include #include "Logger.h" #include #include diff --git a/src/Network/VehicleEvent.cpp b/src/Network/VehicleEvent.cpp old mode 100644 new mode 100755 index 650ab52..5560602 --- a/src/Network/VehicleEvent.cpp +++ b/src/Network/VehicleEvent.cpp @@ -10,7 +10,7 @@ #include #include "Logger.h" #include -#include +#include #include #include "Network/network.h" @@ -133,7 +133,7 @@ void TCPClientMain(const std::string& IP,int Port){ RetCode = connect(TCPSock, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr)); if(RetCode != 0){ UlStatus = "UlConnection Failed!"; - std::cout << "Client: connect failed! Error code: " << WSAGetLastError() << std::endl; + error("Client: connect failed! Error code: " + std::to_string(WSAGetLastError())); KillSocket(TCPSock); WSACleanup(); Terminate = true; diff --git a/src/Security/BeamNG.cpp b/src/Security/BeamNG.cpp old mode 100644 new mode 100755 index a143f37..52df157 --- a/src/Security/BeamNG.cpp +++ b/src/Security/BeamNG.cpp @@ -1,318 +1,317 @@ -// Copyright (c) 2019-present Anonymous275. -// BeamMP Launcher code is not in the public domain and is not free software. -// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. -// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. -/// -/// Created by Anonymous275 on 7/18/2020 -/// - -#include -#include -#include "Logger.h" -#include -#include -#include -#include -#include - -#define MAX_KEY_LENGTH 255 -#define MAX_VALUE_NAME 16383 - -int TraceBack = 0; -std::string GameDir; - -void lowExit(int code){ - TraceBack = 0; - std::string msg = - "Failed to find the game please launch it. Report this if the issue persists code "; - error(msg+std::to_string(code)); - std::this_thread::sleep_for(std::chrono::seconds(10)); - exit(2); -} -void Exit(int code){ - TraceBack = 0; - std::string msg = - "Sorry. We do not support cracked copies report this if you believe this is a mistake code "; - error(msg+std::to_string(code)); - std::this_thread::sleep_for(std::chrono::seconds(10)); - exit(3); -} -void SteamExit(int code){ - TraceBack = 0; - std::string msg = - "Illegal steam modifications detected report this if you believe this is a mistake code "; - error(msg+std::to_string(code)); - std::this_thread::sleep_for(std::chrono::seconds(10)); - exit(4); -} -std::string GetGameDir(){ - if(TraceBack != 4)Exit(0); - return GameDir.substr(0,GameDir.find_last_of('\\')); -} -LONG OpenKey(HKEY root,const char* path,PHKEY hKey){ - return RegOpenKeyEx(root, reinterpret_cast(path), 0, KEY_READ, hKey); -} -std::string QueryKey(HKEY hKey,int ID){ - TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name - DWORD cbName; // size of name string - TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name - DWORD cchClassName = MAX_PATH; // size of class string - DWORD cSubKeys=0; // number of subkeys - DWORD cbMaxSubKey; // longest subkey size - DWORD cchMaxClass; // longest class string - DWORD cValues; // number of values for key - DWORD cchMaxValue; // longest value name - DWORD cbMaxValueData; // longest value data - DWORD cbSecurityDescriptor; // size of security descriptor - FILETIME ftLastWriteTime; // last write time - - DWORD i, retCode; - - TCHAR achValue[MAX_VALUE_NAME]; - DWORD cchValue = MAX_VALUE_NAME; - - retCode = RegQueryInfoKey( - hKey, // key handle - achClass, // buffer for class name - &cchClassName, // size of class string - nullptr, // reserved - &cSubKeys, // number of subkeys - &cbMaxSubKey, // longest subkey size - &cchMaxClass, // longest class string - &cValues, // number of values for this key - &cchMaxValue, // longest value name - &cbMaxValueData, // longest value data - &cbSecurityDescriptor, // security descriptor - &ftLastWriteTime); // last write time - - BYTE* buffer = new BYTE[cbMaxValueData]; - ZeroMemory(buffer, cbMaxValueData); - if (cSubKeys){ - for (i=0; i&a,const std::string& Path){ - for (const auto &entry : fs::directory_iterator(Path)) { - const auto& DPath = entry.path(); - if (!entry.is_directory()) { - a.emplace_back(DPath.u8string()); - }else if(NameValid(DPath.filename().u8string())){ - FileList(a, DPath.u8string()); - } - } -} -bool Find(const std::string& FName,const std::string& Path){ - std::vector FS; - FileList(FS,Path+"\\userdata"); - for(std::string&a : FS){ - if(a.find(FName) != std::string::npos){ - FS.clear(); - return true; - } - } - FS.clear(); - return false; -} -bool FindHack(const std::string& Path){ - bool s = true; - for (const auto &entry : fs::directory_iterator(Path)) { - std::string Name = entry.path().filename().u8string(); - for(char&c : Name)c = char(tolower(c)); - if(Name == "steam.exe")s = false; - if(Name.find("greenluma") != -1){ - error("Found malicious file/folder \"" + Name+"\""); - return true; - } - Name.clear(); - } - return s; -} -std::vector GetID(const std::string& log){ - std::string vec,t,r; - std::vector Ret; - std::ifstream f(log.c_str(), std::ios::binary); - f.seekg(0, std::ios_base::end); - std::streampos fileSize = f.tellg(); - vec.resize(size_t(fileSize) + 1); - f.seekg(0, std::ios_base::beg); - f.read(&vec[0], fileSize); - f.close(); - std::stringstream ss(vec); - bool S = false; - while (std::getline(ss, t, '{')) { - if(!S)S = true; - else{ - for(char& c : t){ - if(isdigit(c))r += c; - } - break; - } - } - Ret.emplace_back(r); - r.clear(); - S = false; - bool L = true; - while (std::getline(ss, t, '}')) { - if(L){ - L = false; - continue; - } - for(char& c : t){ - if(c == '"'){ - if(!S)S = true; - else{ - if(r.length() > 10) { - Ret.emplace_back(r); - } - r.clear(); - S = false; - continue; - } - } - if(isdigit(c))r += c; - } - } - vec.clear(); - return Ret; -} -std::string GetManifest(const std::string& Man){ - std::string vec; - std::ifstream f(Man.c_str(), std::ios::binary); - f.seekg(0, std::ios_base::end); - std::streampos fileSize = f.tellg(); - vec.resize(size_t(fileSize) + 1); - f.seekg(0, std::ios_base::beg); - f.read(&vec[0], fileSize); - f.close(); - std::string ToFind = "\"LastOwner\"\t\t\""; - int pos = int(vec.find(ToFind)); - if(pos != -1){ - pos += int(ToFind.length()); - vec = vec.substr(pos); - return vec.substr(0,vec.find('\"')); - }else return ""; -} -bool IDCheck(std::string Man, std::string steam){ - bool a = false,b = true; - int pos = int(Man.rfind("steamapps")); - if(pos == -1)Exit(5); - Man = Man.substr(0,pos+9) + "\\appmanifest_284160.acf"; - steam += "\\config\\loginusers.vdf"; - if(fs::exists(Man) && fs::exists(steam)){ - for(const std::string&ID : GetID(steam)){ - if(ID == GetManifest(Man))b = false; - } - if(b)Exit(6); - }else a = true; - return a; -} -void LegitimacyCheck(){ - std::string Result,T; - std::string K1 = R"(Software\Valve\Steam)"; - std::string K2 = R"(Software\Valve\Steam\Apps\284160)"; - std::string K3 = R"(Software\BeamNG\BeamNG.drive)"; - HKEY hKey; - - LONG dwRegOPenKey = OpenKey(HKEY_CURRENT_USER, K1.c_str(), &hKey); - - if(dwRegOPenKey == ERROR_SUCCESS) { - Result = QueryKey(hKey, 1); - if(Result.empty())Exit(1); - - if(fs::exists(Result)){ - if(!Find("284160.json",Result))Exit(2); - if(FindHack(Result))SteamExit(1); - }else Exit(3); - - T = Result; - Result.clear(); - TraceBack++; - }else Exit(4); - - K1.clear(); - RegCloseKey(hKey); - dwRegOPenKey = OpenKey(HKEY_CURRENT_USER, K2.c_str(), &hKey); - if(dwRegOPenKey == ERROR_SUCCESS) { - Result = QueryKey(hKey, 2); - if(Result.empty())lowExit(1); - TraceBack++; - }else lowExit(2); - K2.clear(); - RegCloseKey(hKey); - dwRegOPenKey = OpenKey(HKEY_CURRENT_USER, K3.c_str(), &hKey); - if(dwRegOPenKey == ERROR_SUCCESS) { - Result = QueryKey(hKey, 3); - if(Result.empty())lowExit(3); - if(IDCheck(Result,T))lowExit(5); - GameDir = Result; - TraceBack++; - }else lowExit(4); - K3.clear(); - Result.clear(); - RegCloseKey(hKey); - if(TraceBack < 3)exit(-1); -} -std::string CheckVer(const std::string &dir){ - std::string temp,Path = dir + "\\integrity.json"; - std::ifstream f(Path.c_str(), std::ios::binary); - int Size = int(std::filesystem::file_size(Path)); - std::string vec(Size,0); - f.read(&vec[0], Size); - f.close(); - - 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; -} +// Copyright (c) 2019-present Anonymous275. +// BeamMP Launcher code is not in the public domain and is not free software. +// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. +// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. +/// +/// Created by Anonymous275 on 7/18/2020 +/// + +#include +#include +#include "Logger.h" +#include +#include +#include +#include + +#define MAX_KEY_LENGTH 255 +#define MAX_VALUE_NAME 16383 + +int TraceBack = 0; +std::string GameDir; + +void lowExit(int code){ + TraceBack = 0; + std::string msg = + "Failed to find the game please launch it. Report this if the issue persists code "; + error(msg+std::to_string(code)); + std::this_thread::sleep_for(std::chrono::seconds(10)); + exit(2); +} +void Exit(int code){ + TraceBack = 0; + std::string msg = + "Sorry. We do not support cracked copies report this if you believe this is a mistake code "; + error(msg+std::to_string(code)); + std::this_thread::sleep_for(std::chrono::seconds(10)); + exit(3); +} +void SteamExit(int code){ + TraceBack = 0; + std::string msg = + "Illegal steam modifications detected report this if you believe this is a mistake code "; + error(msg+std::to_string(code)); + std::this_thread::sleep_for(std::chrono::seconds(10)); + exit(4); +} +std::string GetGameDir(){ + if(TraceBack != 4)Exit(0); + return GameDir.substr(0,GameDir.find_last_of('\\')); +} +LONG OpenKey(HKEY root,const char* path,PHKEY hKey){ + return RegOpenKeyEx(root, reinterpret_cast(path), 0, KEY_READ, hKey); +} +std::string QueryKey(HKEY hKey,int ID){ + TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name + DWORD cbName; // size of name string + TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name + DWORD cchClassName = MAX_PATH; // size of class string + DWORD cSubKeys=0; // number of subkeys + DWORD cbMaxSubKey; // longest subkey size + DWORD cchMaxClass; // longest class string + DWORD cValues; // number of values for key + DWORD cchMaxValue; // longest value name + DWORD cbMaxValueData; // longest value data + DWORD cbSecurityDescriptor; // size of security descriptor + FILETIME ftLastWriteTime; // last write time + + DWORD i, retCode; + + TCHAR achValue[MAX_VALUE_NAME]; + DWORD cchValue = MAX_VALUE_NAME; + + retCode = RegQueryInfoKey( + hKey, // key handle + achClass, // buffer for class name + &cchClassName, // size of class string + nullptr, // reserved + &cSubKeys, // number of subkeys + &cbMaxSubKey, // longest subkey size + &cchMaxClass, // longest class string + &cValues, // number of values for this key + &cchMaxValue, // longest value name + &cbMaxValueData, // longest value data + &cbSecurityDescriptor, // security descriptor + &ftLastWriteTime); // last write time + + BYTE* buffer = new BYTE[cbMaxValueData]; + ZeroMemory(buffer, cbMaxValueData); + if (cSubKeys){ + for (i=0; i&a,const std::string& Path){ + for (const auto &entry : fs::directory_iterator(Path)) { + const auto& DPath = entry.path(); + if (!entry.is_directory()) { + a.emplace_back(DPath.u8string()); + }else if(NameValid(DPath.filename().u8string())){ + FileList(a, DPath.u8string()); + } + } +} +bool Find(const std::string& FName,const std::string& Path){ + std::vector FS; + FileList(FS,Path+"\\userdata"); + for(std::string&a : FS){ + if(a.find(FName) != std::string::npos){ + FS.clear(); + return true; + } + } + FS.clear(); + return false; +} +bool FindHack(const std::string& Path){ + bool s = true; + for (const auto &entry : fs::directory_iterator(Path)) { + std::string Name = entry.path().filename().u8string(); + for(char&c : Name)c = char(tolower(c)); + if(Name == "steam.exe")s = false; + if(Name.find("greenluma") != -1){ + error("Found malicious file/folder \"" + Name+"\""); + return true; + } + Name.clear(); + } + return s; +} +std::vector GetID(const std::string& log){ + std::string vec,t,r; + std::vector Ret; + std::ifstream f(log.c_str(), std::ios::binary); + f.seekg(0, std::ios_base::end); + std::streampos fileSize = f.tellg(); + vec.resize(size_t(fileSize) + 1); + f.seekg(0, std::ios_base::beg); + f.read(&vec[0], fileSize); + f.close(); + std::stringstream ss(vec); + bool S = false; + while (std::getline(ss, t, '{')) { + if(!S)S = true; + else{ + for(char& c : t){ + if(isdigit(c))r += c; + } + break; + } + } + Ret.emplace_back(r); + r.clear(); + S = false; + bool L = true; + while (std::getline(ss, t, '}')) { + if(L){ + L = false; + continue; + } + for(char& c : t){ + if(c == '"'){ + if(!S)S = true; + else{ + if(r.length() > 10) { + Ret.emplace_back(r); + } + r.clear(); + S = false; + continue; + } + } + if(isdigit(c))r += c; + } + } + vec.clear(); + return Ret; +} +std::string GetManifest(const std::string& Man){ + std::string vec; + std::ifstream f(Man.c_str(), std::ios::binary); + f.seekg(0, std::ios_base::end); + std::streampos fileSize = f.tellg(); + vec.resize(size_t(fileSize) + 1); + f.seekg(0, std::ios_base::beg); + f.read(&vec[0], fileSize); + f.close(); + std::string ToFind = "\"LastOwner\"\t\t\""; + int pos = int(vec.find(ToFind)); + if(pos != -1){ + pos += int(ToFind.length()); + vec = vec.substr(pos); + return vec.substr(0,vec.find('\"')); + }else return ""; +} +bool IDCheck(std::string Man, std::string steam){ + bool a = false,b = true; + int pos = int(Man.rfind("steamapps")); + if(pos == -1)Exit(5); + Man = Man.substr(0,pos+9) + "\\appmanifest_284160.acf"; + steam += "\\config\\loginusers.vdf"; + if(fs::exists(Man) && fs::exists(steam)){ + for(const std::string&ID : GetID(steam)){ + if(ID == GetManifest(Man))b = false; + } + if(b)Exit(6); + }else a = true; + return a; +} +void LegitimacyCheck(){ + std::string Result,T; + std::string K1 = R"(Software\Valve\Steam)"; + std::string K2 = R"(Software\Valve\Steam\Apps\284160)"; + std::string K3 = R"(Software\BeamNG\BeamNG.drive)"; + HKEY hKey; + + LONG dwRegOPenKey = OpenKey(HKEY_CURRENT_USER, K1.c_str(), &hKey); + + if(dwRegOPenKey == ERROR_SUCCESS) { + Result = QueryKey(hKey, 1); + if(Result.empty())Exit(1); + + if(fs::exists(Result)){ + if(!Find("284160.json",Result))Exit(2); + if(FindHack(Result))SteamExit(1); + }else Exit(3); + + T = Result; + Result.clear(); + TraceBack++; + }else Exit(4); + + K1.clear(); + RegCloseKey(hKey); + dwRegOPenKey = OpenKey(HKEY_CURRENT_USER, K2.c_str(), &hKey); + if(dwRegOPenKey == ERROR_SUCCESS) { + Result = QueryKey(hKey, 2); + if(Result.empty())lowExit(1); + TraceBack++; + }else lowExit(2); + K2.clear(); + RegCloseKey(hKey); + dwRegOPenKey = OpenKey(HKEY_CURRENT_USER, K3.c_str(), &hKey); + if(dwRegOPenKey == ERROR_SUCCESS) { + Result = QueryKey(hKey, 3); + if(Result.empty())lowExit(3); + if(IDCheck(Result,T))lowExit(5); + GameDir = Result; + TraceBack++; + }else lowExit(4); + K3.clear(); + Result.clear(); + RegCloseKey(hKey); + if(TraceBack < 3)exit(-1); +} +std::string CheckVer(const std::string &dir){ + std::string temp,Path = dir + "\\integrity.json"; + std::ifstream f(Path.c_str(), std::ios::binary); + int Size = int(std::filesystem::file_size(Path)); + std::string vec(Size,0); + f.read(&vec[0], Size); + f.close(); + + 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; +} diff --git a/src/Security/Login.cpp b/src/Security/Login.cpp old mode 100644 new mode 100755 index 343f910..b3ae762 --- a/src/Security/Login.cpp +++ b/src/Security/Login.cpp @@ -1,109 +1,114 @@ -// Copyright (c) 2019-present Anonymous275. -// BeamMP Launcher code is not in the public domain and is not free software. -// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. -// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. -/// -/// Created by Anonymous275 on 11/26/2020 -/// - -#include "Curl/http.h" -#include -#include "Logger.h" -#include -#include "Json.h" - -using namespace std::filesystem; -std::string PublicKey; -extern bool LoginAuth; -std::string Role; - -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"); - } -} - -/// "username":"value","password":"value" -/// "Guest":"Name" -/// "pk":"private_key" - -std::string GetFail(const std::string& R){ - std::string DRet = R"({"success":false,"message":)"; - DRet += "\""+R+"\"}"; - error(R); - return DRet; -} - -std::string Login(const std::string& fields){ - if(fields == "LO"){ - LoginAuth = false; - UpdateKey(nullptr); - return ""; - } - info("Attempting to authenticate..."); - std::string Buffer = PostHTTP("https://auth.beammp.com/userlogin", fields); - json::Document d; - d.Parse(Buffer.c_str()); - if(Buffer == "-1"){ - return GetFail("Failed to communicate with the auth system!"); - } - - if (Buffer.at(0) != '{' || d.HasParseError()) { - return GetFail("Invalid answer from authentication servers, please try again later!"); - } - if(!d["success"].IsNull() && d["success"].GetBool()){ - LoginAuth = true; - if(!d["private_key"].IsNull()){ - UpdateKey(d["private_key"].GetString()); - } - if(!d["public_key"].IsNull()){ - PublicKey = d["public_key"].GetString(); - } - info("Authentication successful!"); - }else info("Authentication failed!"); - if(!d["message"].IsNull()){ - d.RemoveMember("private_key"); - d.RemoveMember("public_key"); - rapidjson::StringBuffer buffer; - rapidjson::Writer writer(buffer); - d.Accept(writer); - return buffer.GetString(); - } - return GetFail("Invalid message parsing!"); -} - -void CheckLocalKey(){ - 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(); - Buffer = PostHTTP("https://auth.beammp.com/userlogin", R"({"pk":")"+Buffer+"\"}"); - json::Document d; - d.Parse(Buffer.c_str()); - if (Buffer == "-1" || Buffer.at(0) != '{' || d.HasParseError()) { - fatal("Invalid answer from authentication servers, please try again later!"); - } - if(d["success"].GetBool()){ - LoginAuth = true; - UpdateKey(d["private_key"].GetString()); - PublicKey = d["public_key"].GetString(); - Role = d["role"].GetString(); - }else{ - info("Auto-Authentication unsuccessful please re-login!"); - UpdateKey(nullptr); - } - }else{ - warn("Could not open saved key!"); - UpdateKey(nullptr); - } - }else UpdateKey(nullptr); -} +// Copyright (c) 2019-present Anonymous275. +// BeamMP Launcher code is not in the public domain and is not free software. +// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. +// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. +/// +/// Created by Anonymous275 on 11/26/2020 +/// + +#include "Curl/http.h" +#include +#include "Logger.h" +#include +#include "Json.h" + +namespace fs = std::filesystem; +std::string PublicKey; +extern bool LoginAuth; +std::string Role; + +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(fs::exists("key")){ + remove("key"); + } +} + +/// "username":"value","password":"value" +/// "Guest":"Name" +/// "pk":"private_key" + +std::string GetFail(const std::string& R){ + std::string DRet = R"({"success":false,"message":)"; + DRet += "\""+R+"\"}"; + error(R); + return DRet; +} + +std::string Login(const std::string& fields){ + if(fields == "LO"){ + LoginAuth = false; + UpdateKey(nullptr); + return ""; + } + info("Attempting to authenticate..."); + std::string Buffer = PostHTTP("https://auth.beammp.com/userlogin", fields); + json::Document d; + d.Parse(Buffer.c_str()); + if(Buffer == "-1"){ + return GetFail("Failed to communicate with the auth system!"); + } + + if (Buffer.at(0) != '{' || d.HasParseError()) { + error(Buffer); + return GetFail("Invalid answer from authentication servers, please try again later!"); + } + if(!d["success"].IsNull() && d["success"].GetBool()){ + LoginAuth = true; + if(!d["private_key"].IsNull()){ + UpdateKey(d["private_key"].GetString()); + } + if(!d["public_key"].IsNull()){ + PublicKey = d["public_key"].GetString(); + } + info("Authentication successful!"); + }else info("Authentication failed!"); + if(!d["message"].IsNull()){ + d.RemoveMember("private_key"); + d.RemoveMember("public_key"); + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + d.Accept(writer); + return buffer.GetString(); + } + return GetFail("Invalid message parsing!"); +} + +void CheckLocalKey(){ + if(fs::exists("key") && fs::file_size("key") < 100){ + std::ifstream Key("key"); + if(Key.is_open()) { + auto Size = fs::file_size("key"); + std::string Buffer(Size, 0); + Key.read(&Buffer[0], Size); + Key.close(); + + Buffer = PostHTTP("https://auth.beammp.com/userlogin", R"({"pk":")"+Buffer+"\"}"); + + json::Document d; + d.Parse(Buffer.c_str()); + if (Buffer == "-1" || Buffer.at(0) != '{' || d.HasParseError()) { + error(Buffer); + fatal("Invalid answer from authentication servers, please try again later!"); + } + if(d["success"].GetBool()){ + LoginAuth = true; + UpdateKey(d["private_key"].GetString()); + PublicKey = d["public_key"].GetString(); + Role = d["role"].GetString(); + //info(Role); + }else{ + info("Auto-Authentication unsuccessful please re-login!"); + UpdateKey(nullptr); + } + }else{ + warn("Could not open saved key!"); + UpdateKey(nullptr); + } + }else UpdateKey(nullptr); +} diff --git a/src/Startup.cpp b/src/Startup.cpp old mode 100644 new mode 100755 index e9c4faa..d23f2b7 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -1,208 +1,191 @@ -// Copyright (c) 2019-present Anonymous275. -// BeamMP Launcher code is not in the public domain and is not free software. -// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. -// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. -/// -/// Created by Anonymous275 on 7/16/2020 -/// -#include "Discord/discord_info.h" -#include "Network/network.h" -#include "Security/Init.h" -#include "Curl/http.h" -#include -#include -#include "Startup.h" -#include "Logger.h" -#include - -extern int TraceBack; -bool Dev = false; -namespace fs = std::filesystem; - -std::string GetEN(){ - return "BeamMP-Launcher.exe"; -} -std::string GetVer(){ - return "1.81"; -} -std::string GetPatch(){ - return ".0"; -} -std::string GetEP(char*P){ - static std::string Ret = [&](){ - std::string path(P); - return path.substr(0, path.find_last_of("\\/") + 1); - } (); - return Ret; -} -void ReLaunch(int argc,char*args[]){ - std::string Arg; - for(int c = 2; c <= argc; c++){ - Arg += " "; - Arg += args[c-1]; - } - system("cls"); - ShellExecute(nullptr,"runas",(GetEP() + GetEN()).c_str(),Arg.c_str(),nullptr,SW_SHOWNORMAL); - ShowWindow(GetConsoleWindow(),0); - std::this_thread::sleep_for(std::chrono::seconds(1)); - exit(1); -} -void URelaunch(int argc,char* args[]){ - std::string Arg; - for(int c = 2; c <= argc; c++){ - Arg += " "; - Arg += args[c-1]; - } - ShellExecute(nullptr,"open",(GetEP() + GetEN()).c_str(),Arg.c_str(),nullptr,SW_SHOWNORMAL); - ShowWindow(GetConsoleWindow(),0); - std::this_thread::sleep_for(std::chrono::seconds(1)); - exit(1); -} -void CheckName(int argc,char* args[]){ - struct stat info{}; - std::string DN = GetEN(),CDir = args[0],FN = CDir.substr(CDir.find_last_of('\\')+1); - if(FN != DN){ - if(stat(DN.c_str(),&info)==0)remove(DN.c_str()); - if(stat(DN.c_str(),&info)==0)ReLaunch(argc,args); - std::rename(FN.c_str(), DN.c_str()); - URelaunch(argc,args); - } -} - -/// Deprecated -void RequestRole(){ - /*auto NPos = std::string::npos; - std::string HTTP_Result = HTTP_REQUEST("https://beammp.com/entitlement?did="+GetDID()+"&t=l",443); - if(HTTP_Result == "-1"){ - HTTP_Result = HTTP_REQUEST("https://backup1.beammp.com/entitlement?did="+GetDID()+"&t=l",443); - if(HTTP_Result == "-1") { - fatal("Sorry Backend System Outage! Don't worry it will back on soon!"); - } - } - if(HTTP_Result.find("\"MDEV\"") != NPos || HTTP_Result.find("\"CON\"") != NPos){ - Dev = true; - } - if(HTTP_Result.find("Error") != NPos){ - fatal("Sorry You need to be in the official BeamMP Discord to proceed! https://discord.gg/beammp"); - } - info("Client Connected!");*/ -} - -void CheckForUpdates(int argc,char*args[],const std::string& CV){ - std::string link = "https://beammp.com/builds/launcher?version=true"; - std::string HTTP = HTTP_REQUEST(link,443); - bool fallback = false; - if(HTTP.find_first_of("0123456789") == std::string::npos){ - link = "https://backup1.beammp.com/builds/launcher?version=true"; - HTTP = HTTP_REQUEST(link,443); - fallback = true; - if(HTTP.find_first_of("0123456789") == std::string::npos) { - fatal("Primary Servers Offline! sorry for the inconvenience!"); - } - } - if(fallback){ - link = "https://backup1.beammp.com/builds/launcher?download=true"; - }else link = "https://beammp.com/builds/launcher?download=true"; - - std::string EP(GetEP() + GetEN()), Back(GetEP() + "BeamMP-Launcher.back"); - - if(fs::exists(Back))remove(Back.c_str()); - - if(HTTP > CV){ - system("cls"); - info("Update found!"); - info("Updating..."); - if(std::rename(EP.c_str(), Back.c_str()))error("failed creating a backup!"); - int i = Download(link, EP,true); - if(i != -1){ - error("Launcher Update failed! trying again... code : " + std::to_string(i)); - std::this_thread::sleep_for(std::chrono::seconds(2)); - int i2 = Download(link, EP,true); - if(i2 != -1){ - error("Launcher Update failed! code : " + std::to_string(i2)); - std::this_thread::sleep_for(std::chrono::seconds(5)); - ReLaunch(argc,args); - } - } - URelaunch(argc,args); - }else info("Launcher version is up to date"); - TraceBack++; -} - -void CustomPort(int argc, char* argv[]){ - if(argc > 1){ - std::string Port = argv[1]; - if(Port.find_first_not_of("0123456789") == std::string::npos){ - if(std::stoi(Port) > 1000){ - DEFAULT_PORT = std::stoi(Port); - warn("Running on custom port : " + std::to_string(DEFAULT_PORT)); - } - } - if(argc > 2)Dev = true; - } -} -void InitLauncher(int argc, char* argv[]) { - system("cls"); - curl_global_init(CURL_GLOBAL_DEFAULT); - SetConsoleTitleA(("BeamMP Launcher v" + std::string(GetVer()) + GetPatch()).c_str()); - InitLog(); - CheckName(argc, argv); - CheckLocalKey(); //will replace RequestRole - Discord_Main(); - //Dev = true; - //RequestRole(); - - CustomPort(argc, argv); - CheckForUpdates(argc, argv, std::string(GetVer()) + GetPatch()); -} -size_t DirCount(const std::filesystem::path& path){ - return (size_t)std::distance(std::filesystem::directory_iterator{path}, std::filesystem::directory_iterator{}); -} -void CheckMP(const std::string& Path) { - if (!fs::exists(Path))return; - size_t c = DirCount(fs::path(Path)); - if (c > 3) { - warn(std::to_string(c - 1) + " multiplayer mods will be wiped from mods/multiplayer! Close this if you don't want that!"); - std::this_thread::sleep_for(std::chrono::seconds(15)); - } - try { - for (auto& p : fs::directory_iterator(Path)){ - if(p.exists() && !p.is_directory()){ - std::string Name = p.path().filename().u8string(); - for(char&Ch : Name)Ch = char(tolower(Ch)); - if(Name != "beammp.zip")fs::remove(p.path()); - } - } - } catch (...) { - fatal("Please close the game, and try again!"); - } - -} -void PreGame(const std::string& GamePath){ - const std::string CurrVer("0.21.3.0"); - std::string GameVer = CheckVer(GamePath); - info("Game Version : " + GameVer); - if(GameVer < CurrVer){ - fatal("Game version is old! Please update."); - }else if(GameVer > CurrVer){ - warn("Game is newer than recommended, multiplayer may not work as intended!"); - } - CheckMP(GetGamePath() + "mods/multiplayer"); - - if(!Dev) { - info("Downloading mod..."); - try { - if (!fs::exists(GetGamePath() + "mods/multiplayer")) { - fs::create_directories(GetGamePath() + "mods/multiplayer"); - } - }catch(std::exception&e){ - fatal(e.what()); - } - Download("https://beammp.com/builds/client", GetGamePath() + R"(mods\multiplayer\BeamMP.zip)", true); - info("Download Complete!"); - } - - /*debug("Name : " + GetDName()); - debug("Discriminator : " + GetDTag()); - debug("Unique ID : " + GetDID());*/ +// Copyright (c) 2019-present Anonymous275. +// BeamMP Launcher code is not in the public domain and is not free software. +// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. +// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. +/// +/// Created by Anonymous275 on 7/16/2020 +/// + + +#include "Discord/discord_info.h" +#include "Network/network.h" +#include "Security/Init.h" +#include "Curl/http.h" +#include +#include +#include "Startup.h" +#include "Logger.h" +#include + +extern int TraceBack; +bool Dev = false; +namespace fs = std::filesystem; + +std::string GetEN(){ + return "BeamMP-Launcher.exe"; +} +std::string GetVer(){ + return "1.80"; +} +std::string GetPatch(){ + return ".95"; +} +std::string GetEP(char*P){ + static std::string Ret = [&](){ + std::string path(P); + return path.substr(0, path.find_last_of("\\/") + 1); + } (); + return Ret; +} +void ReLaunch(int argc,char*args[]){ + std::string Arg; + for(int c = 2; c <= argc; c++){ + Arg += " "; + Arg += args[c-1]; + } + system("cls"); + ShellExecute(nullptr,"runas",(GetEP() + GetEN()).c_str(),Arg.c_str(),nullptr,SW_SHOWNORMAL); + ShowWindow(GetConsoleWindow(),0); + std::this_thread::sleep_for(std::chrono::seconds(1)); + exit(1); +} +void URelaunch(int argc,char* args[]){ + std::string Arg; + for(int c = 2; c <= argc; c++){ + Arg += " "; + Arg += args[c-1]; + } + ShellExecute(nullptr,"open",(GetEP() + GetEN()).c_str(),Arg.c_str(),nullptr,SW_SHOWNORMAL); + ShowWindow(GetConsoleWindow(),0); + std::this_thread::sleep_for(std::chrono::seconds(1)); + exit(1); +} +void CheckName(int argc,char* args[]){ + std::string DN = GetEN(),CDir = args[0],FN = CDir.substr(CDir.find_last_of('\\')+1); + if(FN != DN){ + if(fs::exists(DN))remove(DN.c_str()); + if(fs::exists(DN))ReLaunch(argc,args); + std::rename(FN.c_str(), DN.c_str()); + URelaunch(argc,args); + } +} + +void CheckForUpdates(int argc,char*args[],const std::string& CV){ + std::string link = "https://beammp.com/builds/launcher?version=true"; + std::string HTTP = HTTP_REQUEST(link,443); + bool fallback = false; + if(HTTP.find_first_of("0123456789") == std::string::npos){ + link = "https://backup1.beammp.com/builds/launcher?version=true"; + HTTP = HTTP_REQUEST(link,443); + fallback = true; + if(HTTP.find_first_of("0123456789") == std::string::npos) { + fatal("Primary Servers Offline! sorry for the inconvenience!"); + } + } + if(fallback){ + link = "https://backup1.beammp.com/builds/launcher?download=true"; + }else link = "https://beammp.com/builds/launcher?download=true"; + + std::string EP(GetEP() + GetEN()), Back(GetEP() + "BeamMP-Launcher.back"); + + if(fs::exists(Back))remove(Back.c_str()); + + if(HTTP > CV){ + system("cls"); + info("Update found!"); + info("Updating..."); + if(std::rename(EP.c_str(), Back.c_str()))error("failed creating a backup!"); + int i = Download(link, EP,true); + if(i != -1){ + error("Launcher Update failed! trying again... code : " + std::to_string(i)); + std::this_thread::sleep_for(std::chrono::seconds(2)); + int i2 = Download(link, EP,true); + if(i2 != -1){ + error("Launcher Update failed! code : " + std::to_string(i2)); + std::this_thread::sleep_for(std::chrono::seconds(5)); + ReLaunch(argc,args); + } + } + URelaunch(argc,args); + }else info("Launcher version is up to date"); + TraceBack++; +} + +void CustomPort(int argc, char* argv[]){ + if(argc > 1){ + std::string Port = argv[1]; + if(Port.find_first_not_of("0123456789") == std::string::npos){ + if(std::stoi(Port) > 1000){ + DEFAULT_PORT = std::stoi(Port); + warn("Running on custom port : " + std::to_string(DEFAULT_PORT)); + } + } + if(argc > 2)Dev = true; + } +} +void InitLauncher(int argc, char* argv[]) { + system("cls"); + curl_global_init(CURL_GLOBAL_DEFAULT); + SetConsoleTitleA(("BeamMP Launcher v" + std::string(GetVer()) + GetPatch()).c_str()); + InitLog(); + CheckName(argc, argv); + CheckLocalKey(); + ConfigInit(); + CustomPort(argc, argv); + Discord_Main(); + CheckForUpdates(argc, argv, std::string(GetVer()) + GetPatch()); + +} +size_t DirCount(const std::filesystem::path& path){ + return (size_t)std::distance(std::filesystem::directory_iterator{path}, std::filesystem::directory_iterator{}); +} +void CheckMP(const std::string& Path) { + if (!fs::exists(Path))return; + size_t c = DirCount(fs::path(Path)); + if (c > 3) { + warn(std::to_string(c - 1) + " multiplayer mods will be wiped from mods/multiplayer! Close this if you don't want that!"); + std::this_thread::sleep_for(std::chrono::seconds(15)); + } + try { + for (auto& p : fs::directory_iterator(Path)){ + if(p.exists() && !p.is_directory()){ + std::string Name = p.path().filename().u8string(); + for(char&Ch : Name)Ch = char(tolower(Ch)); + if(Name != "beammp.zip")fs::remove(p.path()); + } + } + } catch (...) { + fatal("Please close the game, and try again!"); + } + +} +void PreGame(const std::string& GamePath){ + const std::string CurrVer("0.21.4.0"); + std::string GameVer = CheckVer(GamePath); + info("Game Version : " + GameVer); + if(GameVer < CurrVer){ + fatal("Game version is old! Please update."); + }else if(GameVer > CurrVer){ + warn("Game is newer than recommended, multiplayer may not work as intended!"); + } + CheckMP(GetGamePath() + "mods/multiplayer"); + + if(!Dev) { + info("Downloading mod..."); + try { + if (!fs::exists(GetGamePath() + "mods/multiplayer")) { + fs::create_directories(GetGamePath() + "mods/multiplayer"); + } + }catch(std::exception&e){ + fatal(e.what()); + } + + /* Download("https://backend.beammp.com/builds/client?download=true" + "&pk=" + PublicKey + + "&branch=" + Branch, GetGamePath() + R"(mods\multiplayer\BeamMP.zip)", true);*/ + + Download("https://beammp.com/builds/client", GetGamePath() + R"(mods\multiplayer\BeamMP.zip)", true); + info("Download Complete!"); + } + } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp old mode 100644 new mode 100755