mailrelay/server.go

100 lines
2.7 KiB
Go
Raw Normal View History

2018-11-29 01:00:45 +01:00
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"
)
2020-06-08 06:28:59 +02:00
const (
saveWorkersSize = 3
)
2020-05-17 06:15:49 +02:00
2018-11-29 01:00:45 +01:00
// Start starts the server.
2020-05-09 03:29:59 +02:00
func Start(appConfig *mailRelayConfig, verbose bool) (err error) {
2018-11-29 02:24:10 +01:00
listen := fmt.Sprintf("%s:%d", appConfig.LocalListenIP, appConfig.LocalListenPort)
2020-05-09 03:29:59 +02:00
logLevel := "info"
if verbose {
logLevel = "debug"
}
cfg := &guerrilla.AppConfig{
LogFile: log.OutputStdout.String(),
AllowedHosts: appConfig.AllowedHosts,
LogLevel: logLevel,
}
2018-11-29 01:00:45 +01:00
sc := guerrilla.ServerConfig{
2018-11-29 02:24:10 +01:00
ListenInterface: listen,
2018-11-29 01:00:45 +01:00
IsEnabled: true,
2020-06-08 06:28:59 +02:00
MaxSize: appConfig.MaxEmailSize,
Timeout: appConfig.TimeoutSecs,
2018-11-29 01:00:45 +01:00
}
cfg.Servers = append(cfg.Servers, sc)
bcfg := backends.BackendConfig{
2020-05-17 06:15:49 +02:00
"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,
2018-11-29 01:00:45 +01:00
}
cfg.BackendConfig = bcfg
d := guerrilla.Daemon{Config: cfg}
d.AddProcessor("MailRelay", mailRelayProcessor)
return d.Start()
}
2018-11-29 02:24:10 +01:00
type relayConfig struct {
2020-05-17 06:15:49 +02:00
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"`
2018-11-29 02:24:10 +01:00
}
2018-11-29 01:00:45 +01:00
// mailRelayProcessor decorator relays emails to another SMTP server.
var mailRelayProcessor = func() backends.Decorator {
2018-11-29 02:24:10 +01:00
config := &relayConfig{}
2018-11-29 01:00:45 +01:00
initFunc := backends.InitializeWith(func(backendConfig backends.BackendConfig) error {
2018-11-29 02:24:10 +01:00
configType := backends.BaseConfig(&relayConfig{})
2018-11-29 01:00:45 +01:00
bcfg, err := backends.Svc.ExtractConfig(backendConfig, configType)
if err != nil {
return err
}
2018-11-29 02:24:10 +01:00
config = bcfg.(*relayConfig)
2018-11-29 01:00:45 +01:00
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 {
2020-05-09 03:29:59 +02:00
return backends.NewResult(err.Error()), err
2018-11-29 01:00:45 +01:00
}
return p.Process(e, task)
}
return p.Process(e, task)
},
)
}
}