File size: 2,300 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package smtpsrv

import (
	"context"
	"fmt"
	"log/slog"
	"net"
	"strings"
	"time"

	"github.com/emersion/go-smtp"
)

// SMTPLogger implements the smtp.Logger interface using the main app Logger.
type SMTPLogger struct {
	logger *slog.Logger
}

// Printf adheres to smtp.Server's Logger interface.
func (l *SMTPLogger) Printf(format string, v ...interface{}) {
	s := fmt.Sprintf(format, v...)
	// TODO: Uses string compare to filter out errors caused by TCP health checks,
	// remove once https://github.com/emersion/go-smtp/issues/236 has been fixed.
	if strings.Contains(s, "read: connection reset by peer") {
		return
	}
	l.logger.Error("SMTP error.", slog.String("error", s))
}

// Print adheres to smtp.Server's Logger interface.
func (l *SMTPLogger) Println(v ...interface{}) {
	s := fmt.Sprint(v...)
	// TODO: Uses string compare to filter out errors caused by TCP health checks,
	// remove once https://github.com/emersion/go-smtp/issues/236 has been fixed.
	if strings.Contains(s, "read: connection reset by peer") {
		return
	}
	l.logger.Error("SMTP error.", slog.String("error", s))
}

// Server implements an SMTP server that creates alerts.
type Server struct {
	cfg Config
	srv *smtp.Server
}

// NewServer creates a new Server.
func NewServer(cfg Config) *Server {
	s := &Server{cfg: cfg}

	srv := smtp.NewServer(s)
	srv.ErrorLog = &SMTPLogger{logger: cfg.Logger}
	srv.Domain = cfg.Domain
	srv.ReadTimeout = 10 * time.Second
	srv.WriteTimeout = 10 * time.Second
	srv.TLSConfig = cfg.TLSConfig
	s.srv = srv

	if cfg.MaxRecipients == 0 {
		cfg.MaxRecipients = 1
	}
	if cfg.BackgroundContext == nil {
		panic("smtpsrv: BackgroundContext is required")
	}
	if cfg.AuthorizeFunc == nil {
		panic("smtpsrv: AuthorizeFunc is required")
	}
	if cfg.CreateAlertFunc == nil {
		panic("smtpsrv: CreateAlertFunc is required")
	}

	return s
}

var _ smtp.Backend = &Server{}

// ServeSMTP starts the SMTP server on the given listener.
func (s *Server) ServeSMTP(l net.Listener) error { return s.srv.Serve(l) }

// NewSession implements the smtp.Backend interface.
func (s *Server) NewSession(_ *smtp.Conn) (smtp.Session, error) { return &Session{cfg: s.cfg}, nil }

// Shutdown gracefully shuts down the SMTP server.
func (s *Server) Shutdown(ctx context.Context) error { return s.srv.Shutdown(ctx) }