Skip to content
Merged
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
39 changes: 29 additions & 10 deletions apps/player-management-css/src/ConnectGate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@
hook.GetParam<nint>(6),
hook.GetParam<int>(7)
);

// A player this server kicked for a ban, still banned: refused
// here rather than loading in to be kicked again.
if (_rejoin.Refuse(steamId, _book, DateTimeOffset.UtcNow))
{
_loop?.Request();
Logger.LogInformation("refusing banned player {steamId} at connect", steamId);
Refuse(hook);

return HookResult.Continue;
}

eServerAccess access = _access.Decide(steamId.ToString());

if (access is eServerAccess.Unknown or eServerAccess.Open)
Expand All @@ -127,17 +139,9 @@
password != nint.Zero
);

if (
access == eServerAccess.Denied
&& ConnectRefusal.ByPassword(ConVar.Find("sv_password")?.StringValue)
)
{
hook.SetParam(5, RefusedPasswordBuffer);
}
else if (access == eServerAccess.Denied)
if (access == eServerAccess.Denied)
{
hook.SetParam(6, 0);
hook.SetParam(7, 0);
Refuse(hook);
}
else if (password != nint.Zero)
{
Expand All @@ -152,11 +156,26 @@
return HookResult.Continue;
}

// A wrong password on a password server, where the engine refuses it as
// one; elsewhere no auth ticket, which the engine refuses too.
private static void Refuse(DynamicHook hook)
{
if (ConnectRefusal.ByPassword(ConVar.Find("sv_password")?.StringValue))

Check warning on line 163 in apps/player-management-css/src/ConnectGate.cs

View workflow job for this annotation

GitHub Actions / player-management-build (player-management-css)

'ConVar' is obsolete: 'Use ConVar<T> instead for type-safe access to ConVars.'

Check warning on line 163 in apps/player-management-css/src/ConnectGate.cs

View workflow job for this annotation

GitHub Actions / player-management-build (player-management-css)

'ConVar' is obsolete: 'Use ConVar<T> instead for type-safe access to ConVars.'
{
hook.SetParam(5, RefusedPasswordBuffer);
}
else
{
hook.SetParam(6, 0);
hook.SetParam(7, 0);
}
}

// Read at connect time: the owner can change sv_password over RCON while
// the server hibernates, when nothing on a tick would notice.
private nint ServerPassword()
{
string password = ConVar.Find("sv_password")?.StringValue ?? "";

Check warning on line 178 in apps/player-management-css/src/ConnectGate.cs

View workflow job for this annotation

GitHub Actions / player-management-build (player-management-css)

'ConVar' is obsolete: 'Use ConVar<T> instead for type-safe access to ConVars.'

Check warning on line 178 in apps/player-management-css/src/ConnectGate.cs

View workflow job for this annotation

GitHub Actions / player-management-build (player-management-css)

'ConVar' is obsolete: 'Use ConVar<T> instead for type-safe access to ConVars.'

if (password.Length == 0)
{
Expand Down
1 change: 1 addition & 0 deletions apps/player-management-css/src/PlayerManagement.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Enums/eSanctionChange.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Enums/eServerAccess.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/SanctionBook.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/BannedRejoinGate.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/ServerAccessBook.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/ConnectRefusal.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/SanctionsClient.cs" LinkBase="Shared" />
Expand Down
5 changes: 5 additions & 0 deletions apps/player-management-css/src/PlayerManagementPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
private readonly HashSet<ulong> _mutedByUs = new();
private readonly HashSet<ulong> _kicked = new();

// Kept after they leave, unlike _kicked, so a rejoin is refused at connect.
private readonly BannedRejoinGate _rejoin = new();

private readonly HealthBook _health = new();

private long _lastEnforceMs;
Expand Down Expand Up @@ -348,6 +351,8 @@
{
if (_kicked.Add(steamId))
{
_rejoin.Kicked(steamId);

Logger.LogInformation(
"kicking banned player {name} ({steamId})",
player.PlayerName,
Expand Down Expand Up @@ -940,7 +945,7 @@
{
try
{
return ConVar.Find("mp_teammates_are_enemies")?.GetPrimitiveValue<bool>() ?? false;

Check warning on line 948 in apps/player-management-css/src/PlayerManagementPlugin.cs

View workflow job for this annotation

GitHub Actions / player-management-build (player-management-css)

'ConVar' is obsolete: 'Use ConVar<T> instead for type-safe access to ConVars.'

Check warning on line 948 in apps/player-management-css/src/PlayerManagementPlugin.cs

View workflow job for this annotation

GitHub Actions / player-management-build (player-management-css)

'ConVar' is obsolete: 'Use ConVar<T> instead for type-safe access to ConVars.'
}
catch (Exception)
{
Expand Down
1 change: 1 addition & 0 deletions apps/player-management-sw/src/PlayerManagement.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Enums/eSanctionChange.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Enums/eServerAccess.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/SanctionBook.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/BannedRejoinGate.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/ServerAccessBook.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/ConnectRefusal.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/SanctionsClient.cs" LinkBase="Shared" />
Expand Down
55 changes: 55 additions & 0 deletions apps/player-management-sw/src/PlayerManagementPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ public partial class PlayerManagementPlugin : BasePlugin
private readonly HashSet<ulong> _mutedByUs = new();
private readonly HashSet<ulong> _kicked = new();

// Kept after they leave, unlike _kicked, so a rejoin is refused at connect.
private readonly BannedRejoinGate _rejoin = new();

private readonly HealthBook _health = new();

private EventDelegates.OnTick? _tickHandler;
private EventDelegates.OnClientConnected? _connectHandler;
private EventDelegates.OnClientPutInServer? _putInServerHandler;
private EventDelegates.OnClientSteamAuthorize? _authorizeHandler;
private EventDelegates.OnClientDisconnected? _disconnectHandler;
Expand Down Expand Up @@ -112,6 +116,15 @@ public override void Load(bool hotReload)
_tickHandler = OnTick;
Core.Event.OnTick += _tickHandler;

_connectHandler = @event =>
{
if (RefuseBanned(@event.PlayerId))
{
@event.Result = HookResult.Stop;
}
};
Core.Event.OnClientConnected += _connectHandler;

_putInServerHandler = @event => OnJoined(@event.PlayerId);
Core.Event.OnClientPutInServer += _putInServerHandler;

Expand Down Expand Up @@ -226,6 +239,11 @@ public override void Unload()
Core.Event.OnTick -= _tickHandler;
}

if (_connectHandler != null)
{
Core.Event.OnClientConnected -= _connectHandler;
}

if (_putInServerHandler != null)
{
Core.Event.OnClientPutInServer -= _putInServerHandler;
Expand Down Expand Up @@ -329,6 +347,41 @@ private void OnJoined(int playerId)
_loop?.Request();
}

// A player this server kicked for a ban is refused on the way back in while
// the cache still holds the ban, instead of loading in to be kicked again.
// The refusal asks the panel about them, so a lifted ban lets the next try in.
private bool RefuseBanned(int playerId)
{
try
{
IPlayer? player = Core.PlayerManager.GetPlayer(playerId);

if (player == null || player.IsFakeClient)
{
return false;
}

ulong steamId = player.UnauthorizedSteamID;

if (!_rejoin.Refuse(steamId, _book, DateTimeOffset.UtcNow))
{
return false;
}

_loop?.Request();

_logger.LogInformation("refusing banned player {steamId} at connect", steamId);

return true;
}
catch (Exception error)
{
_logger.LogError(error, "ban check at connect failed; letting the connect through");

return false;
}
}

// Throttled off the tick rather than a repeating timer: SwiftlyS2 replays
// every missed run of a timer after hibernation, and a timer whose
// callback throws is never scheduled again.
Expand Down Expand Up @@ -392,6 +445,8 @@ private void Enforce(List<IPlayer> humans)
{
if (_kicked.Add(steamId))
{
_rejoin.Kicked(steamId);

_logger.LogInformation(
"kicking banned player {name} ({steamId})",
player.Name,
Expand Down
95 changes: 95 additions & 0 deletions apps/player-management-sw/test/BannedRejoinGateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using FiveStack.Entities.PlayerManagement;
using FiveStack.Utilities;
using Xunit;

public class BannedRejoinGateTests
{
private static readonly DateTimeOffset Now = new(2026, 11, 24, 12, 0, 0, TimeSpan.Zero);

private static SanctionBook Banned(string steamId, DateTimeOffset? expiresAt = null)
{
SanctionBook book = new();
book.Record(
[steamId],
[
new PlayerSanction
{
steam_id = steamId,
type = "ban",
expires_at = expiresAt,
},
]
);
return book;
}

[Fact]
public void RefusesAPlayerKickedForABanWhileTheBanHolds()
{
SanctionBook book = Banned("1");
BannedRejoinGate gate = new();
gate.Kicked(1);

Assert.True(gate.Refuse(1, book, Now));
Assert.True(gate.Refuse(1, book, Now));
}

// Asking the panel again is how a lifted ban gets back in.
[Fact]
public void ARefusalAsksThePanelAboutThePlayer()
{
SanctionBook book = Banned("1");
BannedRejoinGate gate = new();
gate.Kicked(1);

gate.Refuse(1, book, Now);

Assert.True(book.IsAwaiting("1"));
}

[Fact]
public void LetsInAPlayerThisServerNeverKicked()
{
SanctionBook book = Banned("1");
BannedRejoinGate gate = new();

Assert.False(gate.Refuse(1, book, Now));
Assert.False(book.IsAwaiting("1"));
}

[Fact]
public void LetsInAPlayerWhoseBanWasLiftedAndForgetsTheKick()
{
SanctionBook book = Banned("1");
BannedRejoinGate gate = new();
gate.Kicked(1);

book.Record(["1"], []);

Assert.False(gate.Refuse(1, book, Now));

book.Record(["1"], [new PlayerSanction { steam_id = "1", type = "ban" }]);

Assert.False(gate.Refuse(1, book, Now));
}

[Fact]
public void LetsInAPlayerWhoseBanRanOut()
{
SanctionBook book = Banned("1", Now.AddMinutes(-1));
BannedRejoinGate gate = new();
gate.Kicked(1);

Assert.False(gate.Refuse(1, book, Now));
}

[Fact]
public void LetsInAConnectWithNoSteamId()
{
SanctionBook book = Banned("0");
BannedRejoinGate gate = new();
gate.Kicked(0);

Assert.False(gate.Refuse(0, book, Now));
}
}
14 changes: 11 additions & 3 deletions apps/player-management-sw/test/ConnectRefusalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,18 @@ public void TheSwiftlyAccessGateRefusesWithThePasswordAndOnlyStripsTheTicketWith
[Fact]
public void TheCounterStrikeSharpAccessGateRefusesWithThePasswordAndOnlyStripsTheTicketWithoutOne()
{
string hook = Code("player-management-css", "ConnectGate.cs");

RefusesWithThePasswordBeforeStrippingTheTicket(
Code("player-management-css", "ConnectGate.cs"),
@"if\s*\(\s*access == eServerAccess\.Denied\s*&&\s*ConnectRefusal\.ByPassword\(ConVar\.Find\(""sv_password""\)\?\.StringValue\)\s*\)\s*\{\s*hook\.SetParam\(5, RefusedPasswordBuffer\);\s*\}",
@"else if \(access == eServerAccess\.Denied\)\s*\{\s*hook\.SetParam\(6, 0\);\s*hook\.SetParam\(7, 0\);"
hook,
@"if\s*\(\s*ConnectRefusal\.ByPassword\(ConVar\.Find\(""sv_password""\)\?\.StringValue\)\s*\)\s*\{\s*hook\.SetParam\(5, RefusedPasswordBuffer\);\s*\}",
@"else\s*\{\s*hook\.SetParam\(6, 0\);\s*hook\.SetParam\(7, 0\);"
);
// A denied player and a banned one coming back are refused the same way.
Assert.Matches(@"if \(access == eServerAccess\.Denied\)\s*\{\s*Refuse\(hook\);", hook);
Assert.Matches(
@"if \(_rejoin\.Refuse\(steamId, _book, DateTimeOffset\.UtcNow\)\)\s*\{[\s\S]*?Refuse\(hook\);",
hook
);
}
}
1 change: 1 addition & 0 deletions apps/player-management-sw/test/FiveStack.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Enums/eSanctionChange.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Enums/eServerAccess.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/SanctionBook.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/BannedRejoinGate.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/ServerAccessBook.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/ConnectRefusal.cs" LinkBase="Shared" />
<Compile Include="$(MSBuildThisFileDirectory)../../../shared/dotnet/FiveStack.Utilities/SanctionsClient.cs" LinkBase="Shared" />
Expand Down
45 changes: 45 additions & 0 deletions shared/dotnet/FiveStack.Utilities/BannedRejoinGate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace FiveStack.Utilities;

// A player a server kicked for a ban is refused at connect while the book
// still holds the ban, instead of loading in to be kicked again. Kept after
// they leave, unlike a plugin's kicked list. A refusal marks them awaiting so
// the panel is asked about them, and a lifted ban lets the next try in.
public sealed class BannedRejoinGate
{
private readonly object _lock = new();
private readonly HashSet<ulong> _kickedForBan = new();

public void Kicked(ulong steamId)
{
lock (_lock)
{
_kickedForBan.Add(steamId);
}
}

public bool Refuse(ulong steamId, SanctionBook book, DateTimeOffset now)
{
if (steamId == 0)
{
return false;
}

lock (_lock)
{
if (!_kickedForBan.Contains(steamId))
{
return false;
}

if (!book.StateFor(steamId.ToString(), now).IsBanned)
{
_kickedForBan.Remove(steamId);
return false;
}
}

book.Joined(steamId.ToString());

return true;
}
}
Loading