loginips

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package loginips 记录管理后台登录成功过的来源地址,用于发现“从没见过的地址登录”。

第一次登录只建立基线,不算异常;此后出现的新地址返回 true,由调用方发布告警事件。 IPv6 按 /64 前缀归并:家宽的临时地址每天都会变,逐个地址比较会产生大量误报。 记录数有上限,超出时淘汰最久未见的地址,文件大小因此有界。

Index

Constants

View Source
const MaxEntries = 200

MaxEntries 是最多保留的来源数。

Variables

This section is empty.

Functions

func Normalize

func Normalize(ip string) string

Normalize 把客户端地址归并为比较用的键;无法解析的地址返回空串。

Types

type Entry

type Entry struct {
	// Key 为归并后的地址:IPv4 原样,IPv6 为 /64 前缀
	Key   string    `json:"key"`
	First time.Time `json:"first"`
	Last  time.Time `json:"last"`
}

Entry 是一个已知来源。

type Saver

type Saver func([]Entry) error

Saver 把当前列表持久化。返回错误时内存状态仍然有效。

type Tracker

type Tracker struct {
	// contains filtered or unexported fields
}

Tracker 是并发安全的已知来源表。

func New

func New(saved []Entry, save Saver) *Tracker

New 用已保存的条目创建跟踪器,save 可为 nil。

func (*Tracker) IsKnown

func (t *Tracker) IsKnown(ip string) bool

IsKnown 返回来源地址是否曾成功登录过。只读判断,不改变任何记录;无法解析的地址视为未知。

func (*Tracker) Known

func (t *Tracker) Known() []Entry

Known 返回当前已知来源,按最近出现时间从新到旧。

func (*Tracker) Observe

func (t *Tracker) Observe(ip string, now time.Time) bool

Observe 记录一次成功登录,返回是否出现了新来源。 记录为空(首次登录)时只建立基线,返回 false。无法解析的地址不记录也不告警。

Jump to

Keyboard shortcuts

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