180 lines
11 KiB
C++
180 lines
11 KiB
C++
#pragma once
|
|
#include "wine_wire_abi.h"
|
|
#include <algorithm>
|
|
#include <cerrno>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <string>
|
|
#include <vector>
|
|
#ifndef _WIN32
|
|
#include <fcntl.h>
|
|
#include <sys/file.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
// Optional private gameplay capture. The caller supplies the existing authenticated
|
|
// self-created-world predicate; this helper never reads authentication state/data.
|
|
class PrivateGameplayCapture {
|
|
struct Pending {uint32_t opcode;size_t bytes;uint64_t guid;std::vector<uint8_t> body;bool omitted;};
|
|
std::string path_;int fd_=-1,error_=0;
|
|
#ifdef _WIN32
|
|
std::FILE* file_=nullptr; // Windows: plain append file (diagnostic builds only; env opt-in)
|
|
#endif
|
|
uint64_t count_=0,written_=0,max_bytes_=256ULL*1024*1024;
|
|
size_t pending_bytes_=0;std::vector<Pending> pending_;bool invite_body_written_=false,inspect_body_written_=false,bank_auto_store_body_written_=false,logout_request_body_written_=false,log_disconnect_body_written_=false;
|
|
uint8_t area_trigger_bodies_written_=0,guild_rank_rejections_written_=0,club_chat_bodies_written_=0;
|
|
static bool deny_source(uint32_t opcode){switch(opcode){case 0x33:case 0x34:case 0x35:case 0x39:case 0x1ec:case 0x1ed:case 0x1ee:case 0x209:case 0x20a:case 0x20b:case 0x20c:return true; default:return false;}}
|
|
static bool omit_source_out(uint32_t opcode){switch(opcode){case 0x95:case 0x97:case 0x98:case 0x9a:case 0x9c:case 0x9d:case 0x9e:case 0x9f:case 0xa0:case 0xa1:case 0xa2:case 0xa3:case 0xa4:case 0xa5:case 0xa6:case 0xa7:case 0xa8:case 0x104:case 0x13b:case 0x225:case 0x331:return true; default:return false;}}
|
|
static bool deny_native(uint32_t opcode){switch(opcode){case 0x0:case 0xb:case 0x4300c3:case 0x4300c4:case 0x440001:case 0x450001:case 0x4501b4:case 0x4501b6:case 0x4c0000:return true; default:return false;}}
|
|
void fail(int error) noexcept {
|
|
if(error_){return;}error_=error?error:EIO;
|
|
std::fprintf(stderr,"private gameplay capture disabled (error=%d); gameplay continues\n",error_);std::fflush(stderr);
|
|
#ifndef _WIN32
|
|
if(fd_>=0){::close(fd_);fd_=-1;}
|
|
#else
|
|
if(file_){std::fclose(file_);file_=nullptr;}
|
|
#endif
|
|
}
|
|
bool open_private() noexcept {
|
|
if(fd_>=0){return true;}if(path_.empty()||error_){return false;}
|
|
#ifdef _WIN32
|
|
if(file_)return true;
|
|
file_=std::fopen(path_.c_str(),"ab");if(!file_){fail(errno?errno:EACCES);return false;}
|
|
std::fseek(file_,0,SEEK_END);{long size=std::ftell(file_);written_=size>0?uint64_t(size):0;}return true;
|
|
#else
|
|
try{
|
|
std::filesystem::path path(path_);auto leaf=path.filename().string();
|
|
if(!path.is_absolute()||leaf.empty()||leaf=="."||leaf==".."){fail(EINVAL);return false;}
|
|
int directory=::open(path.parent_path().c_str(),O_RDONLY|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC);
|
|
if(directory<0){fail(errno);return false;}struct stat parent{};
|
|
if(::fstat(directory,&parent)||!S_ISDIR(parent.st_mode)||parent.st_uid!=::geteuid()||(parent.st_mode&0777)!=0700){::close(directory);fail(EACCES);return false;}
|
|
fd_=::openat(directory,leaf.c_str(),O_WRONLY|O_CREAT|O_APPEND|O_NOFOLLOW|O_CLOEXEC,0600);int saved=errno;::close(directory);
|
|
if(fd_<0){fail(saved);return false;}struct stat file{};
|
|
if(::fstat(fd_,&file)||!S_ISREG(file.st_mode)||file.st_uid!=::geteuid()||(file.st_mode&0777)!=0600||file.st_nlink!=1){fail(EACCES);return false;}
|
|
written_=uint64_t(file.st_size);return true;
|
|
}catch(...){fail(EINVAL);return false;}
|
|
#endif
|
|
}
|
|
void append(const char* direction,uint32_t opcode,size_t bytes,std::span<const uint8_t> body,uint64_t guid,bool omitted,const char* stage=nullptr,uint32_t realm=0,std::optional<uint64_t> member={},const char* scope_name="authenticated-self-created-world") noexcept {
|
|
if(!enabled()||!open_private())return;
|
|
try{
|
|
auto time=std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
|
|
std::string line="{\"sequence\":"+std::to_string(count_+1)+",\"time_ns\":"+std::to_string(time)+",\"direction\":\""+direction+"\",\"opcode\":"+std::to_string(opcode)+",\"bytes\":"+std::to_string(bytes)+",\"source_guid\":\""+std::to_string(guid)+"\",\"scope\":\""+std::string(scope_name)+"\"";
|
|
if(omitted)line+=",\"body_omitted\":true";
|
|
else{static constexpr char hex[]="0123456789abcdef";line+=",\"body_hex\":\"";for(auto byte:body){line+=hex[byte>>4];line+=hex[byte&15];}line+='"';}
|
|
if(stage)line+=",\"capture_stage\":\""+std::string(stage)+"\"";
|
|
if(member)line+=",\"realm\":"+std::to_string(realm)+",\"member_unique_id\":\""+std::to_string(*member)+"\"";
|
|
line+="}\n";
|
|
if(written_+line.size()>max_bytes_){fail(EFBIG);return;}
|
|
#ifndef _WIN32
|
|
if(::flock(fd_,LOCK_EX)){fail(errno);return;}auto offset=::lseek(fd_,0,SEEK_END);auto size=::write(fd_,line.data(),line.size());int error=errno;
|
|
if(size!=ssize_t(line.size())){if(offset>=0)(void)::ftruncate(fd_,offset);(void)::flock(fd_,LOCK_UN);fail(size<0?error:EIO);return;}
|
|
(void)::flock(fd_,LOCK_UN);written_+=line.size();++count_;
|
|
#else
|
|
if(std::fwrite(line.data(),1,line.size(),file_)!=line.size()){fail(EIO);return;}
|
|
std::fflush(file_);written_+=line.size();++count_;
|
|
#endif
|
|
}catch(...){fail(ENOMEM);}
|
|
}
|
|
public:
|
|
PrivateGameplayCapture(){if(const char* path=std::getenv("GW_PRIVATE_GAMEPLAY_CAPTURE"))path_=path;
|
|
if(const char* limit=std::getenv("GW_PRIVATE_GAMEPLAY_CAPTURE_MAX_BYTES")){char* end=nullptr;auto n=std::strtoull(limit,&end,10);if(end&&!*end&&n>=1048576&&n<=1073741824)max_bytes_=n;}}
|
|
~PrivateGameplayCapture(){
|
|
#ifndef _WIN32
|
|
if(fd_>=0)::close(fd_);
|
|
#else
|
|
if(file_)std::fclose(file_);
|
|
#endif
|
|
}
|
|
PrivateGameplayCapture(const PrivateGameplayCapture&)=delete;
|
|
bool enabled()const noexcept{return !path_.empty()&&!error_;}
|
|
// One shared Unix-side notice path for the owned host callback and its
|
|
// isolated Windows-admission boundary fixture. Only bounded opt-in specimens.
|
|
bool notice(uint32_t kind,bool scope,uint64_t owner,uint32_t opcode,uint32_t bytes,std::span<const uint8_t> body={}){
|
|
if(kind==4){
|
|
if((opcode!=0x4b0003u && opcode!=0x4b0000u) || bytes<(opcode==0x4b0000u?17u:8u) || bytes>GW_WIRE_PRIVATE_HOTFIX_BODY_MAX || body.size()!=bytes)return false;
|
|
if(!scope || !enabled())return true;
|
|
append("native_in",opcode,bytes,body,owner,false,"pe-after-engine-submit",0,{},"authenticated-world-transport");
|
|
return true;
|
|
}
|
|
if(!body.empty()){
|
|
if(kind!=1 || !bytes || body.size()!=bytes || bytes>gw_wire_private_body_limit(opcode))return false;
|
|
if(opcode==0x3e008a && bytes!=GW_WIRE_PRIVATE_AREA_TRIGGER_BODY_MAX)return false;
|
|
if(opcode==0x460007 && bytes!=GW_WIRE_PRIVATE_LOG_DISCONNECT_BODY_BYTES)return false;
|
|
if(!scope || !owner || !enabled())return true;
|
|
// Opaque original-client specimens for one controlled enter/leave
|
|
// observation. No bit interpretation, Source forwarding or replay.
|
|
if(opcode==0x3e008a){
|
|
if(area_trigger_bodies_written_<2){
|
|
const auto previous=count_;append("native_out",opcode,bytes,body,owner,false,"pe-before-admission");
|
|
if(count_>previous)++area_trigger_bodies_written_;
|
|
return true;
|
|
}
|
|
}else{
|
|
// One opaque reason-report specimen; this does not close or forward.
|
|
auto& specimen_written=opcode==0x460007?log_disconnect_body_written_:
|
|
opcode==0x3f0074?logout_request_body_written_:
|
|
opcode==0x410002?bank_auto_store_body_written_:
|
|
opcode==0x3f00ce?inspect_body_written_:invite_body_written_;
|
|
if(!specimen_written){
|
|
const auto previous=count_;append("native_out",opcode,bytes,body,owner,false,"pe-before-admission");
|
|
if(count_>previous)specimen_written=true;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
if(kind==1)native_out(scope,owner,opcode,bytes);
|
|
else if(kind==2)send_complete(true);
|
|
else if(kind==3)send_complete(false);
|
|
else if(kind!=0)return false;
|
|
return true;
|
|
}
|
|
uint32_t configured()const noexcept{return !path_.empty();}uint32_t error()const noexcept{return uint32_t(error_);}
|
|
uint64_t count()const noexcept{return count_;}uint64_t bytes()const noexcept{return written_;}
|
|
void source_in(bool scope,uint64_t guid,uint32_t opcode,std::span<const uint8_t> body) noexcept {
|
|
if(!scope||!enabled()||deny_source(opcode))return;
|
|
if(body.size()>65535){fail(EFBIG);return;}append("source_in",opcode,body.size(),body,guid,false);
|
|
}
|
|
void native_out(bool scope,uint64_t guid,uint32_t opcode,uint32_t bytes) noexcept {
|
|
if(!scope||!enabled()||deny_native(opcode)){return;}append("native_out",opcode,bytes,{},guid,true);
|
|
}
|
|
// Bounded diagnostics for actual rejected rank edits; no credential/chat
|
|
// opcodes and no change to admission, validation, or Source mutations.
|
|
void guild_rank_rejected(bool scope,uint64_t guid,uint32_t opcode,std::span<const uint8_t> body,const char* reason) noexcept {
|
|
if(!scope||!guid||!enabled()||opcode!=0x2f0008||body.empty()||body.size()>128||guild_rank_rejections_written_>=8)return;
|
|
char safe[161]{};size_t n=0;
|
|
if(reason)for(;reason[n]&&n<sizeof(safe)-1;++n){const unsigned char c=reason[n];safe[n]=(c>=32&&c<127&&c!='"'&&c!='\\')?char(c):'_';}
|
|
append("native_rank_rejected",opcode,body.size(),body,guid,false,safe);
|
|
++guild_rank_rejections_written_;
|
|
}
|
|
void club_chat_request(bool scope,uint64_t guid,std::span<const uint8_t> body) noexcept {
|
|
if(!scope||!guid||!enabled()||body.size()<24||body.size()>512||club_chat_bodies_written_>=8)return;
|
|
uint64_t type=0;for(unsigned i=0;i<8;++i)type|=uint64_t(body[i])<<(i*8);
|
|
if(uint32_t(type>>32)!=0xe273de0e || (uint32_t(type)&0x3fffffffu)!=150)return;
|
|
append("native_club_create",0x440124,body.size(),body,guid,false,"authenticated-service150");++club_chat_bodies_written_;
|
|
}
|
|
bool club_membership_reply(bool scope,uint64_t guid,uint32_t realm,uint64_t member,uint32_t reply_bytes) noexcept {
|
|
if(!scope||!guid||!realm||!enabled())return false;
|
|
const auto previous=count_;
|
|
append("native_club_membership_subscribe",0x4702b0,reply_bytes,{},guid,true,nullptr,realm,member);
|
|
return count_>previous;
|
|
}
|
|
void discard_send()noexcept{pending_.clear();pending_bytes_=0;}
|
|
void stage_source_out(bool scope,uint64_t guid,uint32_t opcode,std::span<const uint8_t> body) noexcept {
|
|
if(!scope||!enabled()||deny_source(opcode))return;
|
|
if(body.size()>65535||pending_.size()>=256||pending_bytes_+body.size()>524288){fail(EFBIG);discard_send();return;}
|
|
try{bool omitted=omit_source_out(opcode);Pending row{opcode,body.size(),guid,{},omitted};if(!omitted)row.body.assign(body.begin(),body.end());pending_bytes_+=body.size();pending_.push_back(std::move(row));}catch(...){fail(ENOMEM);discard_send();}
|
|
}
|
|
void send_complete(bool success)noexcept{
|
|
if(success)for(const auto& row:pending_)append("source_out",row.opcode,row.bytes,row.body,row.guid,row.omitted);
|
|
discard_send();
|
|
}
|
|
};
|