refactor
This commit is contained in:
+20
-25
@@ -1,5 +1,4 @@
|
||||
using System.Runtime.Serialization;
|
||||
using LabApi.Events.Arguments.PlayerEvents;
|
||||
using LabApi.Events.Arguments.PlayerEvents;
|
||||
using LabApi.Events.Handlers;
|
||||
using LabApi.Features;
|
||||
using LabApi.Features.Console;
|
||||
@@ -7,9 +6,14 @@ using LabApi.Loader.Features.Plugins;
|
||||
|
||||
namespace RangeBan;
|
||||
|
||||
public class RangeBan: Plugin<RangeBanConfig>
|
||||
public class RangeBan : Plugin<RangeBanConfig>
|
||||
{
|
||||
|
||||
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 override void Enable()
|
||||
{
|
||||
Logger.Debug("Loading...");
|
||||
@@ -26,16 +30,11 @@ public class RangeBan: Plugin<RangeBanConfig>
|
||||
{
|
||||
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.");
|
||||
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)
|
||||
{
|
||||
@@ -46,29 +45,24 @@ public class RangeBan: Plugin<RangeBanConfig>
|
||||
if (!range.Contains("/"))
|
||||
{
|
||||
//We only handle direct IPs and CIDR
|
||||
if (range.Split('.').Length != 4)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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)
|
||||
@@ -84,11 +78,12 @@ public class RangeBan: Plugin<RangeBanConfig>
|
||||
throw new ArgumentException("Invalid IP address segment");
|
||||
result = (result << 8) | part;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class RangeBanConfig
|
||||
{
|
||||
public string[] IpRanges { get; set; } = {};
|
||||
}
|
||||
public string[] IpRanges { get; set; } = { };
|
||||
}
|
||||
Reference in New Issue
Block a user