Files
praxisandClaude Fable 5.2 8e02dccb86 In-client stack: transport, session lifecycle and engine bridge
175 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

131 lines
9.4 KiB
C++

/* Wine-only opt-in laboratory loader. No socket hooks, security changes,
* external gateway or spawned codec process. The optional PE engine probe is
* isolated from this worker and explicitly preserves real login results.
* Backend evidence alone is deliberately labelled NOT_ENGINE_WORLD_ENTRY. */
#include "session.h"
#include "engine_bridge.hpp"
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <chrono>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <memory>
#include <string>
#include <thread>
#include <unistd.h>
namespace {
namespace fs=std::filesystem;
std::string read(const fs::path& p,size_t bound){std::ifstream f(p,std::ios::binary);std::string s;char c;while(f.get(c)){if(s.size()==bound)throw 1;s+=c;}if(!f.eof())throw 1;return s;}
void run(std::string config){
try{
// Wine sets the actual PE process name after the ELF loader constructor.
bool ready=false;for(unsigned i=0;i<150;++i){if(read("/proc/self/comm",128)=="WowB.exe\n"){ready=true;break;}std::this_thread::sleep_for(std::chrono::milliseconds(100));}
if(!ready)return;
boost::property_tree::ptree options;std::istringstream input(read(config,4096));boost::property_tree::read_json(input,options);
auto dir=fs::canonical(options.get<std::string>("account_directory"));
auto output=fs::path(options.get<std::string>("evidence"));
auto root=fs::canonical(options.get<std::string>("workspace"));
if(!dir.string().starts_with((root/"secrets").string()+"/") || !output.parent_path().string().starts_with((root/"runtime").string()))return;
if(options.get<bool>("native_registration",false))std::thread(gateway::engine::watch_registration,root).detach();
else if(options.get<bool>("engine_bridge_probe",false) && !options.get<bool>("engine_registration_trace",false))std::thread(gateway::engine::prepare_probe,root).detach();
boost::property_tree::ptree account;std::istringstream account_input(read(dir/"account.json",4096));boost::property_tree::read_json(account_input,account);
auto username=account.get<std::string>("username"),password=(dir/"password.txt").string(),hash=(dir/"version-hash.bin").string(),addons=(dir/"addon-tail.bin").string();
gw_native_session* session=nullptr;
boost::property_tree::ptree report;report.put("pid",getpid());report.put("host_comm","WowB.exe");report.put("engine_world_entry",false);report.put("external_gateway_used",false);
int result=-1;
std::unique_ptr<gw_native_login,decltype(&gw_native_login_close)> owned_login(nullptr,gw_native_login_close);
auto write_report=[&]{
std::ostringstream public_output;boost::property_tree::write_json(public_output,report);
gateway::engine::backend_report(public_output.str());
auto temporary=output;temporary+=".new";
{std::ofstream f(temporary);f<<public_output.str();if(!f)throw 1;}
fs::permissions(temporary,fs::perms::owner_read|fs::perms::owner_write);
fs::rename(temporary,output);
};
uint32_t selected_wire_id=0;
if(options.get<bool>("discover_realms",false)){
gw_native_auth_config c{GW_NATIVE_ABI,3724,username.c_str(),password.c_str(),hash.c_str()};
gw_native_login* login=nullptr;result=gw_native_authenticate(&c,&login);report.put("backend_auth_result",result);
owned_login.reset(login);
if(!result){
result=gw_native_discover(login);report.put("backend_discovery_result",result);
if(!result){
auto count=gw_native_realm_count(login);report.put("realm_count",count);
boost::property_tree::ptree realms;
int selected=-1;unsigned available=0;
// No hardcoded realm name, ID, host or world port. If more
// than one local online realm exists, wait for explicit
// host selection rather than silently selecting the first.
auto preferred=options.get<std::string>("realm_name","");
for(uint32_t i=0;i<count;++i){
gw_native_realm realm{};if(gw_native_realm_at(login,i,&realm))continue;
boost::property_tree::ptree row;row.put("index",i);row.put("name",realm.name);row.put("address",realm.address);
row.put("type",realm.type);row.put("flags",realm.flags);row.put("population",realm.population);row.put("characters",realm.characters);row.put("category",realm.category);row.put("wire_id",realm.wire_id);realms.push_back({"",row});
if(!(realm.flags&2) && std::string_view(realm.address).starts_with("127.0.0.1:")){
++available;if(preferred.empty() || preferred==realm.name)selected=int(i);
}
}
report.add_child("realms",realms);
if((preferred.empty() && available!=1) || selected<0){result=-5;report.put("selection_required",true);}
else{gw_native_realm realm{};if(!gw_native_realm_at(login,uint32_t(selected),&realm))selected_wire_id=realm.wire_id;
result=gw_native_select(login,uint32_t(selected),addons.c_str(),5875,&session);report.put("selected_realm_index",selected);report.put("backend_world_auth_result",result);}
}
}
}else{
gw_native_config c{GW_NATIVE_ABI,3724,8090,5875,username.c_str(),password.c_str(),hash.c_str(),addons.c_str()};
result=gw_native_open(&c,&session);report.put("backend_auth_result",result);
}
std::unique_ptr<gw_native_session,decltype(&gw_native_close)> owned_session(session,gw_native_close);
if(!result){
report.put("character_count",gw_native_character_count(session));
if(options.get<bool>("engine_packet_host",false)){
auto delivery=gateway::engine::publish_roster(root,session,selected_wire_id);
report.put("engine_roster_encode_result",delivery.encode_result);report.put("engine_roster_published",delivery.published);
report.put("engine_roster_submitted",delivery.submitted);report.put("engine_roster_received",delivery.received);
report.put("engine_roster_opcode",delivery.opcode);report.put("engine_state_at_receive",delivery.state);
report.put("engine_roster_delivery_error",delivery.error);report.put("engine_roster_bytes",delivery.bytes);
report.put("engine_roster_adopted",false); // UI/native account adoption is a separate requirement.
if(delivery.submitted && delivery.received && !delivery.error){
auto update=[&](const gateway::engine::SessionRetention& lifetime){
report.put("backend_session_retained",lifetime.active);
report.put("backend_account_retained",lifetime.active && bool(owned_login));
report.put("backend_retention_ms",lifetime.elapsed_ms);
report.put("backend_retention_reason",lifetime.reason);
report.put("backend_heartbeat_pings",lifetime.pings);report.put("backend_heartbeat_pongs",lifetime.pongs);
report.put("backend_retention_error",lifetime.error);write_report();
};
auto lifetime=gateway::engine::retain_session(root,session,options.get<uint32_t>("session_retention_seconds",120),update);
update(lifetime);
}
}else for(uint32_t i=0;i<gw_native_character_count(session);++i){gw_native_character character{};if(gw_native_character_at(session,i,&character))continue;
if(character.guid==options.get<uint64_t>("character_guid")){
report.put("character_guid",character.guid);report.put("character_level",character.level);gw_native_world world{};
report.put("backend_world_result",gw_native_enter(session,character.guid,&world));report.put("backend_map",world.map);report.put("backend_x",world.x);report.put("backend_y",world.y);report.put("backend_z",world.z);report.put("backend_packets",world.packets);report.put("backend_objects",world.objects);
}
}
}
owned_session.reset();session=nullptr;
owned_login.reset();report.put("backend_account_retained",false);
report.put("backend_session_retained",false);
// Public gameplay metadata only. Never serialize config, usernames,
// credentials, session keys, proof bytes or arbitrary exception strings.
write_report();
}catch(...){} // Startup diagnostics must not terminate or patch the game.
}
__attribute__((constructor)) void start(){
const char* config=std::getenv("GW_NATIVE_BOOTSTRAP");const char* expected=std::getenv("GW_NATIVE_EXPECTED_EXE");
if(!config || !expected)return;
try{
auto command=read("/proc/self/cmdline",16384);bool match=false;
size_t pos=0;while(pos<command.size()){
auto end=command.find('\0',pos);if(end==std::string::npos)break;auto arg=command.substr(pos,end-pos);
if(arg.starts_with("Z:\\") || arg.starts_with("z:\\")){arg="/"+arg.substr(3);std::replace(arg.begin(),arg.end(),'\\','/');}
if(arg==expected)match=true;
pos=end+1;
}
if(match)std::thread(run,std::string(config)).detach();
}catch(...){}
}
}