iptracker

package
v0.8.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2025 License: Unlicense Imports: 2 Imported by: 0

Documentation

Overview

Package iptracker provides functionality to track and block IP addresses based on failed authentication attempts.

Index

Constants

View Source
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

View Source
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 NewIPTracker

func NewIPTracker() *IPTracker

NewIPTracker creates a new IPTracker instance.

func (*IPTracker) Authenticate added in v0.4.5

func (t *IPTracker) Authenticate(ip string)

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

func (t *IPTracker) GetBlockDuration(ip string) time.Duration

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

func (t *IPTracker) GetBlockedUntil(ip string) time.Time

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

func (t *IPTracker) HasBlockDurationPassed(ip string) bool

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) IsBlocked

func (t *IPTracker) IsBlocked(ip string) bool

IsBlocked checks if the given IP address is currently blocked.

func (*IPTracker) RecordFailedAttempt

func (t *IPTracker) RecordFailedAttempt(ip string) bool

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.

func (*IPTracker) Reset

func (t *IPTracker) Reset(ip string)

Reset completely resets all tracking for the given IP address. This is different from Authenticate as it also resets the offense count.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL