feat(capture): replay bundles against a sandbox stack with a headless client

CaptureTool (built next to the servers) replays packet bundles and compares the answers:

- replay: per bundle a fresh sandbox folder with copied server binaries, rewritten settings
  (replay_sandbox=1, a new SQLite file inside the folder, ports from --port on, no dashboard),
  read back before anything starts; sandbox-setup runs inside it to apply migrations and make
  the replay account and the bundle's characters (setup mode); master is started, the stack is
  stopped as one process group and the folder deleted unless kept
- with replay_sandbox=1 every server refuses a database that isn't SQLite, isn't inside its
  own folder, or is replay_live_sqlite_path (Database::Connect); replay-target against a
  running server needs --i-know-this-is-not-a-sandbox
- the fake client splits the recording into connections, logs in and picks the character
  itself when the recording doesn't, fills in the target's account, session key and IDs,
  learns server-made object IDs from replica constructions by LOT, follows the recorded
  timing and waits for the answers a client waits for; the diff pairs answers by name (and
  constructions by LOT) and ignores fields that differ between runs
- import-live converts the 2014 live captures (folders of *_traffic.zip; pcaps and encrypted
  captures are left alone) into bundles, with secrets removed and CREATE_CHARACTER as setup
- anonymise makes local fixtures; docs/CaptureReplay.md describes capture, the bundle format,
  portability rules, the sandbox and the replay

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-27 09:25:37 -05:00
parent 88a97b893e
commit 3a8838d463
17 changed files with 1885 additions and 13 deletions

View File

@@ -60,6 +60,13 @@ namespace {
std::string NameOf(const Record& record) {
return PacketDecoder::Decode(record.bytes, CaptureTools::FromClient(record.header)).name;
}
// How answers pair up in a diff: by name, and constructions by what they construct
std::string PairKey(const Record& record) {
const auto decoded = PacketDecoder::Decode(record.bytes, CaptureTools::FromClient(record.header));
if (decoded.name == "ID_REPLICA_MANAGER_CONSTRUCTION" && decoded.fields) return decoded.name + " LOT " + std::to_string((*decoded.fields)["lot"].get<int32_t>());
return decoded.name;
}
}
namespace CaptureTools {
@@ -85,7 +92,7 @@ namespace CaptureTools {
"timestamp", "stamps", "instanceID", "instanceId", "zoneInstance", "cloneID", "zoneClone", "serverIP", "serverPort",
"worldServerIP", "worldServerPort", "processID", "port",
// Per account on each server
"playerID", "targetID", "senderID", "username",
"playerID", "targetID", "senderID", "username", "networkID",
};
return fields.contains(name);
}
@@ -207,25 +214,25 @@ namespace CaptureTools {
};
const auto want = answers(expected), got = answers(actual);
report.expected = want.size();
std::vector<std::string> gotNames;
gotNames.reserve(got.size());
for (const auto* g : got) gotNames.push_back(PairKey(*g));
// Each recorded answer pairs with the next unpaired replayed answer of the same name (order kept per name)
std::map<std::string, std::vector<size_t>> byName;
for (size_t i = 0; i < got.size(); i++) byName[gotNames[i]].push_back(i);
std::map<std::string, size_t> next;
std::vector<bool> used(got.size());
size_t from = 0;
for (const auto* w : want) {
const auto name = NameOf(*w);
// The next unused answer with the same name, looking a little ahead so one missing packet doesn't shift everything
size_t found = got.size();
for (size_t i = from; i < got.size() && i < from + 200; i++) {
if (!used[i] && NameOf(*got[i]) == name) {
found = i;
break;
}
}
const auto name = PairKey(*w);
const auto& candidates = byName[name];
auto& at = next[name];
const size_t found = at < candidates.size() ? candidates[at++] : got.size();
if (found == got.size()) {
report.missing++;
report.missingByName[name]++;
continue;
}
used[found] = true;
while (from < used.size() && used[from]) from++;
auto a = PacketDecoder::Decode(w->bytes, false).fields.value_or(json());
auto b = PacketDecoder::Decode(got[found]->bytes, false).fields.value_or(json());
Strip(a);
@@ -246,7 +253,7 @@ namespace CaptureTools {
for (size_t i = 0; i < got.size(); i++) {
if (used[i]) continue;
report.extra++;
report.extraByName[NameOf(*got[i])]++;
report.extraByName[gotNames[i]]++;
}
return report;
}

View File

@@ -184,6 +184,11 @@ namespace {
j = { {"selectedCharacterIndex", p.selectedCharacterIndex}, {"characters", characters} };
}),
nullptr, Scrub<ClientPackets::CharacterListResponse>(nullptr, [](auto& p) { for (auto& c : p.characters) { X(c.name); X(c.unapprovedName); } }) } },
{ K(S::CLIENT, MessageType::Client::CREATE_CHARACTER), { Make<ClientPackets::CreateCharacter>([](const auto& p, json& j) {
j = { {"objectID", Id(p.objectID)}, {"templateID", p.templateID}, {"name", GeneralUtils::UTF16ToWTF8(p.name)}, {"gmLevel", static_cast<int>(p.gmLevel)},
{"reputation", p.reputation}, {"propertyCloneID", p.propertyCloneID}, {"xmlBytes", p.xmlData.size()} };
}),
nullptr, Scrub<ClientPackets::CreateCharacter>(nullptr, [](auto& p) { X(p.name); }) } },
{ K(S::CLIENT, MessageType::Client::CHARACTER_CREATE_RESPONSE), { Make<ClientPackets::CharacterCreateResponse>([](const auto& p, json& j) {
j = { {"response", static_cast<int>(p.response)} };
}) } },
@@ -257,6 +262,7 @@ namespace {
{ K(S::CLIENT, MessageType::Client::LOAD_STATIC_ZONE), Rewrite<ClientPackets::LoadStaticZone>() },
{ K(S::CLIENT, MessageType::Client::CHARACTER_LIST_RESPONSE), Rewrite<ClientPackets::CharacterListResponse>() },
{ K(S::CLIENT, MessageType::Client::CHARACTER_CREATE_RESPONSE), Rewrite<ClientPackets::CharacterCreateResponse>() },
{ K(S::CLIENT, MessageType::Client::CREATE_CHARACTER), Rewrite<ClientPackets::CreateCharacter>() },
{ K(S::CLIENT, MessageType::Client::TRANSFER_TO_WORLD), Rewrite<ClientPackets::TransferToWorld>() },
{ K(S::CHAT, MessageType::Chat::GENERAL_CHAT_MESSAGE), Rewrite<ChatPackets::GeneralChatMessage>() },
{ K(S::CHAT, MessageType::Chat::PRIVATE_CHAT_MESSAGE), Rewrite<ChatPackets::PrivateChatMessage>() },
@@ -328,6 +334,19 @@ namespace PacketDecoder {
out.service = "RAKNET";
out.messageId = static_cast<uint8_t>(bytes[0]);
out.name = RakNetName(static_cast<uint8_t>(bytes[0]));
// A construction starts with the object and its LOT: [ID][bit][u16 network ID][i64 object][i32 LOT]
if (out.messageId == ID_REPLICA_MANAGER_CONSTRUCTION) {
RakNet::BitStream stream(reinterpret_cast<unsigned char*>(const_cast<char*>(bytes.data())), static_cast<unsigned int>(bytes.size()), false);
stream.IgnoreBytes(1);
bool flag{};
uint16_t network{};
LWOOBJID object{};
int32_t lot{};
if (stream.Read(flag) && stream.Read(network) && stream.Read(object) && stream.Read(lot)) {
out.objectId = object;
out.fields = json{ {"networkID", network}, {"objectID", Id(object)}, {"lot", lot} };
}
}
return out;
}
out.lu = true;