Files
MCLEServer/Minecraft.Server/Windows64/ServerAuth.cpp
T
2026-05-08 05:15:08 -07:00

106 lines
3.6 KiB
C++

#include "stdafx.h"
#include "ServerAuth.h"
#include "../../Minecraft.Client/Windows64/Network/WinsockNetLayer.h"
#include <shellapi.h>
#include <shlobj.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <winhttp.h>
float g_sleepPercentage = 100;
int g_autosaveInterval = 120;
bool g_doBoatBreak = true;
std::string username = "";
std::string authenticationToken = "";
const std::string& ServerAuth::GetAuthenticationToken() { return authenticationToken; }
const std::string& ServerAuth::GetUsername() { return username; }
static bool loadedDedicated = false;
std::vector<std::string> split(const std::string& str, char delimiter) {
std::vector<std::string> result;
size_t start = 0, end = 0;
while ((end = str.find(delimiter, start)) != std::string::npos) {
result.push_back(str.substr(start, end - start));
start = end + 1;
}
result.push_back(str.substr(start));
return result;
}
int ServerAuth::API_GetAccountInfo(const std::string token) {
std::vector<std::wstring> headers;
headers.push_back(L"Content-Type: text/plain");
HttpResponse response = WinsockNetLayer::DoWinHttpRequest(L"/getAccountInfo", L"POST", token, headers);
if (response.status == 0) return -1;
if (response.status != 200) return (20000 + response.status);
if (response.body.find('-') == std::string::npos) return stoi(response.body);
username = response.body.erase(0, 1);
return 0;
}
int ServerAuth::API_AttemptAccountRegister(const std::string _username, const std::string password, std::string& tokenOut) {
std::vector<std::wstring> headers;
headers.push_back(L"Content-Type: text/plain");
HttpResponse response = WinsockNetLayer::DoWinHttpRequest(L"/accountRegistration", L"POST", _username + ":" + password, headers);
if (response.status != 200) return (20000 + response.status);
if (response.body.find('-') == std::string::npos) return stoi(response.body);
auto parts = split(response.body.erase(0, 1), ':');
username = parts[0];
authenticationToken = parts[1];
return 0;
}
int ServerAuth::API_AttemptAccountLogin(const std::string _username, const std::string password, std::string& tokenOut) {
std::vector<std::wstring> headers;
headers.push_back(L"Content-Type: text/plain");
HttpResponse response = WinsockNetLayer::DoWinHttpRequest(L"/accountLogin", L"POST", _username + ":" + password, headers);
if (response.status != 200) return (20000 + response.status);
if (response.body.find('-') == std::string::npos) return stoi(response.body);
auto parts = split(response.body.erase(0, 1), ':');
username = parts[0];
authenticationToken = parts[1];
return 0;
}
void ServerAuth::CreateLauncherWindow(HINSTANCE hInstance, std::function<void()> onLaunch) {
int argc = 0;
LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
auto toStr = [](LPWSTR w) -> std::string {
int len = WideCharToMultiByte(CP_UTF8, 0, w, -1, nullptr, 0, nullptr, nullptr);
std::string s(len - 1, '\0');
WideCharToMultiByte(CP_UTF8, 0, w, -1, &s[0], len, nullptr, nullptr);
return s;
};
std::string argUser, argPass;
for (int i = 1; i < argc; ++i) {
std::string a = toStr(argv[i]);
if (a == "-username" && i + 1 < argc) { argUser = toStr(argv[++i]); }
else if (a == "-password" && i + 1 < argc) { argPass = toStr(argv[++i]); }
}
LocalFree(argv);
if (argUser.empty() || argPass.empty()) {
std::cout << "Usage: Minecraft.Server.exe -username <user> -password <pass>" << std::endl;
ExitProcess(1);
}
int result = ServerAuth::API_AttemptAccountLogin(argUser, argPass, authenticationToken);
if (result != 0) {
std::cout << "Wrong username or password." << std::endl;
ExitProcess(1);
}
if (onLaunch) onLaunch();
}