From 2c428b64223301060470e76fd3eeddb32f78a2a0 Mon Sep 17 00:00:00 2001 From: Debug <49997488+DebugOk@users.noreply.github.com> Date: Tue, 20 Feb 2024 20:30:22 +0100 Subject: [PATCH] Whitelist --- .../Connection/ConnectionManager.cs | 55 ++++++++++++++++++- Resources/ConfigPresets/DeltaV/periapsis.toml | 6 +- .../en-US/deltav/connection-messages.ftl | 1 + 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 Resources/Locale/en-US/deltav/connection-messages.ftl diff --git a/Content.Server/Connection/ConnectionManager.cs b/Content.Server/Connection/ConnectionManager.cs index f0dd87e2bd0..e93f4c0ddf1 100644 --- a/Content.Server/Connection/ConnectionManager.cs +++ b/Content.Server/Connection/ConnectionManager.cs @@ -31,9 +31,13 @@ public sealed class ConnectionManager : IConnectionManager [Dependency] private readonly ILocalizationManager _loc = default!; [Dependency] private readonly ServerDbEntryManager _serverDbEntry = default!; + private List _connectedWhitelistedPlayers = new(); // DeltaV - Soft whitelist improvements + public void Initialize() { _netMgr.Connecting += NetMgrOnConnecting; + _netMgr.Connected += OnConnected; // DeltaV - Soft whitelist improvements + _netMgr.Disconnect += OnDisconnected; // DeltaV - Soft whitelist improvements _netMgr.AssignUserIdCallback = AssignUserIdCallback; // Approval-based IP bans disabled because they don't play well with Happy Eyeballs. // _netMgr.HandleApprovalCallback = HandleApproval; @@ -169,7 +173,8 @@ private async Task NetMgrOnConnecting(NetConnectingArgs e) return (ConnectionDenyReason.Ban, message, bans); } - if (_cfg.GetCVar(CCVars.WhitelistEnabled)) + // DeltaV - Replace existing softwhitelist implementation + if (false) //_cfg.GetCVar(CCVars.WhitelistEnabled)) { var min = _cfg.GetCVar(CCVars.WhitelistMinPlayers); var max = _cfg.GetCVar(CCVars.WhitelistMaxPlayers); @@ -186,6 +191,28 @@ private async Task NetMgrOnConnecting(NetConnectingArgs e) } } + // DeltaV - Soft whitelist improvements + if (_cfg.GetCVar(CCVars.WhitelistEnabled)) + { + var connectedPlayers = _plyMgr.PlayerCount; + var connectedWhitelist = _connectedWhitelistedPlayers.Count; + + var slots = _cfg.GetCVar(CCVars.WhitelistMinPlayers); + + var nonWhitelistAllowed = slots > 0 && slots < connectedPlayers - connectedWhitelist; + + if (nonWhitelistAllowed && await _db.GetWhitelistStatusAsync(userId) == false + && adminData is null) + { + var msg = Loc.GetString(_cfg.GetCVar(CCVars.WhitelistReason)); + + if (slots > 0) + msg += "\n" + Loc.GetString("whitelist-playercount-invalid", ("min", slots), ("max", _cfg.GetCVar(CCVars.SoftMaxPlayers))); + + return (ConnectionDenyReason.Whitelist, msg, null); + } + } + return null; } @@ -206,5 +233,31 @@ private async Task NetMgrOnConnecting(NetConnectingArgs e) await _db.AssignUserIdAsync(name, assigned); return assigned; } + + /// + /// DeltaV - Soft whitelist improvements + /// Handles a completed connection, and stores the player if they're whitelisted and the whitelist is enabled + /// + private async void OnConnected(object? sender, NetChannelArgs e) + { + var userId = e.Channel.UserId; + + if (_cfg.GetCVar(CCVars.WhitelistEnabled) && await _db.GetWhitelistStatusAsync(userId)) + { + _connectedWhitelistedPlayers.Add(userId); + } + } + + /// + /// DeltaV - Soft whitelist improvements + /// Handles a disconnection, and removes a stored player from the count if the whitelist is enabled + /// + private async void OnDisconnected(object? sender, NetChannelArgs e) + { + if (_cfg.GetCVar(CCVars.WhitelistEnabled)) + { + _connectedWhitelistedPlayers.Remove(e.Channel.UserId); + } + } } } diff --git a/Resources/ConfigPresets/DeltaV/periapsis.toml b/Resources/ConfigPresets/DeltaV/periapsis.toml index 1cf958e9a7f..a1cdf275a3f 100644 --- a/Resources/ConfigPresets/DeltaV/periapsis.toml +++ b/Resources/ConfigPresets/DeltaV/periapsis.toml @@ -11,9 +11,9 @@ preset_enabled = true map_enabled = true [whitelist] -enabled = false -reason = "whitelist-not-whitelisted" -min_players = 40 +enabled = true +reason = "whitelist-not-whitelisted-peri" +min_players = 10 [ooc] enable_during_round = true diff --git a/Resources/Locale/en-US/deltav/connection-messages.ftl b/Resources/Locale/en-US/deltav/connection-messages.ftl new file mode 100644 index 00000000000..5d2ea9b1511 --- /dev/null +++ b/Resources/Locale/en-US/deltav/connection-messages.ftl @@ -0,0 +1 @@ +whitelist-not-whitelisted-peri = You are not whitelisted. To become whitelisted, apply on our forum. It can be found at https://forum.delta-v.org/