Example #1
0
    public void RecordFailure(HttpContext httpContext)
    {
        string requesterIdentifier = GetUniqueRequesterIdentifier(httpContext) !;

        LogMessages.OnFailure(this._logger, requesterIdentifier, null !);

        Fail2BanRecord newRecord = this._recordStore.AddOrUpdate(
            requesterIdentifier,
            _ => Fail2BanRecord.Create(),
            (_, record) => record.Record()
            );

        if (CheckApplyRateLimiting(newRecord) == true)
        {
            LogMessages.OnRateLimitingApplied(this._logger, requesterIdentifier, null !);
        }
    }
Example #2
0
    private bool?CheckApplyRateLimiting(Fail2BanRecord record)
    {
        TimeSpan timeDiff = record.MostRecentFailure - record.FirstFailure;

        // If this record is stale, kick it out
        if (timeDiff > this._options.DebounceTime)
        {
            return(false);
        }

        // If we haven't reached the count yet, hold on to the record
        if (record.FailureCount <= this._options.MaximumAttempts)
        {
            return(null);
        }

        return(true);
    }
Example #3
0
 private Fail2BanRecord(Fail2BanRecord copy)
 {
     this.FirstFailure = copy.FirstFailure;
     this.FailureCount = copy.FailureCount + 1;
 }