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)
|
||||
}
|
||||
@@ -94,6 +94,10 @@ func (e *Engine) Sweep() {
|
||||
if e.cfg.Roster == nil {
|
||||
return
|
||||
}
|
||||
if !e.sweeping.CompareAndSwap(false, true) {
|
||||
return
|
||||
}
|
||||
defer e.sweeping.Store(false)
|
||||
now := e.cfg.Now()
|
||||
from := now.Add(-LongWindow).Unix()
|
||||
roster := e.cfg.Roster(from)
|
||||
@@ -116,6 +120,7 @@ func (e *Engine) Sweep() {
|
||||
a = a.Unmap().WithZone("")
|
||||
st := e.stateOf(a)
|
||||
t := tallies[a.String()]
|
||||
t.SSH, t.Web = max(t.SSH, r.Est.SSH), max(t.Web, r.Est.Web)
|
||||
in := Input{Now: now, Known: IsKnown(r.Score, r.Level), SSH24: t.SSH, Web24: t.Web,
|
||||
Legit: st.legit >= now.Add(-LegitWindow).Unix(), Exclude: e.excl.Excluded(a)}
|
||||
e.fill(a, st, &in, now)
|
||||
|
||||
@@ -2,6 +2,9 @@ package enforce
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -177,3 +180,54 @@ func itoa(n int) string {
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func TestOperatorAllowBlocksBansAndExpires(t *testing.T) {
|
||||
r := histRig(t, func(c *Config) { c.UnbanPath = filepath.Join(t.TempDir(), "u", "unban.log") })
|
||||
ip := netip.MustParseAddr("203.0.114.80")
|
||||
r.hits(ip.String(), 5, "/x", 404)
|
||||
if len(r.lines(t)) != 1 {
|
||||
t.Fatalf("expected a ban: %v", r.lines(t))
|
||||
}
|
||||
if err := r.e.Unban(ip, 3600, "local"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b, _ := os.ReadFile(r.e.UnbanLogPath()); !strings.HasSuffix(string(b), " urutau unban ip=203.0.114.80 allow=3600\n") {
|
||||
t.Fatalf("file %q", b)
|
||||
}
|
||||
r.now = r.now.Add(time.Minute)
|
||||
r.hits(ip.String(), 8, "/y", 404)
|
||||
if len(r.lines(t)) != 1 {
|
||||
t.Fatalf("allowed address was banned: %v", r.lines(t))
|
||||
}
|
||||
if r.e.counts.Skipped[SkipOperator] == 0 {
|
||||
t.Fatal("skip not counted")
|
||||
}
|
||||
// survives a restart
|
||||
e2 := New(Config{Mode: ModeOn, Path: r.log, StatePath: r.e.cfg.StatePath, Now: func() time.Time { return r.now }})
|
||||
if len(e2.Allows()) != 1 {
|
||||
t.Fatal("allow not persisted")
|
||||
}
|
||||
// expires by itself
|
||||
r.now = r.now.Add(2 * time.Hour)
|
||||
if len(r.e.Allows()) != 0 {
|
||||
t.Fatal("allow did not expire")
|
||||
}
|
||||
r.hits(ip.String(), 8, "/z", 404)
|
||||
if len(r.lines(t)) != 2 {
|
||||
t.Fatalf("not banned after expiry: %v", r.lines(t))
|
||||
}
|
||||
// ending early lets the engine decide again
|
||||
r.e.Unban(ip, 3600, "local")
|
||||
if !r.e.EndAllow(ip) || r.e.EndAllow(ip) {
|
||||
t.Fatal("EndAllow")
|
||||
}
|
||||
if got := r.e.UnbansSince(0); len(got) != 2 || got[0].IP != ip.String() {
|
||||
t.Fatalf("unbans for peers: %v", got)
|
||||
}
|
||||
if rep := r.e.Report(0); rep.Recent[0].Action != "unban" {
|
||||
t.Fatalf("recent: %+v", rep.Recent[0])
|
||||
}
|
||||
if err := New(Config{Mode: ModeOn, Path: r.log}).Unban(ip, 0, "local"); err != ErrNoUnbanLog {
|
||||
t.Fatalf("no file: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -92,11 +92,12 @@ func TTLFor(offences int) time.Duration {
|
||||
|
||||
// Skip reasons, also the keys of Counts.Skipped.
|
||||
const (
|
||||
SkipTrusted = "trusted"
|
||||
SkipPrivate = "private"
|
||||
SkipCrawler = "crawler"
|
||||
SkipLegit = "legit_visitor"
|
||||
SkipPeer = "peer"
|
||||
SkipTrusted = "trusted"
|
||||
SkipPrivate = "private"
|
||||
SkipCrawler = "crawler"
|
||||
SkipLegit = "legit_visitor"
|
||||
SkipOperator = "operator_allow"
|
||||
SkipPeer = "peer"
|
||||
// SkipUnconfirmed: a peer's decision about an address this node never saw attacking.
|
||||
SkipUnconfirmed = "peer_unconfirmed"
|
||||
)
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package enforce
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/evlog"
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
||||
)
|
||||
|
||||
func TestSweepWithRealStoreAndRegistry(t *testing.T) {
|
||||
now := time.Now()
|
||||
s := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
|
||||
l, err := evlog.Open(evlog.Options{Dir: filepath.Join(t.TempDir(), "ev")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetEventLog(l)
|
||||
for i := 0; i < 41; i++ {
|
||||
s.Ingest(core.Event{Time: now.Add(-time.Duration(20+i) * time.Minute), IP: "43.139.48.108", Cat: core.SSHFail, Source: "ssh", User: "root"})
|
||||
}
|
||||
for i := 0; i < 30; i++ {
|
||||
s.Ingest(core.Event{Time: now.Add(-time.Duration(30+i) * time.Minute), IP: "129.213.151.234", Cat: core.Scanner, Source: "traefik",
|
||||
Domain: "a.example", Method: "GET", Path: "/.env", Status: 404, UA: "curl/8"})
|
||||
}
|
||||
l.Flush()
|
||||
r := newRig(t, ModeOn, func(c *Config) {
|
||||
c.Now = time.Now
|
||||
c.Rep = s.Reputation
|
||||
c.Roster = func(from int64) []Seen {
|
||||
var out []Seen
|
||||
for _, x := range s.AttackRoster(from) {
|
||||
out = append(out, Seen{IP: x.IP, Score: x.Score, Level: x.Level, Jails: x.Jails})
|
||||
}
|
||||
return out
|
||||
}
|
||||
c.Tallies = func(from int64) map[string]Tally {
|
||||
out := map[string]Tally{}
|
||||
for k, v := range s.AttackTallies(from) {
|
||||
out[k] = Tally{SSH: v.SSH, Web: v.Web}
|
||||
}
|
||||
return out
|
||||
}
|
||||
})
|
||||
r.e.Sweep()
|
||||
r.e.drain()
|
||||
if n := len(r.lines(t)); n != 2 {
|
||||
t.Fatalf("lines %d: %v / roster %+v / tallies %+v", n, r.lines(t), s.AttackRoster(0), s.AttackTallies(0))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTalliesAfterCompression(t *testing.T) {
|
||||
real := time.Now()
|
||||
shift := time.Duration(0)
|
||||
s := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
|
||||
l, err := evlog.Open(evlog.Options{Dir: filepath.Join(t.TempDir(), "ev"), Now: func() time.Time { return real.Add(shift) }})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.SetEventLog(l)
|
||||
for i := 0; i < 41; i++ {
|
||||
s.Ingest(core.Event{Time: real.Add(-time.Duration(20+i) * time.Minute), IP: "43.139.48.108", Cat: core.SSHFail, Source: "ssh", User: "root"})
|
||||
}
|
||||
l.Flush()
|
||||
shift = 36 * time.Hour // the day ends; Maintain compresses it
|
||||
l.Maintain()
|
||||
shift = 0
|
||||
m := s.AttackTallies(real.Add(-24 * time.Hour).Unix())
|
||||
if m["43.139.48.108"].SSH != 41 {
|
||||
t.Fatalf("tallies: %+v", m)
|
||||
}
|
||||
}
|
||||
|
||||
// The registry may hold less than the store knows (it only records live events):
|
||||
// the per-day counters must still justify the long-window ban.
|
||||
func TestSweepWithoutRegistryUsesDayCounters(t *testing.T) {
|
||||
now := time.Now()
|
||||
s := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
|
||||
for i := 0; i < 41; i++ {
|
||||
s.Ingest(core.Event{Time: now.Add(-time.Duration(20+i) * time.Minute), IP: "43.139.48.108", Cat: core.SSHFail, Source: "ssh", User: "root"})
|
||||
}
|
||||
for i := 0; i < 30; i++ {
|
||||
s.Ingest(core.Event{Time: now.Add(-time.Duration(30+i) * time.Minute), IP: "129.213.151.234", Cat: core.Scanner, Source: "traefik",
|
||||
Domain: "a.example", Method: "GET", Path: "/.env", Status: 404, UA: "curl/8"})
|
||||
}
|
||||
r := newRig(t, ModeOn, func(c *Config) {
|
||||
c.Now = time.Now
|
||||
c.Roster = func(from int64) []Seen {
|
||||
var out []Seen
|
||||
for _, x := range s.AttackRoster(from) {
|
||||
out = append(out, Seen{IP: x.IP, Score: x.Score, Level: x.Level, Jails: x.Jails, Est: Tally{SSH: x.Est.SSH, Web: x.Est.Web}})
|
||||
}
|
||||
return out
|
||||
}
|
||||
})
|
||||
r.e.Sweep()
|
||||
r.e.drain()
|
||||
if n := len(r.lines(t)); n != 2 {
|
||||
t.Fatalf("lines %d: %v", n, r.lines(t))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package enforce
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
// MaxAllow is the longest operator allowance in seconds (30 days).
|
||||
const MaxAllow = 30 * 24 * 3600
|
||||
|
||||
// ErrNoUnbanLog means unbanning is not configured.
|
||||
var ErrNoUnbanLog = errors.New("unban file not configured")
|
||||
|
||||
// UnbanWire is an unban as exchanged between peers.
|
||||
type UnbanWire struct {
|
||||
T int64 `json:"t"`
|
||||
IP string `json:"ip"`
|
||||
Allow int `json:"allow"`
|
||||
}
|
||||
|
||||
// Allow is an operator allowance: the engine does not ban the address until it ends.
|
||||
type Allow struct {
|
||||
IP string `json:"ip"`
|
||||
Until int64 `json:"until"`
|
||||
By string `json:"by"`
|
||||
}
|
||||
|
||||
// exclude is Excluded plus the operator allowances; the lock is held.
|
||||
func (e *Engine) exclude(a netip.Addr, now time.Time) string {
|
||||
if u, ok := e.allows[a.Unmap()]; ok && u > now.Unix() {
|
||||
return SkipOperator
|
||||
}
|
||||
return e.excl.Excluded(a)
|
||||
}
|
||||
|
||||
// UnbanLogPath is the file the host reads unban requests from.
|
||||
func (e *Engine) UnbanLogPath() string { return e.cfg.UnbanPath }
|
||||
|
||||
// UnbanLine formats one request of the unban file (exact host contract).
|
||||
func UnbanLine(t time.Time, a netip.Addr, allow int) string {
|
||||
return fmt.Sprintf("%s urutau unban ip=%s allow=%d\n", t.UTC().Format("2006-01-02T15:04:05Z"), a.Unmap().WithZone("").String(), allow)
|
||||
}
|
||||
|
||||
// Unban writes the request for the host, drops the active ban of the address
|
||||
// and, when allowS > 0, keeps the engine from banning it for that long.
|
||||
func (e *Engine) Unban(a netip.Addr, allowS int, src string) error {
|
||||
if e.cfg.UnbanPath == "" {
|
||||
return ErrNoUnbanLog
|
||||
}
|
||||
if allowS < 0 || allowS > MaxAllow {
|
||||
return fmt.Errorf("allow out of range")
|
||||
}
|
||||
a = a.Unmap().WithZone("")
|
||||
now := e.cfg.Now()
|
||||
if err := e.unlog.write(UnbanLine(now, a, allowS)); err != nil {
|
||||
return err
|
||||
}
|
||||
e.mu.Lock()
|
||||
delete(e.bans, a)
|
||||
delete(e.would, a)
|
||||
delete(e.skipSeen, a)
|
||||
if st := e.ips[a]; st != nil {
|
||||
st.banned, st.urutauAt, st.wouldAt, st.f2bAt = 0, 0, 0, 0
|
||||
st.ev, st.ssh = nil, nil
|
||||
}
|
||||
delete(e.long, a)
|
||||
if allowS > 0 {
|
||||
e.allows[a] = now.Add(time.Duration(allowS) * time.Second).Unix()
|
||||
}
|
||||
e.addRecent(a, Recent{T: now.Unix(), IP: a.String(), Action: "unban", TTL: allowS, Src: src, SkipReason: ""})
|
||||
if src == "local" {
|
||||
e.unbans = append(e.unbans, UnbanWire{T: now.Unix(), IP: a.String(), Allow: allowS})
|
||||
if len(e.unbans) > MaxWire {
|
||||
e.unbans = e.unbans[len(e.unbans)-MaxWire:]
|
||||
}
|
||||
}
|
||||
e.dirty = true
|
||||
e.mu.Unlock()
|
||||
e.saveState()
|
||||
return nil
|
||||
}
|
||||
|
||||
// EndAllow ends an operator allowance early; it reports whether there was one.
|
||||
func (e *Engine) EndAllow(a netip.Addr) bool {
|
||||
a = a.Unmap().WithZone("")
|
||||
e.mu.Lock()
|
||||
_, ok := e.allows[a]
|
||||
delete(e.allows, a)
|
||||
if ok {
|
||||
e.dirty = true
|
||||
}
|
||||
e.mu.Unlock()
|
||||
if ok {
|
||||
e.saveState()
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// Allows lists the active operator allowances, soonest to end first.
|
||||
func (e *Engine) Allows() []Allow {
|
||||
now := e.cfg.Now().Unix()
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
out := []Allow{}
|
||||
for a, u := range e.allows {
|
||||
if u > now {
|
||||
out = append(out, Allow{IP: a.String(), Until: u, By: "operator"})
|
||||
} else {
|
||||
delete(e.allows, a)
|
||||
}
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Until < out[j].Until })
|
||||
return out
|
||||
}
|
||||
|
||||
// UnbansSince returns the local unbans since the unix time, for peers.
|
||||
func (e *Engine) UnbansSince(since int64) []UnbanWire {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
out := []UnbanWire{}
|
||||
for _, w := range e.unbans {
|
||||
if w.T >= since {
|
||||
out = append(out, w)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// AcceptPeerUnban applies unbans of a peer (same effect as a local one, so the
|
||||
// shared decision is not re-applied here); it returns how many were accepted.
|
||||
func (e *Engine) AcceptPeerUnban(name string, in []UnbanWire) (accepted int) {
|
||||
if len(in) > MaxWire {
|
||||
in = in[:MaxWire]
|
||||
}
|
||||
for _, w := range in {
|
||||
a, err := netip.ParseAddr(w.IP)
|
||||
if err != nil || w.Allow < 0 || w.Allow > MaxAllow {
|
||||
continue
|
||||
}
|
||||
if e.Unban(a, w.Allow, "peer:"+token(name, "peer")) == nil {
|
||||
accepted++
|
||||
}
|
||||
}
|
||||
return accepted
|
||||
}
|
||||
Reference in new issue
Block a user