406 lines
16 KiB
C++
406 lines
16 KiB
C++
#include "priority_auth.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cctype>
|
|
#include <cstring>
|
|
#include <limits>
|
|
#include <map>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace gw::priority {
|
|
namespace {
|
|
|
|
void wipe(std::string& value) {
|
|
volatile char* bytes = value.empty() ? nullptr : value.data();
|
|
for (std::size_t i = 0; bytes && i < value.size(); ++i) bytes[i] = 0;
|
|
value.clear();
|
|
}
|
|
|
|
struct WipeString {
|
|
std::string& value;
|
|
~WipeString() { wipe(value); }
|
|
};
|
|
|
|
struct WipeReply {
|
|
HttpReply& reply;
|
|
~WipeReply() { wipe(reply.body); }
|
|
};
|
|
|
|
struct WipeFields {
|
|
std::map<std::string, struct JsonValue>& fields;
|
|
~WipeFields();
|
|
};
|
|
|
|
bool plain_token(std::string_view text) {
|
|
if (text.empty() || text.size() > 4096) return false;
|
|
for (unsigned char c : text)
|
|
if (c < 0x21 || c > 0x7e || c == '"' || c == '\\') return false;
|
|
return true;
|
|
}
|
|
|
|
bool valid_host(std::string_view host) {
|
|
if (host.size() < 3 || host.size() > 253 || host.front() == '.' ||
|
|
host.back() == '.') return false;
|
|
std::size_t label = 0;
|
|
for (std::size_t i = 0; i < host.size(); ++i) {
|
|
char c = host[i];
|
|
if (c == '.') {
|
|
if (!label || label > 63 || host[i - 1] == '-') return false;
|
|
label = 0;
|
|
} else {
|
|
if (!(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') &&
|
|
!(c >= '0' && c <= '9') && c != '-') return false;
|
|
if (!label && c == '-') return false;
|
|
++label;
|
|
}
|
|
}
|
|
return label && label <= 63 && host.back() != '-';
|
|
}
|
|
|
|
bool octo_host(std::string_view host) {
|
|
constexpr std::string_view suffix = "octowow.st";
|
|
if (host.size() < suffix.size()) return false;
|
|
const std::size_t start = host.size() - suffix.size();
|
|
if (start && host[start - 1] != '.') return false;
|
|
for (std::size_t i = 0; i < suffix.size(); ++i) {
|
|
const unsigned char c = static_cast<unsigned char>(host[start + i]);
|
|
if (std::tolower(c) != suffix[i]) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool public_ipv4(std::string_view text) {
|
|
std::array<unsigned, 4> a{};
|
|
std::size_t start = 0;
|
|
for (int i = 0; i < 4; ++i) {
|
|
const std::size_t end = i == 3 ? text.size() : text.find('.', start);
|
|
if (end == std::string_view::npos || end <= start || end - start > 3 ||
|
|
(end - start > 1 && text[start] == '0')) return false;
|
|
unsigned value = 0;
|
|
for (std::size_t j = start; j < end; ++j) {
|
|
if (text[j] < '0' || text[j] > '9') return false;
|
|
value = value * 10 + static_cast<unsigned>(text[j] - '0');
|
|
}
|
|
if (value > 255) return false;
|
|
a[i] = value;
|
|
start = end + 1;
|
|
}
|
|
if (a[0] == 0 || a[0] == 10 || a[0] == 127 || a[0] >= 224 ||
|
|
(a[0] == 100 && a[1] >= 64 && a[1] <= 127) ||
|
|
(a[0] == 169 && a[1] == 254) ||
|
|
(a[0] == 172 && a[1] >= 16 && a[1] <= 31) ||
|
|
(a[0] == 192 && a[1] == 168) ||
|
|
(a[0] == 192 && a[1] == 0 && a[2] == 0) ||
|
|
(a[0] == 192 && a[1] == 0 && a[2] == 2) ||
|
|
(a[0] == 198 && (a[1] == 18 || a[1] == 19)) ||
|
|
(a[0] == 198 && a[1] == 51 && a[2] == 100) ||
|
|
(a[0] == 203 && a[1] == 0 && a[2] == 113)) return false;
|
|
return true;
|
|
}
|
|
|
|
struct JsonValue {
|
|
enum class Kind { string, boolean, integer, null_value } kind;
|
|
std::string text;
|
|
bool boolean = false;
|
|
std::uint64_t integer = 0;
|
|
};
|
|
|
|
WipeFields::~WipeFields() {
|
|
for (auto& entry : fields) wipe(entry.second.text);
|
|
}
|
|
|
|
class JsonReader {
|
|
public:
|
|
explicit JsonReader(std::string_view input) : input_(input) {}
|
|
bool object(std::map<std::string, JsonValue>& fields) {
|
|
if (input_.size() > 16384 || !take('{')) return false;
|
|
if (take('}')) return done();
|
|
do {
|
|
std::string key;
|
|
WipeString wipe_key{key};
|
|
JsonValue value{};
|
|
WipeString wipe_value{value.text};
|
|
if (!string(key) || !take(':') || !parse(value) ||
|
|
!fields.emplace(std::move(key), std::move(value)).second) return false;
|
|
if (take('}')) return done();
|
|
} while (take(','));
|
|
return false;
|
|
}
|
|
|
|
private:
|
|
void whitespace() {
|
|
while (at_ < input_.size() && (input_[at_] == ' ' || input_[at_] == '\n' ||
|
|
input_[at_] == '\r' || input_[at_] == '\t')) ++at_;
|
|
}
|
|
bool take(char expected) {
|
|
whitespace();
|
|
if (at_ >= input_.size() || input_[at_] != expected) return false;
|
|
++at_;
|
|
return true;
|
|
}
|
|
bool done() { whitespace(); return at_ == input_.size(); }
|
|
bool literal(std::string_view word) {
|
|
whitespace();
|
|
if (input_.substr(at_, word.size()) != word) return false;
|
|
at_ += word.size();
|
|
return true;
|
|
}
|
|
bool string(std::string& output) {
|
|
if (!take('"')) return false;
|
|
while (at_ < input_.size()) {
|
|
unsigned char c = static_cast<unsigned char>(input_[at_++]);
|
|
if (c == '"') return true;
|
|
if (c < 0x20) return false;
|
|
if (c != '\\') { output.push_back(static_cast<char>(c)); continue; }
|
|
if (at_ >= input_.size()) return false;
|
|
switch (char escape = input_[at_++]) {
|
|
case '"': case '\\': case '/': output.push_back(escape); break;
|
|
case 'b': output.push_back('\b'); break;
|
|
case 'f': output.push_back('\f'); break;
|
|
case 'n': output.push_back('\n'); break;
|
|
case 'r': output.push_back('\r'); break;
|
|
case 't': output.push_back('\t'); break;
|
|
case 'u': {
|
|
auto hex = [](char c) -> int {
|
|
if (c >= '0' && c <= '9') return c - '0';
|
|
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
|
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
|
return -1;
|
|
};
|
|
if (input_.size() - at_ < 4) return false;
|
|
unsigned codepoint = 0;
|
|
for (int i = 0; i < 4; ++i) {
|
|
const int digit = hex(input_[at_++]);
|
|
if (digit < 0) return false;
|
|
codepoint = (codepoint << 4) | static_cast<unsigned>(digit);
|
|
}
|
|
if (codepoint >= 0xd800 && codepoint <= 0xdfff) return false;
|
|
if (codepoint <= 0x7f) output.push_back(static_cast<char>(codepoint));
|
|
else if (codepoint <= 0x7ff) {
|
|
output.push_back(static_cast<char>(0xc0 | (codepoint >> 6)));
|
|
output.push_back(static_cast<char>(0x80 | (codepoint & 0x3f)));
|
|
} else {
|
|
output.push_back(static_cast<char>(0xe0 | (codepoint >> 12)));
|
|
output.push_back(static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
|
|
output.push_back(static_cast<char>(0x80 | (codepoint & 0x3f)));
|
|
}
|
|
break;
|
|
}
|
|
default: return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
bool parse(JsonValue& out) {
|
|
whitespace();
|
|
if (at_ >= input_.size()) return false;
|
|
if (input_[at_] == '"') {
|
|
out.kind = JsonValue::Kind::string;
|
|
return string(out.text);
|
|
}
|
|
if (literal("true")) { out.kind = JsonValue::Kind::boolean; out.boolean = true; return true; }
|
|
if (literal("false")) { out.kind = JsonValue::Kind::boolean; return true; }
|
|
if (literal("null")) { out.kind = JsonValue::Kind::null_value; return true; }
|
|
if (input_[at_] < '0' || input_[at_] > '9') return false;
|
|
out.kind = JsonValue::Kind::integer;
|
|
std::uint64_t n = 0;
|
|
while (at_ < input_.size() && input_[at_] >= '0' && input_[at_] <= '9') {
|
|
const unsigned digit = static_cast<unsigned>(input_[at_++] - '0');
|
|
if (n > (std::numeric_limits<std::uint64_t>::max() - digit) / 10) return false;
|
|
n = n * 10 + digit;
|
|
}
|
|
out.integer = n;
|
|
return true;
|
|
}
|
|
std::string_view input_;
|
|
std::size_t at_ = 0;
|
|
};
|
|
|
|
bool decode_component(std::string_view input, std::string& output) {
|
|
auto digit = [](char c) -> int {
|
|
if (c >= '0' && c <= '9') return c - '0';
|
|
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
|
|
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
|
return -1;
|
|
};
|
|
if (input.size() > 1024) return false;
|
|
for (std::size_t i = 0; i < input.size(); ++i) {
|
|
unsigned char c = static_cast<unsigned char>(input[i]);
|
|
if (c == '%') {
|
|
if (i + 2 >= input.size()) return false;
|
|
const int hi = digit(input[i + 1]), lo = digit(input[i + 2]);
|
|
if (hi < 0 || lo < 0) return false;
|
|
c = static_cast<unsigned char>((hi << 4) | lo);
|
|
i += 2;
|
|
} else if (c == '+') c = ' ';
|
|
if (c < 0x21 || c > 0x7e) return false;
|
|
output.push_back(static_cast<char>(c));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool equal_state(std::string_view a, std::string_view b) {
|
|
if (a.size() != b.size()) return false;
|
|
unsigned difference = 0;
|
|
for (std::size_t i = 0; i < a.size(); ++i)
|
|
difference |= static_cast<unsigned char>(a[i]) ^ static_cast<unsigned char>(b[i]);
|
|
return difference == 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool parse_callback_target(std::string_view target, std::string_view state,
|
|
std::string& code) {
|
|
wipe(code);
|
|
if (target.size() > 2048 || state.size() != 32) return false;
|
|
const std::size_t question = target.find('?');
|
|
if (question == std::string_view::npos || target.substr(0, question) != "/callback")
|
|
return false;
|
|
bool saw_code = false, saw_state = false;
|
|
std::string received_state, parsed_code;
|
|
WipeString wipe_state{received_state}, wipe_parsed_code{parsed_code};
|
|
std::size_t at = question + 1;
|
|
while (at < target.size()) {
|
|
const std::size_t amp = target.find('&', at);
|
|
const auto pair = target.substr(at, amp == std::string_view::npos ?
|
|
target.size() - at : amp - at);
|
|
const std::size_t equal = pair.find('=');
|
|
if (equal == std::string_view::npos) return false;
|
|
std::string key, value;
|
|
WipeString wipe_value{value};
|
|
if (!decode_component(pair.substr(0, equal), key) ||
|
|
!decode_component(pair.substr(equal + 1), value)) return false;
|
|
if (key == "code") {
|
|
if (saw_code || value.empty() || value.size() > 512) return false;
|
|
parsed_code = value; saw_code = true;
|
|
} else if (key == "st") {
|
|
if (saw_state || value.size() != 32) return false;
|
|
received_state = value; saw_state = true;
|
|
} else return false;
|
|
if (amp == std::string_view::npos) break;
|
|
at = amp + 1;
|
|
}
|
|
if (!saw_code || !saw_state || !equal_state(state, received_state)) return false;
|
|
code = parsed_code;
|
|
return true;
|
|
}
|
|
|
|
Session::~Session() { clear(); }
|
|
void Session::clear() { wipe(bearer_); }
|
|
|
|
Status Session::resume(std::string_view launcher_auth_json) {
|
|
clear();
|
|
std::map<std::string, JsonValue> fields;
|
|
WipeFields wipe_fields{fields};
|
|
if (!JsonReader(launcher_auth_json).object(fields)) return Status::invalid_response;
|
|
const auto token = fields.find("sessionToken");
|
|
if (token == fields.end() || token->second.kind != JsonValue::Kind::string ||
|
|
!plain_token(token->second.text)) return Status::auth_rejected;
|
|
bearer_ = token->second.text;
|
|
return Status::session_ready;
|
|
}
|
|
|
|
Status Session::resume_bearer(std::string_view bearer) {
|
|
clear();
|
|
if (!plain_token(bearer)) return Status::auth_rejected;
|
|
bearer_.assign(bearer);
|
|
return Status::session_ready;
|
|
}
|
|
|
|
bool Session::copy_bearer(std::string& bearer) const {
|
|
wipe(bearer);
|
|
if (!plain_token(bearer_)) return false;
|
|
bearer = bearer_;
|
|
return true;
|
|
}
|
|
|
|
Status Session::sign_in(BrowserCode& browser, HttpClient& http,
|
|
std::atomic_bool const* cancel) {
|
|
clear();
|
|
if (cancel && cancel->load(std::memory_order_acquire)) return Status::cancelled;
|
|
std::string code;
|
|
if (!browser.obtain("https://octowow.st/launcher/authorize", code)) return Status::cancelled;
|
|
if (cancel && cancel->load(std::memory_order_acquire)) { wipe(code); return Status::cancelled; }
|
|
if (!plain_token(code) || code.size() > 512) { wipe(code); return Status::invalid_response; }
|
|
std::string body;
|
|
body.reserve(code.size() + 11);
|
|
body.append("{\"code\":\"");
|
|
body.append(code);
|
|
body.append("\"}");
|
|
wipe(code);
|
|
if (cancel && cancel->load(std::memory_order_acquire)) { wipe(body); return Status::cancelled; }
|
|
HttpReply reply = http.post("/api/launcher/session", body, {});
|
|
WipeReply wipe_reply{reply};
|
|
wipe(body);
|
|
if (cancel && cancel->load(std::memory_order_acquire)) return Status::cancelled;
|
|
if (reply.status == 429) return Status::auth_rejected;
|
|
if (reply.status <= 0) return Status::network_error;
|
|
if (reply.status != 200) return Status::auth_rejected;
|
|
std::map<std::string, JsonValue> fields;
|
|
WipeFields wipe_fields{fields};
|
|
if (!JsonReader(reply.body).object(fields)) return Status::invalid_response;
|
|
const auto token = fields.find("token");
|
|
if (token == fields.end() || token->second.kind != JsonValue::Kind::string ||
|
|
!plain_token(token->second.text)) return Status::invalid_response;
|
|
if (cancel && cancel->load(std::memory_order_acquire)) return Status::cancelled;
|
|
bearer_ = token->second.text;
|
|
return Status::session_ready;
|
|
}
|
|
|
|
Route Session::select(HttpClient& http, RouteProbe& probe, Clock const& clock,
|
|
std::atomic_bool const* cancel) {
|
|
Route route{};
|
|
if (cancel && cancel->load(std::memory_order_acquire)) {
|
|
route.status = Status::cancelled; return route;
|
|
}
|
|
if (bearer_.empty()) { route.status = Status::auth_rejected; return route; }
|
|
HttpReply reply = http.post("/api/launcher/address", "{}", bearer_);
|
|
WipeReply wipe_reply{reply};
|
|
if (cancel && cancel->load(std::memory_order_acquire)) {
|
|
route.status = Status::cancelled; return route;
|
|
}
|
|
if (reply.status == 401) { clear(); route.status = Status::token_expired; return route; }
|
|
if (reply.status <= 0 || reply.status == 429 || reply.status >= 500) {
|
|
route.status = Status::network_error; return route;
|
|
}
|
|
if (reply.status != 200) { route.status = Status::auth_rejected; return route; }
|
|
std::map<std::string, JsonValue> fields;
|
|
WipeFields wipe_fields{fields};
|
|
if (!JsonReader(reply.body).object(fields)) {
|
|
route.status = Status::invalid_response; return route;
|
|
}
|
|
const auto address = fields.find("address"), eligible = fields.find("eligible"), ttl = fields.find("ttl");
|
|
if (address == fields.end() || address->second.kind != JsonValue::Kind::string ||
|
|
!((valid_host(address->second.text) && octo_host(address->second.text)) ||
|
|
public_ipv4(address->second.text)) ||
|
|
(eligible != fields.end() && eligible->second.kind != JsonValue::Kind::boolean) ||
|
|
(ttl != fields.end() && ttl->second.kind != JsonValue::Kind::integer &&
|
|
ttl->second.kind != JsonValue::Kind::null_value)) {
|
|
route.status = Status::invalid_response; return route;
|
|
}
|
|
route.eligible = eligible != fields.end() && eligible->second.boolean;
|
|
if (!route.eligible) { route.status = Status::public_fallback; return route; }
|
|
if (cancel && cancel->load(std::memory_order_acquire)) {
|
|
route.status = Status::cancelled; return route;
|
|
}
|
|
std::string numeric;
|
|
if (!probe.challenge(address->second.text, numeric) || !public_ipv4(numeric) ||
|
|
numeric.size() >= sizeof(route.ipv4)) {
|
|
route.status = Status::public_fallback; return route;
|
|
}
|
|
if (cancel && cancel->load(std::memory_order_acquire)) {
|
|
route.status = Status::cancelled; return route;
|
|
}
|
|
std::memcpy(route.ipv4, numeric.data(), numeric.size());
|
|
const std::uint64_t seconds = ttl != fields.end() && ttl->second.kind == JsonValue::Kind::integer &&
|
|
ttl->second.integer > 0 ?
|
|
std::min<std::uint64_t>(ttl->second.integer, 3600) : 3600;
|
|
route.expires_at_monotonic_ms = clock.monotonic_ms() + seconds * 1000;
|
|
route.status = Status::priority_ready;
|
|
return route;
|
|
}
|
|
|
|
} // namespace gw::priority
|