Files
aevolution/apps/evo-session-probe/main.cpp
T
dingoandClaude Opus 5.5 27d2645aaa
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (network) (push) Successful in 9m46s
Foundation / checks (client) (push) Successful in 13m6s
Foundation / checks (tsan) (push) Successful in 8m2s
Foundation / checks (checks) (push) Successful in 32m25s
Foundation / checks (postgres) (push) Successful in 38m45s
Foundation / postgres-tools (push) Successful in 16s
Foundation / image (push) Successful in 2m34s
Foundation / server-image (push) Successful in 4m2s
Probe: resolve host names and report the QUIC close cause
evo-session-probe resolves a host name to IPv4 and prints the address DNS answered,
and its closed line names why the QUIC connection ended (untrusted: the server
answered but failed the pinned check; idle: nothing answered), via the new
RegionSessionClient::transport_cause. Records the UDP route reachability check
(not yet the authenticated acceptance run).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 18:31:19 -04:00

355 lines
14 KiB
C++

// M10.4c headless session client (ADR-0091, ADR-0092). Development and test only.
//
// evo-session-probe keys --out DIR
// Writes a fresh dev issuer key pair: DIR/issuer.key (32-byte P-256 scalar, owner-only)
// and DIR/issuer.pub (65-byte public key for evo-server --m10-issuer).
// evo-session-probe run --connect HOST:PORT --server-name NAME --ca FILE --issuer-key FILE
// --world N --frame N --region N --account HEX32 --serial N [--lifetime S]
// [--intents N] [--actions N --action-base ID] [--query ID]... [--timeout-ms N]
// Signs a dev grant for the account and serial, logs in over QUIC, sends N intentions
// (one per tick, two ticks ahead) and N probe actions (three ticks ahead), queries the
// given action ids, and prints one line per answer:
// welcome SESSION EPOCH TICK | intention SEQ STATUS | simulated SEQ TICK
// receipt ID STATUS TICK | outcome ID RESULT TICK COMMIT | done
// resolved HOST ADDRESS (a host name was looked up; IPv4)
// closed REASON TRANSPORT (TRANSPORT names why QUIC ended: untrusted means the server
// answered but failed the pinned CA/name check; idle means nothing answered)
// Statuses, results and reasons are the session-v1 wire values. Exits 0 once every
// action is final and every query answered, 1 on timeout, 3 when the server closes.
// The issuer key is a development credential: the probe refuses any environment but dev.
#include "evo/client/region_session.hpp"
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <winsock2.h>
// After <winsock2.h>.
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include "evo/network/session_grant.hpp"
#include <algorithm>
#include <array>
#include <charconv>
#include <chrono>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <thread>
#include <vector>
namespace {
using namespace evo;
using namespace evo::network;
using Clock = std::chrono::steady_clock;
std::optional<std::uint64_t> number(std::string_view v) {
std::uint64_t n = 0;
const auto [end, ec] = std::from_chars(v.data(), v.data() + v.size(), n);
if (ec != std::errc{} || end != v.data() + v.size())
return std::nullopt;
return n;
}
std::string read(const std::filesystem::path& path) {
std::ifstream file{path, std::ios::binary};
return {std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
}
bool write(const std::filesystem::path& path, std::span<const std::uint8_t> bytes) {
std::ofstream file{path, std::ios::binary | std::ios::trunc};
file.write(reinterpret_cast<const char*>(bytes.data()),
static_cast<std::streamsize>(bytes.size()));
return file.good();
}
std::optional<AccountId> account_of(std::string_view hex) {
if (hex.size() != 32)
return std::nullopt;
AccountId id;
for (std::size_t i = 0; i < 16; ++i) {
unsigned value = 0;
const auto [end, ec] =
std::from_chars(hex.data() + (2 * i), hex.data() + (2 * i) + 2, value, 16);
if (ec != std::errc{} || end != hex.data() + (2 * i) + 2)
return std::nullopt;
id.bytes.at(i) = static_cast<std::uint8_t>(value);
}
return id.valid() ? std::optional{id} : std::nullopt;
}
// The QUIC endpoint takes a numeric address (the server name is verified separately):
// look a host name up once, IPv4, and print what DNS answered.
std::optional<std::string> resolve(const std::string& host) {
#ifdef _WIN32
WSADATA data{};
if (WSAStartup(MAKEWORD(2, 2), &data) != 0)
return std::nullopt;
#endif
addrinfo hints{};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
addrinfo* found = nullptr;
if (getaddrinfo(host.c_str(), nullptr, &hints, &found) != 0 || found == nullptr)
return std::nullopt;
char text[INET_ADDRSTRLEN] = {};
const auto* address = reinterpret_cast<const sockaddr_in*>(found->ai_addr);
const bool ok = inet_ntop(AF_INET, &address->sin_addr, text, sizeof text) != nullptr;
freeaddrinfo(found);
if (!ok)
return std::nullopt;
return std::string{text};
}
const char* cause_name(std::optional<quic::CloseCause> c) {
if (!c)
return "none";
switch (*c) {
case quic::CloseCause::local:
return "local";
case quic::CloseCause::peer:
return "peer";
case quic::CloseCause::idle:
return "idle";
case quic::CloseCause::untrusted:
return "untrusted";
case quic::CloseCause::overloaded:
return "overloaded";
case quic::CloseCause::protocol:
return "protocol";
default:
return "transport";
}
}
int keys(const std::filesystem::path& out) {
std::filesystem::create_directories(out);
for (int attempt = 0; attempt < 8; ++attempt) {
std::array<std::uint8_t, grant_private_key_bytes> scalar{};
if (!random_bytes(scalar))
return 1;
auto key = GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar);
if (!key)
continue; // Out of the curve's range: draw again.
const auto pub = key->public_key();
if (!pub || !write(out / "issuer.key", scalar) || !write(out / "issuer.pub", *pub))
return 1;
std::filesystem::permissions(out / "issuer.key",
std::filesystem::perms::owner_read |
std::filesystem::perms::owner_write,
std::filesystem::perm_options::replace);
std::puts("keys written");
return 0;
}
return 1;
}
struct RunOptions {
std::string host, server_name, ca, issuer_key;
std::uint16_t port{};
std::uint64_t world{}, frame{}, region{}, serial{}, lifetime{60}, intents{}, actions{},
action_base{1}, timeout_ms{20000};
std::optional<AccountId> account;
std::vector<std::uint64_t> queries;
};
int run(const RunOptions& o) {
const auto scalar = read(o.issuer_key);
const auto key = GrantSigningKey::import(
DeploymentEnvironment::dev, 1,
std::span{reinterpret_cast<const std::uint8_t*>(scalar.data()), scalar.size()});
if (!key) {
std::puts("error issuer-key");
return 2;
}
GrantClaims claims;
std::array<std::uint8_t, 8> id{};
if (!random_bytes(id)) {
std::puts("error random");
return 2;
}
for (std::size_t i = 0; i < id.size(); ++i)
claims.grant_id |= static_cast<std::uint64_t>(id.at(i)) << (8U * i);
claims.grant_id = std::max<std::uint64_t>(claims.grant_id, 1);
if (!o.account)
return 2; // Required at parse time.
claims.account = *o.account;
claims.serial = o.serial;
claims.world = {o.world, o.frame};
claims.region = o.region;
claims.issued_at =
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
claims.expires_at = claims.issued_at + o.lifetime;
const auto grant = key->sign(claims);
if (!grant) {
std::puts("error grant");
return 2;
}
client::RegionSessionClientConfig config;
const auto address = resolve(o.host);
if (!address) {
std::puts("error resolve");
return 2;
}
if (*address != o.host)
std::printf("resolved %s %s\n", o.host.c_str(), address->c_str());
config.transport.server_address = *address;
config.transport.port = o.port;
config.transport.server_name = o.server_name;
config.transport.authority_pem = read(o.ca);
config.transport.authority_pem_file = o.ca;
config.grant = *grant;
auto made = client::RegionSessionClient::connect(std::move(config), Clock::now());
if (!made) {
std::puts("error connect");
return 2;
}
auto& c = **made;
const auto deadline = Clock::now() + std::chrono::milliseconds{o.timeout_ms};
bool welcomed = false, queried = false;
std::uint64_t sent_intents = 0, sent_actions = 0, last_tick = 0, simulated = 0;
std::set<std::uint64_t> pending_actions, pending_queries(o.queries.begin(), o.queries.end());
for (std::uint64_t i = 0; i < o.actions; ++i)
pending_actions.insert(o.action_base + i);
while (Clock::now() < deadline) {
c.poll(Clock::now());
if (c.state() == client::RegionSessionState::closed) {
std::printf("closed %u %s\n",
static_cast<unsigned>(
c.close_reason().value_or(SessionCloseReason::connection_lost)),
cause_name(c.transport_cause()));
return 3;
}
if (c.state() != client::RegionSessionState::established) {
std::this_thread::sleep_for(std::chrono::milliseconds{2});
continue;
}
if (!welcomed) {
welcomed = true;
const auto& w = *c.welcome();
std::printf("welcome %llu %llu %llu\n", static_cast<unsigned long long>(w.session),
static_cast<unsigned long long>(w.epoch),
static_cast<unsigned long long>(w.server_tick));
}
const auto tick = c.server_tick();
if (tick != last_tick) {
last_tick = tick;
if (sent_intents < o.intents && c.send_intention(tick + 2, 100, 0))
++sent_intents;
if (sent_actions < o.actions &&
c.send_action(o.action_base + sent_actions, ActionKind::probe, tick + 3))
++sent_actions;
}
if (!queried) {
queried = true;
for (const auto query : o.queries)
(void)c.query_action(query);
}
for (const auto& r : c.take_intention_receipts())
std::printf("intention %llu %u\n", static_cast<unsigned long long>(r.sequence),
static_cast<unsigned>(r.status));
if (const auto s = c.simulated(); s && s->sequence != simulated) {
simulated = s->sequence;
std::printf("simulated %llu %llu\n", static_cast<unsigned long long>(s->sequence),
static_cast<unsigned long long>(s->tick));
}
for (const auto& r : c.take_action_receipts())
std::printf("receipt %llu %u %llu\n", static_cast<unsigned long long>(r.action_id),
static_cast<unsigned>(r.status), static_cast<unsigned long long>(r.tick));
for (const auto& out : c.take_action_outcomes()) {
std::printf(
"outcome %llu %u %llu %llu\n", static_cast<unsigned long long>(out.action_id),
static_cast<unsigned>(out.result), static_cast<unsigned long long>(out.tick),
static_cast<unsigned long long>(out.commit_sequence));
pending_queries.erase(out.action_id);
if (out.result == ActionResult::committed_no_effect ||
out.result == ActionResult::not_committed)
pending_actions.erase(out.action_id);
}
std::fflush(stdout);
if (sent_actions == o.actions && pending_actions.empty() && pending_queries.empty() &&
sent_intents == o.intents && simulated >= std::min<std::uint64_t>(o.intents, 1)) {
std::puts("done");
c.close();
return 0;
}
std::this_thread::sleep_for(std::chrono::milliseconds{2});
}
std::puts("timeout");
return 1;
}
} // namespace
int main(int argc, char** argv) {
const std::vector<std::string_view> args(argv + 1, argv + argc);
if (args.size() == 3 && args[0] == "keys" && args[1] == "--out")
return keys(std::filesystem::path{std::string{args[2]}});
RunOptions o;
bool ok = !args.empty() && args[0] == "run" && args.size() % 2 == 1;
for (std::size_t i = 1; ok && i + 1 < args.size(); i += 2) {
const auto flag = args[i];
const auto value = args[i + 1];
const auto n = number(value);
if (flag == "--connect") {
const auto colon = value.rfind(':');
const auto port =
colon == std::string_view::npos ? std::nullopt : number(value.substr(colon + 1));
ok = port && *port > 0 && *port <= 65535;
if (ok) {
o.host = std::string{value.substr(0, colon)};
o.port = static_cast<std::uint16_t>(*port);
}
} else if (flag == "--server-name")
o.server_name = value;
else if (flag == "--ca")
o.ca = value;
else if (flag == "--issuer-key")
o.issuer_key = value;
else if (flag == "--account")
ok = (o.account = account_of(value)).has_value();
else if (flag == "--query" && n)
o.queries.push_back(*n);
else if (n && flag == "--world")
o.world = *n;
else if (n && flag == "--frame")
o.frame = *n;
else if (n && flag == "--region")
o.region = *n;
else if (n && flag == "--serial")
o.serial = *n;
else if (n && flag == "--lifetime" && *n > 0 && *n <= 3600)
o.lifetime = *n;
else if (n && flag == "--intents" && *n <= 1000)
o.intents = *n;
else if (n && flag == "--actions" && *n <= 1000)
o.actions = *n;
else if (n && flag == "--action-base" && *n > 0)
o.action_base = *n;
else if (n && flag == "--timeout-ms" && *n > 0)
o.timeout_ms = *n;
else
ok = false;
}
if (!ok || o.host.empty() || o.server_name.empty() || o.ca.empty() || o.issuer_key.empty() ||
!o.account || o.world == 0 || o.frame == 0 || o.region == 0 || o.serial == 0) {
std::fputs("usage: evo-session-probe keys --out DIR | run --connect HOST:PORT "
"--server-name NAME --ca FILE --issuer-key FILE --world N --frame N "
"--region N --account HEX32 --serial N [--lifetime S] [--intents N] "
"[--actions N --action-base ID] [--query ID]... [--timeout-ms N]\n",
stderr);
return 2;
}
return run(o);
}