Files
2026-11-23 03:28:32 +02:00

156 lines
5.8 KiB
C++

#include "priority_auth_c.h"
#include "priority_auth.hpp"
#include <cstring>
#include <atomic>
#include <new>
struct gw_priority_session {
std::atomic_bool cancelled{false};
gw::priority::Session session;
gw::priority::WindowsHttp http;
gw::priority::WindowsBrowser browser;
gw::priority::WindowsProbe probe;
gw::priority::WindowsClock clock;
gw_priority_session() { browser.set_cancel(&cancelled); }
};
namespace {
void wipe(std::string& value) {
volatile char* data = value.empty() ? nullptr : value.data();
for (std::size_t i = 0; data && i < value.size(); ++i) data[i] = 0;
value.clear();
}
struct WipeState {
std::string& value;
~WipeState() { wipe(value); }
};
int status_code(gw::priority::Status status) {
using gw::priority::Status;
switch (status) {
case Status::session_ready: return GW_PRIORITY_SESSION_READY;
case Status::priority_ready: return GW_PRIORITY_ROUTE_READY;
case Status::public_fallback: return GW_PRIORITY_PUBLIC_FALLBACK;
case Status::cancelled: return GW_PRIORITY_CANCELLED;
case Status::auth_rejected: return GW_PRIORITY_AUTH_REJECTED;
case Status::invalid_response: return GW_PRIORITY_INVALID_RESPONSE;
case Status::network_error: return GW_PRIORITY_NETWORK_ERROR;
case Status::token_expired: return GW_PRIORITY_TOKEN_EXPIRED;
}
return GW_PRIORITY_INVALID_RESPONSE;
}
} // namespace
extern "C" gw_priority_session* gw_priority_session_create(void) {
try { return new gw_priority_session; }
catch (...) { return nullptr; }
}
extern "C" void gw_priority_session_destroy(gw_priority_session* session) {
delete session;
}
extern "C" void gw_priority_session_clear(gw_priority_session* session) {
if (session) session->session.clear();
}
extern "C" void gw_priority_session_cancel(gw_priority_session* session) {
if (session) session->cancelled.store(true, std::memory_order_release);
}
extern "C" int gw_priority_sign_in(gw_priority_session* session) {
if (!session) return GW_PRIORITY_INVALID_RESPONSE;
if (session->cancelled.load(std::memory_order_acquire)) return GW_PRIORITY_CANCELLED;
try {
const auto result = session->session.sign_in(session->browser, session->http,
&session->cancelled);
if (session->cancelled.load(std::memory_order_acquire)) {
session->session.clear();
return GW_PRIORITY_CANCELLED;
}
return status_code(result);
}
catch (...) { session->session.clear(); return GW_PRIORITY_INVALID_RESPONSE; }
}
extern "C" int gw_priority_resume_launcher(gw_priority_session* session) {
if (!session) return GW_PRIORITY_INVALID_RESPONSE;
if (session->cancelled.load(std::memory_order_acquire)) return GW_PRIORITY_CANCELLED;
session->session.clear();
std::string state;
WipeState wipe_state{state};
try {
if (!gw::priority::load_launcher_auth_state(state)) return GW_PRIORITY_AUTH_REJECTED;
if (session->cancelled.load(std::memory_order_acquire)) return GW_PRIORITY_CANCELLED;
const auto result = session->session.resume(state);
if (session->cancelled.load(std::memory_order_acquire)) {
session->session.clear();
return GW_PRIORITY_CANCELLED;
}
return status_code(result);
} catch (...) {
session->session.clear();
return GW_PRIORITY_INVALID_RESPONSE;
}
}
extern "C" int gw_priority_resume_saved(gw_priority_session* session) {
if (!session) return GW_PRIORITY_INVALID_RESPONSE;
if (session->cancelled.load(std::memory_order_acquire)) return GW_PRIORITY_CANCELLED;
session->session.clear();
std::string token;
WipeState wipe_token{token};
try {
if (!gw::priority::load_client_priority_token(token)) return GW_PRIORITY_AUTH_REJECTED;
if (session->cancelled.load(std::memory_order_acquire)) return GW_PRIORITY_CANCELLED;
const auto result = session->session.resume_bearer(token);
if (session->cancelled.load(std::memory_order_acquire)) {
session->session.clear();
return GW_PRIORITY_CANCELLED;
}
return status_code(result);
} catch (...) {
session->session.clear();
return GW_PRIORITY_INVALID_RESPONSE;
}
}
extern "C" int gw_priority_store_current(gw_priority_session* session) {
if (!session || session->cancelled.load(std::memory_order_acquire)) return -1;
std::string token;
WipeState wipe_token{token};
try {
if (!session->session.copy_bearer(token)) return -1;
return gw::priority::store_client_priority_token(token, &session->cancelled) ? 0 : -1;
} catch (...) { return -1; }
}
extern "C" void gw_priority_delete_saved(void) {
try { gw::priority::delete_client_priority_token(); } catch (...) {}
}
extern "C" int gw_priority_select(gw_priority_session* session, gw_priority_route* route) {
if (!route) return GW_PRIORITY_INVALID_RESPONSE;
std::memset(route, 0, sizeof(*route));
route->status = GW_PRIORITY_INVALID_RESPONSE;
if (!session) return route->status;
if (session->cancelled.load(std::memory_order_acquire)) return GW_PRIORITY_CANCELLED;
try {
const gw::priority::Route result = session->session.select(
session->http, session->probe, session->clock, &session->cancelled);
if (session->cancelled.load(std::memory_order_acquire)) {
session->session.clear();
return GW_PRIORITY_CANCELLED;
}
route->status = status_code(result.status);
route->eligible = result.eligible ? 1 : 0;
std::memcpy(route->ipv4, result.ipv4, sizeof(route->ipv4));
route->expires_at_monotonic_ms = result.expires_at_monotonic_ms;
return route->status;
} catch (...) {
return route->status;
}
}