mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-04-08 08:46:19 +00:00
Update checking and minor tweaks
This commit is contained in:
112
src/Network/Login.cpp
Normal file
112
src/Network/Login.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 1/17/22
|
||||
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
|
||||
///
|
||||
|
||||
#include "Launcher.h"
|
||||
#include "Logger.h"
|
||||
#include <fstream>
|
||||
#include "Http.h"
|
||||
#include "Json.h"
|
||||
|
||||
void UpdateKey(const char* newKey){
|
||||
if(newKey){
|
||||
std::ofstream Key("key");
|
||||
if(Key.is_open()){
|
||||
Key << newKey;
|
||||
Key.close();
|
||||
}else LOG(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+"\"}";
|
||||
LOG(ERROR) << R;
|
||||
return DRet;
|
||||
}
|
||||
|
||||
std::string Launcher::Login(const std::string& fields) {
|
||||
if(fields == "LO"){
|
||||
LoginAuth = false;
|
||||
UpdateKey(nullptr);
|
||||
return "";
|
||||
}
|
||||
LOG(INFO) << "Attempting to authenticate...";
|
||||
std::string Buffer = HTTP::Post("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()) {
|
||||
LOG(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();
|
||||
}
|
||||
LOG(INFO) << "Authentication successful!";
|
||||
}else LOG(WARNING) << "Authentication failed!";
|
||||
|
||||
if(!d["message"].IsNull()) {
|
||||
d.RemoveMember("private_key");
|
||||
d.RemoveMember("public_key");
|
||||
Json::StringBuffer buffer;
|
||||
Json::Writer<rapidjson::StringBuffer> writer(buffer);
|
||||
d.Accept(writer);
|
||||
return buffer.GetString();
|
||||
}
|
||||
return GetFail("Invalid message parsing!");
|
||||
}
|
||||
|
||||
void Launcher::CheckKey() {
|
||||
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], std::streamsize(Size));
|
||||
Key.close();
|
||||
|
||||
Buffer = HTTP::Post("https://auth.beammp.com/userlogin", R"({"pk":")" + Buffer + "\"}");
|
||||
|
||||
Json::Document d;
|
||||
d.Parse(Buffer.c_str());
|
||||
if (Buffer == "-1" || Buffer.at(0) != '{' || d.HasParseError()) {
|
||||
LOG(ERROR) << Buffer;
|
||||
LOG(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();
|
||||
UserRole = d["role"].GetString();
|
||||
//info(Role);
|
||||
}else{
|
||||
LOG(WARNING) << "Auto-Authentication unsuccessful please re-login!";
|
||||
UpdateKey(nullptr);
|
||||
}
|
||||
}else{
|
||||
LOG(WARNING) << "Could not open saved key!";
|
||||
UpdateKey(nullptr);
|
||||
}
|
||||
}else UpdateKey(nullptr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
54
src/Network/Update.cpp
Normal file
54
src/Network/Update.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 1/18/22
|
||||
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
|
||||
///
|
||||
|
||||
#include "Launcher.h"
|
||||
#include "Logger.h"
|
||||
#include "Http.h"
|
||||
|
||||
|
||||
void Launcher::UpdateCheck() {
|
||||
std::string link;
|
||||
std::string HTTP = HTTP::Get("https://beammp.com/builds/launcher?version=true");
|
||||
bool fallback = false;
|
||||
if(HTTP.find_first_of("0123456789") == std::string::npos){
|
||||
HTTP = HTTP::Get("https://backup1.beammp.com/builds/launcher?version=true");
|
||||
fallback = true;
|
||||
if(HTTP.find_first_of("0123456789") == std::string::npos) {
|
||||
LOG(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(CurrentPath.string()), Back(CurrentPath.parent_path().string() + "\\BeamMP-Launcher.back");
|
||||
|
||||
if(fs::exists(Back))remove(Back.c_str());
|
||||
std::string RemoteVer;
|
||||
for(char& c : HTTP) {
|
||||
if(std::isdigit(c) || c == '.') {
|
||||
RemoteVer += c;
|
||||
}
|
||||
}
|
||||
if(RemoteVer > FullVersion){
|
||||
system("cls");
|
||||
LOG(INFO) << "Update found! Downloading...";
|
||||
if(std::rename(EP.c_str(), Back.c_str())){
|
||||
LOG(ERROR) << "Failed to create a backup!";
|
||||
}
|
||||
|
||||
if(!HTTP::Download(link, EP)){
|
||||
LOG(ERROR) << "Launcher Update failed! trying again...";
|
||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
|
||||
if(!HTTP::Download(link, EP)){
|
||||
LOG(ERROR) << "Launcher Update failed!";
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
AdminRelaunch();
|
||||
}
|
||||
}
|
||||
Relaunch();
|
||||
}else LOG(INFO) << "Launcher version is up to date";
|
||||
}
|
||||
Reference in New Issue
Block a user