test / test (push) Has been cancelled
Review pass over the 1.4.0-1.9.1 work. Port 25 no longer inherits main.cf's submission milter chain: smtpd_milters is always overridden on smtp/inet, to the antispam milter or to nothing. Without that, OpenDKIM (strict, tempfail) gated relayed mail during a signing outage, and the journal-milter filed inbound mail into the outbound send log under the sender's domain -- so Deliveries showed other people's mail and a forged From: consumed that domain's level-2 budget. architecture.md already described the intended behaviour; the code now matches. DMARC reports are checked before they are stored: the policy domain must be a configured sending domain, and must match the +tag of a per-domain hosted address. The rua address form is predictable, so anyone able to reach it could file reports under any domain and evict genuine ones through the retention cap. Zip attachments are now decompressed as well as gzip. The auto level-2 ceiling is scaled to the level-1 window before the multiplier is applied; any window other than 3600s shifted the allowance by window/3600. Migration 0010 indexes send_log(app_login, created_at) -- both the per-MAIL FROM rate-limit count and the per-application statistics were scanning the created_at range. e2e now regenerates the Postfix config in-container for all four port-25 flag combinations and requires postfix check to pass; the 1.9.1 crash-loop shipped in 1.4.0 and survived three releases because only the flags-off path was exercised. Migration 0009 stays as shipped -- its conversion branch only fires for a database carrying application-scope allowed_ips from before 1.9.0. The schema reference notes that the old list was permissive and the new column is restrictive; the guide documents the allow-list as a sending control on a fail-open path, not an authentication boundary. Also: gofmt across cmd/ and internal/, unused duplicate validators dropped from internal/web/validate, export no longer swallows store errors. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
102 lines
3.1 KiB
Go
102 lines
3.1 KiB
Go
package store
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"time"
|
|
)
|
|
|
|
// StatsWindowDays is the rolling window for send statistics on the domain page.
|
|
const StatsWindowDays = 30
|
|
|
|
// SendStats holds message-volume metrics over a stats window (guide § Rate
|
|
// limiting — same counting rules as CountMessages).
|
|
type SendStats struct {
|
|
Total int64
|
|
PeakPerHour int64
|
|
AvgPerHour float64
|
|
WindowDays int // actual days queried (min(30, retention))
|
|
WindowHours float64 // hours used for the average-rate denominator
|
|
}
|
|
|
|
// StatsWindow computes the since timestamp and hours denominator for send
|
|
// statistics. The query window is min(30, retention) days; hours_in_window is
|
|
// min(720, entity age in hours, retention hours).
|
|
func StatsWindow(retentionDays int, entityCreated time.Time) (since time.Time, hours float64, windowDays int) {
|
|
if retentionDays <= 0 {
|
|
retentionDays = SendLogRetentionDaysDefault
|
|
}
|
|
windowDays = StatsWindowDays
|
|
if retentionDays < StatsWindowDays {
|
|
windowDays = retentionDays
|
|
}
|
|
since = time.Now().UTC().AddDate(0, 0, -windowDays)
|
|
|
|
retentionHours := float64(retentionDays) * 24
|
|
ageHours := time.Since(entityCreated).Hours()
|
|
if ageHours < 0 {
|
|
ageHours = 0
|
|
}
|
|
|
|
hours = math.Min(720, math.Min(ageHours, retentionHours))
|
|
if hours < 1 {
|
|
hours = 1
|
|
}
|
|
return since, hours, windowDays
|
|
}
|
|
|
|
// DomainSendStats returns 30-day (or shorter when retention is lower) send
|
|
// statistics for a domain name.
|
|
func (s *Store) DomainSendStats(name string, retentionDays int, createdAt time.Time) (SendStats, error) {
|
|
since, hours, windowDays := StatsWindow(retentionDays, createdAt)
|
|
stats, err := s.sendStats("domain", name, since, hours)
|
|
if err != nil {
|
|
return SendStats{}, err
|
|
}
|
|
stats.WindowDays = windowDays
|
|
stats.WindowHours = hours
|
|
return stats, nil
|
|
}
|
|
|
|
// AppSendStats returns send statistics for an application login.
|
|
func (s *Store) AppSendStats(login string, retentionDays int, createdAt time.Time) (SendStats, error) {
|
|
since, hours, windowDays := StatsWindow(retentionDays, createdAt)
|
|
stats, err := s.sendStats("app_login", login, since, hours)
|
|
if err != nil {
|
|
return SendStats{}, err
|
|
}
|
|
stats.WindowDays = windowDays
|
|
stats.WindowHours = hours
|
|
return stats, nil
|
|
}
|
|
|
|
func (s *Store) sendStats(column, ref string, since time.Time, hours float64) (SendStats, error) {
|
|
sinceStr := since.UTC().Format(time.RFC3339)
|
|
|
|
var total int64
|
|
err := s.db.QueryRow(
|
|
`SELECT COUNT(DISTINCT queue_id) FROM send_log
|
|
WHERE `+column+` = ? AND status != ? AND created_at >= ?`,
|
|
ref, StatusRejected, sinceStr,
|
|
).Scan(&total)
|
|
if err != nil {
|
|
return SendStats{}, fmt.Errorf("send stats total %s %q: %w", column, ref, err)
|
|
}
|
|
|
|
var peak int64
|
|
err = s.db.QueryRow(
|
|
`SELECT COALESCE(MAX(bucket_count), 0) FROM (
|
|
SELECT COUNT(DISTINCT queue_id) AS bucket_count FROM send_log
|
|
WHERE `+column+` = ? AND status != ? AND created_at >= ?
|
|
GROUP BY substr(created_at, 1, 13)
|
|
)`,
|
|
ref, StatusRejected, sinceStr,
|
|
).Scan(&peak)
|
|
if err != nil {
|
|
return SendStats{}, fmt.Errorf("send stats peak %s %q: %w", column, ref, err)
|
|
}
|
|
|
|
avg := float64(total) / hours
|
|
return SendStats{Total: total, PeakPerHour: peak, AvgPerHour: avg}, nil
|
|
}
|