Skip to content

Commit

Permalink
This is slow but works
Browse files Browse the repository at this point in the history
  • Loading branch information
NikoCat233 committed Apr 26, 2024
1 parent 1bd836f commit f07c2c2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
2 changes: 0 additions & 2 deletions src/Impostor.Server/Net/Manager/ClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ internal partial class ClientManager
private int _idLast;

public static Dictionary<string, string> _puids;

Check warning on line 29 in src/Impostor.Server/Net/Manager/ClientManager.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_puids' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
public static Dictionary<string, int> _onlineCount;

public ClientManager(ILogger<ClientManager> logger, IEventManager eventManager, IClientFactory clientFactory, ICompatibilityManager compatibilityManager, IOptions<CompatibilityConfig> compatibilityConfig)
{
Expand All @@ -36,7 +35,6 @@ public ClientManager(ILogger<ClientManager> logger, IEventManager eventManager,
_clientFactory = clientFactory;
_clients = new ConcurrentDictionary<int, ClientBase>();
_puids = new Dictionary<string, string>();
_onlineCount = new Dictionary<string, int>();
_compatibilityManager = compatibilityManager;
_compatibilityConfig = compatibilityConfig.Value;
}
Expand Down
33 changes: 13 additions & 20 deletions src/Impostor.Server/Net/State/Game.Incoming.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Impostor.Api.Games;
Expand Down Expand Up @@ -197,6 +198,18 @@ private async ValueTask<GameJoinResult> AddClientSafeAsync(ClientBase client)
return GameJoinResult.FromError(GameJoinError.GameDestroyed);
}

// Rejoining clients need to bypass this limit with isNew
if (Client._antiCheatConfig!.MaxOnlineFromSameIp != 0)
{
var count = _clientManager.Clients.Count(x => x.Connection.EndPoint.Address.ToString() == client.Connection.EndPoint.Address.ToString() && x.Player != null);

if (count > Client._antiCheatConfig!.MaxOnlineFromSameIp)
{
_logger.LogInformation("Client {0} ({1}) x {2} reached max clients online limit. Kicked.", client.Name, client.Connection.EndPoint.Address.ToString(), count);
return GameJoinResult.CreateCustomError(string.Format("[Impostor Anticheat+]\nToo many clients from a same ip.\n({0}) x {1}", client.Connection.EndPoint.Address.ToString(), count));
}
}

// Check if;
// - The player is already in this game.
// - The game is full.
Expand All @@ -221,26 +234,6 @@ private async ValueTask<GameJoinResult> AddClientSafeAsync(ClientBase client)
client.Player = clientPlayer;
}

// Rejoining clients need to bypass this limit with isNew
if (Client._antiCheatConfig!.MaxOnlineFromSameIp != 0 && isNew)
{
if (ClientManager._onlineCount.TryGetValue(client.Connection.EndPoint.Address.ToString(), out var count))
{
if (count >= Client._antiCheatConfig.MaxOnlineFromSameIp)
{
_logger.LogInformation("Client {0} ({1}) x {2} reached max clients online limit. Kicked.", client.Name, client.Connection.EndPoint.Address.ToString(), count);
return GameJoinResult.CreateCustomError(string.Format("[Impostor Anticheat+]\nToo many clients from a same ip.\n({0}) x {1}", client.Connection.EndPoint.Address.ToString(), count));
}

ClientManager._onlineCount[client.Connection.EndPoint.Address.ToString()] = count + 1;
}
else
{
ClientManager._onlineCount.TryAdd(client.Connection.EndPoint.Address.ToString(), 1);
ClientManager._onlineCount[client.Connection.EndPoint.Address.ToString()] = 1;
}
}

if (ClientManager._puids.TryGetValue(client.Connection.EndPoint.Address.ToString(), out var puid))
{
client.Puid = puid;
Expand Down
15 changes: 0 additions & 15 deletions src/Impostor.Server/Net/State/Game.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,6 @@ private async ValueTask<bool> PlayerRemove(int playerId, bool isBan = false)
return false;
}

if (Client._antiCheatConfig!.MaxOnlineFromSameIp != 0)
{
if (ClientManager._onlineCount.TryGetValue(player.Client.Connection.EndPoint.Address.ToString(), out var count))
{
if (count == 1)
{
ClientManager._onlineCount.Remove(player.Client.Connection.EndPoint.Address.ToString());
}
else
{
ClientManager._onlineCount[player.Client.Connection.EndPoint.Address.ToString()]--;
}
}
}

_logger.LogInformation("{0} - Player {1} ({2}) has left. hashpuid : {3}", Code, player.Client.Name, playerId, player.Client.HashedPuid());

if (GameState == GameStates.Starting || GameState == GameStates.Started || GameState == GameStates.NotStarted)
Expand Down

0 comments on commit f07c2c2

Please sign in to comment.