mirror of
https://git.neolegacy.dev/neoStudiosLCE/neoLegacy.git
synced 2026-12-02 22:18:09 +01:00
+Discord account connection +Friends List +UPnP Connection +Cosmetics (Iggy menus need to be fixed up a little)
183 lines
6.2 KiB
C++
183 lines
6.2 KiB
C++
#include "stdafx.h"
|
|
#include "OnlineVerifier.h"
|
|
#include "SecurityConfig.h"
|
|
#include <winhttp.h>
|
|
#include <thread>
|
|
|
|
#pragma comment(lib, "winhttp.lib")
|
|
|
|
namespace ServerRuntime
|
|
{
|
|
namespace Online
|
|
{
|
|
namespace
|
|
{
|
|
std::wstring Widen(const std::string &value)
|
|
{
|
|
if (value.empty())
|
|
return std::wstring();
|
|
int length = MultiByteToWideChar(CP_UTF8, 0, value.c_str(), static_cast<int>(value.size()), nullptr, 0);
|
|
std::wstring out(length, L'\0');
|
|
MultiByteToWideChar(CP_UTF8, 0, value.c_str(), static_cast<int>(value.size()), &out[0], length);
|
|
return out;
|
|
}
|
|
|
|
std::string Narrow(const std::wstring &value)
|
|
{
|
|
if (value.empty())
|
|
return std::string();
|
|
int length = WideCharToMultiByte(CP_UTF8, 0, value.c_str(), static_cast<int>(value.size()), nullptr, 0, nullptr, nullptr);
|
|
std::string out(length, '\0');
|
|
WideCharToMultiByte(CP_UTF8, 0, value.c_str(), static_cast<int>(value.size()), &out[0], length, nullptr, nullptr);
|
|
return out;
|
|
}
|
|
|
|
std::string JsonEscape(const std::string &value)
|
|
{
|
|
std::string out = "\"";
|
|
for (char c : value)
|
|
{
|
|
if (c == '"' || c == '\\')
|
|
{
|
|
out += '\\';
|
|
out += c;
|
|
}
|
|
else if (static_cast<unsigned char>(c) >= 0x20)
|
|
{
|
|
out += c;
|
|
}
|
|
}
|
|
out += '"';
|
|
return out;
|
|
}
|
|
|
|
std::string FindString(const std::string &json, const std::string &key)
|
|
{
|
|
std::string needle = "\"" + key + "\"";
|
|
size_t pos = json.find(needle);
|
|
if (pos == std::string::npos)
|
|
return std::string();
|
|
pos = json.find(':', pos + needle.size());
|
|
if (pos == std::string::npos)
|
|
return std::string();
|
|
pos = json.find('"', pos);
|
|
if (pos == std::string::npos)
|
|
return std::string();
|
|
size_t end = json.find('"', pos + 1);
|
|
if (end == std::string::npos)
|
|
return std::string();
|
|
return json.substr(pos + 1, end - pos - 1);
|
|
}
|
|
|
|
bool FindTrue(const std::string &json, const std::string &key)
|
|
{
|
|
std::string needle = "\"" + key + "\"";
|
|
size_t pos = json.find(needle);
|
|
if (pos == std::string::npos)
|
|
return false;
|
|
pos = json.find(':', pos + needle.size());
|
|
if (pos == std::string::npos)
|
|
return false;
|
|
pos = json.find_first_not_of(" \t\r\n", pos + 1);
|
|
return pos != std::string::npos && json.compare(pos, 4, "true") == 0;
|
|
}
|
|
|
|
bool Post(const std::wstring &apiUrl, const std::string &path, const std::string &body, int &status, std::string &response)
|
|
{
|
|
URL_COMPONENTS parts = {};
|
|
parts.dwStructSize = sizeof(parts);
|
|
wchar_t host[256] = {};
|
|
wchar_t urlPath[1024] = {};
|
|
parts.lpszHostName = host;
|
|
parts.dwHostNameLength = 256;
|
|
parts.lpszUrlPath = urlPath;
|
|
parts.dwUrlPathLength = 1024;
|
|
if (!WinHttpCrackUrl(apiUrl.c_str(), 0, 0, &parts) || host[0] == 0)
|
|
return false;
|
|
std::wstring basePath = urlPath;
|
|
while (!basePath.empty() && basePath.back() == L'/')
|
|
basePath.pop_back();
|
|
|
|
HINTERNET session = WinHttpOpen(L"neoLegacyServer/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
|
if (session == nullptr)
|
|
return false;
|
|
WinHttpSetTimeouts(session, 4000, 4000, 6000, 6000);
|
|
bool ok = false;
|
|
HINTERNET connection = WinHttpConnect(session, host, parts.nPort, 0);
|
|
if (connection != nullptr)
|
|
{
|
|
std::wstring fullPath = basePath + Widen(path);
|
|
HINTERNET request = WinHttpOpenRequest(connection, L"POST", fullPath.c_str(), nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, parts.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0);
|
|
if (request != nullptr)
|
|
{
|
|
DWORD length = static_cast<DWORD>(body.size());
|
|
if (WinHttpSendRequest(request, L"Content-Type: application/json\r\n", static_cast<DWORD>(-1), const_cast<char *>(body.data()), length, length, 0) && WinHttpReceiveResponse(request, nullptr))
|
|
{
|
|
DWORD code = 0;
|
|
DWORD size = sizeof(code);
|
|
WinHttpQueryHeaders(request, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, WINHTTP_HEADER_NAME_BY_INDEX, &code, &size, WINHTTP_NO_HEADER_INDEX);
|
|
status = static_cast<int>(code);
|
|
while (response.size() < 16384)
|
|
{
|
|
DWORD available = 0;
|
|
if (!WinHttpQueryDataAvailable(request, &available) || available == 0)
|
|
break;
|
|
std::string chunk(available, '\0');
|
|
DWORD read = 0;
|
|
if (!WinHttpReadData(request, &chunk[0], available, &read))
|
|
break;
|
|
chunk.resize(read);
|
|
response += chunk;
|
|
}
|
|
ok = true;
|
|
}
|
|
WinHttpCloseHandle(request);
|
|
}
|
|
WinHttpCloseHandle(connection);
|
|
}
|
|
WinHttpCloseHandle(session);
|
|
return ok;
|
|
}
|
|
|
|
void RunVerify(std::shared_ptr<VerifyTicket> ticket, std::string token, std::wstring apiUrl, std::string address)
|
|
{
|
|
int status = 0;
|
|
std::string response;
|
|
std::string body = "{\"joinToken\":" + JsonEscape(token) + ",\"serverAddress\":" + JsonEscape(address) + "}";
|
|
if (!Post(apiUrl, "/session/verify", body, status, response))
|
|
{
|
|
ticket->error = "unreachable";
|
|
ticket->state = eVerify_Unavailable;
|
|
return;
|
|
}
|
|
if (status == 200 && FindTrue(response, "valid"))
|
|
{
|
|
ticket->username = Widen(FindString(response, "username"));
|
|
ticket->onlineId = _strtoui64(FindString(response, "onlineId").c_str(), nullptr, 16);
|
|
ticket->state = (!ticket->username.empty() && ticket->onlineId != 0) ? eVerify_Valid : eVerify_Rejected;
|
|
return;
|
|
}
|
|
ticket->error = FindString(response, "error");
|
|
if (ticket->error.empty())
|
|
ticket->error = "status_" + std::to_string(status);
|
|
ticket->state = (status >= 500 || status == 429) ? eVerify_Unavailable : eVerify_Rejected;
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<VerifyTicket> BeginVerify(const std::wstring &joinToken)
|
|
{
|
|
std::shared_ptr<VerifyTicket> ticket = std::make_shared<VerifyTicket>();
|
|
const Security::SecuritySettings &settings = Security::GetSettings();
|
|
std::string token = Narrow(joinToken);
|
|
if (token.size() != 64 || token.find_first_not_of("0123456789abcdefABCDEF") != std::string::npos)
|
|
{
|
|
ticket->error = "missing_token";
|
|
ticket->state = eVerify_Rejected;
|
|
return ticket;
|
|
}
|
|
std::thread(RunVerify, ticket, token, Widen(settings.onlineApiUrl), settings.onlineAddress).detach();
|
|
return ticket;
|
|
}
|
|
}
|
|
}
|