-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmain.go
More file actions
272 lines (226 loc) · 6.77 KB
/
main.go
File metadata and controls
272 lines (226 loc) · 6.77 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"net/smtp"
"os"
"os/signal"
"syscall"
"github.com/jpillora/ipfilter"
log "github.com/phires/go-guerrilla/log"
)
const (
DefaultSMTPPort = 465
DefaultMaxEmailSize = 83886080 // 80 MB (80 * 1024 * 1024)
DefaultLocalListenIP = "0.0.0.0"
DefaultLocalListenPort = 2525
DefaultTimeoutSecs = 300 // 5 minutes
MinEmailSizeBytes = 1024
)
// Logger is the global logger.
var Logger log.Logger
// AllowedSendersFilter holds the global list of allowed sender IPs.
var AllowedSendersFilter = ipfilter.New(ipfilter.Options{})
type mailRelayConfig struct {
SMTPServer string `json:"smtp_server"`
SMTPPort int `json:"smtp_port"`
SMTPStartTLS bool `json:"smtp_starttls"`
SMTPLoginAuthType bool `json:"smtp_login_auth_type"`
SMTPUsername string `json:"smtp_username"`
SMTPPassword string `json:"smtp_password"`
SMTPHelo string `json:"smtp_helo"`
SkipCertVerify bool `json:"smtp_skip_cert_verify"`
MaxEmailSize int64 `json:"smtp_max_email_size"`
LocalListenIP string `json:"local_listen_ip"`
LocalListenPort int `json:"local_listen_port"`
AllowedHosts []string `json:"allowed_hosts"`
AllowedSenders string `json:"allowed_senders"`
TimeoutSecs int `json:"timeout_secs"`
LogLevel string `json:"log_level"`
}
func main() {
err := run()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
os.Exit(0)
}
func run() error {
configFile, test, testsender, testrcpt, checkIP, ipToCheck, verbose := parseFlags()
appConfig, err := loadConfig(configFile)
if err != nil {
flag.Usage()
return fmt.Errorf("loading config: %w", err)
}
if err := setupIPFilter(appConfig); err != nil {
return err
}
if err := setupLogger(verbose, appConfig.LogLevel); err != nil {
return err
}
if err := Start(appConfig, verbose); err != nil {
flag.Usage()
return fmt.Errorf("starting server: %w", err)
}
if test {
return runTest(testsender, testrcpt, appConfig.LocalListenPort)
}
if checkIP {
return runIPCheck(ipToCheck)
}
return waitForSignal()
}
func parseFlags() (string, bool, string, string, bool, string, bool) {
var configFile string
var test bool
var testsender string
var testrcpt string
var checkIP bool
var ipToCheck string
var verbose bool
flag.StringVar(&configFile, "config", "/etc/mailrelay.json", "specifies JSON config file")
flag.BoolVar(&test, "test", false, "sends a test message to SMTP server")
flag.StringVar(&testsender, "sender", "", "used with 'test' to specify sender email address")
flag.StringVar(&testrcpt, "rcpt", "", "used with 'test' to specify recipient email address")
flag.BoolVar(&verbose, "verbose", false, "verbose output")
flag.BoolVar(&checkIP, "checkIP", false, "checks a provided IP address to see if it would be allowed")
flag.StringVar(&ipToCheck, "ip", "", "used with 'checkIP' to specify IP address to test")
flag.Parse()
return configFile, test, testsender, testrcpt, checkIP, ipToCheck, verbose
}
func setupIPFilter(appConfig *mailRelayConfig) error {
if appConfig.AllowedSenders == "*" {
return nil
}
file, err := os.Open(appConfig.AllowedSenders)
if err != nil {
return fmt.Errorf("failed opening file: %w", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var allowedIPsAndRanges []string
for scanner.Scan() {
allowedIPsAndRanges = append(allowedIPsAndRanges, scanner.Text())
}
AllowedSendersFilter = ipfilter.New(ipfilter.Options{
AllowedIPs: allowedIPsAndRanges,
BlockByDefault: true,
})
return nil
}
func setupLogger(verbose bool, logLevel string) error {
if verbose {
logLevel = "debug"
}
var err error
Logger, err = log.GetLogger("stdout", logLevel)
if err != nil {
return fmt.Errorf("creating logger: %w", err)
}
return nil
}
func runTest(testsender, testrcpt string, port int) error {
err := sendTest(testsender, testrcpt, port)
if err != nil {
return fmt.Errorf("sending test message: %w", err)
}
return nil
}
func runIPCheck(ipToCheck string) error {
if ipToCheck == "" {
return errors.New("IP address to check is required when `checkIP` flag is used. " +
"Provide an IP address using the `-ip` flag")
}
result := ""
if !AllowedSendersFilter.Blocked(ipToCheck) {
result = "NOT "
}
fmt.Printf("IP address %s is %sallowed to send email\n", ipToCheck, result)
return nil
}
func waitForSignal() error {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
return nil
}
func loadConfig(path string) (*mailRelayConfig, error) {
var cfg mailRelayConfig
configDefaults(&cfg)
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
parser := json.NewDecoder(file)
if err := parser.Decode(&cfg); err != nil {
return nil, err
}
if err := validateConfig(&cfg); err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
}
return &cfg, nil
}
func configDefaults(config *mailRelayConfig) {
config.SMTPPort = DefaultSMTPPort
config.SMTPStartTLS = false
config.SMTPLoginAuthType = false
config.MaxEmailSize = DefaultMaxEmailSize
config.SkipCertVerify = false
config.LocalListenIP = DefaultLocalListenIP
config.LocalListenPort = DefaultLocalListenPort
config.AllowedHosts = []string{"*"}
config.AllowedSenders = "*"
config.TimeoutSecs = DefaultTimeoutSecs
config.LogLevel = "info"
}
// validateConfig validates the configuration values.
func validateConfig(config *mailRelayConfig) error {
if config.SMTPServer == "" {
return errors.New("smtp_server is required")
}
if config.SMTPPort < 1 || config.SMTPPort > 65535 {
return errors.New("smtp_port must be between 1 and 65535")
}
if config.LocalListenPort < 1 || config.LocalListenPort > 65535 {
return errors.New("local_listen_port must be between 1 and 65535")
}
if config.MaxEmailSize < MinEmailSizeBytes {
return errors.New("smtp_max_email_size must be at least 1024 bytes")
}
if config.TimeoutSecs < 1 || config.TimeoutSecs > 3600 {
return errors.New("timeout_secs must be between 1 and 3600 seconds")
}
return nil
}
// sendTest sends a test message to the SMTP server specified in mailrelay.json.
func sendTest(sender string, rcpt string, port int) error {
conn, err := smtp.Dial(fmt.Sprintf("localhost:%d", port))
if err != nil {
return err
}
if err := conn.Mail(sender); err != nil {
return err
}
if err := conn.Rcpt(rcpt); err != nil {
return err
}
if err := writeBody(conn, sender); err != nil {
return err
}
return conn.Quit()
}
func writeBody(conn *smtp.Client, sender string) error {
wc, err := conn.Data()
if err != nil {
return err
}
defer wc.Close()
_, err = fmt.Fprintf(wc, "From: %s\nSubject: Test message\n\nThis is a test email from mailrelay.\n", sender)
return err
}