149 lines
6.4 KiB
C++
149 lines
6.4 KiB
C++
// Process-local update notification. Signature verification remains in OctoUpdater.
|
|
#include "client_update_policy.hpp"
|
|
#include <atomic>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <string>
|
|
#ifdef _WIN32
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#endif
|
|
#include <windows.h>
|
|
#include <winhttp.h>
|
|
#define UPDATE_EXPORT extern "C" __declspec(dllexport)
|
|
#else
|
|
#define UPDATE_EXPORT extern "C"
|
|
#endif
|
|
namespace {
|
|
using gateway::client_update::State;
|
|
#ifdef _WIN32
|
|
std::atomic<int> state{0};
|
|
std::atomic<bool> busy{false}, launching{false};
|
|
std::atomic<ULONGLONG> next_poll{0};
|
|
constexpr std::size_t max_signature = 16384;
|
|
struct HttpHandle {
|
|
HINTERNET value;
|
|
~HttpHandle() { if (value) WinHttpCloseHandle(value); }
|
|
};
|
|
std::filesystem::path client_root() {
|
|
wchar_t exe[32768];
|
|
DWORD count = GetModuleFileNameW(nullptr, exe, 32768);
|
|
if (!count || count >= 32768) return {};
|
|
auto game = std::filesystem::path(std::wstring(exe, count)).parent_path();
|
|
if (game.filename() != L"_classic_beta_") return {};
|
|
return game.parent_path();
|
|
}
|
|
bool read_signature(const std::filesystem::path& path, std::string& bytes) {
|
|
std::ifstream in(path, std::ios::binary);
|
|
if (!in) return false;
|
|
char buffer[1024];
|
|
while (in.read(buffer, sizeof buffer) || in.gcount()) {
|
|
bytes.append(buffer, static_cast<std::size_t>(in.gcount()));
|
|
if (bytes.size() > max_signature) return false;
|
|
}
|
|
return in.eof() && !bytes.empty();
|
|
}
|
|
std::wstring wide(const std::string& s) {
|
|
if (s.empty()) return {};
|
|
int n = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), int(s.size()), nullptr, 0);
|
|
if (!n) return {};
|
|
std::wstring result(n, L'\0');
|
|
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s.data(), int(s.size()), result.data(), n);
|
|
return result;
|
|
}
|
|
bool fetch_signature(const std::string& address, std::string& bytes) {
|
|
auto url = wide(address);
|
|
URL_COMPONENTS parts{}; parts.dwStructSize = sizeof parts;
|
|
parts.dwHostNameLength = parts.dwUrlPathLength = parts.dwExtraInfoLength = DWORD(-1);
|
|
if (!WinHttpCrackUrl(url.c_str(), 0, 0, &parts) ||
|
|
(parts.nScheme != INTERNET_SCHEME_HTTPS && parts.nScheme != INTERNET_SCHEME_HTTP)) return false;
|
|
std::wstring host(parts.lpszHostName, parts.dwHostNameLength);
|
|
std::wstring path(parts.lpszUrlPath, parts.dwUrlPathLength);
|
|
if (parts.dwExtraInfoLength) path.append(parts.lpszExtraInfo, parts.dwExtraInfoLength);
|
|
HttpHandle session{WinHttpOpen(L"OctoWoW-UpdateCheck/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
|
|
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0)};
|
|
if (!session.value) return false;
|
|
WinHttpSetTimeouts(session.value, 5000, 5000, 5000, 5000);
|
|
HttpHandle connection{WinHttpConnect(session.value, host.c_str(), parts.nPort, 0)};
|
|
if (!connection.value) return false;
|
|
HttpHandle request{WinHttpOpenRequest(connection.value, L"GET", path.c_str(), nullptr,
|
|
WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
|
|
parts.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0)};
|
|
if (!request.value || !WinHttpSendRequest(request.value, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
|
|
WINHTTP_NO_REQUEST_DATA, 0, 0, 0) || !WinHttpReceiveResponse(request.value, nullptr)) return false;
|
|
DWORD status = 0, length = sizeof status;
|
|
if (!WinHttpQueryHeaders(request.value, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
|
|
WINHTTP_HEADER_NAME_BY_INDEX, &status, &length, WINHTTP_NO_HEADER_INDEX) || status != 200) return false;
|
|
for (;;) {
|
|
char buffer[1024]; DWORD got = 0;
|
|
if (!WinHttpReadData(request.value, buffer, sizeof buffer, &got)) return false;
|
|
if (!got) return !bytes.empty();
|
|
bytes.append(buffer, got);
|
|
if (bytes.size() > max_signature) return false;
|
|
}
|
|
}
|
|
void check() {
|
|
State result = State::unknown;
|
|
try {
|
|
auto root = client_root();
|
|
if (!root.empty()) {
|
|
std::ifstream config(root / "OctoUpdater.cfg");
|
|
auto settings = gateway::client_update::read_config(config);
|
|
std::string local, remote;
|
|
bool local_ok = read_signature(root / "_patch" / "manifest.txt.sig", local);
|
|
bool remote_ok = fetch_signature(gateway::client_update::manifest_url(settings), remote);
|
|
result = gateway::client_update::compare_signatures(remote_ok, remote, local_ok, local);
|
|
}
|
|
} catch (...) {} // A notification failure never interrupts login.
|
|
state.store(static_cast<int>(result));
|
|
next_poll.store(GetTickCount64() + 15ULL * 60 * 1000);
|
|
busy.store(false);
|
|
}
|
|
DWORD WINAPI worker(void* module) {
|
|
check();
|
|
// All C++ locals have left scope before releasing the owned module reference.
|
|
FreeLibraryAndExitThread(static_cast<HMODULE>(module), 0);
|
|
}
|
|
#endif
|
|
}
|
|
UPDATE_EXPORT int gw_native_update_state() {
|
|
#ifdef _WIN32
|
|
if (GetTickCount64() >= next_poll.load() && !busy.exchange(true)) {
|
|
HMODULE module = nullptr;
|
|
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
|
|
reinterpret_cast<LPCWSTR>(&gw_native_update_state), &module)) {
|
|
HANDLE thread = CreateThread(nullptr, 0, worker, module, 0, nullptr);
|
|
if (thread) CloseHandle(thread);
|
|
else { FreeLibrary(module); busy.store(false); }
|
|
} else busy.store(false);
|
|
}
|
|
return state.load();
|
|
#else
|
|
return static_cast<int>(State::unknown);
|
|
#endif
|
|
}
|
|
// Returns success only after the updater process exists. Lua then invokes the
|
|
// original ForceQuit; the updater waits for this process before touching files.
|
|
UPDATE_EXPORT int gw_native_update_apply() {
|
|
#ifdef _WIN32
|
|
if (state.load() != static_cast<int>(State::pending) || launching.exchange(true)) return 0;
|
|
try {
|
|
auto root = client_root();
|
|
if (!root.empty()) {
|
|
auto exe = root / "OctoUpdater.exe";
|
|
std::wstring command = L"\"" + exe.wstring() + L"\" --launch --wait-pid " +
|
|
std::to_wstring(GetCurrentProcessId());
|
|
STARTUPINFOW startup{}; startup.cb = sizeof startup;
|
|
PROCESS_INFORMATION process{};
|
|
if (CreateProcessW(exe.c_str(), command.data(), nullptr, nullptr, FALSE, 0,
|
|
nullptr, root.c_str(), &startup, &process)) {
|
|
CloseHandle(process.hThread); CloseHandle(process.hProcess);
|
|
return 1;
|
|
}
|
|
}
|
|
} catch (...) {}
|
|
launching.store(false);
|
|
#endif
|
|
return 0;
|
|
}
|