Skip to content

Commit

Permalink
Whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
DebugOk committed Feb 20, 2024
1 parent df9c2b6 commit 2c428b6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
55 changes: 54 additions & 1 deletion Content.Server/Connection/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ public sealed class ConnectionManager : IConnectionManager
[Dependency] private readonly ILocalizationManager _loc = default!;
[Dependency] private readonly ServerDbEntryManager _serverDbEntry = default!;

private List<NetUserId> _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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -206,5 +233,31 @@ private async Task NetMgrOnConnecting(NetConnectingArgs e)
await _db.AssignUserIdAsync(name, assigned);
return assigned;
}

/// <summary>
/// DeltaV - Soft whitelist improvements
/// Handles a completed connection, and stores the player if they're whitelisted and the whitelist is enabled
/// </summary>
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);
}
}

/// <summary>
/// DeltaV - Soft whitelist improvements
/// Handles a disconnection, and removes a stored player from the count if the whitelist is enabled
/// </summary>
private async void OnDisconnected(object? sender, NetChannelArgs e)
{
if (_cfg.GetCVar(CCVars.WhitelistEnabled))
{
_connectedWhitelistedPlayers.Remove(e.Channel.UserId);
}
}
}
}
6 changes: 3 additions & 3 deletions Resources/ConfigPresets/DeltaV/periapsis.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Resources/Locale/en-US/deltav/connection-messages.ftl
Original file line number Diff line number Diff line change
@@ -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/

0 comments on commit 2c428b6

Please sign in to comment.