servuo/Scripts/Accounting/IPLimiter.cs
Voxpire 3b74a31652 IPLimiter uses NetState.Addresses
ServerList resolves private network IPs
Near-instant entity delta updates
2025-01-27 03:00:34 +00:00

57 lines
1.4 KiB
C#

using System.Collections.Generic;
using System.Net;
using Server.Network;
namespace Server.Misc
{
public static class IPLimiter
{
public static bool Enabled { get; set; } = true;
public static bool SocketBlock { get; set; } = true; // true to block at connection, false to block at login request
public static int MaxAddresses { get; set; } = 10;
public static IPAddress[] Exemptions { get; set; } = // For hosting services where there are cases where IPs can be proxied
{
IPAddress.Loopback,
};
public static bool IsExempt(IPAddress ip)
{
for (int i = 0; i < Exemptions.Length; i++)
{
if (ip.Equals(Exemptions[i]))
return true;
}
return false;
}
public static bool Verify(IPAddress ourAddress)
{
if (!Enabled || IsExempt(ourAddress))
return true;
IList<IPAddress> netStates = NetState.Addresses;
int count = 0;
for (int i = 0; i < netStates.Count; ++i)
{
IPAddress compState = netStates[i];
if (ourAddress.Equals(compState))
{
++count;
if (count >= MaxAddresses)
return false;
}
}
return true;
}
}
}