A bounce could be recorded as a successful delivery. The log-tailer's Fixed: - A bounce could be recorded as a successful delivery. The log-tailer's - Log-tailer resumes where it stopped instead of jumping to end-of-file on - Level-2 rate limit no longer overshoots under concurrency: messages that Changed: - The project has a single public home: `github.com/mixeme/selfpost`. Codeberg - Code comments no longer cite the archived specification. References like - `docs/code-review.md` is gone. Its plan is finished — phases 0 (bar the - `docs/architecture.md` gained a *Code layers* section: a diagram of - Phase 1 doc/code hygiene (`docs/code-review.md`): removed ~30 stale … (1 more in CHANGELOG) Security: - Pre-release security review (plan § D, model Fable, 2026-08-06): full pass - `saslpasswd2` argv: the application login is now passed after a `--` Added: - Optional password encryption for the two secret-bearing downloads (plan - Domain import accepts an encrypted export: the envelope is detected by its - `selfpost-backup` writes encrypted archives and reads them back: - `TestParseDelivery` covers the exotic mail.log shapes the review asked for - docs: README *Encrypting a backup or export*; `docs/security.md` § … (14 more in CHANGELOG) Removed: - docs: `security.md` — accepted risk «restore old backup revives session rows» Changed: - docs: `implementation-plan.md` trimmed to the sole open v1.x gate — pre-release - docs: `architecture.md` — sessions (SQLite, idle renew, password change), - docs: `development.md` — expanded e2e stack and `release.yml` matrix workflow. - docs: `security.md` — accepted risk for send-log rows stuck at `queued` after - docs: `roadmap.md` — optional send-log / `mail.log` follow-ups under v1.x tail. … (10 more in CHANGELOG) Full detail: CHANGELOG.md (0.5.0).
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package web
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// rateLimiter is a simple fixed-window per-key counter used to throttle the
|
|
// setup and login routes (security.md). Keys are client IPs. It is not a
|
|
// precise sliding window — a coarse backstop against brute-force and log noise
|
|
// is all these routes need.
|
|
type rateLimiter struct {
|
|
max int
|
|
window time.Duration
|
|
|
|
mu sync.Mutex
|
|
buckets map[string]*rlBucket
|
|
}
|
|
|
|
type rlBucket struct {
|
|
count int
|
|
windowEnds time.Time
|
|
}
|
|
|
|
func newRateLimiter(max int, window time.Duration) *rateLimiter {
|
|
return &rateLimiter{
|
|
max: max,
|
|
window: window,
|
|
buckets: make(map[string]*rlBucket),
|
|
}
|
|
}
|
|
|
|
// Allow records an attempt for key and reports whether it is within the limit.
|
|
// The current window is reset lazily once it elapses.
|
|
func (r *rateLimiter) Allow(key string) bool {
|
|
now := time.Now()
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
|
|
b := r.buckets[key]
|
|
if b == nil || now.After(b.windowEnds) {
|
|
r.buckets[key] = &rlBucket{count: 1, windowEnds: now.Add(r.window)}
|
|
r.sweep(now)
|
|
return true
|
|
}
|
|
if b.count >= r.max {
|
|
return false
|
|
}
|
|
b.count++
|
|
return true
|
|
}
|
|
|
|
// sweep drops expired buckets so the map cannot grow without bound. Called
|
|
// under the lock while a window is being reset, which is often enough given the
|
|
// low request volume of these routes.
|
|
func (r *rateLimiter) sweep(now time.Time) {
|
|
for k, b := range r.buckets {
|
|
if now.After(b.windowEnds) {
|
|
delete(r.buckets, k)
|
|
}
|
|
}
|
|
}
|