diff options
Diffstat (limited to 'utils/smtp/server.go')
| -rw-r--r-- | utils/smtp/server.go | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/utils/smtp/server.go b/utils/smtp/server.go index 2773647..5260d20 100644 --- a/utils/smtp/server.go +++ b/utils/smtp/server.go @@ -1,6 +1,7 @@ package smtp import ( + "crypto/tls" "dove/config" "dove/utils/logger" "fmt" @@ -19,10 +20,26 @@ var activeServers []ServerInstance func Start() { plainAddress := fmt.Sprintf("%s:%d", config.SMTP.Host, config.SMTP.Port) plainServer := createServer(plainAddress) + activeServers = append(activeServers, ServerInstance{Server: plainServer, Label: "SMTP"}) + go startListener(plainServer, "SMTP", plainAddress) - activeServers = append(activeServers, ServerInstance{Server: plainServer, Label: LogPrefix}) + if config.SMTP.TLSEnabled { + tlsConfig := loadTLSConfig() + if tlsConfig != nil { + smtpsAddress := fmt.Sprintf("%s:%d", config.SMTP.Host, config.SMTP.SMTPSPort) + smtpsServer := createServer(smtpsAddress) + smtpsServer.TLSConfig = tlsConfig + activeServers = append(activeServers, ServerInstance{Server: smtpsServer, Label: "SMTPS"}) + go startTLSListener(smtpsServer, "SMTPS", smtpsAddress) - go startListener(plainServer, LogPrefix, plainAddress) + starttlsAddress := fmt.Sprintf("%s:%d", config.SMTP.Host, config.SMTP.StartTLSPort) + starttlsServer := createServer(starttlsAddress) + starttlsServer.TLSConfig = tlsConfig + starttlsServer.EnableSMTPUTF8 = true + activeServers = append(activeServers, ServerInstance{Server: starttlsServer, Label: "STARTTLS"}) + go startListener(starttlsServer, "STARTTLS", starttlsAddress) + } + } } func Shutdown() { @@ -51,6 +68,18 @@ func createServer(address string) *gosmtp.Server { return smtpServer } +func loadTLSConfig() *tls.Config { + certificate, loadError := tls.LoadX509KeyPair(config.SMTP.TLSCertPath, config.SMTP.TLSKeyPath) + if loadError != nil { + logger.Errorf(LogPrefix, TLSCertLoadFailed, loadError) + return nil + } + + return &tls.Config{ + Certificates: []tls.Certificate{certificate}, + } +} + func startListener(smtpServer *gosmtp.Server, label string, address string) { logger.Successf(LogPrefix, ServerStarting, label, address) @@ -58,3 +87,11 @@ func startListener(smtpServer *gosmtp.Server, label string, address string) { logger.Fatalf(LogPrefix, ListenFailed, label, listenError) } } + +func startTLSListener(smtpServer *gosmtp.Server, label string, address string) { + logger.Successf(LogPrefix, ServerStarting, label, address) + + if listenError := smtpServer.ListenAndServeTLS(); listenError != nil { + logger.Fatalf(LogPrefix, ListenFailed, label, listenError) + } +} |
