Add RangeBan
This commit is contained in:
@@ -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; } = {};
|
||||
}
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user