Add RangeBan
This commit is contained in:
parent
a784ec1ff8
commit
e099ee5071
27
RangeBan.Tests/RangeBan.Tests.csproj
Normal file
27
RangeBan.Tests/RangeBan.Tests.csproj
Normal file
@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
|
||||
<PackageReference Include="NUnit" Version="4.3.2" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="4.4.0"/>
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="NUnit.Framework"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\RangeBan\RangeBan.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
61
RangeBan.Tests/UnitTest1.cs
Normal file
61
RangeBan.Tests/UnitTest1.cs
Normal file
@ -0,0 +1,61 @@
|
||||
namespace RangeBan.Tests;
|
||||
|
||||
[TestFixture]
|
||||
public class IpRangeTests
|
||||
{
|
||||
[Test]
|
||||
public void IsInRange_WithNullInput_ReturnsFalse()
|
||||
{
|
||||
Assert.That(RangeBan.IsInRange("192.168.1.0/24", null), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsInRange_WithEmptyInput_ReturnsFalse()
|
||||
{
|
||||
Assert.That(RangeBan.IsInRange("192.168.1.0/24", ""), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsInRange_WithInvalidRange_ReturnsFalse()
|
||||
{
|
||||
Assert.That(RangeBan.IsInRange("invalid-range", "192.168.1.1"), Is.False);
|
||||
}
|
||||
|
||||
[TestCase("192.168.1.0/24", "192.168.1.1", true)]
|
||||
[TestCase("192.168.1.0/24", "192.168.1.254", true)]
|
||||
[TestCase("192.168.1.0/24", "192.168.2.1", false)]
|
||||
[TestCase("192.168.1.0/24", "10.0.0.1", false)]
|
||||
[TestCase("176.2.0.0/16", "176.2.73.23", true)]
|
||||
[TestCase("176.2.0.0/16", "176.2.70.50", true)]
|
||||
public void IsInRange_WithCidrNotation_ReturnsExpectedResult(string range, string ip, bool expected)
|
||||
{
|
||||
Assert.That(RangeBan.IsInRange(range, ip), Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("192.168.2.0/33")]
|
||||
[TestCase("192.168.1.0/invalid")]
|
||||
public void IsInRange_WithInvalidCidrNotation_ReturnsFalse(string range)
|
||||
{
|
||||
Assert.That(RangeBan.IsInRange(range, "192.168.1.1"), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsInRange_WithInvalidIpAddress_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => RangeBan.IsInRange("192.168.1.0/24", "invalid.ip.address"));
|
||||
}
|
||||
|
||||
[TestCase("192.168.1.0/24", "192.168.1")]
|
||||
[TestCase("192.168.1.0/24", "192.168.1.1.1")]
|
||||
[TestCase("192.168.1.0/24", "not.an.ip.address")]
|
||||
public void IsInRange_WithMalformedIpAddress_ThrowsArgumentException(string range, string ip)
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => RangeBan.IsInRange(range, ip));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsInRange_WithValidPublicIp_DoesNotThrow()
|
||||
{
|
||||
Assert.DoesNotThrow(() => RangeBan.IsInRange("192.168.1.0/24", "8.8.8.8"));
|
||||
}
|
||||
}
|
94
RangeBan/RangeBan.cs
Normal file
94
RangeBan/RangeBan.cs
Normal file
@ -0,0 +1,94 @@
|
||||
using System.Runtime.Serialization;
|
||||
using LabApi.Events.Arguments.PlayerEvents;
|
||||
using LabApi.Events.Handlers;
|
||||
using LabApi.Features;
|
||||
using LabApi.Features.Console;
|
||||
using LabApi.Loader.Features.Plugins;
|
||||
|
||||
namespace RangeBan;
|
||||
|
||||
public class RangeBan: Plugin<RangeBanConfig>
|
||||
{
|
||||
|
||||
public override void Enable()
|
||||
{
|
||||
Logger.Debug("Loading...");
|
||||
PlayerEvents.PreAuthenticating += OnAuth;
|
||||
}
|
||||
|
||||
public override void Disable()
|
||||
{
|
||||
Logger.Debug("Disabling...");
|
||||
PlayerEvents.PreAuthenticating -= OnAuth;
|
||||
}
|
||||
|
||||
private void OnAuth(PlayerPreAuthenticatingEventArgs ev)
|
||||
{
|
||||
Logger.Debug($"Ranges: {string.Join(" ; ", Config!.IpRanges)}");
|
||||
if (!Config!.IpRanges.Any(configIpRange => IsInRange(configIpRange, ev.IpAddress))) return;
|
||||
ev.RejectCustom("Your IP belongs to a banned player, please contact the server administrator for more information.");
|
||||
Logger.Warn($"Player with IP {ev.IpAddress} got kicked. UserId: {ev.UserId}");
|
||||
}
|
||||
|
||||
public override string Name => "RangeBan";
|
||||
public override string Author => "Code002Lover";
|
||||
public override Version Version { get; } = new(1, 0, 0);
|
||||
public override string Description => "Ban IP Ranges with ease";
|
||||
public override Version RequiredApiVersion { get; } = new(LabApiProperties.CompiledVersion);
|
||||
|
||||
|
||||
public static bool IsInRange(string range, string ip)
|
||||
{
|
||||
if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(range))
|
||||
return false;
|
||||
|
||||
// Handle CIDR notation (e.g., "192.168.1.0/24")
|
||||
if (!range.Contains("/"))
|
||||
{
|
||||
//We only handle direct IPs and CIDR
|
||||
if (range.Split('.').Length != 4)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return ip == range;
|
||||
};
|
||||
|
||||
var parts = range.Split('/');
|
||||
if (parts.Length != 2 || !int.TryParse(parts[1], out var cidrBits))
|
||||
return false;
|
||||
|
||||
if (cidrBits > 32)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var networkAddress = IPToUInt32(parts[0]);
|
||||
var mask = uint.MaxValue << (32 - cidrBits);
|
||||
var ipAddress = IPToUInt32(ip);
|
||||
|
||||
return (ipAddress & mask) == (networkAddress & mask);
|
||||
|
||||
}
|
||||
|
||||
private static uint IPToUInt32(string ipAddress)
|
||||
{
|
||||
var parts = ipAddress.Split('.');
|
||||
if (parts.Length != 4)
|
||||
throw new ArgumentException("Invalid IP address format");
|
||||
|
||||
uint result = 0;
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
if (!byte.TryParse(parts[i], out var part))
|
||||
throw new ArgumentException("Invalid IP address segment");
|
||||
result = (result << 8) | part;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class RangeBanConfig
|
||||
{
|
||||
public string[] IpRanges { get; set; } = {};
|
||||
}
|
37
RangeBan/RangeBan.csproj
Normal file
37
RangeBan/RangeBan.csproj
Normal file
@ -0,0 +1,37 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net48</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<Optimize>true</Optimize>
|
||||
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
|
||||
<DebugType>full</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<Optimize>true</Optimize>
|
||||
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
|
||||
<DebugType>none</DebugType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="Assembly-CSharp">
|
||||
<HintPath>..\..\.local\share\Steam\steamapps\common\SCP Secret Laboratory Dedicated Server\SCPSL_Data\Managed\Assembly-CSharp.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Mirror">
|
||||
<HintPath>..\..\.local\share\Steam\steamapps\common\SCP Secret Laboratory Dedicated Server\SCPSL_Data\Managed\Mirror.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="UnityEngine.CoreModule">
|
||||
<HintPath>..\..\.local\share\Steam\steamapps\common\SCP Secret Laboratory Dedicated Server\SCPSL_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Northwood.LabAPI" Version="1.0.2" />
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -12,6 +12,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SCPTeamHint", "SCPTeamHint\
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LogEvents", "LogEvents\LogEvents.csproj", "{2C9FD537-231C-4486-8C1B-6359E5120D19}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RangeBan", "RangeBan\RangeBan.csproj", "{17798161-3317-4E64-880D-1CD7DE0EBE56}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RangeBan.Tests", "RangeBan.Tests\RangeBan.Tests.csproj", "{AA495D0E-0122-4C26-8D26-C728B65BFF12}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -42,5 +46,13 @@ Global
|
||||
{2C9FD537-231C-4486-8C1B-6359E5120D19}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2C9FD537-231C-4486-8C1B-6359E5120D19}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2C9FD537-231C-4486-8C1B-6359E5120D19}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{17798161-3317-4E64-880D-1CD7DE0EBE56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{17798161-3317-4E64-880D-1CD7DE0EBE56}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{17798161-3317-4E64-880D-1CD7DE0EBE56}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{17798161-3317-4E64-880D-1CD7DE0EBE56}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{AA495D0E-0122-4C26-8D26-C728B65BFF12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{AA495D0E-0122-4C26-8D26-C728B65BFF12}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{AA495D0E-0122-4C26-8D26-C728B65BFF12}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{AA495D0E-0122-4C26-8D26-C728B65BFF12}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
Loading…
x
Reference in New Issue
Block a user