fix: Exempts localhost from IPLimiter. Preps testing for io_uring (#2316)

### Summary
* Exempts localhost from IPLimiter
* Updates tests to have proper sequential testing with packets
* Updates tests to clean up NetState
This commit is contained in:
Kamron Batman 2026-01-20 08:54:15 -08:00 • committed by GitHub
parent 8133983b64
commit 1e4c32c809
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
77 changed files with 4612 additions and 4616 deletions

View file

@ -56,6 +56,12 @@ public class IPRateLimiter
public bool Verify(IPAddress ip, out int totalAttempts)
{
if (ip.IsPrivateNetwork())
{
totalAttempts = 0;
return true;
}
var nowTicks = Core.TickCount;
var ipStats = _ipAttempts.GetOrAdd(ip, _ => GetOrCreateIPStats());
var added = ipStats.AttemptCount == 0;

View file

@ -35,7 +35,7 @@ namespace Server.Network;
public delegate void DecodePacket(Span<byte> buffer, ref int length);
public delegate int EncodePacket(ReadOnlySpan<byte> inputBuffer, Span<byte> outputBuffer);
public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetState>
public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetState>, IDisposable
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(NetState));
@ -1142,8 +1142,11 @@ public partial class NetState : IComparable<NetState>, IValueLinkListNode<NetSta
}
}
private void Dispose()
// Do not run this directly. Use Disconnect instead.
// This is available for testing cleanup only.
public void Dispose()
{
_running = false;
// It's possible we could queue for dispose multiple times
if (Connection == null)
{

View file

@ -0,0 +1,49 @@
using System.Net;
using System.Net.Sockets;
using Server.Network;
namespace Server;
public static class NetworkUtilities
{
public static bool IsPrivateNetwork(this IPAddress ip) =>
ip.AddressFamily switch
{
AddressFamily.InterNetwork => ip.IsPrivateNetworkV4(),
AddressFamily.InterNetworkV6 => ip.IsPrivateNetworkV6(),
_ => false
};
private static readonly IFirewallEntry[] _privateNetworkV4 =
[
new CidrFirewallEntry("127.0.0.1/8"),
new CidrFirewallEntry("192.168.0.0/16"),
new CidrFirewallEntry("10.0.0.0/8"),
new CidrFirewallEntry("172.16.0.0/12"),
new CidrFirewallEntry("169.254.0.0/16"),
new CidrFirewallEntry("100.64.0.0/10")
];
private static readonly IFirewallEntry[] _privateNetworkV6 =
[
new CidrFirewallEntry("fc00::/7"),
new CidrFirewallEntry("fe80::/10")
];
public static bool IsPrivateNetworkV4(this IPAddress ip)
{
for (var i = 0; i < _privateNetworkV4.Length; i++)
{
if (_privateNetworkV4[i].IsBlocked(ip))
{
return true;
}
}
return false;
}
public static bool IsPrivateNetworkV6(this IPAddress ip) =>
_privateNetworkV6[0].IsBlocked(ip) ||
_privateNetworkV6[1].IsBlocked(ip);
}