Add M10.3b session login over QUIC
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 3m13s
Foundation / checks (client) (push) Successful in 12m12s
Foundation / checks (network) (push) Successful in 8m56s
Foundation / checks (tsan) (push) Successful in 7m42s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 19m14s
Foundation / checks (checks) (push) Canceled after 31m28s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 3m13s
Foundation / checks (client) (push) Successful in 12m12s
Foundation / checks (network) (push) Successful in 8m56s
Foundation / checks (tsan) (push) Successful in 7m42s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 19m14s
Foundation / checks (checks) (push) Canceled after 31m28s
Session protocol v1 session messages (Hello with grant, Welcome, Refresh, RefreshResult, Close with reasons) on the QUIC reliable stream. RegionSessions verifies grants with the issuer public keys and admits them through SessionControl: a newer grant closes the account's old connection as superseded, expiry and epoch advances close sessions, every close is announced and flushed. RegionSessionClient logs in, refreshes in place and reports why a session ended. ADR-0091. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,266 @@
|
||||
#include "evo/server/region_sessions.hpp"
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
using network::ControlError;
|
||||
using network::SessionCloseReason;
|
||||
using network::SessionGrantError;
|
||||
using Clock = std::chrono::steady_clock;
|
||||
|
||||
SessionCloseReason reason_for(ControlError e) {
|
||||
switch (e) {
|
||||
case ControlError::wrong_environment:
|
||||
case ControlError::wrong_world:
|
||||
case ControlError::wrong_region:
|
||||
return SessionCloseReason::wrong_scope;
|
||||
case ControlError::expired:
|
||||
return SessionCloseReason::expired;
|
||||
case ControlError::stale_grant:
|
||||
return SessionCloseReason::stale_grant;
|
||||
case ControlError::rate_limited:
|
||||
return SessionCloseReason::rate_limited;
|
||||
case ControlError::sessions_full:
|
||||
case ControlError::accounts_full:
|
||||
return SessionCloseReason::capacity;
|
||||
default:
|
||||
return SessionCloseReason::unauthenticated;
|
||||
}
|
||||
}
|
||||
SessionCloseReason reason_for(SessionGrantError e) {
|
||||
return e == SessionGrantError::wrong_environment ? SessionCloseReason::wrong_scope
|
||||
: SessionCloseReason::unauthenticated;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
struct RegionSessions::Impl {
|
||||
enum class State : std::uint8_t { hello, established, closing };
|
||||
struct Connection {
|
||||
network::quic::ConnectionId id{};
|
||||
State state{State::hello};
|
||||
std::uint64_t session{};
|
||||
std::string account;
|
||||
Clock::time_point deadline;
|
||||
bool close_requested{};
|
||||
};
|
||||
|
||||
Impl(RegionSessionsConfig c, std::unique_ptr<network::quic::Endpoint> e,
|
||||
network::GrantVerifier v, network::SessionControl s)
|
||||
: config{std::move(c)}, endpoint{std::move(e)}, verifier{std::move(v)},
|
||||
control{std::move(s)} {}
|
||||
|
||||
RegionSessionsConfig config;
|
||||
std::unique_ptr<network::quic::Endpoint> endpoint;
|
||||
network::GrantVerifier verifier;
|
||||
network::SessionControl control;
|
||||
std::vector<Connection> connections;
|
||||
std::vector<RegionSessionEvent> events;
|
||||
|
||||
Connection* find(network::quic::ConnectionId id) {
|
||||
const auto it =
|
||||
std::ranges::find_if(connections, [&](const Connection& c) { return c.id == id; });
|
||||
return it == connections.end() ? nullptr : &*it;
|
||||
}
|
||||
Connection* by_session(std::uint64_t session) {
|
||||
const auto it = std::ranges::find_if(connections, [&](const Connection& c) {
|
||||
return c.session == session && c.state == State::established;
|
||||
});
|
||||
return it == connections.end() ? nullptr : &*it;
|
||||
}
|
||||
void send(const Connection& c, const network::SessionMessage& m) {
|
||||
// A refused send (budget) leaves the peer to observe the transport close instead.
|
||||
(void)endpoint->send_message(c.id, network::encode_session(m)).has_value();
|
||||
}
|
||||
// Sends Close, releases control, reports, and closes after the flush deadline.
|
||||
void close(Connection& c, SessionCloseReason reason, Clock::time_point now) {
|
||||
if (c.state == State::closing)
|
||||
return;
|
||||
send(c, network::SessionClose{reason});
|
||||
RegionSessionEvent event;
|
||||
event.kind = c.state == State::established ? RegionSessionEvent::Kind::closed
|
||||
: RegionSessionEvent::Kind::refused;
|
||||
event.session = c.session;
|
||||
event.account = c.account;
|
||||
event.reason = reason;
|
||||
events.push_back(std::move(event));
|
||||
if (c.session != 0) // May already be released (superseded, expired, epoch).
|
||||
(void)control.close(c.session).has_value();
|
||||
c.session = 0;
|
||||
c.state = State::closing;
|
||||
c.deadline = now + config.close_flush;
|
||||
}
|
||||
|
||||
void hello(Connection& c, const network::SessionHello& m, std::uint64_t now_seconds,
|
||||
Clock::time_point now) {
|
||||
if (m.required_features != 0)
|
||||
return close(c, SessionCloseReason::protocol, now);
|
||||
if (!control.attempt(now_seconds)) // Region-wide, before signature work.
|
||||
return close(c, SessionCloseReason::rate_limited, now);
|
||||
const auto grant = verifier.verify(m.grant);
|
||||
if (!grant)
|
||||
return close(c, reason_for(grant.error()), now);
|
||||
c.account = network::log_label(grant->claims().account);
|
||||
const auto opened = control.open(*grant, now_seconds);
|
||||
if (!opened)
|
||||
return close(c, reason_for(opened.error()), now);
|
||||
if (opened->superseded != 0)
|
||||
if (auto* old = by_session(opened->superseded))
|
||||
close(*old, SessionCloseReason::superseded, now);
|
||||
c.state = State::established;
|
||||
c.session = opened->session;
|
||||
network::SessionWelcome welcome;
|
||||
welcome.session = opened->session;
|
||||
welcome.epoch = opened->epoch;
|
||||
welcome.world = config.control.world.world;
|
||||
welcome.frame = config.control.world.frame;
|
||||
welcome.region = config.control.region;
|
||||
welcome.expires_at = opened->expires_at;
|
||||
welcome.server_seconds = now_seconds;
|
||||
send(c, welcome);
|
||||
events.push_back({RegionSessionEvent::Kind::opened, c.session, c.account, {}});
|
||||
}
|
||||
|
||||
void refresh(Connection& c, const network::SessionRefresh& m, std::uint64_t now_seconds) {
|
||||
network::SessionRefreshResult result;
|
||||
if (!control.attempt(now_seconds)) {
|
||||
result.status = static_cast<std::uint8_t>(SessionCloseReason::rate_limited);
|
||||
} else if (const auto grant = verifier.verify(m.grant); !grant) {
|
||||
result.status = static_cast<std::uint8_t>(reason_for(grant.error()));
|
||||
} else if (const auto renewed = control.refresh(c.session, *grant, now_seconds); !renewed) {
|
||||
result.status = static_cast<std::uint8_t>(renewed.error() == ControlError::wrong_account
|
||||
? SessionCloseReason::unauthenticated
|
||||
: reason_for(renewed.error()));
|
||||
} else {
|
||||
result.expires_at = *renewed;
|
||||
}
|
||||
send(c, result);
|
||||
}
|
||||
|
||||
void message(Connection& c, std::span<const std::uint8_t> bytes, std::uint64_t now_seconds,
|
||||
Clock::time_point now) {
|
||||
if (c.state == State::closing)
|
||||
return;
|
||||
const auto decoded = network::decode_session(bytes);
|
||||
if (!decoded)
|
||||
return close(c,
|
||||
decoded.error() == network::SessionCodecError::unsupported_version
|
||||
? SessionCloseReason::unsupported_version
|
||||
: SessionCloseReason::protocol,
|
||||
now);
|
||||
if (c.state == State::hello) {
|
||||
if (const auto* m = std::get_if<network::SessionHello>(&*decoded))
|
||||
return hello(c, *m, now_seconds, now);
|
||||
return close(c, SessionCloseReason::protocol, now);
|
||||
}
|
||||
if (const auto* m = std::get_if<network::SessionRefresh>(&*decoded))
|
||||
return refresh(c, *m, now_seconds);
|
||||
if (const auto* m = std::get_if<network::SessionClose>(&*decoded);
|
||||
m && m->reason == SessionCloseReason::client_closed) {
|
||||
close(c, SessionCloseReason::client_closed, now);
|
||||
c.deadline = now; // Nothing left to flush to a peer that is leaving.
|
||||
return;
|
||||
}
|
||||
close(c, SessionCloseReason::protocol, now);
|
||||
}
|
||||
};
|
||||
|
||||
RegionSessions::RegionSessions(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
|
||||
RegionSessions::~RegionSessions() = default;
|
||||
|
||||
std::expected<std::unique_ptr<RegionSessions>, RegionSessionsError>
|
||||
RegionSessions::create(RegionSessionsConfig config) {
|
||||
// Connections beyond sessions are headroom for logins and closes still flushing.
|
||||
if (config.issuer_keys.empty() ||
|
||||
config.transport.limits.max_connections < config.control.max_sessions ||
|
||||
config.hello_deadline.count() <= 0 || config.close_flush.count() <= 0)
|
||||
return std::unexpected(RegionSessionsError::configuration);
|
||||
auto control = network::SessionControl::create(config.control);
|
||||
if (!control)
|
||||
return std::unexpected(RegionSessionsError::configuration);
|
||||
auto verifier = network::GrantVerifier::create(config.control.environment);
|
||||
if (!verifier)
|
||||
return std::unexpected(RegionSessionsError::keys);
|
||||
for (const auto& k : config.issuer_keys)
|
||||
if (!verifier->add_key(k.key_id, k.public_key))
|
||||
return std::unexpected(RegionSessionsError::keys);
|
||||
auto endpoint = network::quic::Endpoint::serve(config.transport);
|
||||
if (!endpoint)
|
||||
return std::unexpected(RegionSessionsError::transport);
|
||||
return std::unique_ptr<RegionSessions>{new RegionSessions{std::make_unique<Impl>(
|
||||
std::move(config), std::move(*endpoint), std::move(*verifier), std::move(*control))}};
|
||||
}
|
||||
|
||||
void RegionSessions::poll(std::uint64_t epoch, std::uint64_t now_seconds, Clock::time_point now) {
|
||||
auto& m = *impl_;
|
||||
if (epoch > m.control.epoch()) {
|
||||
// Every session ends with the epoch (ADR-0089); control has released them.
|
||||
const auto closed = m.control.advance_epoch(epoch);
|
||||
if (closed)
|
||||
for (const auto session : *closed)
|
||||
if (auto* c = m.by_session(session))
|
||||
m.close(*c, SessionCloseReason::stale_epoch, now);
|
||||
}
|
||||
for (auto& e : m.endpoint->poll()) {
|
||||
using network::quic::EventKind;
|
||||
if (e.kind == EventKind::connected) {
|
||||
m.connections.push_back(
|
||||
{e.connection, Impl::State::hello, 0, {}, now + m.config.hello_deadline, false});
|
||||
} else if (e.kind == EventKind::message) {
|
||||
if (auto* c = m.find(e.connection))
|
||||
m.message(*c, e.bytes, now_seconds, now);
|
||||
} else if (e.kind == EventKind::closed) {
|
||||
const auto it = std::ranges::find_if(
|
||||
m.connections, [&](const Impl::Connection& c) { return c.id == e.connection; });
|
||||
if (it == m.connections.end())
|
||||
continue;
|
||||
if (it->state == Impl::State::established) {
|
||||
(void)m.control.close(it->session).has_value();
|
||||
// The client closes its connection right after sending Close, which may be
|
||||
// discarded with it: a peer close is the client leaving, anything else is loss.
|
||||
m.events.push_back({RegionSessionEvent::Kind::closed, it->session, it->account,
|
||||
e.cause == network::quic::CloseCause::peer
|
||||
? SessionCloseReason::client_closed
|
||||
: SessionCloseReason::connection_lost});
|
||||
}
|
||||
m.connections.erase(it);
|
||||
} // Datagrams carry nothing before 10.4.
|
||||
}
|
||||
for (const auto session : m.control.expire(now_seconds))
|
||||
if (auto* c = m.by_session(session))
|
||||
m.close(*c, SessionCloseReason::expired, now);
|
||||
for (auto& c : m.connections) {
|
||||
if (c.state == Impl::State::hello && now >= c.deadline)
|
||||
m.close(c, SessionCloseReason::timeout, now);
|
||||
if (c.state == Impl::State::closing && now >= c.deadline && !c.close_requested) {
|
||||
c.close_requested = true;
|
||||
m.endpoint->close(c.id); // Its closed event removes it.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RegionSessions::shutdown(Clock::time_point now) {
|
||||
for (auto& c : impl_->connections)
|
||||
impl_->close(c, SessionCloseReason::shutdown, now);
|
||||
}
|
||||
|
||||
std::vector<RegionSessionEvent> RegionSessions::take_events() {
|
||||
return std::exchange(impl_->events, {});
|
||||
}
|
||||
|
||||
std::optional<std::uint64_t> RegionSessions::holder(const network::AccountId& account) const {
|
||||
return impl_->control.holder(account);
|
||||
}
|
||||
|
||||
std::size_t RegionSessions::sessions() const noexcept {
|
||||
return impl_->control.sessions();
|
||||
}
|
||||
|
||||
std::size_t RegionSessions::connections() const noexcept {
|
||||
return impl_->connections.size();
|
||||
}
|
||||
|
||||
std::uint16_t RegionSessions::port() const noexcept {
|
||||
return impl_->endpoint->port();
|
||||
}
|
||||
} // namespace evo::server
|
||||
Reference in New Issue
Block a user