finally removed the logging and removed the duplicate case from begin auth session hook. probably fine

This commit is contained in:
jenz 2026-06-25 14:32:41 +01:00
parent 0aab8e0bd6
commit 4c7ec14d52

View File

@ -153,11 +153,6 @@ CDetour* detourCBaseServer__ConnectClient = nullptr;
CDetour *g_Detour_CSteam3Server__OnValidateAuthTicketResponse = NULL;
CDetour *g_Detour_CBaseClient__SetSteamID = NULL;
bool g_bSuppressBeginAuthSession = false;
CSteamID g_lastClientSteamID;
const void* g_lastAuthTicket;
int g_lastcbAuthTicket;
char passwordBuffer[255];
@ -174,29 +169,6 @@ CSteam3Server *Steam3Server()
return g_pSteam3ServerFunc();
}
SH_DECL_MANUALHOOK3(MHook_BeginAuthSession, 0, 0, 0, EBeginAuthSessionResult, const void *, int, CSteamID);
EBeginAuthSessionResult Hook_BeginAuthSession(const void *pAuthTicket, int cbAuthTicket, CSteamID steamID)
{
if (!g_bSuppressBeginAuthSession)
{
g_pSM->LogMessage(myself, "inside Hook_ BeginAuthSession 1. %s", steamID.Render());
RETURN_META_VALUE(MRES_IGNORED, k_EBeginAuthSessionResultOK);
}
g_bSuppressBeginAuthSession = false;
if (strcmp(steamID.Render(), g_lastClientSteamID.Render()) == 0
&& g_lastAuthTicket == pAuthTicket
&& g_lastcbAuthTicket == cbAuthTicket)
{
// Let the server know everything is fine
g_pSM->LogMessage(myself, "inside Hook_ BeginAuthSession 2. %s", steamID.Render());
RETURN_META_VALUE(MRES_SUPERCEDE, k_EBeginAuthSessionResultOK);
}
g_pSM->LogMessage(myself, "inside Hook_ BeginAuthSession 3. %s", steamID.Render());
RETURN_META_VALUE(MRES_IGNORED, k_EBeginAuthSessionResultDuplicateRequest);
}
struct ValidateAuthTicketResponse_t
{
enum { k_iCallback = k_iSteamUserCallbacks + 43 };
@ -230,6 +202,7 @@ public:
bool SteamAuthFailed;
bool ManualRetry;
bool Webclient;
bool SuppressBeginAuthSession;
void *pvTicket;
int cbTicket;
@ -254,6 +227,7 @@ public:
this->SteamAuthFailed = false;
this->ManualRetry = false;
this->Webclient = false;
this->SuppressBeginAuthSession = false;
// Calculate and store the ticket pointer
this->pvTicket = (void *)((intptr_t)this->pCookie + sizeof(uint64));
@ -262,6 +236,23 @@ public:
};
StringHashMap<ConnectClientStorage> g_ConnectClientStorage;
SH_DECL_MANUALHOOK3(MHook_BeginAuthSession, 0, 0, 0, EBeginAuthSessionResult, const void *, int, CSteamID);
EBeginAuthSessionResult Hook_BeginAuthSession(const void *pAuthTicket, int cbAuthTicket, CSteamID steamID)
{
ConnectClientStorage Storage;
g_ConnectClientStorage.retrieve(steamID.Render(), &Storage);
if (!Storage.SuppressBeginAuthSession)
{
//g_pSM->LogMessage(myself, "inside Hook_ BeginAuthSession 1. %s", steamID.Render());
RETURN_META_VALUE(MRES_IGNORED, k_EBeginAuthSessionResultOK);
}
Storage.SuppressBeginAuthSession = false;
g_ConnectClientStorage.replace(steamID.Render(), Storage);
//g_pSM->LogMessage(myself, "inside Hook_ BeginAuthSession 2. %s", steamID.Render());
RETURN_META_VALUE(MRES_SUPERCEDE, k_EBeginAuthSessionResultOK);
}
//ai generated function
uint64 GenerateSteamIDFromName(const char *pchName)
@ -296,7 +287,8 @@ DETOUR_DECL_MEMBER1(CBaseClient__SetSteamID, void, const CSteamID &, steamID)
char ipString[30];
V_snprintf(ipString, sizeof(ipString), "%u.%u.%u.%u", storage.address.ip[0], storage.address.ip[1], storage.address.ip[2], storage.address.ip[3]);
//smutils->LogMessage(myself, "inside SetSteamID steamAuthFailed. IpsAllowed: %s. ipString: %s", IpsAllowed, ipString);
if (g_SvLogging.GetInt())
smutils->LogMessage(myself, "inside SetSteamID steamAuthFailed. IpsAllowed: %s. ipString: %s", IpsAllowed, ipString);
//connects from the IP's designated for the webclients.
if (strcmp(IpsAllowed, ipString) == 0)
{
@ -364,7 +356,7 @@ DETOUR_DECL_MEMBER1(CSteam3Server__OnValidateAuthTicketResponse, int, ValidateAu
{
if (slotID.steamid == clientID.steamid)
{
g_pSM->LogMessage(myself, "Disconnecting idle left over session for steamID: %s", aSteamID);
//g_pSM->LogMessage(myself, "Disconnecting idle left over session for steamID: %s", aSteamID);
pSlot->Disconnect("Disconnecting dead left over session.");
break;
}
@ -387,13 +379,13 @@ DETOUR_DECL_MEMBER9(CBaseServer__ConnectClient, IClient*, netadr_t&, address, in
return DETOUR_MEMBER_CALL(CBaseServer__ConnectClient)(address, nProtocol, iChallenge, iClientChallenge, nAuthProtocol, pchName, pchPassword, pCookie, cbCookie);
}
g_pSM->LogMessage(myself, "entered MEMBER9");
//g_pSM->LogMessage(myself, "entered MEMBER9");
if (g_pSteam3Server->m_pSteamGameServer != g_pLastHookedSteamGameServer)
{
//the hook tends to die, hence its reapplied whenever needed. thats probably very dumb.
//does however seem to stop "S3: Client connected with invalid ticket:" for nosteamers.
g_pSM->LogMessage(myself, "Steam GameServer pointer changed! Old: %p, New: %p", g_pLastHookedSteamGameServer, g_pSteam3Server->m_pSteamGameServer);
//g_pSM->LogMessage(myself, "Steam GameServer pointer changed! Old: %p, New: %p", g_pLastHookedSteamGameServer, g_pSteam3Server->m_pSteamGameServer);
if (g_pLastHookedSteamGameServer != NULL)
{
// Remove old hook if it exists
@ -429,38 +421,29 @@ DETOUR_DECL_MEMBER9(CBaseServer__ConnectClient, IClient*, netadr_t&, address, in
V_strncpy(passwordBuffer, pchPassword, 255);
uint64 ullSteamID = *(uint64 *)pCookie;
g_lastClientSteamID = CSteamID(ullSteamID);
void *pvTicket = (void *)((intptr_t)pCookie + sizeof(uint64));
int cbTicket = cbCookie - sizeof(uint64);
CSteamID ClientSteamID = CSteamID(ullSteamID);
char aSteamID[32];
V_strncpy(aSteamID, g_lastClientSteamID.Render(), 32);
V_strncpy(aSteamID, ClientSteamID.Render(), 32);
// If client is in async state remove the old object and fake an async retVal
// This can happen if the async ClientPreConnectEx takes too long to be called
// and the client auto-retries.
bool AsyncWaiting = false;
bool ExistingSteamid = false;
bool SkipEndAuthSession = false;
EBeginAuthSessionResult result;
ConnectClientStorage Storage(address, nProtocol, iChallenge, iClientChallenge, nAuthProtocol, pchName, pchPassword, pCookie, cbCookie);
if (g_ConnectClientStorage.retrieve(aSteamID, &Storage))
{
g_pSM->LogMessage(myself, "entered has clientstorage. %s", aSteamID);
//g_pSM->LogMessage(myself, "entered has clientstorage. %s", aSteamID);
ExistingSteamid = true;
g_ConnectClientStorage.remove(aSteamID);
if (!Storage.SteamAuthFailed)
{
g_pSM->LogMessage(myself, "inside clientstorage about to do endauthsession for steamID: %s", aSteamID);
g_pEndAuthSession(steamGameServer, g_lastClientSteamID);
}
// Only wait for async on auto-retry, manual retries should go through the full chain
// Don't want to leave the client waiting forever if something breaks in the async forward
if (Storage.iClientChallenge == iClientChallenge)
{
g_pSM->LogMessage(myself, "entered async retry. %s", aSteamID);
//g_pSM->LogMessage(myself, "entered async retry. %s", aSteamID);
AsyncWaiting = true;
//reject async nosteamers or async steam player who is not authenticated
@ -471,63 +454,29 @@ DETOUR_DECL_MEMBER9(CBaseServer__ConnectClient, IClient*, netadr_t&, address, in
return NULL;
}
//async steam player with a clientstorage, use the ticket from the previous clientstorage.
g_lastcbAuthTicket = Storage.cbTicket;
g_lastAuthTicket = Storage.pvTicket;
result = g_pBeginAuthSession(steamGameServer, Storage.pvTicket, Storage.cbTicket, g_lastClientSteamID);
result = g_pBeginAuthSession(steamGameServer, Storage.pvTicket, Storage.cbTicket, ClientSteamID);
}
else
{
//client did a manual retry,
g_pSM->LogMessage(myself, "entered manual retry. %s", aSteamID);
if (Storage.SteamAuthFailed)
{
//its a nosteamer. or a steam player who failed auth once so far. so dont use the clientstorage ticket
g_lastcbAuthTicket = cbTicket;
g_lastAuthTicket = pvTicket;
SkipEndAuthSession = true;
g_pSM->LogMessage(myself, "manual retry with SteamAuthFailed before Begin Auth Session. %s", aSteamID);
//result = k_EBeginAuthSessionResultInvalidTicket;
result = g_pBeginAuthSession(steamGameServer, pvTicket, cbTicket, g_lastClientSteamID);
g_pSM->LogMessage(myself, "manual retry with SteamAuthFailed after Begin Auth Session. %s", aSteamID);
}
else
{
g_pSM->LogMessage(myself, "in client storage about to do new storage. %s", aSteamID);
//create a new client storage for the steamplayer.
//create new auth storage with fresh ticket and challenge
Storage = ConnectClientStorage(address, nProtocol, iChallenge, iClientChallenge, nAuthProtocol, pchName, pchPassword, pCookie, cbCookie);
//use the ticket from the new clientstorage.
g_lastcbAuthTicket = Storage.cbTicket;
g_lastAuthTicket = Storage.pvTicket;
g_pSM->LogMessage(myself, "in client storage about to do beginauthsession. %s", aSteamID);
result = g_pBeginAuthSession(steamGameServer, Storage.pvTicket, Storage.cbTicket, g_lastClientSteamID);
g_pSM->LogMessage(myself, "in client storage after beginauthsession. %s", aSteamID);
}
//g_pSM->LogMessage(myself, "entered manual retry. %s", aSteamID);
result = g_pBeginAuthSession(steamGameServer, Storage.pvTicket, Storage.cbTicket, ClientSteamID);
}
}
else
{
//nosteamer or steam player without clientstorage,
g_lastcbAuthTicket = cbTicket;
g_lastAuthTicket = pvTicket;
result = g_pBeginAuthSession(steamGameServer, Storage.pvTicket, Storage.cbTicket, ClientSteamID);
//g_pSM->LogMessage(myself, "without client storage after beginauth session. %s", aSteamID);
}
//NOTE: if the first connecting client on a map is in k_OnClientPreConnectEx_Reject then this beginAuthSession call can crash the server
//however if the first connecting client instead is in k_OnClientPreConnectEx_Accept then the after following
//k_OnClientPreConnectEx_Reject will work fine. Ex_Accept is always used until the server is full, hence should Ex_Reject
//never be the first to connect on a new map.
g_pSM->LogMessage(myself, "without client storage about to do beginauth session. %s", aSteamID);
result = g_pBeginAuthSession(steamGameServer, pvTicket, cbTicket, g_lastClientSteamID);
g_pSM->LogMessage(myself, "without client storage after beginauth session. %s", aSteamID);
}
if (!SkipEndAuthSession)
{
g_pSM->LogMessage(myself, "about to do endAuthSession. %s", aSteamID);
g_pEndAuthSession(steamGameServer, g_lastClientSteamID); //xen said engine might start its own authsession, so end ours here.
}
//g_pSM->LogMessage(myself, "about to do endAuthSession. %s", aSteamID);
g_pEndAuthSession(steamGameServer, ClientSteamID); //xen said engine might start its own authsession, so end ours here.
bool NoSteam = g_SvNoSteam.GetInt();
bool SteamAuthFailed = false;
g_pSM->LogMessage(myself, "g_pBeginAuthSession result: %d. %s", result, aSteamID);
//g_pSM->LogMessage(myself, "g_pBeginAuthSession result: %d. %s", result, aSteamID);
if (result != k_EBeginAuthSessionResultOK)
{
@ -551,7 +500,7 @@ DETOUR_DECL_MEMBER9(CBaseServer__ConnectClient, IClient*, netadr_t&, address, in
//if the nosteam player is trying to spoof a steam player reject the nosteamer.
if (!Storage.SteamAuthFailed)
{
g_pSM->LogMessage(myself, "about to do rejectconnection 1. %s", aSteamID);
//g_pSM->LogMessage(myself, "about to do rejectconnection 1. %s", aSteamID);
g_pRejectConnectionFunc((CBaseServer*)this, address, iClientChallenge, "Steam ID already in use.");
return NULL;
}
@ -560,7 +509,7 @@ DETOUR_DECL_MEMBER9(CBaseServer__ConnectClient, IClient*, netadr_t&, address, in
//a nosteamer spoofing another nosteamer that is not ingame is fine too.
if (Storage.pClient && Storage.pClient->IsConnected() && Storage.pClient->IsActive())
{
g_pSM->LogMessage(myself, "about to do rejectconnection 2. %s", aSteamID);
//g_pSM->LogMessage(myself, "about to do rejectconnection 2. %s", aSteamID);
g_pRejectConnectionFunc((CBaseServer*)this, address, iClientChallenge, "Steam ID already in use.");
return NULL;
}
@ -569,12 +518,12 @@ DETOUR_DECL_MEMBER9(CBaseServer__ConnectClient, IClient*, netadr_t&, address, in
// Kick existing player
if(Storage.pClient)
{
g_pSM->LogMessage(myself, "about to do disconnect. %s", aSteamID);
//g_pSM->LogMessage(myself, "about to do disconnect. %s", aSteamID);
Storage.pClient->Disconnect("Same Steam ID connected.");
}
else
{
g_pSM->LogMessage(myself, "about to do rejectconnection 3. %s", aSteamID);
//g_pSM->LogMessage(myself, "about to do rejectconnection 3. %s", aSteamID);
g_pRejectConnectionFunc((CBaseServer*)this, address, iClientChallenge, "Please try again later.");
return NULL;
}
@ -593,7 +542,7 @@ DETOUR_DECL_MEMBER9(CBaseServer__ConnectClient, IClient*, netadr_t&, address, in
g_pConnectForward->PushString(pchName);
g_pConnectForward->PushStringEx(passwordBuffer, 255, SM_PARAM_STRING_UTF8 | SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
g_pConnectForward->PushString(ipString);
g_pConnectForward->PushString(g_lastClientSteamID.Render());
g_pConnectForward->PushString(ClientSteamID.Render());
g_pConnectForward->PushStringEx(rejectReason, 255, SM_PARAM_STRING_UTF8 | SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
g_pConnectForward->Execute(&retVal);
}
@ -607,10 +556,10 @@ DETOUR_DECL_MEMBER9(CBaseServer__ConnectClient, IClient*, netadr_t&, address, in
{
//calling this on nosteamers does sometimes cause a server explosion.
//but not when creating a nosteamer with their first ticket
g_pSM->LogMessage(myself, "k_OnClientPreConnectEx_Reject reached about to do endAuthSession. steamID: %s", aSteamID);
g_pEndAuthSession(steamGameServer, g_lastClientSteamID);
//g_pSM->LogMessage(myself, "k_OnClientPreConnectEx_Reject reached about to do endAuthSession. steamID: %s", aSteamID);
g_pEndAuthSession(steamGameServer, ClientSteamID);
}
g_pSM->LogMessage(myself, "k_OnClientPreConnectEx_Reject reached about to do g_pRejectConnectionFunc. steamID: %s", aSteamID);
//g_pSM->LogMessage(myself, "k_OnClientPreConnectEx_Reject reached about to do g_pRejectConnectionFunc. steamID: %s", aSteamID);
g_pRejectConnectionFunc((CBaseServer*)this, address, iClientChallenge, rejectReason);
return NULL;
}
@ -633,12 +582,16 @@ DETOUR_DECL_MEMBER9(CBaseServer__ConnectClient, IClient*, netadr_t&, address, in
}
// k_OnClientPreConnectEx_Accept
g_pSM->LogMessage(myself, "about to do DETOUR_MEMBER_CALL. %s", aSteamID);
g_bSuppressBeginAuthSession = true;
//g_pSM->LogMessage(myself, "about to do DETOUR_MEMBER_CALL. %s", aSteamID);
Storage.SuppressBeginAuthSession = true;
g_ConnectClientStorage.replace(aSteamID, Storage);
auto client = DETOUR_MEMBER_CALL(CBaseServer__ConnectClient)(address, nProtocol, iChallenge, iClientChallenge, nAuthProtocol, pchName, pchPassword, pCookie, cbCookie);
//auto client = DETOUR_MEMBER_MCALL_ORIGINAL(CBaseServer__ConnectClient, this)(address, nProtocol, iChallenge, iClientChallenge, nAuthProtocol, pchName, pchPassword, pCookie, cbCookie);
g_bSuppressBeginAuthSession = false;
g_pSM->LogMessage(myself, "finished DETOUR_MEMBER_CALL. %s", aSteamID);
Storage.SuppressBeginAuthSession = false;
g_ConnectClientStorage.replace(aSteamID, Storage);
//g_pSM->LogMessage(myself, "finished DETOUR_MEMBER_CALL. %s", aSteamID);
//doing another retrieve in case of SetSteamID for webclient.
ConnectClientStorage storage_setSteamID;
@ -656,7 +609,7 @@ DETOUR_DECL_MEMBER9(CBaseServer__ConnectClient, IClient*, netadr_t&, address, in
Response.m_SteamID = newSteamID;
Response.m_eAuthSessionResponse = k_EAuthSessionResponseAuthTicketInvalid; //nosteamer monkeys
Response.m_OwnerSteamID = Response.m_SteamID;
g_pSM->LogMessage(myself, "about to do OnValidateAuthTicketResponse webclient. %s", newSteamID.Render());
//g_pSM->LogMessage(myself, "about to do OnValidateAuthTicketResponse webclient. %s", newSteamID.Render());
DETOUR_MEMBER_MCALL_CALLBACK(CSteam3Server__OnValidateAuthTicketResponse, g_pSteam3Server)(&Response);
}
}
@ -668,14 +621,14 @@ DETOUR_DECL_MEMBER9(CBaseServer__ConnectClient, IClient*, netadr_t&, address, in
if (client && SteamAuthFailed)
{
ValidateAuthTicketResponse_t Response;
Response.m_SteamID = g_lastClientSteamID;
Response.m_SteamID = ClientSteamID;
Response.m_eAuthSessionResponse = k_EAuthSessionResponseAuthTicketInvalid; //nosteamer monkeys
Response.m_OwnerSteamID = Response.m_SteamID;
g_pSM->LogMessage(myself, "about to do OnValidateAuthTicketResponse. %s", aSteamID);
//g_pSM->LogMessage(myself, "about to do OnValidateAuthTicketResponse. %s", aSteamID);
DETOUR_MEMBER_MCALL_CALLBACK(CSteam3Server__OnValidateAuthTicketResponse, g_pSteam3Server)(&Response);
}
}
g_pSM->LogMessage(myself, "MEMBER 9 end. %s", aSteamID);
//g_pSM->LogMessage(myself, "MEMBER 9 end. %s", aSteamID);
return client;
}