feat: 1.4.6 unban from the UI, operator allows, day-counter fallback for long windows
Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Successful in 1m47s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 27s
CI / Tests (Linux, race detector) (push) Successful in 1m20s
CI / Dependency audit (push) Successful in 31s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 5s
Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Successful in 1m47s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 27s
CI / Tests (Linux, race detector) (push) Successful in 1m20s
CI / Dependency audit (push) Successful in 31s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 5s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1ef5a0ce9f
commit
da333d70f1
25 files changed
+841
-33
No files matched your search
+33
-14
@@ -18,6 +18,7 @@ import (
|
||||
type Config struct {
|
||||
Mode string
|
||||
Path string // decision file
|
||||
UnbanPath string // unban requests read by the host (empty: unbanning is unavailable)
|
||||
StatePath string // enforce.json, empty: no persistence
|
||||
Host string // name of this instance, written in every line
|
||||
Threshold int // attack events in Window
|
||||
@@ -46,6 +47,7 @@ type Seen struct {
|
||||
Score float64
|
||||
Level core.Level
|
||||
Jails []string
|
||||
Est Tally // estimate from per-day counters, used when the registry holds less
|
||||
}
|
||||
|
||||
// Tally counts malicious events of one address in LongWindow.
|
||||
@@ -162,10 +164,12 @@ const (
|
||||
|
||||
// Engine is the second firewall.
|
||||
type Engine struct {
|
||||
cfg Config
|
||||
log *logFile
|
||||
ch chan item
|
||||
dropQ atomic.Int64
|
||||
cfg Config
|
||||
log *logFile
|
||||
ch chan item
|
||||
dropQ atomic.Int64
|
||||
sweeping atomic.Bool
|
||||
unlog *logFile
|
||||
|
||||
mu sync.Mutex
|
||||
ips map[netip.Addr]*ipState
|
||||
@@ -182,6 +186,8 @@ type Engine struct {
|
||||
lastSave time.Time
|
||||
hostIPs map[string][]netip.Addr // last good resolution per host
|
||||
long map[netip.Addr]*longRec
|
||||
allows map[netip.Addr]int64 // operator allowances: address -> until (unix)
|
||||
unbans []UnbanWire
|
||||
queue []cand
|
||||
}
|
||||
|
||||
@@ -206,7 +212,8 @@ func New(c Config) *Engine {
|
||||
}
|
||||
e := &Engine{cfg: c, log: &logFile{path: c.Path}, ch: make(chan item, 4096), ips: map[netip.Addr]*ipState{},
|
||||
bans: map[netip.Addr]banRec{}, would: map[netip.Addr]int64{}, skipSeen: map[netip.Addr]int64{},
|
||||
counts: Counts{Skipped: map[string]int{}}, long: map[netip.Addr]*longRec{}}
|
||||
counts: Counts{Skipped: map[string]int{}}, long: map[netip.Addr]*longRec{}, allows: map[netip.Addr]int64{}}
|
||||
e.unlog = &logFile{path: c.UnbanPath}
|
||||
e.hostIPs = map[string][]netip.Addr{}
|
||||
e.excl = Exclusions{Trusted: c.Trusted, Peers: map[netip.Addr]bool{}, Infra: map[netip.Addr]bool{}}
|
||||
e.refreshPeers()
|
||||
@@ -268,13 +275,13 @@ func (e *Engine) Run(ctx context.Context) {
|
||||
e.handle(it.ev, it.fam)
|
||||
case <-sweepT.C:
|
||||
if swept {
|
||||
e.Sweep()
|
||||
go e.Sweep()
|
||||
}
|
||||
case <-flush.C:
|
||||
e.maybeSave()
|
||||
if !swept && e.cfg.Now().Sub(started) >= 20*time.Second && (e.cfg.Ready == nil || e.cfg.Ready()) {
|
||||
swept = true
|
||||
e.Sweep()
|
||||
go e.Sweep()
|
||||
}
|
||||
e.drain()
|
||||
case <-peers.C:
|
||||
@@ -493,7 +500,7 @@ func (e *Engine) process(ev core.Event, fam string) {
|
||||
}
|
||||
}
|
||||
in := Input{Now: now, Attacks: len(st.ev), Top: topSlug(st.ev), SSHFails: len(st.ssh), Crits: crits,
|
||||
Legit: st.legit >= now.Add(-LegitWindow).Unix(), Exclude: e.excl.Excluded(a)}
|
||||
Legit: st.legit >= now.Add(-LegitWindow).Unix(), Exclude: e.exclude(a, now)}
|
||||
if crit {
|
||||
in.Crit = slug
|
||||
}
|
||||
@@ -668,7 +675,7 @@ func (e *Engine) AcceptPeer(name string, in []Wire) (accepted, invalid int) {
|
||||
continue // already expired
|
||||
}
|
||||
st := e.stateOf(a)
|
||||
in := Input{Now: now, Crit: w.Reason, Crits: CritHits, Exclude: e.excl.Excluded(a)}
|
||||
in := Input{Now: now, Crit: w.Reason, Crits: CritHits, Exclude: e.exclude(a, now)}
|
||||
if in.Exclude == "" && st.lastAttack < now.Add(-peerConfirmAge).Unix() {
|
||||
in.Exclude = SkipUnconfirmed // a peer's word alone never bans: this node must have seen the address attack
|
||||
}
|
||||
@@ -715,7 +722,7 @@ func (e *Engine) Report(from int64) Report {
|
||||
}
|
||||
}
|
||||
c := e.counts
|
||||
c.Skipped = map[string]int{SkipTrusted: 0, SkipPrivate: 0, SkipCrawler: 0, SkipLegit: 0, SkipPeer: 0, SkipInfra: 0, SkipUnconfirmed: 0}
|
||||
c.Skipped = map[string]int{SkipOperator: 0, SkipTrusted: 0, SkipPrivate: 0, SkipCrawler: 0, SkipLegit: 0, SkipPeer: 0, SkipInfra: 0, SkipUnconfirmed: 0}
|
||||
for k, v := range e.counts.Skipped {
|
||||
c.Skipped[k] = v
|
||||
}
|
||||
@@ -730,8 +737,9 @@ func (e *Engine) Report(from int64) Report {
|
||||
}
|
||||
|
||||
type stateFile struct {
|
||||
V int `json:"v"`
|
||||
Bans map[string]banRec `json:"bans"`
|
||||
V int `json:"v"`
|
||||
Bans map[string]banRec `json:"bans"`
|
||||
Allows map[string]int64 `json:"allows,omitempty"`
|
||||
}
|
||||
|
||||
func (e *Engine) loadState() {
|
||||
@@ -752,6 +760,12 @@ func (e *Engine) loadState() {
|
||||
e.bans[a.Unmap()] = v
|
||||
}
|
||||
}
|
||||
nowU := e.cfg.Now().Unix()
|
||||
for k, u := range sf.Allows {
|
||||
if a, err := netip.ParseAddr(k); err == nil && u > nowU && u <= nowU+MaxAllow {
|
||||
e.allows[a.Unmap()] = u
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// maybeSave writes the state at most every saveEvery.
|
||||
@@ -774,10 +788,15 @@ func (e *Engine) saveState() {
|
||||
}
|
||||
e.lastSave = e.cfg.Now()
|
||||
e.trimMaps(e.lastSave)
|
||||
sf := stateFile{V: 1, Bans: map[string]banRec{}}
|
||||
sf := stateFile{V: 1, Bans: map[string]banRec{}, Allows: map[string]int64{}}
|
||||
for a, b := range e.bans {
|
||||
sf.Bans[a.String()] = b
|
||||
}
|
||||
for a, u := range e.allows {
|
||||
if u > e.lastSave.Unix() {
|
||||
sf.Allows[a.String()] = u
|
||||
}
|
||||
}
|
||||
e.dirty = false
|
||||
e.mu.Unlock()
|
||||
b, _ := json.Marshal(sf)
|
||||
@@ -818,7 +837,7 @@ func (e *Engine) processProbe(a netip.Addr, ev core.Event, now time.Time) {
|
||||
delete(st.probes, key)
|
||||
}
|
||||
}
|
||||
in := Input{Now: now, Probes: len(st.probes), Exclude: e.excl.Excluded(a)}
|
||||
in := Input{Now: now, Probes: len(st.probes), Exclude: e.exclude(a, now)}
|
||||
e.fill(a, st, &in, now)
|
||||
e.apply(a, st, Decide(in, Params{Threshold: e.cfg.Threshold}), "local", now)
|
||||
}
|
||||
Reference in new issue
Block a user