- proxy tweaking
This commit is contained in:
Anonymous275 2023-12-15 19:38:47 +00:00
parent 3b479abf64
commit 2781179b4b

View File

@ -291,36 +291,53 @@ void PreGame(const std::string& GamePath){
} }
} }
void set_headers(httplib::Response& res) {
res.set_header("Access-Control-Allow-Origin", "*");
}
void StartProxy() { void StartProxy() {
std::thread proxy([&](){ std::thread proxy([&](){
httplib::Server HTTPProxy; httplib::Server HTTPProxy;
httplib::Headers headers = {
{"User-Agent", "BeamMP-Launcher/" + GetVer() + GetPatch()},
{"Accept", "*/*"}
};
std::string pattern = "/:any1";
for (int i = 2; i <= 4; i++) {
HTTPProxy.Get(pattern, [&](const httplib::Request &req, httplib::Response &res) {
httplib::Client cli("https://backend.beammp.com");
set_headers(res);
if (req.has_header("X-BMP-Authentication")) {
headers.emplace("X-BMP-Authentication", PrivateKey);
}
if (req.has_header("X-API-Version")) {
headers.emplace("X-API-Version", req.get_header_value("X-API-Version"));
}
if (auto cli_res = cli.Get(req.path, headers); 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.Get("/:any", [](const httplib::Request& req, httplib::Response& res) { HTTPProxy.Post(pattern, [&](const httplib::Request &req, httplib::Response &res) {
httplib::Client cli("https://backend.beammp.com"); httplib::Client cli("https://backend.beammp.com");
auto headers = req.headers; set_headers(res);
if (req.has_header("X-BMP-Authentication")) { if (req.has_header("X-BMP-Authentication")) {
headers.emplace("X-BMP-Authentication", PrivateKey); headers.emplace("X-BMP-Authentication", PrivateKey);
} }
if (auto cli_res = cli.Get(req.path, headers); cli_res) { if (req.has_header("X-API-Version")) {
res.set_content(cli_res->body,cli_res->get_header_value("Content-Type")); headers.emplace("X-API-Version", req.get_header_value("X-API-Version"));
} else { }
res.set_content(to_string(cli_res.error()), "text/plain"); if (auto cli_res = cli.Post(req.path, headers, req.body,
} req.get_header_value("Content-Type")); cli_res) {
}); res.set_content(cli_res->body, cli_res->get_header_value("Content-Type"));
} else {
HTTPProxy.Post("/:any", [](const httplib::Request& req, httplib::Response& res) { res.set_content(to_string(cli_res.error()), "text/plain");
httplib::Client cli("https://backend.beammp.com"); }
auto headers = req.headers; });
if (req.has_header("X-BMP-Authentication")) { pattern += "/:any" + std::to_string(i);
headers.emplace("X-BMP-Authentication", PrivateKey); }
}
if (auto cli_res = cli.Post(req.path, headers, 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");
}
});
ProxyPort = HTTPProxy.bind_to_any_port("0.0.0.0"); ProxyPort = HTTPProxy.bind_to_any_port("0.0.0.0");
HTTPProxy.listen_after_bind(); HTTPProxy.listen_after_bind();
}); });