fix: Fixes stalled connections and infinite throttle (#1796)

> [!Warning]
> **Developer Warning**
> The `PacketThrottle` callback return value is now reversed. `true` indicates the connection is _throttled_.

### Summary
- Fixes an issue where connections get stalled forever
- Fixes an issue where the throttler is not working properly
- Removes account attack limiter
- Rewrites IP limiter
- Removes IP restrictions (they weren't used, and not practical)
- Fixes issue where IP limiter was counting before firewall was blocking.

View without whitespace:
https://github.com/modernuo/ModernUO/pull/1796/files?diff=split&w=1
This commit is contained in:
Kamron Batman 2024-05-27 21:32:55 -07:00 • committed by GitHub
parent ccad915464
commit a4522b9d43
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 1538 additions and 1783 deletions

View file

@ -3,121 +3,136 @@ using System.IO;
using System.Net;
using Server.Accounting.Security;
namespace Server.Accounting
namespace Server.Accounting;
public partial class Account
{
public partial class Account
// Deleted IP Restrictions
private void MigrateFrom(V5Content content)
{
// Username was not interned
private void MigrateFrom(V4Content content)
_username = content.Username;
_passwordAlgorithm = content.PasswordAlgorithm;
_password = content.Password;
_accessLevel = content.AccessLevel;
_flags = content.Flags;
_lastLogin = content.LastLogin;
_totalGold = content.TotalGold;
_totalPlat = content.TotalPlat;
_mobiles = content.Mobiles;
_comments = content.Comments;
_tags = content.Tags;
_loginIPs = content.LoginIPs;
_totalGameTime = content.TotalGameTime;
_email = content.Email;
}
// Username was not interned
private void MigrateFrom(V4Content content)
{
_username = content.Username;
_passwordAlgorithm = content.PasswordAlgorithm;
_password = content.Password;
_accessLevel = content.AccessLevel;
_flags = content.Flags;
_lastLogin = content.LastLogin;
_totalGold = content.TotalGold;
_totalPlat = content.TotalPlat;
_mobiles = content.Mobiles;
_comments = content.Comments;
_tags = content.Tags;
_loginIPs = content.LoginIPs;
_totalGameTime = content.TotalGameTime;
_email = content.Email;
}
private void MigrateFrom(V3Content content)
{
_username = content.Username;
_username.Intern();
_passwordAlgorithm = content.PasswordAlgorithm;
_password = content.Password;
_accessLevel = content.AccessLevel;
_flags = content.Flags;
Created = content.Created;
_lastLogin = content.LastLogin;
_totalGold = content.TotalGold;
_totalPlat = content.TotalPlat;
_mobiles = content.Mobiles;
_comments = content.Comments;
_tags = content.Tags;
_loginIPs = content.LoginIPs;
_totalGameTime = content.TotalGameTime;
_email = content.Email;
}
private void Deserialize(IGenericReader reader, int version)
{
if (version != 2)
{
_username = content.Username;
_passwordAlgorithm = content.PasswordAlgorithm;
_password = content.Password;
_accessLevel = content.AccessLevel;
_flags = content.Flags;
_lastLogin = content.LastLogin;
_totalGold = content.TotalGold;
_totalPlat = content.TotalPlat;
_mobiles = content.Mobiles;
_comments = content.Comments;
_tags = content.Tags;
_loginIPs = content.LoginIPs;
_ipRestrictions = content.IpRestrictions;
_totalGameTime = content.TotalGameTime;
_email = content.Email;
// Due to a bug where we were not versioning at all, reset so we don't have an issue deserializing
reader.Seek(0, SeekOrigin.Begin);
}
private void MigrateFrom(V3Content content)
_username = reader.ReadString(true);
_passwordAlgorithm = version < 2 ? (PasswordProtectionAlgorithm)reader.ReadInt() : reader.ReadEnum<PasswordProtectionAlgorithm>();
_password = reader.ReadString();
_accessLevel = version < 2 ? (AccessLevel)reader.ReadInt() : reader.ReadEnum<AccessLevel>();
_flags = reader.ReadInt();
Created = reader.ReadDateTime();
_lastLogin = reader.ReadDateTime();
_totalGold = reader.ReadInt();
_totalPlat = reader.ReadInt();
var length = reader.ReadInt();
_mobiles = new Mobile[length];
for (int i = 0; i < length; i++)
{
_username = content.Username;
_username.Intern();
_passwordAlgorithm = content.PasswordAlgorithm;
_password = content.Password;
_accessLevel = content.AccessLevel;
_flags = content.Flags;
Created = content.Created;
_lastLogin = content.LastLogin;
_totalGold = content.TotalGold;
_totalPlat = content.TotalPlat;
_mobiles = content.Mobiles;
_comments = content.Comments;
_tags = content.Tags;
_loginIPs = content.LoginIPs;
_ipRestrictions = content.IpRestrictions;
_totalGameTime = content.TotalGameTime;
_email = content.Email;
_mobiles[i] = reader.ReadEntity<Mobile>();
}
private void Deserialize(IGenericReader reader, int version)
length = reader.ReadInt();
_comments = length > 0 ? new List<AccountComment>(length) : null;
for (int i = 0; i < length; i++)
{
if (version != 2)
_comments!.Add(new AccountComment(reader));
}
length = reader.ReadInt();
_tags = length > 0 ? new List<AccountTag>(length) : null;
for (int i = 0; i < length; i++)
{
_tags!.Add(new AccountTag(reader));
}
length = reader.ReadInt();
_loginIPs = new IPAddress[length];
for (int i = 0; i < length; i++)
{
if (version < 2)
{
// Due to a bug where we were not versioning at all, reset so we don't have an issue deserializing
reader.Seek(0, SeekOrigin.Begin);
}
_username = reader.ReadString(true);
_passwordAlgorithm = version < 2 ? (PasswordProtectionAlgorithm)reader.ReadInt() : reader.ReadEnum<PasswordProtectionAlgorithm>();
_password = reader.ReadString();
_accessLevel = version < 2 ? (AccessLevel)reader.ReadInt() : reader.ReadEnum<AccessLevel>();
_flags = reader.ReadInt();
Created = reader.ReadDateTime();
_lastLogin = reader.ReadDateTime();
_totalGold = reader.ReadInt();
_totalPlat = reader.ReadInt();
var length = reader.ReadInt();
_mobiles = new Mobile[length];
for (int i = 0; i < length; i++)
{
_mobiles[i] = reader.ReadEntity<Mobile>();
}
length = reader.ReadInt();
_comments = length > 0 ? new List<AccountComment>(length) : null;
for (int i = 0; i < length; i++)
{
_comments!.Add(new AccountComment(reader));
}
length = reader.ReadInt();
_tags = length > 0 ? new List<AccountTag>(length) : null;
for (int i = 0; i < length; i++)
{
_tags!.Add(new AccountTag(reader));
}
length = reader.ReadInt();
_loginIPs = new IPAddress[length];
for (int i = 0; i < length; i++)
{
if (version < 2)
if (IPAddress.TryParse(reader.ReadString(), out var address))
{
if (IPAddress.TryParse(reader.ReadString(), out var address))
{
_loginIPs[i] = Utility.Intern(address);
}
}
else
{
_loginIPs[i] = reader.ReadIPAddress();
_loginIPs[i] = Utility.Intern(address);
}
}
length = reader.ReadInt();
_ipRestrictions = new string[length];
for (int i = 0; i < length; i++)
else
{
_ipRestrictions[i] = reader.ReadString();
_loginIPs[i] = reader.ReadIPAddress();
}
}
_totalGameTime = reader.ReadTimeSpan();
length = reader.ReadInt();
for (int i = 0; i < length; i++)
{
reader.ReadString(); // IP Restrictions
}
if (version > 1)
{
_email = reader.ReadString();
}
_totalGameTime = reader.ReadTimeSpan();
if (version > 1)
{
_email = reader.ReadString();
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,163 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Server.Network;
namespace Server.Accounting
{
public static class AccountAttackLimiter
{
public static bool Enabled;
private static readonly List<InvalidAccountAccessLog> m_List = new();
public static void Configure()
{
Enabled = ServerConfiguration.GetOrUpdateSetting("accountAttackLimiter.enable", true);
}
public static unsafe void Initialize()
{
if (!Enabled)
{
return;
}
IncomingPackets.RegisterThrottler(0x80, &Throttle);
IncomingPackets.RegisterThrottler(0x91, &Throttle);
IncomingPackets.RegisterThrottler(0xCF, &Throttle);
}
public static bool Throttle(int packetId, NetState ns, out bool drop)
{
var accessLog = FindAccessLog(ns);
if (accessLog == null)
{
drop = false;
return true;
}
var date = Core.Now;
var access = accessLog.LastAccessTime + ComputeThrottle(accessLog.Counts);
var allow = date >= access;
drop = !allow;
return allow;
}
public static InvalidAccountAccessLog FindAccessLog(NetState ns)
{
if (ns == null)
{
return null;
}
var ipAddress = ns.Address;
for (var i = 0; i < m_List.Count; ++i)
{
var accessLog = m_List[i];
if (accessLog.HasExpired)
{
m_List.RemoveAt(i--);
}
else if (accessLog.Address.Equals(ipAddress))
{
return accessLog;
}
}
return null;
}
public static void RegisterInvalidAccess(NetState ns)
{
if (ns == null || !Enabled)
{
return;
}
var accessLog = FindAccessLog(ns);
if (accessLog == null)
{
m_List.Add(accessLog = new InvalidAccountAccessLog(ns.Address));
}
accessLog.Counts += 1;
accessLog.RefreshAccessTime();
if (accessLog.Counts >= 3)
{
try
{
using var op = new StreamWriter("throttle.log", true);
op.WriteLine(
"{0}\t{1}\t{2}",
Core.Now,
ns,
accessLog.Counts
);
}
catch
{
// ignored
}
}
}
public static TimeSpan ComputeThrottle(int counts)
{
if (counts >= 15)
{
return TimeSpan.FromMinutes(5.0);
}
if (counts >= 10)
{
return TimeSpan.FromMinutes(1.0);
}
if (counts >= 5)
{
return TimeSpan.FromSeconds(20.0);
}
if (counts >= 3)
{
return TimeSpan.FromSeconds(10.0);
}
if (counts >= 1)
{
return TimeSpan.FromSeconds(2.0);
}
return TimeSpan.Zero;
}
}
public class InvalidAccountAccessLog
{
public InvalidAccountAccessLog(IPAddress address)
{
Address = address;
RefreshAccessTime();
}
public IPAddress Address { get; set; }
public DateTime LastAccessTime { get; set; }
public bool HasExpired => Core.Now >= LastAccessTime + TimeSpan.FromHours(1.0);
public int Counts { get; set; }
public void RefreshAccessTime()
{
LastAccessTime = Core.Now;
}
}
}

View file

@ -369,11 +369,6 @@ public static class AccountHandler
acct.LogAccess(e.State);
}
if (!e.Accepted)
{
AccountAttackLimiter.RegisterInvalidAccess(e.State);
}
}
public static void EventSink_GameLogin(GameLoginEventArgs e)
@ -409,11 +404,6 @@ public static class AccountHandler
e.Accepted = true;
e.CityInfo = CharacterCreation.GetStartingCities(acct.Young);
}
if (!e.Accepted)
{
AccountAttackLimiter.RegisterInvalidAccess(e.State);
}
}
public static bool CheckAccount(Mobile mobCheck, Mobile accCheck)