From 9e6db178b09387e21ac19ce85369cf1ca7a443e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Tue, 27 Mar 2018 10:23:33 +0200 Subject: Adding durafmt library and use it from enterprise global relay export (#8487) * Adding durafmt library and use it from enterprise global relay export * Allow to specify different server host and server name on smtp connections * Fixing utils/smtp tests --- utils/mail.go | 21 ++++++++++++--------- utils/mail_test.go | 18 +++++++++++------- 2 files changed, 23 insertions(+), 16 deletions(-) (limited to 'utils') diff --git a/utils/mail.go b/utils/mail.go index 2a2da9bf1..43630471c 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -29,7 +29,8 @@ func encodeRFC2047Word(s string) string { type SmtpConnectionInfo struct { SmtpUsername string SmtpPassword string - SmtpServer string + SmtpServerName string + SmtpServerHost string SmtpPort string SkipCertVerification bool ConnectionSecurity string @@ -42,11 +43,11 @@ type authChooser struct { } func (a *authChooser) Start(server *smtp.ServerInfo) (string, []byte, error) { - smtpAddress := a.connectionInfo.SmtpServer + ":" + a.connectionInfo.SmtpPort + smtpAddress := a.connectionInfo.SmtpServerName + ":" + a.connectionInfo.SmtpPort a.Auth = LoginAuth(a.connectionInfo.SmtpUsername, a.connectionInfo.SmtpPassword, smtpAddress) for _, method := range server.Auth { if method == "PLAIN" { - a.Auth = smtp.PlainAuth("", a.connectionInfo.SmtpUsername, a.connectionInfo.SmtpPassword, a.connectionInfo.SmtpServer+":"+a.connectionInfo.SmtpPort) + a.Auth = smtp.PlainAuth("", a.connectionInfo.SmtpUsername, a.connectionInfo.SmtpPassword, a.connectionInfo.SmtpServerName+":"+a.connectionInfo.SmtpPort) break } } @@ -91,11 +92,11 @@ func ConnectToSMTPServerAdvanced(connectionInfo *SmtpConnectionInfo) (net.Conn, var conn net.Conn var err error - smtpAddress := connectionInfo.SmtpServer + ":" + connectionInfo.SmtpPort + smtpAddress := connectionInfo.SmtpServerHost + ":" + connectionInfo.SmtpPort if connectionInfo.ConnectionSecurity == model.CONN_SECURITY_TLS { tlsconfig := &tls.Config{ InsecureSkipVerify: connectionInfo.SkipCertVerification, - ServerName: connectionInfo.SmtpServer, + ServerName: connectionInfo.SmtpServerName, } conn, err = tls.Dial("tcp", smtpAddress, tlsconfig) @@ -117,14 +118,15 @@ func ConnectToSMTPServer(config *model.Config) (net.Conn, *model.AppError) { &SmtpConnectionInfo{ ConnectionSecurity: config.EmailSettings.ConnectionSecurity, SkipCertVerification: *config.EmailSettings.SkipServerCertificateVerification, - SmtpServer: config.EmailSettings.SMTPServer, + SmtpServerName: config.EmailSettings.SMTPServer, + SmtpServerHost: config.EmailSettings.SMTPServer, SmtpPort: config.EmailSettings.SMTPPort, }, ) } func NewSMTPClientAdvanced(conn net.Conn, hostname string, connectionInfo *SmtpConnectionInfo) (*smtp.Client, *model.AppError) { - c, err := smtp.NewClient(conn, connectionInfo.SmtpServer+":"+connectionInfo.SmtpPort) + c, err := smtp.NewClient(conn, connectionInfo.SmtpServerName+":"+connectionInfo.SmtpPort) if err != nil { l4g.Error(T("utils.mail.new_client.open.error"), err) return nil, model.NewAppError("SendMail", "utils.mail.connect_smtp.open_tls.app_error", nil, err.Error(), http.StatusInternalServerError) @@ -141,7 +143,7 @@ func NewSMTPClientAdvanced(conn net.Conn, hostname string, connectionInfo *SmtpC if connectionInfo.ConnectionSecurity == model.CONN_SECURITY_STARTTLS { tlsconfig := &tls.Config{ InsecureSkipVerify: connectionInfo.SkipCertVerification, - ServerName: connectionInfo.SmtpServer, + ServerName: connectionInfo.SmtpServerName, } c.StartTLS(tlsconfig) } @@ -161,7 +163,8 @@ func NewSMTPClient(conn net.Conn, config *model.Config) (*smtp.Client, *model.Ap &SmtpConnectionInfo{ ConnectionSecurity: config.EmailSettings.ConnectionSecurity, SkipCertVerification: *config.EmailSettings.SkipServerCertificateVerification, - SmtpServer: config.EmailSettings.SMTPServer, + SmtpServerName: config.EmailSettings.SMTPServer, + SmtpServerHost: config.EmailSettings.SMTPServer, SmtpPort: config.EmailSettings.SMTPPort, Auth: *config.EmailSettings.EnableSMTPAuth, SmtpUsername: config.EmailSettings.SMTPUsername, diff --git a/utils/mail_test.go b/utils/mail_test.go index 50cf09dac..99ab395bf 100644 --- a/utils/mail_test.go +++ b/utils/mail_test.go @@ -47,7 +47,8 @@ func TestMailConnectionAdvanced(t *testing.T) { &SmtpConnectionInfo{ ConnectionSecurity: cfg.EmailSettings.ConnectionSecurity, SkipCertVerification: *cfg.EmailSettings.SkipServerCertificateVerification, - SmtpServer: cfg.EmailSettings.SMTPServer, + SmtpServerName: cfg.EmailSettings.SMTPServer, + SmtpServerHost: cfg.EmailSettings.SMTPServer, SmtpPort: cfg.EmailSettings.SMTPPort, }, ); err != nil { @@ -60,7 +61,8 @@ func TestMailConnectionAdvanced(t *testing.T) { &SmtpConnectionInfo{ ConnectionSecurity: cfg.EmailSettings.ConnectionSecurity, SkipCertVerification: *cfg.EmailSettings.SkipServerCertificateVerification, - SmtpServer: cfg.EmailSettings.SMTPServer, + SmtpServerName: cfg.EmailSettings.SMTPServer, + SmtpServerHost: cfg.EmailSettings.SMTPServer, SmtpPort: cfg.EmailSettings.SMTPPort, Auth: *cfg.EmailSettings.EnableSMTPAuth, SmtpUsername: cfg.EmailSettings.SMTPUsername, @@ -76,7 +78,8 @@ func TestMailConnectionAdvanced(t *testing.T) { &SmtpConnectionInfo{ ConnectionSecurity: cfg.EmailSettings.ConnectionSecurity, SkipCertVerification: *cfg.EmailSettings.SkipServerCertificateVerification, - SmtpServer: "wrongServer", + SmtpServerName: "wrongServer", + SmtpServerHost: "wrongServer", SmtpPort: "553", }, ); err == nil { @@ -225,10 +228,11 @@ func TestSendMailUsingConfigAdvanced(t *testing.T) { func TestAuthMethods(t *testing.T) { auth := &authChooser{ connectionInfo: &SmtpConnectionInfo{ - SmtpUsername: "test", - SmtpPassword: "fakepass", - SmtpServer: "fakeserver", - SmtpPort: "25", + SmtpUsername: "test", + SmtpPassword: "fakepass", + SmtpServerName: "fakeserver", + SmtpServerHost: "fakeserver", + SmtpPort: "25", }, } tests := []struct { -- cgit v1.2.3-1-g7c22