Fixes code according to modern code style. Fixes a few expression bugs.
This commit is contained in:
parent
89eea25e5f
commit
970fd563b2
3324 changed files with 441118 additions and 433755 deletions
|
|
@ -6,121 +6,123 @@ using Server.Network;
|
|||
|
||||
namespace Server.Accounting
|
||||
{
|
||||
public class AccountAttackLimiter
|
||||
{
|
||||
public static bool Enabled = true;
|
||||
public class AccountAttackLimiter
|
||||
{
|
||||
public static bool Enabled = true;
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
if ( !Enabled )
|
||||
return;
|
||||
private static List<InvalidAccountAccessLog> m_List = new List<InvalidAccountAccessLog>();
|
||||
|
||||
PacketHandlers.RegisterThrottler( 0x80, Throttle_Callback );
|
||||
PacketHandlers.RegisterThrottler( 0x91, Throttle_Callback );
|
||||
PacketHandlers.RegisterThrottler( 0xCF, Throttle_Callback );
|
||||
}
|
||||
public static void Initialize()
|
||||
{
|
||||
if (!Enabled)
|
||||
return;
|
||||
|
||||
public static bool Throttle_Callback( NetState ns )
|
||||
{
|
||||
InvalidAccountAccessLog accessLog = FindAccessLog( ns );
|
||||
PacketHandlers.RegisterThrottler(0x80, Throttle_Callback);
|
||||
PacketHandlers.RegisterThrottler(0x91, Throttle_Callback);
|
||||
PacketHandlers.RegisterThrottler(0xCF, Throttle_Callback);
|
||||
}
|
||||
|
||||
if ( accessLog == null )
|
||||
return true;
|
||||
public static bool Throttle_Callback(NetState ns)
|
||||
{
|
||||
InvalidAccountAccessLog accessLog = FindAccessLog(ns);
|
||||
|
||||
return ( DateTime.UtcNow >= (accessLog.LastAccessTime + ComputeThrottle( accessLog.Counts )) );
|
||||
}
|
||||
if (accessLog == null)
|
||||
return true;
|
||||
|
||||
private static List<InvalidAccountAccessLog> m_List = new List<InvalidAccountAccessLog>();
|
||||
return DateTime.UtcNow >= accessLog.LastAccessTime + ComputeThrottle(accessLog.Counts);
|
||||
}
|
||||
|
||||
public static InvalidAccountAccessLog FindAccessLog( NetState ns )
|
||||
{
|
||||
if ( ns == null )
|
||||
return null;
|
||||
public static InvalidAccountAccessLog FindAccessLog(NetState ns)
|
||||
{
|
||||
if (ns == null)
|
||||
return null;
|
||||
|
||||
IPAddress ipAddress = ns.Address;
|
||||
IPAddress ipAddress = ns.Address;
|
||||
|
||||
for ( int i = 0; i < m_List.Count; ++i )
|
||||
{
|
||||
InvalidAccountAccessLog accessLog = m_List[i];
|
||||
for (int i = 0; i < m_List.Count; ++i)
|
||||
{
|
||||
InvalidAccountAccessLog accessLog = m_List[i];
|
||||
|
||||
if ( accessLog.HasExpired )
|
||||
m_List.RemoveAt( i-- );
|
||||
else if ( accessLog.Address.Equals( ipAddress ) )
|
||||
return accessLog;
|
||||
}
|
||||
if (accessLog.HasExpired)
|
||||
m_List.RemoveAt(i--);
|
||||
else if (accessLog.Address.Equals(ipAddress))
|
||||
return accessLog;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void RegisterInvalidAccess( NetState ns )
|
||||
{
|
||||
if ( ns == null || !Enabled )
|
||||
return;
|
||||
public static void RegisterInvalidAccess(NetState ns)
|
||||
{
|
||||
if (ns == null || !Enabled)
|
||||
return;
|
||||
|
||||
InvalidAccountAccessLog accessLog = FindAccessLog( ns );
|
||||
InvalidAccountAccessLog accessLog = FindAccessLog(ns);
|
||||
|
||||
if ( accessLog == null )
|
||||
m_List.Add( accessLog = new InvalidAccountAccessLog( ns.Address ) );
|
||||
if (accessLog == null)
|
||||
m_List.Add(accessLog = new InvalidAccountAccessLog(ns.Address));
|
||||
|
||||
accessLog.Counts += 1;
|
||||
accessLog.RefreshAccessTime();
|
||||
accessLog.Counts += 1;
|
||||
accessLog.RefreshAccessTime();
|
||||
|
||||
if ( accessLog.Counts >= 3 ) {
|
||||
try {
|
||||
using ( StreamWriter op = new StreamWriter( "throttle.log", true ) ) {
|
||||
op.WriteLine(
|
||||
"{0}\t{1}\t{2}",
|
||||
DateTime.UtcNow,
|
||||
ns,
|
||||
accessLog.Counts
|
||||
);
|
||||
}
|
||||
}
|
||||
catch {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (accessLog.Counts >= 3)
|
||||
try
|
||||
{
|
||||
using (StreamWriter op = new StreamWriter("throttle.log", true))
|
||||
{
|
||||
op.WriteLine(
|
||||
"{0}\t{1}\t{2}",
|
||||
DateTime.UtcNow,
|
||||
ns,
|
||||
accessLog.Counts
|
||||
);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public static TimeSpan ComputeThrottle( int counts )
|
||||
{
|
||||
if ( counts >= 15 )
|
||||
return TimeSpan.FromMinutes( 5.0 );
|
||||
public static TimeSpan ComputeThrottle(int counts)
|
||||
{
|
||||
if (counts >= 15)
|
||||
return TimeSpan.FromMinutes(5.0);
|
||||
|
||||
if ( counts >= 10 )
|
||||
return TimeSpan.FromMinutes( 1.0 );
|
||||
if (counts >= 10)
|
||||
return TimeSpan.FromMinutes(1.0);
|
||||
|
||||
if ( counts >= 5 )
|
||||
return TimeSpan.FromSeconds( 20.0 );
|
||||
if (counts >= 5)
|
||||
return TimeSpan.FromSeconds(20.0);
|
||||
|
||||
if ( counts >= 3 )
|
||||
return TimeSpan.FromSeconds( 10.0 );
|
||||
if (counts >= 3)
|
||||
return TimeSpan.FromSeconds(10.0);
|
||||
|
||||
if ( counts >= 1 )
|
||||
return TimeSpan.FromSeconds( 2.0 );
|
||||
if (counts >= 1)
|
||||
return TimeSpan.FromSeconds(2.0);
|
||||
|
||||
return TimeSpan.Zero;
|
||||
}
|
||||
}
|
||||
return TimeSpan.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
public class InvalidAccountAccessLog
|
||||
{
|
||||
public IPAddress Address { get; set; }
|
||||
public class InvalidAccountAccessLog
|
||||
{
|
||||
public InvalidAccountAccessLog(IPAddress address)
|
||||
{
|
||||
Address = address;
|
||||
RefreshAccessTime();
|
||||
}
|
||||
|
||||
public DateTime LastAccessTime { get; set; }
|
||||
public IPAddress Address{ get; set; }
|
||||
|
||||
public bool HasExpired => ( DateTime.UtcNow >= ( LastAccessTime + TimeSpan.FromHours( 1.0 ) ) );
|
||||
public DateTime LastAccessTime{ get; set; }
|
||||
|
||||
public int Counts { get; set; }
|
||||
public bool HasExpired => DateTime.UtcNow >= LastAccessTime + TimeSpan.FromHours(1.0);
|
||||
|
||||
public void RefreshAccessTime()
|
||||
{
|
||||
LastAccessTime = DateTime.UtcNow;
|
||||
}
|
||||
public int Counts{ get; set; }
|
||||
|
||||
public InvalidAccountAccessLog( IPAddress address )
|
||||
{
|
||||
Address = address;
|
||||
RefreshAccessTime();
|
||||
}
|
||||
}
|
||||
public void RefreshAccessTime()
|
||||
{
|
||||
LastAccessTime = DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue