Table of Contents

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

int

FailingCount

IPs currently held with at least one failure.

public int FailingCount { get; }

Property Value

int

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

ip string

IsBlocked(int, int)

The block rule: blocked at maxFailures failures or more.

public static bool IsBlocked(int failures, int maxFailures)

Parameters

failures int
maxFailures int

Returns

bool

IsBlocked(string, double, int, TimeSpan)

Whether ip is currently blocked for failures.

public bool IsBlocked(string ip, double nowSeconds, int maxFailures, TimeSpan retention)

Parameters

ip string
nowSeconds double
maxFailures int
retention TimeSpan

Returns

bool

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

lastActivitySeconds double
nowSeconds double
retention TimeSpan

Returns

bool

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

nowSeconds double
retention TimeSpan
maxRemove int

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

ip string

Normalized client IP.

nowSeconds double

Current monotonic time in seconds.

rateLimit TimeSpan

Minimum time between accepted attempts from one IP.

maxFailures int

Failures at which the IP is blocked.

retention TimeSpan

How long a record lasts after its last activity (the block duration).

Returns

IpAbuseTracker.AttemptVerdict

TryRecordFailure(string, double, TimeSpan, int)

Records a failure against ip.

public bool TryRecordFailure(string ip, double nowSeconds, TimeSpan retention, int maxFailingIps)

Parameters

ip string

Normalized client IP.

nowSeconds double

Current monotonic time in seconds.

retention TimeSpan

How long a record lasts after its last activity.

maxFailingIps int

Most 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

false when 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.