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 }