engine: server: add function SV_HavePassword that correctly checks whether this server have set up password

This commit is contained in:
Alibek Omarov 2024-07-07 02:09:48 +03:00
parent b6967a432a
commit dc6f03b4e4
3 changed files with 27 additions and 10 deletions

View file

@ -548,6 +548,7 @@ void SV_UpdateServerInfo( void );
void SV_EndRedirect( host_redirect_t *rd ); void SV_EndRedirect( host_redirect_t *rd );
void SV_RejectConnection( netadr_t from, const char *fmt, ... ) _format( 2 ); void SV_RejectConnection( netadr_t from, const char *fmt, ... ) _format( 2 );
void SV_GetPlayerCount( int *clients, int *bots ); void SV_GetPlayerCount( int *clients, int *bots );
qboolean SV_HavePassword( void );
// //
// sv_cmds.c // sv_cmds.c

View file

@ -45,6 +45,20 @@ static int g_userid = 1;
static void SV_UserinfoChanged( sv_client_t *cl ); static void SV_UserinfoChanged( sv_client_t *cl );
static void SV_ExecuteClientCommand( sv_client_t *cl, const char *s ); static void SV_ExecuteClientCommand( sv_client_t *cl, const char *s );
/*
=================
SV_HavePassword
=================
*/
qboolean SV_HavePassword( void )
{
if( COM_CheckStringEmpty( sv_password.string ) && Q_stricmp( sv_password.string, "none" ))
return true;
return false;
}
/* /*
================= =================
SV_GetPlayerCount SV_GetPlayerCount
@ -370,10 +384,13 @@ static void SV_ConnectClient( netadr_t from )
Q_strncpy( userinfo, s, sizeof( userinfo )); Q_strncpy( userinfo, s, sizeof( userinfo ));
// check connection password (don't verify local client) // check connection password (don't verify local client)
if( !NET_IsLocalAddress( from ) && sv_password.string[0] && Q_stricmp( sv_password.string, Info_ValueForKey( userinfo, "password" ))) if( !NET_IsLocalAddress( from ) && SV_HavePassword( ))
{ {
SV_RejectConnection( from, "invalid password\n" ); if( Q_stricmp( sv_password.string, Info_ValueForKey( userinfo, "password" )))
return; {
SV_RejectConnection( from, "invalid password\n" );
return;
}
} }
// if there is already a slot for this ip, reuse it // if there is already a slot for this ip, reuse it
@ -915,7 +932,6 @@ static void SV_Info( netadr_t from, int protocolVersion )
int bots; int bots;
int remaining; int remaining;
char temp[sizeof( s )]; char temp[sizeof( s )];
qboolean have_password = COM_CheckStringEmpty( sv_password.string );
SV_GetPlayerCount( &count, &bots ); SV_GetPlayerCount( &count, &bots );
@ -928,7 +944,7 @@ static void SV_Info( netadr_t from, int protocolVersion )
Info_SetValueForKeyf( s, "numcl", sizeof( s ), "%i", count ); Info_SetValueForKeyf( s, "numcl", sizeof( s ), "%i", count );
Info_SetValueForKeyf( s, "maxcl", sizeof( s ), "%i", svs.maxclients ); Info_SetValueForKeyf( s, "maxcl", sizeof( s ), "%i", svs.maxclients );
Info_SetValueForKey( s, "gamedir", GI->gamefolder, sizeof( s )); Info_SetValueForKey( s, "gamedir", GI->gamefolder, sizeof( s ));
Info_SetValueForKey( s, "password", have_password ? "1" : "0", sizeof( s )); Info_SetValueForKey( s, "password", SV_HavePassword() ? "1" : "0", sizeof( s ));
// write host last so we can try to cut off too long hostnames // write host last so we can try to cut off too long hostnames
// TODO: value size limit for infostrings // TODO: value size limit for infostrings

View file

@ -37,12 +37,9 @@ static void SV_SourceQuery_Details( netadr_t from )
sizebuf_t buf; sizebuf_t buf;
char answer[2048]; char answer[2048];
int bot_count, client_count; int bot_count, client_count;
int is_private = 0;
SV_GetPlayerCount( &client_count, &bot_count ); SV_GetPlayerCount( &client_count, &bot_count );
client_count += bot_count; // bots are counted as players in this reply client_count += bot_count; // bots are counted as players in this reply
if( COM_CheckStringEmpty( sv_password.string ) && Q_stricmp( sv_password.string, "none" ))
is_private = 1;
MSG_Init( &buf, "TSourceEngineQuery", answer, sizeof( answer )); MSG_Init( &buf, "TSourceEngineQuery", answer, sizeof( answer ));
@ -67,7 +64,10 @@ static void SV_SourceQuery_Details( netadr_t from )
#else #else
MSG_WriteByte( &buf, 'l' ); MSG_WriteByte( &buf, 'l' );
#endif #endif
MSG_WriteByte( &buf, is_private );
if( SV_HavePassword( ))
MSG_WriteByte( &buf, 1 );
else MSG_WriteByte( &buf, 0 );
MSG_WriteByte( &buf, GI->secure ); MSG_WriteByte( &buf, GI->secure );
MSG_WriteString( &buf, XASH_VERSION ); MSG_WriteString( &buf, XASH_VERSION );
@ -130,7 +130,7 @@ static void SV_SourceQuery_Players( netadr_t from )
client_count += bot_count; // bots are counted as players in this reply client_count += bot_count; // bots are counted as players in this reply
// respect players privacy // respect players privacy
if( client_count <= 0 || !sv_expose_player_list.value || !COM_CheckStringEmpty( sv_password.string )) if( client_count <= 0 || !sv_expose_player_list.value || SV_HavePassword( ))
return; return;
MSG_Init( &buf, "TSourceEngineQueryPlayers", answer, sizeof( answer )); MSG_Init( &buf, "TSourceEngineQueryPlayers", answer, sizeof( answer ));