Skip to content

Commit

Permalink
fix: actually working ban functionality
Browse files Browse the repository at this point in the history
Changes:
- ignore new sockets from banned IP addresses way earlier.
- ignore all packets by banned profiles.

Intentionally keeping the connection open instead of d/c banned clients.
This is to prevent endless server logs due to automatically reconnecting clients.

Before:
Reconnecting clients aren't entering `ClientJoined` and therefore the d/c is only working on first connections.
Effectively banned clients got a d/c and then automatically reconnected again without getting a d/c again.
Therefore allowing them to play normally.
  • Loading branch information
Istador authored and piplup55 committed Sep 28, 2023
1 parent 5f9ea18 commit 1483e48
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions Server/BanLists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private static void Save() {


public static void Crash(Client user, bool permanent = false) {
user.Ignored = true;
Task.Run(async () => {
await user.Send(new ChangeStagePacket {
Id = (permanent ? "$agogus/ban4lyfe" : "$among$us/cr4sh%"),
Expand Down
1 change: 1 addition & 0 deletions Server/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Server;
public class Client : IDisposable {
public readonly ConcurrentDictionary<string, object?> Metadata = new ConcurrentDictionary<string, object?>(); // can be used to store any information about a player
public bool Connected = false;
public bool Ignored = false;
public CostumePacket? CurrentCostume = null; // required for proper client sync
public string Name {
get => Logger.Name;
Expand Down
3 changes: 0 additions & 3 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ async Task LoadShines()
await LoadShines();

server.ClientJoined += (c, _) => {
if (BanLists.Enabled && BanLists.IsClientBanned(c)) {
throw new Exception($"Banned player attempted join: {c.Name}");
}
c.Metadata["shineSync"] = new ConcurrentBag<int>();
c.Metadata["loadedSave"] = false;
c.Metadata["scenario"] = (byte?) 0;
Expand Down
22 changes: 21 additions & 1 deletion Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public async Task Listen(CancellationToken? token = null) {
Socket socket = token.HasValue ? await serverSocket.AcceptAsync(token.Value) : await serverSocket.AcceptAsync();
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);

if (BanLists.Enabled && BanLists.IsIPv4Banned(((IPEndPoint) socket.RemoteEndPoint!).Address!)) {
Logger.Warn($"Ignoring banned IPv4 address {socket.RemoteEndPoint}");
continue;
}

Logger.Warn($"Accepted connection for client {socket.RemoteEndPoint}");

try {
Expand Down Expand Up @@ -64,7 +69,7 @@ public async Task Listen(CancellationToken? token = null) {

public static void FillPacket<T>(PacketHeader header, T packet, Memory<byte> memory) where T : struct, IPacket {
Span<byte> data = memory.Span;

header.Serialize(data[..Constants.HeaderSize]);
packet.Serialize(data[Constants.HeaderSize..]);
}
Expand Down Expand Up @@ -167,13 +172,28 @@ async Task<bool> Read(Memory<byte> readMem, int readSize, int readOffset) {
break;
}

if (client.Ignored) {
memory.Dispose();
continue;
}

// connection initialization
if (first) {
first = false;
if (header.Type != PacketType.Connect) throw new Exception($"First packet was not init, instead it was {header.Type}");

ConnectPacket connect = new ConnectPacket();
connect.Deserialize(memory.Memory.Span[packetRange]);

if (BanLists.Enabled && BanLists.IsProfileBanned(header.Id)) {
client.Id = header.Id;
client.Name = connect.ClientName;
client.Ignored = true;
client.Logger.Warn($"Ignoring banned profile ID {header.Id}");
memory.Dispose();
continue;
}

lock (Clients) {
if (Clients.Count(x => x.Connected) == Settings.Instance.Server.MaxPlayers) {
client.Logger.Error($"Turned away as server is at max clients");
Expand Down

0 comments on commit 1483e48

Please sign in to comment.