Skip to content

Commit

Permalink
Refactor logging in smtpd-proxy.go and server.go
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardinius committed Feb 11, 2024
1 parent 7618ec8 commit 1bfef3e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 23 deletions.
19 changes: 10 additions & 9 deletions app/cmd/smtpd-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ func Main(ctx context.Context, args ...string) error {

fmt.Printf("smtpd-proxy revision %s-%s\n", BRANCH, COMMIT)
opts.ConfigYamlFile = filepath.Clean(opts.ConfigYamlFile)
slog.InfoContext(ctx, "parsing yaml", "path", opts.ConfigYamlFile)
logger.InfoContext(ctx, "parsing yaml", "path", opts.ConfigYamlFile)
cfg, err := config.ParseFile(opts.ConfigYamlFile)
if err != nil {
slog.ErrorContext(ctx, "failed to parse configuration", "path", opts.ConfigYamlFile, "err", err)
logger.ErrorContext(ctx, "failed to parse configuration", "path", opts.ConfigYamlFile, "err", err)
return err
}
cfg, err = cfg.LoadDefaults()
if err != nil {
slog.ErrorContext(ctx, "failed to load configuration", "path", opts.ConfigYamlFile, "err", err)
logger.ErrorContext(ctx, "failed to load configuration", "path", opts.ConfigYamlFile, "err", err)
return err
}

Expand All @@ -77,7 +77,7 @@ func ListenProxyAndServe(ctx context.Context, c *config.Config) error {
return err
}

upstreamServers, err := createUpstreamServers(ctx, srvConfig.UpstreamServers)
upstreamServers, err := createUpstreamServers(ctx, logger, srvConfig.UpstreamServers)
if err != nil {
return err
}
Expand All @@ -88,6 +88,7 @@ func ListenProxyAndServe(ctx context.Context, c *config.Config) error {

srv := server.NewServer(
ctx,
logger,
srvConfig.Listen,
srvConfig.Ehlo,
).WithOptions(
Expand Down Expand Up @@ -133,21 +134,21 @@ func loadTLSConfig(serverCertificatePath, serverKeyPath string) (*tls.Config, er
return &tls.Config{Certificates: []tls.Certificate{cer}, MinVersion: tls.VersionTLS12}, nil
}

func createUpstreamServers(ctx context.Context, upstreamServersConfig []config.UpstreamServer) (reg upstream.Registry, err error) {
reg = upstream.NewEmptyRegistry()
func createUpstreamServers(ctx context.Context, logger *slog.Logger, upstreamServersConfig []config.UpstreamServer) (reg upstream.Registry, err error) {
reg = upstream.NewEmptyRegistry(logger)
for _, serverConfig := range upstreamServersConfig {
var handler upstream.Forwarder
var _err error

switch serverConfig.Type {
case "smtp":
srv := forwarder.NewSMTPServer()
srv := forwarder.NewSMTPServer(logger)
handler, _err = srv.Configure(ctx, serverConfig.Settings)
case "ses":
srv := forwarder.NewSESServer()
srv := forwarder.NewSESServer(logger)
handler, _err = srv.Configure(ctx, serverConfig.Settings)
case "log":
srv := forwarder.NewLogServer()
srv := forwarder.NewLogServer(logger)
handler, err = srv.Configure(ctx, serverConfig.Settings)
default:
_err = fmt.Errorf("unrecognized server type: %s. allowed values: smtp, ses, log", serverConfig.Type)
Expand Down
3 changes: 1 addition & 2 deletions app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func (srv *SrvBackend) ListenAndServe() error {
}

// NewServer prepares SMTP server
func NewServer(ctx context.Context, addr, domain string) *SrvBackend {
logger := slog.Default().With("server", addr, "ehlo", domain)
func NewServer(ctx context.Context, logger *slog.Logger, addr, domain string) *SrvBackend {
bkd := newBackend(ctx, logger, NoOpAuthFunc())
s := smtp.NewServer(bkd)
s.Addr = addr
Expand Down
7 changes: 4 additions & 3 deletions app/upstream/forwarder/log-forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ type logUpstreamSettings struct {

type logServer struct {
settings logUpstreamSettings
logger *slog.Logger
}

var _ upstream.Server = (*logServer)(nil)
var _ upstream.Forwarder = (*logServer)(nil)

// NewLogServer new ses upstream
func NewLogServer() upstream.Server {
return new(logServer)
func NewLogServer(logger *slog.Logger) upstream.Server {
return &logServer{logger: logger}
}

func (u *logServer) Configure(ctx context.Context, settings map[string]any) (upstream.Forwarder, error) {
Expand All @@ -44,7 +45,7 @@ func (u *logServer) Forward(ctx context.Context, mail *upstream.Email) error {
}
text := string(([]rune(string(mail.Text)))[:20]) + "..."

slog.InfoContext(ctx, "log-forwarder",
u.logger.InfoContext(ctx, "log-forwarder",
"uid", uid,
"from", mail.From,
"to", mail.To,
Expand Down
6 changes: 4 additions & 2 deletions app/upstream/forwarder/ses.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package forwarder
import (
"context"
"encoding/json"
"log/slog"
gohttp "net/http"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -28,14 +29,15 @@ type sesUpstreamSettings struct {
type sesUpstream struct {
settings sesUpstreamSettings
ses *ses.Client
logger *slog.Logger
}

var _ upstream.Server = (*sesUpstream)(nil)
var _ upstream.Forwarder = (*sesUpstream)(nil)

// NewSESServer new ses upstream
func NewSESServer() upstream.Server {
return new(sesUpstream)
func NewSESServer(logger *slog.Logger) upstream.Server {
return &sesUpstream{logger: logger}
}

func (u *sesUpstream) Configure(ctx context.Context, settings map[string]any) (upstream.Forwarder, error) {
Expand Down
6 changes: 4 additions & 2 deletions app/upstream/forwarder/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net"
"net/smtp"

Expand All @@ -22,14 +23,15 @@ type smtpUpstreamSettings struct {
type smptUpstream struct {
settings smtpUpstreamSettings
auth smtp.Auth
logger *slog.Logger
}

var _ upstream.Server = (*smptUpstream)(nil)
var _ upstream.Forwarder = (*smptUpstream)(nil)

// NewSMTPServer new smtp upstream
func NewSMTPServer() upstream.Server {
return new(smptUpstream)
func NewSMTPServer(logger *slog.Logger) upstream.Server {
return &smptUpstream{logger: logger}
}

func (u *smptUpstream) Configure(ctx context.Context, settings map[string]any) (upstream.Forwarder, error) {
Expand Down
12 changes: 7 additions & 5 deletions app/upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type RegistryMap struct {
entriesSorted []registryEntry
totalWeight int
rnd randInt
logger *slog.Logger
}

// randInt interface for random values.
Expand Down Expand Up @@ -109,10 +110,11 @@ func (s *randIntStruc) Int() int {
}

// NewEmptyRegistry creates empty new registry
func NewEmptyRegistry() *RegistryMap {
r := new(RegistryMap)
r.rnd = newRandIntStruc()
return r
func NewEmptyRegistry(logger *slog.Logger) *RegistryMap {
return &RegistryMap{
rnd: newRandIntStruc(),
logger: logger,
}
}

func (r *RegistryMap) AddForwarder(forwarder Forwarder, weight int) {
Expand Down Expand Up @@ -159,7 +161,7 @@ func (r *RegistryMap) Forward(ctx context.Context, mail *Email) error {
uid := entry.meta.UID
err = sender.Forward(context.WithValue(ctx, entryContextKey, &entry.meta), mail)
if err != nil {
slog.WarnContext(ctx, "forward error", "uid", uid, "err", err)
r.logger.WarnContext(ctx, "forward error", "uid", uid, "err", err)
}
return err
}
Expand Down

0 comments on commit 1bfef3e

Please sign in to comment.