Class IpAbuseTracker
- Namespace
- FishMMO.Server.Core.LoginServer
- Assembly
- FishMMO.Server.dll
Per-IP account-creation abuse state: the rate limit on creation attempts and the failure count that blocks an IP, in one record per IP, expired head-first in activity order.
public sealed class IpAbuseTracker
- Inheritance
-
IpAbuseTracker
- Inherited Members
Remarks
The rules, in one place. An IP may begin a creation attempt no more often than the rate limit allows. Failures count against it; at the failure threshold it is blocked, and a blocked or rate-limited attempt changes nothing — so a blocked IP cannot keep its own block alive by hammering it. An IP's record, and with it any block, lapses once the retention period (the configured block duration) has passed since its last accepted attempt or recorded failure. A success clears the failure count.
What this replaced. Two ConcurrentDictionaries — last attempt per IP and failures per IP — swept once a minute by enumerating a few hundred entries from each dictionary's head. Enumeration always starts in the same place, so once those entries were live the sweep never reached anything behind them: roughly 128 entries a minute were reclaimed and the rest leaked until the failure tracker reached its 50,000-entry cap, after which new IPs' failures went untracked and the IP block stopped working. A failure recorded by the verification path, which creates no attempt record, was also dropped by whichever sweep first reached it — anywhere from under a minute to never — instead of lasting the block duration. Here records are kept in the order of their last activity, so the oldest is always at the head and a sweep stops at the first live one.
Time is monotonic seconds (NowSeconds): the rate limit and the block are local durations, and on the wall clock an NTP step would lift every block at once or hold them all for the length of the step.
Thread-safe: ingress runs on the main thread and failures are recorded from async workers. Every operation takes one short lock.
Constructors
IpAbuseTracker()
Initializes an empty tracker. IPs are compared ordinally: callers pass them normalized.
public IpAbuseTracker()
Properties
Count
IPs currently held, including any past retention that no sweep has reached yet.
public int Count { get; }
Property Value
FailingCount
IPs currently held with at least one failure.
public int FailingCount { get; }
Property Value
Methods
Clear()
Drops every record.
public void Clear()
ClearFailures(string)
Clears ip's failure count after a success. The attempt record stays,
so the rate limit still applies.
public void ClearFailures(string ip)
Parameters
ipstring
IsBlocked(int, int)
The block rule: blocked at maxFailures failures or more.
public static bool IsBlocked(int failures, int maxFailures)
Parameters
Returns
IsBlocked(string, double, int, TimeSpan)
Whether ip is currently blocked for failures.
public bool IsBlocked(string ip, double nowSeconds, int maxFailures, TimeSpan retention)
Parameters
Returns
IsLapsed(double, double, TimeSpan)
Whether a record last active at lastActivitySeconds has lapsed at nowSeconds.
public static bool IsLapsed(double lastActivitySeconds, double nowSeconds, TimeSpan retention)
Parameters
Returns
SweepExpired(double, TimeSpan, int)
Removes records past retention, oldest activity first, stopping at the first live one.
public int SweepExpired(double nowSeconds, TimeSpan retention, int maxRemove)
Parameters
Returns
- int
Records removed.
TryBeginAttempt(string, double, TimeSpan, int, TimeSpan)
Decides whether ip may begin a creation attempt now, and records it
when it may.
public IpAbuseTracker.AttemptVerdict TryBeginAttempt(string ip, double nowSeconds, TimeSpan rateLimit, int maxFailures, TimeSpan retention)
Parameters
ipstringNormalized client IP.
nowSecondsdoubleCurrent monotonic time in seconds.
rateLimitTimeSpanMinimum time between accepted attempts from one IP.
maxFailuresintFailures at which the IP is blocked.
retentionTimeSpanHow long a record lasts after its last activity (the block duration).
Returns
TryRecordFailure(string, double, TimeSpan, int)
Records a failure against ip.
public bool TryRecordFailure(string ip, double nowSeconds, TimeSpan retention, int maxFailingIps)
Parameters
ipstringNormalized client IP.
nowSecondsdoubleCurrent monotonic time in seconds.
retentionTimeSpanHow long a record lasts after its last activity.
maxFailingIpsintMost IPs that may hold failures at once. An IP that already has failures is always counted; only a new one is refused at the cap.
Returns
- bool
falsewhen the IP could not be tracked because the cap was reached. Callers fail closed on that: ignoring the failure instead would let an attacker who filled the tracker stay just under the block threshold indefinitely.