Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/Http.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include <string>
class HTTP {
public:
static bool Download(const std::string& IP, const std::string& Path);
static bool Download(const std::string& IP, const std::string& Fields, const std::string& Path);
static std::string Post(const std::string& IP, const std::string& Fields);
static std::string Get(const std::string& IP);
static std::string Get(const std::string& IP, const std::string& Fields = "");
static bool ProgressBar(size_t c, size_t t);
static void StartProxy();
public:
Expand Down
11 changes: 8 additions & 3 deletions src/Network/Http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static size_t CurlWriteCallback(void* contents, size_t size, size_t nmemb, void*
}

bool HTTP::isDownload = false;
std::string HTTP::Get(const std::string& IP) {
std::string HTTP::Get(const std::string& IP, const std::string& Fields) {
std::string Ret;
static thread_local CURL* curl = curl_easy_init();
if (curl) {
Expand All @@ -78,6 +78,11 @@ std::string HTTP::Get(const std::string& IP) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&Ret);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10); // seconds
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
if (!Fields.empty()) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size());
}
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
error("GET to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
Expand Down Expand Up @@ -119,12 +124,12 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
return Ret;
}

bool HTTP::Download(const std::string& IP, const std::string& Path) {
bool HTTP::Download(const std::string& IP, const std::string& Fields, const std::string& Path) {
static std::mutex Lock;
std::scoped_lock Guard(Lock);

info("Downloading an update (this may take a while)");
std::string Ret = Get(IP);
std::string Ret = Get(IP, Fields);

if (Ret.empty()) {
error("Download failed");
Expand Down
20 changes: 7 additions & 13 deletions src/Startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ void CheckName() {
}

void CheckForUpdates(const std::string& CV) {
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/launcher?branch=" + Branch + "&pk=" + PublicKey);
std::string LatestVersion = HTTP::Get(
"https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
std::string requestBody = R"({"branch": ")" + Branch + R"(","pk":")" + PublicKey + R"("})";
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/launcher", requestBody);
std::string LatestVersion = HTTP::Get("https://backend.beammp.com/version/launcher", requestBody);

transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower);
std::string EP(GetEP() + GetEN()), Back(GetEP() + "BeamMP-Launcher.back");
Expand All @@ -182,11 +182,7 @@ void CheckForUpdates(const std::string& CV) {
fs::remove(Back);
fs::rename(EP, Back);
info("Downloading Launcher update " + LatestHash);
HTTP::Download(
"https://backend.beammp.com/builds/launcher?download=true"
"&pk="
+ PublicKey + "&branch=" + Branch,
EP);
HTTP::Download("https://backend.beammp.com/builds/launcher?download=true", requestBody, EP);
URelaunch();
#endif
} else {
Expand Down Expand Up @@ -307,7 +303,8 @@ void PreGame(const std::string& GamePath) {
info("Game user path: " + GetGamePath());

if (!options.no_download) {
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
std::string requestBody = R"({"branch": ")" + Branch + R"(","pk":")" + PublicKey + R"("})";
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod", requestBody);
transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower);
LatestHash.erase(std::remove_if(LatestHash.begin(), LatestHash.end(),
[](auto const& c) -> bool { return !std::isalnum(c); }),
Expand All @@ -332,10 +329,7 @@ void PreGame(const std::string& GamePath) {

if (FileHash != LatestHash) {
info("Downloading BeamMP Update " + LatestHash);
HTTP::Download("https://backend.beammp.com/builds/client?download=true"
"&pk="
+ PublicKey + "&branch=" + Branch,
ZipPath);
HTTP::Download("https://backend.beammp.com/builds/client?download=true", requestBody, ZipPath);
}

std::string Target(GetGamePath() + "mods/unpacked/beammp");
Expand Down