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
55 changes: 35 additions & 20 deletions apps/player-management-css/src/ConnectGate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,42 @@
);

// 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))
// here on a password server. Without one, Enforce kicks them again
// (an empty ticket loops).
if (
_rejoin.Refuse(steamId, _book, DateTimeOffset.UtcNow)
&& ConnectRefusal.ByPassword(ConVar.Find("sv_password")?.StringValue)

Check warning on line 118 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 118 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.'
)
{
_loop?.Request();
Logger.LogInformation("refusing banned player {steamId} at connect", steamId);
Refuse(hook);
hook.SetParam(5, RefusedPasswordBuffer);

return HookResult.Continue;
}

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

if (steamId != 0)
{
// Its own guard: a failure here must not skip the password swap.
try
{
_access.Connecting(
steamId.ToString(),
access == eServerAccess.Denied,
Humans().Any(player =>
SteamIdOf(player) == steamId
&& player.Connected == PlayerConnectedState.Connected
)
);
}
catch (Exception error)
{
Logger.LogError(error, "access bookkeeping failed for {steamId}", steamId);
}
}

if (access is eServerAccess.Unknown or eServerAccess.Open)
{
return HookResult.Continue;
Expand All @@ -139,9 +163,15 @@
password != nint.Zero
);

if (access == eServerAccess.Denied)
// Without a password there is no clean refusal at the door (an
// empty ticket loops), so a refused player is let in and kicked
// with the reason by Enforce.
if (
access == eServerAccess.Denied
&& ConnectRefusal.ByPassword(ConVar.Find("sv_password")?.StringValue)

Check warning on line 171 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 171 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.'
)
{
Refuse(hook);
hook.SetParam(5, RefusedPasswordBuffer);
}
else if (password != nint.Zero)
{
Expand All @@ -156,26 +186,11 @@
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))
{
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 193 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 193 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
71 changes: 50 additions & 21 deletions apps/player-management-css/src/PlayerManagementPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using CounterStrikeSharp.API.Core.Translations;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.ValveConstants.Protobuf;
using FiveStack.Entities.PlayerManagement;
using FiveStack.Enums;
Expand Down Expand Up @@ -336,6 +337,48 @@
{
ulong steamId = SteamIdOf(player);

// Before the awaiting skip: a refused player must not play while
// the sanctions answer is still out. CounterStrikeSharp disconnects
// with a reason code only, so the reason is shown once the player
// is in game and the kick follows a few seconds later.
if (_access.IsDenied(steamId.ToString()))
{
if (player.Connected == PlayerConnectedState.Connected && _kicked.Add(steamId))
{
Logger.LogInformation(
"kicking {name} ({steamId}): not on the server's access list",
player.PlayerName,
steamId
);

string reason = _access.KickReason(
Localizer.ForPlayer(player, "access.denied")
);
player.ChangeTeam(CsTeam.Spectator);
player.PrintToChat(reason);
player.PrintToCenter(reason);

AddTimer(
RefusedKickDelaySeconds,
() =>
{
if (
player.IsValid
&& SteamIdOf(player) == steamId
&& _access.IsDenied(steamId.ToString())
)
{
player.Disconnect(
NetworkDisconnectionReason.NETWORK_DISCONNECT_KICKED
);
}
}
);
}

continue;
}

if (_book.IsAwaiting(steamId.ToString()))
{
continue;
Expand Down Expand Up @@ -365,27 +408,6 @@
continue;
}

// CounterStrikeSharp disconnects with a reason code only, so the
// text goes to chat on the way out.
if (_access.IsDenied(steamId.ToString()))
{
if (_kicked.Add(steamId))
{
Logger.LogInformation(
"kicking {name} ({steamId}): not on the server's access list",
player.PlayerName,
steamId
);

player.PrintToChat(
_access.KickReason(Localizer.ForPlayer(player, "access.denied"))
);
player.Disconnect(NetworkDisconnectionReason.NETWORK_DISCONNECT_KICKED);
}

continue;
}

// Re-asserted rather than set once: the engine resets voice flags
// across a reconnect and a map change.
if (state.IsMuted && !player.VoiceFlags.HasFlag(VoiceFlags.Muted))
Expand Down Expand Up @@ -945,7 +967,7 @@
{
try
{
return ConVar.Find("mp_teammates_are_enemies")?.GetPrimitiveValue<bool>() ?? false;

Check warning on line 970 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 970 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 Expand Up @@ -982,6 +1004,11 @@
return HookResult.Continue;
}

if (_access.IsDenied(SteamIdOf(player).ToString()))
{
return HookResult.Stop;
}

SanctionState state = _book.StateFor(SteamIdOf(player).ToString(), DateTimeOffset.UtcNow);

if (!state.IsGagged)
Expand Down Expand Up @@ -1012,6 +1039,8 @@
);
}

private const float RefusedKickDelaySeconds = 4f;

private static List<CCSPlayerController> Humans()
{
return Utilities.GetPlayers().Where(IsHuman).ToList();
Expand Down
27 changes: 12 additions & 15 deletions apps/player-management-sw/src/ConnectGate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,9 @@ private void InstallConnectGate()
);
}

if (access == eServerAccess.Denied)
{
return next()(
server,
name,
address,
netInfo,
connectMsg,
password,
nint.Zero,
0,
isLowViolence
);
}

// Without a password there is no clean refusal at the door (an
// empty ticket loops), so the player is let in and kicked
// with the reason by Enforce.
return next()(
server,
name,
Expand Down Expand Up @@ -199,6 +187,15 @@ int authTicketLength
ulong steamId = ServerAccessBook.TicketSteamId(authTicket, authTicketLength);
eServerAccess access = _access.Decide(steamId.ToString());

if (steamId != 0)
{
_access.Connecting(
steamId.ToString(),
access == eServerAccess.Denied,
Humans().Any(player => SteamIdOf(player) == steamId)
);
}

if (access is eServerAccess.Unknown or eServerAccess.Open)
{
return (access, nint.Zero);
Expand Down
45 changes: 26 additions & 19 deletions apps/player-management-sw/src/PlayerManagementPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,27 @@ private void Enforce(List<IPlayer> humans)
{
ulong steamId = SteamIdOf(player);

// Before the awaiting skip: a refused player must not play while
// the sanctions answer is still out.
if (_access.IsDenied(steamId.ToString()))
{
if (_kicked.Add(steamId))
{
_logger.LogInformation(
"kicking {name} ({steamId}): not on the server's access list",
player.Name,
steamId
);

player.Kick(
_access.KickReason(LocalizerFor(player)["access.denied"]),
ENetworkDisconnectionReason.NETWORK_DISCONNECT_KICKED
);
}

continue;
}

if (_book.IsAwaiting(steamId.ToString()))
{
continue;
Expand Down Expand Up @@ -462,25 +483,6 @@ private void Enforce(List<IPlayer> humans)
continue;
}

if (_access.IsDenied(steamId.ToString()))
{
if (_kicked.Add(steamId))
{
_logger.LogInformation(
"kicking {name} ({steamId}): not on the server's access list",
player.Name,
steamId
);

player.Kick(
_access.KickReason(LocalizerFor(player)["access.denied"]),
ENetworkDisconnectionReason.NETWORK_DISCONNECT_KICKED
);
}

continue;
}

// Re-asserted rather than set once: the engine resets voice flags
// across a reconnect and a map change.
if (state.IsMuted && !player.VoiceFlags.HasFlag(VoiceFlagValue.Muted))
Expand Down Expand Up @@ -1146,6 +1148,11 @@ private HookResult OnChat(int playerId)
return HookResult.Continue;
}

if (_access.IsDenied(SteamIdOf(player).ToString()))
{
return HookResult.Stop;
}

SanctionState state = _book.StateFor(SteamIdOf(player).ToString(), DateTimeOffset.UtcNow);

if (!state.IsGagged)
Expand Down
72 changes: 48 additions & 24 deletions apps/player-management-sw/test/ConnectRefusalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,45 +48,69 @@ public void TheRefusedPasswordIsOneNobodyCouldSet()
Assert.Contains(ConnectRefusal.Password, character => char.IsControl(character));
}

private static void RefusesWithThePasswordBeforeStrippingTheTicket(string hook, string byPassword, string stripped)
// Without a password the Swiftly gate lets the client in and kicks it
// with the reason, so it never strips the ticket.
[Fact]
public void TheSwiftlyAccessGateRefusesWithThePasswordAndOtherwiseMarksTheKick()
{
Match password = Regex.Match(hook, byPassword);
Match ticket = Regex.Match(hook, stripped);
string hook = Code("player-management-sw", "ConnectGate.cs");

Assert.True(password.Success, "the refused password path is gone or changed");
Assert.True(ticket.Success, "the empty ticket path is gone or changed");
Assert.True(password.Index < ticket.Index);
Assert.Matches(
@"RefusedPasswordBuffer = Marshal\.StringToCoTaskMemUTF8\(\s*ConnectRefusal\.Password\s*\)",
@"if\s*\(\s*access == eServerAccess\.Denied\s*&&\s*ConnectRefusal\.ByPassword\(\s*Core\.ConVar\.Find<string>\(""sv_password""\)\?\.Value\s*\)\s*\)\s*\{\s*return next\(\)\(\s*server,\s*name,\s*address,\s*netInfo,\s*connectMsg,\s*RefusedPasswordBuffer,\s*authTicket,\s*authTicketLength,\s*isLowViolence\s*\);",
hook
);
}

[Fact]
public void TheSwiftlyAccessGateRefusesWithThePasswordAndOnlyStripsTheTicketWithoutOne()
{
RefusesWithThePasswordBeforeStrippingTheTicket(
Code("player-management-sw", "ConnectGate.cs"),
@"if\s*\(\s*access == eServerAccess\.Denied\s*&&\s*ConnectRefusal\.ByPassword\(\s*Core\.ConVar\.Find<string>\(""sv_password""\)\?\.Value\s*\)\s*\)\s*\{\s*return next\(\)\(\s*server,\s*name,\s*address,\s*netInfo,\s*connectMsg,\s*RefusedPasswordBuffer,\s*authTicket,\s*authTicketLength,\s*isLowViolence\s*\);",
@"if\s*\(access == eServerAccess\.Denied\)\s*\{\s*return next\(\)\(\s*server,\s*name,\s*address,\s*netInfo,\s*connectMsg,\s*password,\s*nint\.Zero,\s*0,"
Assert.DoesNotMatch(@"nint\.Zero,\s*0,\s*isLowViolence", hook);
Assert.Matches(
@"_access\.Connecting\(\s*steamId\.ToString\(\),\s*access == eServerAccess\.Denied,",
hook
);
}

[Fact]
public void TheCounterStrikeSharpAccessGateRefusesWithThePasswordAndOnlyStripsTheTicketWithoutOne()
public void TheCounterStrikeSharpAccessGateRefusesWithThePasswordAndOtherwiseMarksTheKick()
{
string hook = Code("player-management-css", "ConnectGate.cs");

RefusesWithThePasswordBeforeStrippingTheTicket(
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\);"
Assert.Matches(
@"if\s*\(\s*access == eServerAccess\.Denied\s*&&\s*ConnectRefusal\.ByPassword\(ConVar\.Find\(""sv_password""\)\?\.StringValue\)\s*\)\s*\{\s*hook\.SetParam\(5, RefusedPasswordBuffer\);\s*\}",
hook
);
Assert.Matches(
@"RefusedPasswordBuffer = Marshal\.StringToCoTaskMemUTF8\(\s*ConnectRefusal\.Password\s*\)",
hook
);
// 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.DoesNotMatch(@"hook\.SetParam\(\s*[67]\s*,", hook);
Assert.Matches(
@"if \(_rejoin\.Refuse\(steamId, _book, DateTimeOffset\.UtcNow\)\)\s*\{[\s\S]*?Refuse\(hook\);",
@"_access\.Connecting\(\s*steamId\.ToString\(\),\s*access == eServerAccess\.Denied,\s*Humans\(\)\.Any\(",
hook
);
// A banned player coming back is refused the same way, only on a password server.
Assert.Matches(
@"_rejoin\.Refuse\(steamId, _book, DateTimeOffset\.UtcNow\)\s*&&\s*ConnectRefusal\.ByPassword\(ConVar\.Find\(""sv_password""\)\?\.StringValue\)\s*\)\s*\{[\s\S]*?hook\.SetParam\(5, RefusedPasswordBuffer\);",
hook
);
}

// A refused player is kicked even while the sanctions answer is out, and
// says nothing on the way.
[Theory]
[InlineData("player-management-sw")]
[InlineData("player-management-css")]
public void ARefusedPlayerIsKickedBeforeTheSanctionsAnswerAndCannotChat(string app)
{
string plugin = Code(app, "PlayerManagementPlugin.cs");

int enforce = plugin.IndexOf("private void Enforce(", StringComparison.Ordinal);
Assert.True(enforce >= 0, "Enforce is gone or renamed");

int denied = plugin.IndexOf("_access.IsDenied(steamId.ToString())", enforce, StringComparison.Ordinal);
int awaiting = plugin.IndexOf("_book.IsAwaiting(steamId.ToString())", enforce, StringComparison.Ordinal);
Assert.True(denied >= 0 && awaiting >= 0);
Assert.True(denied < awaiting, "the access kick must come before the awaiting skip");

Assert.Matches(
@"if\s*\(_access\.IsDenied\(SteamIdOf\(player\)\.ToString\(\)\)\)\s*\{\s*return HookResult\.Stop;\s*\}\s*SanctionState state = _book\.StateFor\(",
plugin
);
}
}
Loading
Loading