- add backend proxy

- fix crash bug
This commit is contained in:
Anonymous275
2023-12-02 16:09:42 +00:00
parent dd3d1494c6
commit b09c9f4c7a
4 changed files with 86 additions and 43 deletions

View File

@@ -24,40 +24,46 @@ LONG WINAPI CrashHandler(EXCEPTION_POINTERS* p) {
Launcher::Launcher(int argc, char* argv[]) :
CurrentPath(fs::current_path()),
DiscordMessage("Just launched") {
Log::Init();
Shutdown.store(false);
Exit.store(false);
Launcher::StaticAbort(this);
DiscordTime = std::time(nullptr);
WindowsInit();
SetUnhandledExceptionFilter(CrashHandler);
LOG(INFO) << "Starting Launcher v" << FullVersion;
try {
Log::Init();
Shutdown.store(false);
Exit.store(false);
Launcher::StaticAbort(this);
DiscordTime = std::time(nullptr);
WindowsInit();
SetUnhandledExceptionFilter(CrashHandler);
LOG(INFO) << "Starting Launcher v" << FullVersion;
BackendProxy = std::thread(&Launcher::StartProxy, this);
fs::path config_path(CurrentPath / "Launcher.toml");
fs::path config_path(CurrentPath / "Launcher.toml");
std::string arg, arg2;
if(argc > 2) {
arg = argv[1];
arg2 = argv[2];
} else if (argc > 1) {
arg = argv[1];
}
if (arg.starts_with('0')) {
LOG(INFO) << "Debug param in effect";
DebugMode = true;
Memory::DebugMode = true;
} else if (arg2.starts_with("beammp://")) {
CurrentPath = arg;
config_path = CurrentPath / "Launcher.toml";
if (arg2.starts_with("beammp://connect/")) {
std::string arg, arg2;
if(argc > 2) {
arg = argv[1];
arg2 = argv[2];
} else if (argc > 1) {
arg = argv[1];
}
if (arg.starts_with('0')) {
LOG(INFO) << "Debug param in effect";
DebugMode = true;
Memory::DebugMode = true;
} else if (arg2.starts_with("beammp://")) {
CurrentPath = arg;
config_path = CurrentPath / "Launcher.toml";
if (arg2.starts_with("beammp://connect/")) {
ConnectURI = arg2.substr(17);
}
} else if (!arg.empty()) {
config_path = CurrentPath / arg;
}
}
} else if (!arg.empty()) {
config_path = CurrentPath / arg;
}
LoadConfig(config_path);
LauncherCache = CurrentPath/"Resources";
LoadConfig(config_path);
LauncherCache = CurrentPath/"Resources";
} catch (const ShutdownException& e) {
Abort();
throw ShutdownException(e.what());
}
}
void Launcher::Abort() {
@@ -69,6 +75,10 @@ void Launcher::Abort() {
if (IPCSystem.joinable()) {
IPCSystem.join();
}
HTTPProxy.stop();
if (BackendProxy.joinable()) {
BackendProxy.join();
}
if (!MPUserPath.empty()) {
ResetMods();
}
@@ -203,6 +213,31 @@ void Launcher::WaitForGame() {
GamePID = 0;
}
void Launcher::StartProxy() {
HTTPProxy.Get("/:any", [](const httplib::Request& req, httplib::Response& res) {
httplib::Client cli("https://backend.beammp.com");
if (auto cli_res = cli.Get(req.path); cli_res) {
res.set_content(cli_res->body,cli_res->get_header_value("Content-Type"));
} else {
res.set_content(to_string(cli_res.error()), "text/plain");
}
});
HTTPProxy.Post("/:any", [](const httplib::Request& req, httplib::Response& res) {
httplib::Client cli("https://backend.beammp.com");
if (auto cli_res = cli.Post(req.path, req.body, req.get_header_value("Content-Type")); cli_res) {
res.set_content(cli_res->body,cli_res->get_header_value("Content-Type"));
} else {
res.set_content(to_string(cli_res.error()), "text/plain");
}
res.set_content(req.path, "text/plain");
});
ProxyPort = HTTPProxy.bind_to_any_port("0.0.0.0");
LOG(INFO) << "HTTP Proxy running on port " << ProxyPort;
HTTPProxy.listen_after_bind();
}
void Launcher::ListenIPC() {
while (!Shutdown.load()) {
IPCFromGame->receive();

View File

@@ -2,9 +2,8 @@
/// Created by Anonymous275 on 1/17/22
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
///
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "HttpAPI.h"
#include <httplib.h>
#include <cmath>
#include <fstream>
#include <iostream>