329 lines
12 KiB
C
329 lines
12 KiB
C
#ifndef WIN32_LEAN_AND_MEAN
|
|
#define WIN32_LEAN_AND_MEAN 1
|
|
#endif
|
|
#include <windows.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include "priority_route_slot.h"
|
|
#include "priority_auth_c.h"
|
|
extern void gw_priority_session_cancel(gw_priority_session *);
|
|
|
|
/* The owner calls start only after the admitted post-render login frame. The
|
|
* account worker calls take once for the same credential generation. The
|
|
* background worker uses a user-scoped saved session, then the official
|
|
* launcher session, and offers Octo browser sign-in only when neither works.
|
|
* It never sees game credentials. */
|
|
static SRWLOCK lock = SRWLOCK_INIT;
|
|
static uint32_t current_generation;
|
|
static LONG state;
|
|
static volatile LONG cancelled;
|
|
static gw_priority_session *active_session;
|
|
static char selected_ipv4[16];
|
|
static ULONGLONG selected_expiry;
|
|
static volatile LONG worker_running;
|
|
static uint32_t queued_generation;
|
|
#ifndef GW_PRIORITY_SLOT_TEST
|
|
static HMODULE worker_module;
|
|
#endif
|
|
|
|
static int launch_worker(uint32_t generation);
|
|
|
|
static int worker_cancelled(void) {
|
|
return InterlockedCompareExchange(&cancelled, 0, 0) != 0;
|
|
}
|
|
|
|
/* Do not retain a bearer from an obsolete or cancelled login generation. */
|
|
static int worker_may_store(uint32_t generation) {
|
|
int allowed;
|
|
AcquireSRWLockShared(&lock);
|
|
allowed = current_generation == generation &&
|
|
state == GW_PRIORITY_SLOT_PENDING && !worker_cancelled();
|
|
ReleaseSRWLockShared(&lock);
|
|
return allowed;
|
|
}
|
|
|
|
static int selected_session_ready(int outcome) {
|
|
return outcome == GW_PRIORITY_ROUTE_READY ||
|
|
outcome == GW_PRIORITY_PUBLIC_FALLBACK;
|
|
}
|
|
|
|
static void store_current_if_live(gw_priority_session *session,
|
|
uint32_t generation) {
|
|
if (!worker_may_store(generation)) return;
|
|
if (gw_priority_store_current(session) == 0 &&
|
|
!worker_may_store(generation)) {
|
|
/* A newer generation can cancel while DPAPI and the atomic file
|
|
* replacement run. Workers are serialized, so the next worker has
|
|
* not read this file yet; retire the stale generation's token. */
|
|
gw_priority_delete_saved();
|
|
}
|
|
}
|
|
|
|
static void wipe_route(void) {
|
|
SecureZeroMemory(selected_ipv4, sizeof(selected_ipv4));
|
|
selected_expiry = 0;
|
|
}
|
|
|
|
static DWORD WINAPI authorization_worker(void *argument) {
|
|
const uint32_t generation = (uint32_t)(uintptr_t)argument;
|
|
uint32_t follow = 0;
|
|
gw_priority_session *session = gw_priority_session_create();
|
|
gw_priority_route route = {0};
|
|
int attached = 0, outcome = GW_PRIORITY_INVALID_RESPONSE;
|
|
#ifndef GW_PRIORITY_SLOT_TEST
|
|
HMODULE self = worker_module;
|
|
#endif
|
|
AcquireSRWLockExclusive(&lock);
|
|
if (session && current_generation == generation &&
|
|
state == GW_PRIORITY_SLOT_PENDING && !cancelled) {
|
|
active_session = session;
|
|
attached = 1;
|
|
}
|
|
ReleaseSRWLockExclusive(&lock);
|
|
if (attached) {
|
|
int finished = 0;
|
|
|
|
/* A valid client-owned token is preferred, and its 401 is the only
|
|
* reason to discard it. A transient address failure stays public for
|
|
* this generation without spawning a browser prompt. */
|
|
outcome = gw_priority_resume_saved(session);
|
|
if (outcome == GW_PRIORITY_SESSION_READY && !worker_cancelled()) {
|
|
outcome = gw_priority_select(session, &route);
|
|
if (outcome == GW_PRIORITY_TOKEN_EXPIRED && !worker_cancelled()) {
|
|
gw_priority_delete_saved();
|
|
} else {
|
|
finished = 1;
|
|
}
|
|
} else if (outcome == GW_PRIORITY_CANCELLED || worker_cancelled()) {
|
|
finished = 1;
|
|
} else {
|
|
/* Missing, corrupt or undecryptable client-owned state is never
|
|
* copied to logs or retried in the same login generation. */
|
|
gw_priority_delete_saved();
|
|
}
|
|
|
|
if (!finished && !worker_cancelled()) {
|
|
outcome = gw_priority_resume_launcher(session);
|
|
if (outcome == GW_PRIORITY_SESSION_READY && !worker_cancelled()) {
|
|
outcome = gw_priority_select(session, &route);
|
|
if (outcome == GW_PRIORITY_TOKEN_EXPIRED && !worker_cancelled()) {
|
|
gw_priority_delete_saved();
|
|
} else {
|
|
finished = 1;
|
|
/* Import a validated official session into the client's
|
|
* encrypted store, removing the launcher-file dependency
|
|
* on later starts. A public/disabled priority response
|
|
* still proves the bearer was accepted. */
|
|
if (selected_session_ready(outcome))
|
|
store_current_if_live(session, generation);
|
|
}
|
|
} else if (outcome == GW_PRIORITY_CANCELLED || worker_cancelled()) {
|
|
finished = 1;
|
|
}
|
|
}
|
|
|
|
if (!finished && !worker_cancelled()) {
|
|
/* Browser authorization is offered at most once per generation.
|
|
* Cancellation keeps the normal public-route fallback. */
|
|
outcome = gw_priority_sign_in(session);
|
|
if (outcome == GW_PRIORITY_SESSION_READY && !worker_cancelled()) {
|
|
outcome = gw_priority_select(session, &route);
|
|
if (outcome == GW_PRIORITY_TOKEN_EXPIRED) {
|
|
gw_priority_delete_saved();
|
|
} else if (selected_session_ready(outcome)) {
|
|
store_current_if_live(session, generation);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
AcquireSRWLockExclusive(&lock);
|
|
if (current_generation == generation && state == GW_PRIORITY_SLOT_PENDING) {
|
|
if (outcome == GW_PRIORITY_ROUTE_READY && route.eligible == 1 &&
|
|
route.expires_at_monotonic_ms > GetTickCount64() &&
|
|
route.ipv4[0] && route.ipv4[15] == 0) {
|
|
memcpy(selected_ipv4, route.ipv4, sizeof(selected_ipv4));
|
|
selected_expiry = route.expires_at_monotonic_ms;
|
|
state = GW_PRIORITY_SLOT_PRIVATE;
|
|
} else {
|
|
wipe_route();
|
|
state = GW_PRIORITY_SLOT_PUBLIC;
|
|
}
|
|
}
|
|
if (active_session == session) active_session = NULL;
|
|
ReleaseSRWLockExclusive(&lock);
|
|
SecureZeroMemory(&route, sizeof(route));
|
|
gw_priority_session_destroy(session);
|
|
AcquireSRWLockExclusive(&lock);
|
|
if (queued_generation && queued_generation == current_generation &&
|
|
state == GW_PRIORITY_SLOT_PENDING) {
|
|
follow = queued_generation;
|
|
queued_generation = 0;
|
|
InterlockedExchange(&cancelled, 0);
|
|
} else {
|
|
queued_generation = 0;
|
|
InterlockedExchange(&worker_running, 0);
|
|
}
|
|
ReleaseSRWLockExclusive(&lock);
|
|
if (follow) (void)launch_worker(follow);
|
|
#ifndef GW_PRIORITY_SLOT_TEST
|
|
if (self) FreeLibraryAndExitThread(self, 0);
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
static int launch_worker(uint32_t generation) {
|
|
HANDLE worker;
|
|
#ifdef GW_PRIORITY_SLOT_TEST
|
|
extern volatile LONG gw_priority_slot_test_pause_launch;
|
|
extern volatile LONG gw_priority_slot_test_launch_entered;
|
|
extern volatile LONG gw_priority_slot_test_fail_launch;
|
|
if (InterlockedCompareExchange(&gw_priority_slot_test_pause_launch, 0, 0)) {
|
|
InterlockedExchange(&gw_priority_slot_test_launch_entered, 1);
|
|
while (InterlockedCompareExchange(&gw_priority_slot_test_pause_launch, 0, 0))
|
|
Sleep(1);
|
|
}
|
|
if (InterlockedCompareExchange(&gw_priority_slot_test_fail_launch, 0, 0))
|
|
goto failed;
|
|
#endif
|
|
#ifndef GW_PRIORITY_SLOT_TEST
|
|
HMODULE pinned = NULL;
|
|
/* Pin before publishing a new thread, so a concurrent shutdown cannot
|
|
* unload Wire before that thread reaches its first instruction. */
|
|
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
|
|
(LPCWSTR)(const void *)&authorization_worker,
|
|
&pinned)) {
|
|
goto failed;
|
|
}
|
|
worker_module = pinned;
|
|
#endif
|
|
worker = CreateThread(NULL, 0, authorization_worker,
|
|
(void *)(uintptr_t)generation, 0, NULL);
|
|
if (!worker) {
|
|
goto failed;
|
|
}
|
|
CloseHandle(worker);
|
|
return 0;
|
|
failed:
|
|
#ifndef GW_PRIORITY_SLOT_TEST
|
|
if (pinned) FreeLibrary(pinned);
|
|
worker_module = NULL;
|
|
#endif
|
|
AcquireSRWLockExclusive(&lock);
|
|
/* A newer generation may have queued while this launch was in progress.
|
|
* Resource failure makes that pending route explicitly public too. */
|
|
queued_generation = 0;
|
|
if (state == GW_PRIORITY_SLOT_PENDING) {
|
|
wipe_route();
|
|
state = GW_PRIORITY_SLOT_PUBLIC;
|
|
}
|
|
InterlockedExchange(&worker_running, 0);
|
|
ReleaseSRWLockExclusive(&lock);
|
|
return -1;
|
|
}
|
|
|
|
int gw_priority_slot_start(uint32_t generation) {
|
|
int busy;
|
|
if (!generation) return -1;
|
|
AcquireSRWLockExclusive(&lock);
|
|
if (generation < current_generation) {
|
|
ReleaseSRWLockExclusive(&lock);
|
|
return -1;
|
|
}
|
|
if (generation == current_generation && state != GW_PRIORITY_SLOT_NONE) {
|
|
ReleaseSRWLockExclusive(&lock);
|
|
return 0;
|
|
}
|
|
busy = InterlockedCompareExchange(&worker_running, 1, 0);
|
|
if (busy) {
|
|
/* A prior generation may still be inside bounded WinHTTP. Keep the
|
|
* new route pending; the old worker launches it after cancellation. */
|
|
InterlockedExchange(&cancelled, 1);
|
|
if (active_session) gw_priority_session_cancel(active_session);
|
|
queued_generation = generation;
|
|
} else {
|
|
queued_generation = 0;
|
|
InterlockedExchange(&cancelled, 0);
|
|
}
|
|
current_generation = generation;
|
|
wipe_route();
|
|
state = GW_PRIORITY_SLOT_PENDING;
|
|
ReleaseSRWLockExclusive(&lock);
|
|
return busy ? 0 : launch_worker(generation);
|
|
}
|
|
|
|
int gw_priority_slot_status(uint32_t generation) {
|
|
int result;
|
|
AcquireSRWLockExclusive(&lock);
|
|
result = generation == current_generation ? state : GW_PRIORITY_SLOT_NONE;
|
|
if (result == GW_PRIORITY_SLOT_PRIVATE &&
|
|
GetTickCount64() >= selected_expiry) {
|
|
wipe_route();
|
|
state = result = GW_PRIORITY_SLOT_PUBLIC;
|
|
}
|
|
ReleaseSRWLockExclusive(&lock);
|
|
return result;
|
|
}
|
|
|
|
int gw_priority_slot_cancel(uint32_t generation) {
|
|
int result = -1;
|
|
AcquireSRWLockExclusive(&lock);
|
|
if (generation == current_generation && generation &&
|
|
state != GW_PRIORITY_SLOT_CONSUMED) {
|
|
InterlockedExchange(&cancelled, 1);
|
|
if (active_session) gw_priority_session_cancel(active_session);
|
|
queued_generation = 0;
|
|
wipe_route();
|
|
state = GW_PRIORITY_SLOT_PUBLIC;
|
|
result = 0;
|
|
}
|
|
ReleaseSRWLockExclusive(&lock);
|
|
return result;
|
|
}
|
|
|
|
int gw_priority_slot_public(uint32_t generation) {
|
|
if (!generation) return -1;
|
|
AcquireSRWLockExclusive(&lock);
|
|
InterlockedExchange(&cancelled, 1);
|
|
if (active_session) gw_priority_session_cancel(active_session);
|
|
queued_generation = 0;
|
|
current_generation = generation;
|
|
wipe_route();
|
|
state = GW_PRIORITY_SLOT_PUBLIC;
|
|
ReleaseSRWLockExclusive(&lock);
|
|
return 0;
|
|
}
|
|
|
|
void gw_priority_slot_shutdown(void) {
|
|
AcquireSRWLockExclusive(&lock);
|
|
InterlockedExchange(&cancelled, 1);
|
|
if (active_session) gw_priority_session_cancel(active_session);
|
|
queued_generation = 0;
|
|
wipe_route();
|
|
current_generation = 0;
|
|
state = GW_PRIORITY_SLOT_NONE;
|
|
ReleaseSRWLockExclusive(&lock);
|
|
}
|
|
|
|
int gw_priority_slot_take(uint32_t generation, char out[16]) {
|
|
int result = -1;
|
|
if (!out) return -1;
|
|
memset(out, 0, 16);
|
|
AcquireSRWLockExclusive(&lock);
|
|
if (generation == current_generation && generation) {
|
|
if (state == GW_PRIORITY_SLOT_PRIVATE &&
|
|
GetTickCount64() < selected_expiry) {
|
|
memcpy(out, selected_ipv4, 16);
|
|
result = 1;
|
|
} else if (state == GW_PRIORITY_SLOT_PRIVATE ||
|
|
state == GW_PRIORITY_SLOT_PUBLIC) {
|
|
result = 0;
|
|
}
|
|
if (result >= 0) {
|
|
wipe_route();
|
|
state = GW_PRIORITY_SLOT_CONSUMED;
|
|
}
|
|
}
|
|
ReleaseSRWLockExclusive(&lock);
|
|
return result;
|
|
}
|