-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
108 lines (86 loc) · 2.36 KB
/
main.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"crypto/tls"
"flag"
"fmt"
"math/rand"
"net"
"os"
"time"
"github.com/emersion/go-smtp"
log "github.com/inconshreveable/log15"
)
var (
configFileFlag = flag.String("c", "willi.conf", "Path to configuration file")
versionFlag = flag.Bool("V", false, "Print version and exit")
version = "undefined" // updated during release build
)
func main() {
rand.Seed(time.Now().UnixNano())
flag.Parse()
if *versionFlag {
fmt.Printf("willi smtp proxy - version %s\n", version)
os.Exit(0)
}
fmt.Fprintf(os.Stderr, "Loading config file %s\n", *configFileFlag)
config, err := loadConfigFile(*configFileFlag)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load config file %s: %v\n", *configFileFlag, err)
os.Exit(1)
}
log.Root().SetHandler(
log.LvlFilterHandler(log.Lvl(config.LogLevel),
log.StreamHandler(os.Stdout, LogfmtFormatWithoutTimestamp())))
log.Info("Starting willi", "version", version)
for _, mapping := range config.Mappings {
log.Info("Using mapping", "mapping", mapping)
}
var tlsConfig *tls.Config
if config.TlsCert != "" && config.TlsKey != "" {
cer, err := tls.LoadX509KeyPair(config.TlsCert, config.TlsKey)
if err != nil {
log.Error("Failed to load TLS key/cert", "error", err)
os.Exit(1)
}
tlsConfig = &tls.Config{Certificates: []tls.Certificate{cer}}
}
loggers := &SessionLoggers{
loggers: make(map[net.Addr]log.Logger),
}
be := &ProxyBackend{
loggers: loggers,
domain: config.Domain,
mappings: config.Mappings,
recipientDelimiter: config.RecipientDelimiter,
}
s := smtp.NewServer(be)
s.Addr = config.Listen
s.Domain = config.Domain
s.ReadTimeout = time.Duration(config.ReadTimeout)
s.WriteTimeout = time.Duration(config.WriteTimeout)
s.MaxMessageBytes = int(config.MaxMessageBytes)
s.MaxRecipients = config.MaxRecipients
s.EnableSMTPUTF8 = true
s.AuthDisabled = true
s.TLSConfig = tlsConfig
log.Info("Starting server", "address", s.Addr)
if err := ListenAndServe(s, loggers); err != nil {
log.Error("Failed to start server", "error", err)
os.Exit(1)
}
}
func ListenAndServe(s *smtp.Server, loggers *SessionLoggers) error {
network := "tcp"
if s.LMTP {
network = "unix"
}
addr := s.Addr
if !s.LMTP && addr == "" {
addr = ":smtp"
}
l, err := net.Listen(network, addr)
if err != nil {
return err
}
return s.Serve(&SessionListener{l: l, loggers: loggers})
}