Documentation
¶
Overview ¶
Package iptracker provides functionality to track and block IP addresses based on failed authentication attempts.
Index ¶
- Constants
- Variables
- type IPTracker
- func (t *IPTracker) Authenticate(ip string)
- func (t *IPTracker) GetBlockDuration(ip string) time.Duration
- func (t *IPTracker) GetBlockedUntil(ip string) time.Time
- func (t *IPTracker) HasBlockDurationPassed(ip string) bool
- func (t *IPTracker) IsBlocked(ip string) bool
- func (t *IPTracker) RecordFailedAttempt(ip string) bool
- func (t *IPTracker) Reset(ip string)
Constants ¶
const ( // BlockDuration is the duration for which an IP will be blocked after // exceeding the maximum number of failed attempts. BlockDuration = 10 * time.Minute )
Variables ¶
var Global = NewIPTracker()
Global instance of IPTracker for use across the application
Functions ¶
This section is empty.
Types ¶
type IPTracker ¶
type IPTracker struct {
// contains filtered or unexported fields
}
IPTracker tracks failed authentication attempts by IP address and provides functionality to block IPs that exceed a threshold.
func (*IPTracker) Authenticate ¶ added in v0.4.5
Authenticate records a successful authentication for an IP address. If the IP was blocked, it removes the block but preserves the offense count. This allows the IP to access the system again, but if it offends in the future, the penalty will still be doubled based on past offenses.
func (*IPTracker) GetBlockDuration ¶ added in v0.4.5
GetBlockDuration returns the current block duration for the given IP address. This is useful for displaying how long the IP would have been blocked before requiring authentication.
func (*IPTracker) GetBlockedUntil ¶
GetBlockedUntil returns the time until which the given IP address is blocked. If the IP is not blocked, it returns the zero time. Note: With the new blocking behavior, an IP remains blocked even after this time until it successfully authenticates.
func (*IPTracker) HasBlockDurationPassed ¶ added in v0.4.5
HasBlockDurationPassed checks if the block duration for an IP has passed, even though the IP remains blocked until authentication. This is useful for display purposes.
func (*IPTracker) RecordFailedAttempt ¶
RecordFailedAttempt records a failed authentication attempt for the given IP address. If the number of failed attempts exceeds the threshold, the IP is blocked. For repeat offenders, the block duration doubles with each offense. Returns true if the IP is now blocked, false otherwise.