#include "stdafx.h" #include "ServerAuth.h" #include "../../Minecraft.Client/Windows64/Network/WinsockNetLayer.h" #include #include #include #include #include #include #include #include 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 split(const std::string& str, char delimiter) { std::vector 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 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 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 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 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 -password " << 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(); }