Skip to content

Commit

Permalink
Add sameiponlineclient limit
Browse files Browse the repository at this point in the history
  • Loading branch information
NikoCat233 committed Apr 25, 2024
1 parent 9323150 commit 30c4f49
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/Impostor.Api/Config/AntiCheatConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class AntiCheatConfig

public bool EnableTargetChecks { get; set; } = true;

// Set 0 = no limit
public int MaxOnlineFromSameIp { get; set; } = 1;

public bool ForbidProtocolExtensions { get; set; } = true;
}
}
2 changes: 1 addition & 1 deletion src/Impostor.Server/Net/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Client(ILogger<Client> logger, IOptions<AntiCheatConfig> antiCheatOptions

public override async ValueTask<bool> ReportCheatAsync(CheatContext context, CheatCategory category, string message)
{
if (!_antiCheatConfig.Enabled)
if (!_antiCheatConfig!.Enabled)
{
return false;
}
Expand Down
2 changes: 0 additions & 2 deletions src/Impostor.Server/Net/ClientBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Impostor.Api;
using Impostor.Api.Innersloth;
Expand Down
2 changes: 2 additions & 0 deletions src/Impostor.Server/Net/Manager/ClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ 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;

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

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_onlineCount' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

public ClientManager(ILogger<ClientManager> logger, IEventManager eventManager, IClientFactory clientFactory, ICompatibilityManager compatibilityManager, IOptions<CompatibilityConfig> compatibilityConfig)
{
Expand All @@ -35,6 +36,7 @@ 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
18 changes: 18 additions & 0 deletions src/Impostor.Server/Net/State/Game.Incoming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ private async ValueTask<GameJoinResult> AddClientSafeAsync(ClientBase client)
return GameJoinResult.FromError(GameJoinError.Banned);
}

if (Client._antiCheatConfig!.MaxOnlineFromSameIp != 0)
{
if (ClientManager._onlineCount.TryGetValue(client.Connection.EndPoint.Address.ToString(), out var count))
{
if (count >= Client._antiCheatConfig.MaxOnlineFromSameIp)
{
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;
}
}

var player = client.Player;

// Check if the player is running the same version as the host
Expand Down
16 changes: 16 additions & 0 deletions src/Impostor.Server/Net/State/Game.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Impostor.Hazel;
using Impostor.Server.Events;
using Impostor.Server.Net.Hazel;
using Impostor.Server.Net.Manager;
using Microsoft.Extensions.Logging;

namespace Impostor.Server.Net.State
Expand Down Expand Up @@ -36,6 +37,21 @@ 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 30c4f49

Please sign in to comment.