137 lines
4.4 KiB
C++
137 lines
4.4 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <atomic>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace gw::priority {
|
|
|
|
struct HttpReply {
|
|
int status = 0;
|
|
std::string body;
|
|
};
|
|
|
|
class HttpClient {
|
|
public:
|
|
virtual ~HttpClient() = default;
|
|
virtual HttpReply post(std::string_view path, std::string_view json,
|
|
std::string_view bearer) = 0;
|
|
};
|
|
|
|
class BrowserCode {
|
|
public:
|
|
virtual ~BrowserCode() = default;
|
|
// The implementation creates the listener before opening the browser.
|
|
virtual bool obtain(std::string_view authorize_url, std::string& code) = 0;
|
|
};
|
|
|
|
class RouteProbe {
|
|
public:
|
|
virtual ~RouteProbe() = default;
|
|
// Must require a Classic logon challenge reply, not a TCP connect alone.
|
|
virtual bool challenge(std::string_view host, std::string& numeric_ipv4) = 0;
|
|
};
|
|
|
|
class Clock {
|
|
public:
|
|
virtual ~Clock() = default;
|
|
virtual std::uint64_t monotonic_ms() const = 0;
|
|
};
|
|
|
|
enum class Status {
|
|
session_ready,
|
|
priority_ready,
|
|
public_fallback,
|
|
cancelled,
|
|
auth_rejected,
|
|
invalid_response,
|
|
network_error,
|
|
token_expired
|
|
};
|
|
|
|
struct Route {
|
|
Status status = Status::public_fallback;
|
|
bool eligible = false;
|
|
char ipv4[16] = {};
|
|
std::uint64_t expires_at_monotonic_ms = 0;
|
|
};
|
|
|
|
// A one-process session. Bearer and code never appear in Route or logs.
|
|
class Session {
|
|
public:
|
|
Session() = default;
|
|
Session(Session const&) = delete;
|
|
Session& operator=(Session const&) = delete;
|
|
~Session();
|
|
|
|
Status sign_in(BrowserCode& browser, HttpClient& http,
|
|
std::atomic_bool const* cancel = nullptr);
|
|
// Import the official launcher's saved session in memory. The Octo
|
|
// address endpoint still validates it before a route is selected.
|
|
Status resume(std::string_view launcher_auth_json);
|
|
// Import/export only a validated bearer. Callers must wipe exported copies.
|
|
Status resume_bearer(std::string_view bearer);
|
|
bool copy_bearer(std::string& bearer) const;
|
|
Route select(HttpClient& http, RouteProbe& probe, Clock const& clock,
|
|
std::atomic_bool const* cancel = nullptr);
|
|
void clear();
|
|
bool signed_in() const { return !bearer_.empty(); }
|
|
|
|
private:
|
|
std::string bearer_;
|
|
};
|
|
|
|
// Strict callback parser for a fresh locally generated state.
|
|
bool parse_callback_target(std::string_view target, std::string_view state,
|
|
std::string& code);
|
|
|
|
// Production Windows adapters. Only call from a deliberate owned worker;
|
|
// never from DllMain or the three-second credential preflight.
|
|
class WindowsHttp final : public HttpClient {
|
|
public:
|
|
explicit WindowsHttp(std::string launcher_version = "1.5.0");
|
|
HttpReply post(std::string_view path, std::string_view json,
|
|
std::string_view bearer) override;
|
|
private:
|
|
std::string version_;
|
|
};
|
|
|
|
class WindowsBrowser final : public BrowserCode {
|
|
public:
|
|
bool obtain(std::string_view authorize_url, std::string& code) override;
|
|
void set_cancel(std::atomic_bool* cancel) { cancel_ = cancel; }
|
|
private:
|
|
std::atomic_bool* cancel_ = nullptr;
|
|
};
|
|
|
|
class WindowsProbe final : public RouteProbe {
|
|
public:
|
|
bool challenge(std::string_view host, std::string& numeric_ipv4) override;
|
|
};
|
|
|
|
class WindowsClock final : public Clock {
|
|
public:
|
|
std::uint64_t monotonic_ms() const override;
|
|
};
|
|
|
|
// Read only the installed official launcher's bounded auth state. The caller
|
|
// wipes the returned bytes immediately after Session::resume.
|
|
bool load_launcher_auth_state(std::string& json);
|
|
// Separate path reader permits synthetic-file tests without touching the
|
|
// actual launcher's session or overriding the game's profile environment.
|
|
bool load_auth_state_file(std::wstring_view path, std::string& json);
|
|
|
|
// Client-owned, current-user DPAPI store under LocalAppData, outside the game.
|
|
// Callers must wipe plaintext returned in token. A missing/corrupt store fails
|
|
// closed and leaves the normal launcher or browser fallback available.
|
|
bool load_client_priority_token(std::string& token);
|
|
bool store_client_priority_token(std::string_view token,
|
|
std::atomic_bool const* cancel = nullptr);
|
|
void delete_client_priority_token();
|
|
// Explicit-path variants permit synthetic-file tests without the user's store.
|
|
bool load_protected_priority_token_file(std::wstring_view path, std::string& token);
|
|
bool store_protected_priority_token_file(std::wstring_view path, std::string_view token);
|
|
|
|
} // namespace gw::priority
|