use /64 for ipv6 bans (#22121)

This commit is contained in:
avery
2023-12-03 22:36:49 -08:00
committed by GitHub
parent 6c2eec0afc
commit 01df46602d

View File

@@ -29,6 +29,8 @@ public sealed class BanPanelEui : BaseEui, IPostInjectInit
private string PlayerName { get; set; } = string.Empty; private string PlayerName { get; set; } = string.Empty;
private IPAddress? LastAddress { get; set; } private IPAddress? LastAddress { get; set; }
private ImmutableArray<byte>? LastHwid { get; set; } private ImmutableArray<byte>? LastHwid { get; set; }
private const int Ipv4_CIDR = 32;
private const int Ipv6_CIDR = 64;
public BanPanelEui() public BanPanelEui()
{ {
@@ -78,20 +80,20 @@ public sealed class BanPanelEui : BaseEui, IPostInjectInit
if (split.Length > 1) if (split.Length > 1)
hid = split[1]; hid = split[1];
if (!IPAddress.TryParse(ipAddressString, out var ipAddress) || !uint.TryParse(hid, out var hidInt) || hidInt > 128 || hidInt > 32 && ipAddress.AddressFamily == AddressFamily.InterNetwork) if (!IPAddress.TryParse(ipAddressString, out var ipAddress) || !uint.TryParse(hid, out var hidInt) || hidInt > Ipv6_CIDR || hidInt > Ipv4_CIDR && ipAddress.AddressFamily == AddressFamily.InterNetwork)
{ {
_chat.DispatchServerMessage(Player, Loc.GetString("ban-panel-invalid-ip")); _chat.DispatchServerMessage(Player, Loc.GetString("ban-panel-invalid-ip"));
return; return;
} }
if (hidInt == 0) if (hidInt == 0)
hidInt = (uint) (ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? 128 : 32); hidInt = (uint) (ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? Ipv6_CIDR : Ipv4_CIDR);
addressRange = (ipAddress, (int) hidInt); addressRange = (ipAddress, (int) hidInt);
} }
var targetUid = target is not null ? PlayerId : null; var targetUid = target is not null ? PlayerId : null;
addressRange = useLastIp && LastAddress is not null ? (LastAddress, LastAddress.AddressFamily == AddressFamily.InterNetworkV6 ? 128 : 32) : addressRange; addressRange = useLastIp && LastAddress is not null ? (LastAddress, LastAddress.AddressFamily == AddressFamily.InterNetworkV6 ? Ipv6_CIDR : Ipv4_CIDR) : addressRange;
var targetHWid = useLastHwid ? LastHwid : hwid; var targetHWid = useLastHwid ? LastHwid : hwid;
if (target != null && target != PlayerName || Guid.TryParse(target, out var parsed) && parsed != PlayerId) if (target != null && target != PlayerName || Guid.TryParse(target, out var parsed) && parsed != PlayerId)
{ {
@@ -108,8 +110,8 @@ public sealed class BanPanelEui : BaseEui, IPostInjectInit
if (targetAddress.IsIPv4MappedToIPv6) if (targetAddress.IsIPv4MappedToIPv6)
targetAddress = targetAddress.MapToIPv4(); targetAddress = targetAddress.MapToIPv4();
// Ban /128 for IPv6, /32 for IPv4. // Ban /64 for IPv6, /32 for IPv4.
var hid = targetAddress.AddressFamily == AddressFamily.InterNetworkV6 ? 128 : 32; var hid = targetAddress.AddressFamily == AddressFamily.InterNetworkV6 ? Ipv6_CIDR : Ipv4_CIDR;
addressRange = (targetAddress, hid); addressRange = (targetAddress, hid);
} }
targetHWid = useLastHwid ? located.LastHWId : hwid; targetHWid = useLastHwid ? located.LastHWId : hwid;