parsimony/internal/app/app.go
tgiachi 38552f8025 fix(app): add read, write and idle timeouts to the HTTP servers
- the API + admin UI server and the public web server get ReadTimeout 30s,
  WriteTimeout 60s and IdleTimeout 120s next to ReadHeaderTimeout
- the archive import extends its own read and write deadlines to 30
  minutes with http.ResponseController, so a big mbox is not cut
- tests: a stalled body and an idle connection are cut on both servers,
  an import longer than the default timeouts succeeds, fields are pinned
- README: document the limits and timeouts
2026-11-10 11:54:50 +01:00

288 lines
7.7 KiB
Go

// Package app wires the store, router, LMTP server and REST API together.
package app
import (
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"sync"
"time"
"github.com/emersion/go-smtp"
"codeberg.org/squidd/parsimony/internal/adminui"
"codeberg.org/squidd/parsimony/internal/api"
"codeberg.org/squidd/parsimony/internal/config"
"codeberg.org/squidd/parsimony/internal/dkimsign"
"codeberg.org/squidd/parsimony/internal/ingest"
"codeberg.org/squidd/parsimony/internal/outbound"
"codeberg.org/squidd/parsimony/internal/queue"
"codeberg.org/squidd/parsimony/internal/router"
"codeberg.org/squidd/parsimony/internal/store"
"codeberg.org/squidd/parsimony/internal/web"
)
// nowFunc is the clock of the store (queue schedules, token expiry). Tests move it.
var nowFunc = time.Now
// HTTP timeouts of the API + admin UI server and of the public web server, so
// a slow or silent client cannot pin a goroutine. The archive import extends
// its own deadlines (see api.importArchive). Variables so tests can shorten them.
var (
httpReadTimeout = 30 * time.Second
httpWriteTimeout = 60 * time.Second
httpIdleTimeout = 120 * time.Second
)
func newHTTPServer(h http.Handler) *http.Server {
return &http.Server{
Handler: h,
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: httpReadTimeout,
WriteTimeout: httpWriteTimeout,
IdleTimeout: httpIdleTimeout,
}
}
// heloName is the EHLO name: the configured one, else this machine's host name.
func heloName(configured string) string {
if configured != "" {
return configured
}
if h, err := os.Hostname(); err == nil && h != "" {
return h
}
return "localhost"
}
// allowListener drops connections from clients that are not in the allow list,
// before the LMTP greeting: LMTP has no authentication of its own.
type allowListener struct {
net.Listener
nets []*net.IPNet
}
func (l allowListener) Accept() (net.Conn, error) {
for {
c, err := l.Listener.Accept()
if err != nil {
return nil, err
}
if l.allowed(c.RemoteAddr()) {
return c, nil
}
log.Printf("lmtp: refused a connection from %s (not in lmtp.allow)", c.RemoteAddr())
c.Close()
}
}
func (l allowListener) allowed(addr net.Addr) bool {
tcp, ok := addr.(*net.TCPAddr)
if !ok {
return false
}
for _, n := range l.nets {
if n.Contains(tcp.IP) {
return true
}
}
return false
}
// lmtpIsOpen reports whether the LMTP listener is reachable beyond this machine.
func lmtpIsOpen(network, addr string) bool {
if network != "tcp" && network != "tcp4" && network != "tcp6" {
return false // a unix socket is protected by the permissions of its file
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
return true
}
if host == "localhost" {
return false
}
ip := net.ParseIP(host)
return ip == nil || !ip.IsLoopback()
}
// App is a running daemon.
type App struct {
// LMTPAddr and APIAddr are the addresses actually bound.
// APIAddr is empty when the API is disabled.
LMTPAddr string
APIAddr string
WebAddr string // empty when the public web is disabled
store *store.Store
lmtp *smtp.Server
lns []net.Listener // closed by Close so Serve returns even if Close wins the race with it
web *http.Server
pub *http.Server
wg sync.WaitGroup
stop context.CancelFunc // stops the queue worker
once sync.Once
}
// releaser lets the API approve held messages through the router.
type releaser struct{ rt *router.Router }
func (r releaser) Release(_ context.Context, id int64) error { return r.rt.Release(id) }
// Start opens the database and begins serving. The REST API runs only when
// apiToken is not empty.
func Start(cfg config.Config, apiToken string) (*App, error) {
// Load the signing keys first: a broken key stops the daemon now, not at the first send.
var sign func([]byte) ([]byte, error)
if len(cfg.DKIM) > 0 {
keys := make([]dkimsign.Key, 0, len(cfg.DKIM))
for _, k := range cfg.DKIM {
key, err := dkimsign.LoadKey(k.Domain, k.Selector, k.KeyFile)
if err != nil {
return nil, fmt.Errorf("dkim key for %s: %w", k.Domain, err)
}
keys = append(keys, key)
}
signer, err := dkimsign.New(keys)
if err != nil {
return nil, err
}
sign = signer.Sign
}
// Same for the relay secrets: read them now, not at the first send.
relayPassword, err := cfg.RelayPassword()
if err != nil {
return nil, err
}
relayCAs, err := cfg.RelayRootCAs()
if err != nil {
return nil, err
}
st, err := store.Open(cfg.DB)
if err != nil {
return nil, fmt.Errorf("store: %w", err)
}
st.Now = nowFunc
a := &App{store: st}
rt := router.New(st)
rt.BaseURL = cfg.BaseURL
worker := &queue.Worker{
Store: st,
Sender: &outbound.SMTPRelay{
Addr: cfg.Relay.Addr, TLS: cfg.Relay.TLS, ServerName: cfg.Relay.ServerName, RootCAs: relayCAs,
Username: cfg.Relay.Username, Password: relayPassword, Helo: heloName(cfg.Relay.Helo),
},
Concurrency: cfg.Relay.Concurrency,
Sign: sign,
}
rt.OnEnqueue = worker.Wake
a.lmtp = ingest.NewServer(rt)
if cfg.LMTP.Network == "unix" {
os.Remove(cfg.LMTP.Addr)
}
lmtpLn, err := net.Listen(cfg.LMTP.Network, cfg.LMTP.Addr)
if err != nil {
st.Close()
return nil, fmt.Errorf("listen lmtp: %w", err)
}
a.LMTPAddr = lmtpLn.Addr().String()
a.lns = append(a.lns, lmtpLn)
serveLn := net.Listener(lmtpLn)
if allow := cfg.LMTPAllowed(); len(allow) > 0 && (cfg.LMTP.Network == "tcp" || cfg.LMTP.Network == "tcp4" || cfg.LMTP.Network == "tcp6") {
serveLn = allowListener{Listener: lmtpLn, nets: allow}
} else if lmtpIsOpen(cfg.LMTP.Network, cfg.LMTP.Addr) {
log.Printf("warning: LMTP on %s accepts any client and has no authentication, so anyone who can reach it can post as any member: set lmtp.allow or listen on 127.0.0.1", cfg.LMTP.Addr)
}
var apiLn net.Listener
if apiToken != "" {
apiLn, err = net.Listen("tcp", cfg.API.Listen)
if err != nil {
lmtpLn.Close()
st.Close()
return nil, fmt.Errorf("listen api: %w", err)
}
a.APIAddr = apiLn.Addr().String()
a.lns = append(a.lns, apiLn)
// The admin UI and the API share a listener, so the UI needs no CORS.
mux := http.NewServeMux()
mux.Handle("/api/", api.New(st, apiToken, releaser{rt}))
mux.Handle("/", adminui.Embedded())
a.web = newHTTPServer(mux)
}
var pubLn net.Listener
if cfg.Web.Listen != "" {
pubLn, err = net.Listen("tcp", cfg.Web.Listen)
if err != nil {
for _, ln := range a.lns {
ln.Close()
}
st.Close()
return nil, fmt.Errorf("listen web: %w", err)
}
a.WebAddr = pubLn.Addr().String()
a.lns = append(a.lns, pubLn)
a.pub = newHTTPServer(web.New(st, rt))
}
var workerCtx context.Context
workerCtx, a.stop = context.WithCancel(context.Background())
a.wg.Add(1)
go func() {
defer a.wg.Done()
worker.Run(workerCtx)
}()
a.wg.Add(1)
go func() {
defer a.wg.Done()
if err := a.lmtp.Serve(serveLn); err != nil && !errors.Is(err, smtp.ErrServerClosed) && !errors.Is(err, net.ErrClosed) {
log.Printf("lmtp: %v", err)
}
}()
if a.web != nil {
a.wg.Add(1)
go func() {
defer a.wg.Done()
if err := a.web.Serve(apiLn); err != nil && !errors.Is(err, http.ErrServerClosed) && !errors.Is(err, net.ErrClosed) {
log.Printf("api: %v", err)
}
}()
}
if a.pub != nil {
a.wg.Add(1)
go func() {
defer a.wg.Done()
if err := a.pub.Serve(pubLn); err != nil && !errors.Is(err, http.ErrServerClosed) && !errors.Is(err, net.ErrClosed) {
log.Printf("web: %v", err)
}
}()
}
return a, nil
}
// Close stops the servers and closes the database. It is safe to call twice.
func (a *App) Close() {
a.once.Do(func() {
for _, ln := range a.lns {
ln.Close()
}
a.lmtp.Close()
if a.web != nil {
a.web.Close()
}
if a.pub != nil {
a.pub.Close()
}
a.stop() // in-flight sends are interrupted and stay queued
a.wg.Wait()
a.store.Close()
})
}