Files
praxisandClaude Fable 5.2 8a24c445b0 Content pipeline and operator tools
367 files, from working commit b1a079a.

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

90 lines
6.1 KiB
C++

// SPDX-License-Identifier: GPL-2.0-or-later
// Local-only authenticated protocol peer for actual Forever multiplayer tests.
// It is not a replacement game client and never proves Forever compatibility.
#include "gateway/legacy_client.hpp"
#include "local_party_peer.hpp"
#include <filesystem>
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <poll.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace gateway;
namespace {
int owned_party_peer(const char* directory,bool initial_solo=false,const char* error_path=nullptr){
bool reached_world=false;uint16_t last_opcode=0;size_t last_bytes=0;
try{
// Private username is supplied once on stdin, never command-line/logged.
std::string username;if(!std::getline(std::cin,username) || username.empty() || username.size()>32)throw Error("private input bound");
const auto root=std::filesystem::canonical(directory);
if(root.filename()!="GW82BF4A7B4D")throw Error("wrong owned peer input directory");
LegacySession session(3724,8090,username,(root/"password.txt").string(),
(root/"version-hash.bin").string(),(root/"addon-tail.bin").string(),5875);
const auto self=std::find_if(session.roster().begin(),session.roster().end(),[](const auto& c){return c.guid==4514 && c.name=="Gatewayref" && c.race==1;});
if(self==session.roster().end())throw Error("owned party peer absent");
session.login(4514);LocalPartyPeer state;
state.initial_solo=initial_solo;state.solo=initial_solo;
const auto deadline=std::chrono::steady_clock::now()+std::chrono::seconds(180);
while(std::chrono::steady_clock::now()<deadline){
pollfd input{0,POLLIN,0};
if(poll(&input,1,0)>0){
std::string command;if(!std::getline(std::cin,command))throw Error("peer controller closed without logout");
auto request=state.command(command);session.send(request.opcode,request.body);state.sent(command);
std::cout<<"SENT "<<command<<std::endl;
}
auto packet=session.receive_for(50);if(!packet)continue;
last_opcode=packet->opcode;last_bytes=packet->body.size();
auto event=state.observe(*packet);reached_world=state.ready;
if(!event.empty())std::cout<<event<<std::endl;
if(event=="LOGGED_OUT")return 0;
}
throw Error("bounded owned party peer expired");
}catch(const std::exception& e){
if(error_path){
int fd=::open(error_path,O_WRONLY|O_CREAT|O_TRUNC,0600);
if(fd>=0){::fchmod(fd,0600);auto detail=reached_world?std::string(e.what()).substr(0,1024):std::string("pre-world failure; authentication details withheld");
::dprintf(fd,"stage=%s\nlast_opcode=0x%04x\nlast_body_bytes=%zu\nerror=%s\n",reached_world?"world":"pre-world",last_opcode,last_bytes,detail.c_str());::close(fd);}
}
std::cerr<<"Owned party peer stopped; private authentication/server details omitted; do not retry blindly.\n";return 1;}
}
}
int main(int argc,char** argv){
if((argc==3 || argc==4) && std::string_view(argv[1])=="--owned-party-fixture")return owned_party_peer(argv[2],false,argc==4?argv[3]:nullptr);
if((argc==3 || argc==4) && std::string_view(argv[1])=="--owned-party-fixture-initial-solo")return owned_party_peer(argv[2],true,argc==4?argv[3]:nullptr);
if(argc!=6){std::cerr<<"Require private test-account inputs and existing character name; endpoints fixed local3724/8090.\n";return 2;}
try{
LegacySession session(3724,8090,argv[1],argv[2],argv[3],argv[4],5875);
uint64_t guid=0;for(const auto& c:session.roster())if(c.name==argv[5])guid=c.guid;
if(!guid)throw Error("authorized peer character absent");
session.login(guid);bool ready=false,quitting=false;auto deadline=std::chrono::steady_clock::now()+std::chrono::seconds(180);
while(std::chrono::steady_clock::now()<deadline){
pollfd input{0,POLLIN,0};
if(poll(&input,1,0)>0){
std::string command;if(!std::getline(std::cin,command))break;
if(command=="quit"){session.send(0x4b,{});quitting=true;}
else if(command.starts_with("send ") && ready && !quitting){
auto token=command.substr(5);
if(token.empty() || token.size()>128 || !std::all_of(token.begin(),token.end(),[](unsigned char c){return (c>='a'&&c<='z') || (c>='A'&&c<='Z') || (c>='0'&&c<='9') || c=='-';}))throw Error("invalid public test token");
Bytes wire;le(wire,3,4);le(wire,0,4);cstring(wire,token);session.send(0x95,wire);std::cout<<"SENT "<<token<<std::endl;
}else throw Error("unsupported peer test command");
}
auto packet=session.receive_for(50);if(!packet)continue;
if(packet->opcode==0x2e6)throw Error("unsupported server integrity challenge; stop without bypass");
if(packet->opcode==0x236 && !ready){ready=true;std::cout<<"READY "<<guid<<std::endl;}
if(packet->opcode==0x4d && quitting){std::cout<<"LOGGED_OUT"<<std::endl;return 0;}
if(packet->opcode!=0x96)continue;
Reader r(packet->body);if(r.le(1)!=3)continue;r.le(4);auto sender=r.le(8);auto n=r.le(4);if(!n || n>4096)throw Error("invalid guild message bound");auto text=r.take(n);r.le(1);r.end();
if(text.back()!=0)throw Error("invalid guild text terminator");
// Only our unique public probe tokens are exported; ordinary chat
// and account/authentication payloads never enter the test log.
std::string message(text.begin(),text.end()-1);
auto at=message.find("GW-peer-");
if(at!=std::string::npos){auto token=message.substr(at);if(auto color=token.find('|');color!=std::string::npos)token.resize(color);if(token.size()>128)throw Error("peer token bound");std::cout<<"RECEIVED "<<sender<<" "<<token<<std::endl;}
}
throw Error("bounded peer test ended before clean logout");
}catch(const std::exception&){std::cerr<<"Local guild peer failed; authentication details omitted.\n";return 1;}
}