mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-04-05 07:16:29 +00:00
switched to nlohmann_json
This commit is contained in:
@@ -5,12 +5,11 @@
|
||||
|
||||
#include "Launcher.h"
|
||||
#include "Logger.h"
|
||||
#include <fstream>
|
||||
#include "Http.h"
|
||||
#include "Json.h"
|
||||
|
||||
void UpdateKey(const char* newKey){
|
||||
if(newKey){
|
||||
void UpdateKey(const std::string& newKey){
|
||||
if(!newKey.empty()){
|
||||
std::ofstream Key("key");
|
||||
if(Key.is_open()){
|
||||
Key << newKey;
|
||||
@@ -38,41 +37,37 @@ std::string GetFail(const std::string& R){
|
||||
std::string Launcher::Login(const std::string& fields) {
|
||||
if(fields == "LO"){
|
||||
LoginAuth = false;
|
||||
UpdateKey(nullptr);
|
||||
UpdateKey("");
|
||||
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());
|
||||
Json d = Json::parse(Buffer, nullptr, false);
|
||||
|
||||
if(Buffer == "-1"){
|
||||
return GetFail("Failed to communicate with the auth system!");
|
||||
}
|
||||
|
||||
if (Buffer.at(0) != '{' || d.HasParseError()) {
|
||||
if (Buffer.at(0) != '{' || d.is_discarded()) {
|
||||
LOG(ERROR) << Buffer;
|
||||
return GetFail("Invalid answer from authentication servers, please try again later!");
|
||||
}
|
||||
|
||||
if(!d["success"].IsNull() && d["success"].GetBool()){
|
||||
if(!d["success"].is_null() && d["success"].get<bool>()){
|
||||
LoginAuth = true;
|
||||
if(!d["private_key"].IsNull()){
|
||||
UpdateKey(d["private_key"].GetString());
|
||||
if(!d["private_key"].is_null()){
|
||||
UpdateKey(d["private_key"].get<std::string>());
|
||||
}
|
||||
if(!d["public_key"].IsNull()){
|
||||
PublicKey = d["public_key"].GetString();
|
||||
if(!d["public_key"].is_null()){
|
||||
PublicKey = d["public_key"].get<std::string>();
|
||||
}
|
||||
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();
|
||||
if(!d["message"].is_null()) {
|
||||
d.erase("private_key");
|
||||
d.erase("public_key");
|
||||
return d;
|
||||
}
|
||||
return GetFail("Invalid message parsing!");
|
||||
}
|
||||
@@ -88,28 +83,27 @@ void Launcher::CheckKey() {
|
||||
|
||||
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()) {
|
||||
Json d = Json::parse(Buffer, nullptr, false);
|
||||
if (Buffer == "-1" || Buffer.at(0) != '{' || d.is_discarded()) {
|
||||
LOG(DEBUG) << Buffer;
|
||||
LOG(FATAL) << "Invalid answer from authentication servers, please try again later!";
|
||||
throw ShutdownException("Fatal Error");
|
||||
}
|
||||
if(d["success"].GetBool()){
|
||||
if(d["success"].get<bool>()){
|
||||
LoginAuth = true;
|
||||
UpdateKey(d["private_key"].GetString());
|
||||
PublicKey = d["public_key"].GetString();
|
||||
UserRole = d["role"].GetString();
|
||||
//info(Role);
|
||||
UpdateKey(d["private_key"].get<std::string>());
|
||||
PublicKey = d["public_key"].get<std::string>();
|
||||
UserRole = d["role"].get<std::string>();
|
||||
LOG(INFO) << "Auto-Authentication was successful";
|
||||
}else{
|
||||
LOG(WARNING) << "Auto-Authentication unsuccessful please re-login!";
|
||||
UpdateKey(nullptr);
|
||||
UpdateKey("");
|
||||
}
|
||||
}else{
|
||||
LOG(WARNING) << "Could not open saved key!";
|
||||
UpdateKey(nullptr);
|
||||
UpdateKey("");
|
||||
}
|
||||
}else UpdateKey(nullptr);
|
||||
}else UpdateKey("");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -107,17 +107,13 @@ void Launcher::EnableMP() {
|
||||
std::string Data(Size, 0);
|
||||
db.read(&Data[0], std::streamsize(Size));
|
||||
db.close();
|
||||
Json::Document d;
|
||||
d.Parse(Data.c_str());
|
||||
if(Data.at(0) != '{' || d.HasParseError())return;
|
||||
if(!d["mods"].IsNull() && !d["mods"]["multiplayerbeammp"].IsNull()){
|
||||
Json d = Json::parse(Data, nullptr, false);
|
||||
if(Data.at(0) != '{' || d.is_discarded())return;
|
||||
if(!d["mods"].is_null() && !d["mods"]["multiplayerbeammp"].is_null()){
|
||||
d["mods"]["multiplayerbeammp"]["active"] = true;
|
||||
Json::StringBuffer buffer;
|
||||
Json::Writer<Json::StringBuffer> writer(buffer);
|
||||
d.Accept(writer);
|
||||
std::ofstream ofs(File);
|
||||
if(ofs.is_open()){
|
||||
ofs << buffer.GetString();
|
||||
ofs << std::setw(4) << d;
|
||||
ofs.close();
|
||||
} else {
|
||||
LOG(ERROR) << "Failed to write " << File;
|
||||
|
||||
Reference in New Issue
Block a user