-
Notifications
You must be signed in to change notification settings - Fork 20
/
server.go
99 lines (85 loc) · 2.73 KB
/
server.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
package main
import (
"fmt"
guerrilla "github.com/flashmob/go-guerrilla"
"github.com/flashmob/go-guerrilla/backends"
"github.com/flashmob/go-guerrilla/log"
"github.com/flashmob/go-guerrilla/mail"
)
const (
saveWorkersSize = 3
)
// Start starts the server.
func Start(appConfig *mailRelayConfig, verbose bool) (err error) {
listen := fmt.Sprintf("%s:%d", appConfig.LocalListenIP, appConfig.LocalListenPort)
logLevel := "info"
if verbose {
logLevel = "debug"
}
cfg := &guerrilla.AppConfig{
LogFile: log.OutputStdout.String(),
AllowedHosts: appConfig.AllowedHosts,
LogLevel: logLevel,
}
sc := guerrilla.ServerConfig{
ListenInterface: listen,
IsEnabled: true,
MaxSize: appConfig.MaxEmailSize,
Timeout: appConfig.TimeoutSecs,
}
cfg.Servers = append(cfg.Servers, sc)
bcfg := backends.BackendConfig{
"save_workers_size": saveWorkersSize,
"save_process": "HeadersParser|Header|Hasher|Debugger|MailRelay",
"log_received_mails": true,
"primary_mail_host": "homeoffice.com",
"smtp_username": appConfig.SMTPUsername,
"smtp_password": appConfig.SMTPPassword,
"smtp_server": appConfig.SMTPServer,
"smtp_port": appConfig.SMTPPort,
"smtp_starttls": appConfig.SMTPStartTLS,
"smtp_login_auth_type": appConfig.SMTPLoginAuthType,
"smtp_skip_cert_verify": appConfig.SkipCertVerify,
}
cfg.BackendConfig = bcfg
d := guerrilla.Daemon{Config: cfg}
d.AddProcessor("MailRelay", mailRelayProcessor)
return d.Start()
}
type relayConfig struct {
Server string `json:"smtp_server"`
Port int `json:"smtp_port"`
STARTTLS bool `json:"smtp_starttls"`
LoginAuthType bool `json:"smtp_login_auth_type"`
Username string `json:"smtp_username"`
Password string `json:"smtp_password"`
SkipVerify bool `json:"smtp_skip_cert_verify"`
}
// mailRelayProcessor decorator relays emails to another SMTP server.
var mailRelayProcessor = func() backends.Decorator {
config := &relayConfig{}
initFunc := backends.InitializeWith(func(backendConfig backends.BackendConfig) error {
configType := backends.BaseConfig(&relayConfig{})
bcfg, err := backends.Svc.ExtractConfig(backendConfig, configType)
if err != nil {
return err
}
config = bcfg.(*relayConfig)
return nil
})
backends.Svc.AddInitializer(initFunc)
return func(p backends.Processor) backends.Processor {
return backends.ProcessWith(
func(e *mail.Envelope, task backends.SelectTask) (backends.Result, error) {
if task == backends.TaskSaveMail {
err := sendMail(e, config)
if err != nil {
return backends.NewResult(err.Error()), err
}
return p.Process(e, task)
}
return p.Process(e, task)
},
)
}
}