Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Source.Engine/CL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Source.Common.Launcher;
using Source.Common.MaterialSystem;
using Source.Common.Networking;
using Source.Common.Utilities;
using Source.Engine.Client;
using Source.Engine.Server;

Expand Down Expand Up @@ -520,6 +521,32 @@ internal bool CheckCRCs(ReadOnlySpan<char> levelFileName) {
return true;
}

public static PureServerWhitelist? LoadWhitelist(INetworkStringTable table, ReadOnlySpan<char> name) {
int stringIdx = table.FindStringIndex(name);
if (stringIdx == INetworkStringTable.INVALID_STRING_INDEX)
return null;

byte[]? data = table.GetStringUserData(stringIdx);
if (data == null)
return null;

UtlBuffer buf = new(data, UtlBuffer.BufferFlags.ReadOnly);

PureServerWhitelist whitelist = PureServerWhitelist.Create(g_pFileSystem);
whitelist.Decode(buf);
return whitelist;
}

public static void CheckForPureServerWhitelist() {
if (cl.MaxClients <= 1 || cl.IsHLTV)
return;

if (cl.ServerStartupTable != null)
LoadWhitelist(cl.ServerStartupTable, "PureServerWhitelist");
}

public static void PrintWhitelistInfo() => cl.PureServerWhitelist?.PrintWhitelistContents();

readonly byte[] voiceData = new byte[2048];

public void SendVoicePacket(bool final) {
Expand Down
6 changes: 6 additions & 0 deletions Source.Engine/Client/ClientState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public class ClientState : BaseClientState
public INetworkStringTable? LightStyleTable;
public INetworkStringTable? UserInfoTable;
public INetworkStringTable? ServerStartupTable;
public PureServerWhitelist? PureServerWhitelist; // note: GMod never sets it
public INetworkStringTable? DynamicModelsTable;
public INetworkStringTable? DownloadableFileTable;

Expand Down Expand Up @@ -259,6 +260,9 @@ public override bool HookClientStringTable(ReadOnlySpan<char> tableName) {
case Protocol.USER_INFO_TABLENAME:
UserInfoTable = table;
return true;
case Protocol.SERVER_STARTUP_DATA_TABLENAME:
ServerStartupTable = table;
return true;
case Protocol.DOWNLOADABLE_FILE_TABLENAME:
DownloadableFileTable = table;
return true;
Expand Down Expand Up @@ -919,6 +923,8 @@ public void FinishSignonState_New() {
modelloader.ResetModelServerCounts();
SetModel(1);

CL.CheckForPureServerWhitelist();

CL.RegisterResources();

if (host_state.WorldModel == null) {
Expand Down
78 changes: 77 additions & 1 deletion Source.Engine/SV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,83 @@ public partial class SV(IServiceProvider services, Cbuf Cbuf, ED ED, Host Host,
EngineVGui EngineVGui => field ??= Singleton<EngineVGui>();
#endif

public static readonly ConVar sv_pure_kick_clients = new("sv_pure_kick_clients", "1", 0, "If set to 1, the server will kick clients with mismatching files. Otherwise, it will issue a warning to the client.");
#if GMOD_DLL
static int g_sv_pure_mode = -2;

static int GetSvPureModeFromCommandLine() {
if (commandLine.CheckParm("+sv_pure"))
return commandLine.ParmValue("+sv_pure", 1);

if (commandLine.CheckParm("-sv_pure"))
return commandLine.ParmValue("-sv_pure", 1);

return -1;
}

public static int GetSvPureMode() {
if (g_sv_pure_mode == -2)
g_sv_pure_mode = GetSvPureModeFromCommandLine();

return g_sv_pure_mode;
}
#else
static int g_sv_pure_mode;

public static int GetSvPureMode() => g_sv_pure_mode;
#endif

[ConCommand(helpText: "Show user data.")]
void sv_pure(in TokenizedCommand args) {
int pureMode = -2;
if (args.ArgC() == 2)
pureMode = atoi(args[1]);

Msg("--------------------------------------------------------\n");
if (pureMode >= -1 && pureMode <= 2) {
#if GMOD_DLL
if (pureMode > 0 && GetSvPureModeFromCommandLine() <= 0) {
Msg("sv_pure must be specified on the command line to function properly. sv_pure mode not changed\n");
return;
}
#endif

if (pureMode == GetSvPureMode())
Msg("sv_pure value unchanged (current value is %d).\n", GetSvPureMode());
else {
g_sv_pure_mode = pureMode;
Msg("sv_pure set to %d.\n", g_sv_pure_mode);

#if GMOD_DLL
if (sv.IsActive())
Msg("Note: Changes to sv_pure take effect when the next map is loaded.\n");
#endif
}
}
else
Msg("sv_pure: Only allow client to use certain files.\n" +
"\n" +
" -1 - Do not apply any rules or restrict which files the client may load.\n" +
" 0 - Apply rules in cfg/pure_server_minimal.txt only.\n" +
" 1 - Apply rules in cfg/pure_server_full.txt and then cfg/pure_server_whitelist.txt.\n" +
" 2 - Apply rules in cfg/pure_server_full.txt.\n" +
"\n" +
" See cfg/pure_server_whitelist_example.txt for more details.\n");

if (pureMode == -2) {
#if !SWDS
if (cl.IsConnected()) {
Msg("\n\n");
CL.PrintWhitelistInfo();
}
else
#endif
Msg("\nCurrent sv_pure value is %d.\n", GetSvPureMode());
}

Msg("--------------------------------------------------------\n");
}

public static readonly ConVar sv_pure_kick_clients = new("sv_pure_kick_clients", "1", 0,"If set to 1, the server will kick clients with mismatching files. Otherwise, it will issue a warning to the client.");
public static readonly ConVar sv_pure_trace = new("sv_pure_trace", "0", 0, "If set to 1, the server will print a message whenever a client is verifying a CRC for a file.");
public static readonly ConVar sv_pure_consensus = new("sv_pure_consensus", "5", 0, "Minimum number of file hashes to agree to form a consensus.");
public static readonly ConVar sv_pure_retiretime = new("sv_pure_retiretime", "900", 0, "Seconds of server idle time to flush the sv_pure file hash cache.");
Expand Down
4 changes: 4 additions & 0 deletions Source.Engine/Server/GameClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ protected override bool ProcessRespondCvarValue(CLC_RespondCvarValue msg) {


protected override bool ProcessFileCRCCheck(CLC_FileCRCCheck msg) {
#if GMOD_DLL
return true;
#else
if (!sv.IsInPureServerMode())
return true;

Expand Down Expand Up @@ -198,6 +201,7 @@ protected override bool ProcessFileCRCCheck(CLC_FileCRCCheck msg) {
}

return true;
#endif
}

protected override bool ProcessFileMD5Check(CLC_FileMD5Check msg) {
Expand Down
33 changes: 31 additions & 2 deletions Source.Engine/Server/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Source.Common.Engine;
using Source.Common.Filesystem;
using Source.Common.Formats.BSP;
using Source.Common.Formats.Keyvalues;
using Source.Common.Networking;
using Source.Common.Server;
using Source.Common.Utilities;
Expand Down Expand Up @@ -80,6 +81,34 @@ protected override BaseClient CreateNewClient(int slot) {
public void SetQueryPortFromSteamServer() {
// todo
}

public void CopyPureServerWhitelistToStringTable() {
if (PureServerWhitelist == null)
return;

UtlBuffer buf = new();
PureServerWhitelist.Encode(buf);
ServerStartupDataTable!.AddString(true, "PureServerWhitelist", buf.TellPut(), buf.Base().AsSpan(0, buf.TellPut()));
}

public void ReloadWhitelist(ReadOnlySpan<char> mapName) {
PureServerWhitelist = null;

if (GetMaxClients() <= 1)
return;

if (SV.GetSvPureMode() < 0)
return;

PureServerWhitelist = PureServerWhitelist.Create(g_pFileSystem);
PureServerWhitelist.Load(SV.GetSvPureMode());

if (SV.GetSvPureMode() == 1) {
KeyValues kv = new("");
if (kv.LoadFromFile(g_pFileSystem, $"maps/{mapName}_whitelist.txt"))
PureServerWhitelist.LoadCommandsFromKeyValues(kv);
}
}
internal bool IsLevelMainMenuBackground() {
return LevelMainMenuBackground;
}
Expand Down Expand Up @@ -147,7 +176,7 @@ public void CreateEngineStringTables() {
ServerStartupDataTable = StringTables.CreateStringTable(Protocol.SERVER_STARTUP_DATA_TABLENAME, 4);

SetQueryPortFromSteamServer();
// CopyPureServerWhitelistToStringTable();
CopyPureServerWhitelistToStringTable();

Assert(
DownloadableFileTable != null &&
Expand Down Expand Up @@ -589,7 +618,7 @@ internal bool SpawnServer(ReadOnlySpan<char> mapName, ReadOnlySpan<char> mapFile
}
#endif

// ReloadWhitelist(mapName);
ReloadWhitelist(mapName);
Common.TimestampedLog($"SV_SpawnServer({mapName})");
#if !SWDS
EngineVGui().UpdateProgressBar(LevelLoadingProgress.SpawnServer);
Expand Down
Loading
Loading