From c32787596a75f61d367caaf5044b5d711a1e2bff Mon Sep 17 00:00:00 2001 From: Zhang Huangbin Date: Thu, 17 Dec 2020 13:49:30 +0800 Subject: [PATCH 1/7] Remove field `Domain` in struct `Server`. Add field `Banner` in struct `Server`. --- README.md | 1 - backendutil/transform_test.go | 1 - cmd/smtp-debug-server/main.go | 1 - conn.go | 2 +- example_test.go | 1 - server.go | 13 +++++++++++-- server_test.go | 2 -- 7 files changed, 12 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 15c8bf0..af306d3 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,6 @@ func main() { s := smtp.NewServer(be) s.Addr = ":1025" - s.Domain = "localhost" s.ReadTimeout = 10 * time.Second s.WriteTimeout = 10 * time.Second s.MaxMessageBytes = 1024 * 1024 diff --git a/backendutil/transform_test.go b/backendutil/transform_test.go index 3c77638..74a1d6a 100755 --- a/backendutil/transform_test.go +++ b/backendutil/transform_test.go @@ -122,7 +122,6 @@ func testServer(t *testing.T, fn ...serverConfigureFunc) (be *backend, s *smtp.S TransformData: transformMailReader, } s = smtp.NewServer(tbe) - s.Domain = "localhost" s.AllowInsecureAuth = true for _, f := range fn { f(s) diff --git a/cmd/smtp-debug-server/main.go b/cmd/smtp-debug-server/main.go index 28d9843..8862d7c 100644 --- a/cmd/smtp-debug-server/main.go +++ b/cmd/smtp-debug-server/main.go @@ -51,7 +51,6 @@ func main() { s := smtp.NewServer(&backend{}) s.Addr = addr - s.Domain = "localhost" s.AllowInsecureAuth = true s.Debug = os.Stdout diff --git a/conn.go b/conn.go index 8ecd273..c0a49e1 100644 --- a/conn.go +++ b/conn.go @@ -934,7 +934,7 @@ func (c *Conn) Reject() { } func (c *Conn) greet() { - c.WriteResponse(220, NoEnhancedCode, fmt.Sprintf("%v ESMTP Service Ready", c.server.Domain)) + c.WriteResponse(220, NoEnhancedCode, fmt.Sprintf("%v ESMTP Service Ready", c.server.Banner)) } func (c *Conn) WriteResponse(code int, enhCode EnhancedCode, text ...string) { diff --git a/example_test.go b/example_test.go index 607c971..7f5c0c2 100644 --- a/example_test.go +++ b/example_test.go @@ -139,7 +139,6 @@ func ExampleNewServer() { s := smtp.NewServer(be) s.Addr = ":1025" - s.Domain = "localhost" s.WriteTimeout = 10 * time.Second s.ReadTimeout = 10 * time.Second s.MaxMessageBytes = 1024 * 1024 diff --git a/server.go b/server.go index a7c37c9..8cc3cb2 100644 --- a/server.go +++ b/server.go @@ -34,7 +34,7 @@ type Server struct { // TCP listener. LMTP bool - Domain string + Domain string // DEPRECATED MaxRecipients int MaxMessageBytes int MaxLineLength int @@ -45,6 +45,9 @@ type Server struct { ReadTimeout time.Duration WriteTimeout time.Duration + // The text that follows the 220 status code in the SMTP greeting banner. + Banner string + // Advertise SMTPUTF8 (RFC 6531) capability. // Should be used only if backend supports it. EnableSMTPUTF8 bool @@ -75,7 +78,7 @@ type Server struct { // New creates a new SMTP server. func NewServer(be Backend) *Server { - return &Server{ + s := &Server{ // Doubled maximum line length per RFC 5321 (Section 4.5.3.1.6) MaxLineLength: 2000, @@ -103,6 +106,12 @@ func NewServer(be Backend) *Server { }, conns: make(map[*Conn]struct{}), } + + if s.Banner == "" { + s.Banner = "ESMTP Service Ready" + } + + return s } // Serve accepts incoming connections on the Listener l. diff --git a/server_test.go b/server_test.go index f2bfe9f..9de381e 100644 --- a/server_test.go +++ b/server_test.go @@ -171,7 +171,6 @@ func testServer(t *testing.T, fn ...serverConfigureFunc) (be *backend, s *smtp.S be = new(backend) s = smtp.NewServer(be) - s.Domain = "localhost" s.AllowInsecureAuth = true for _, f := range fn { f(s) @@ -728,7 +727,6 @@ func testStrictServer(t *testing.T) (s *smtp.Server, c net.Conn, scanner *bufio. } s = smtp.NewServer(new(backend)) - s.Domain = "localhost" s.AllowInsecureAuth = true s.AuthDisabled = true s.Strict = true From 74b7248b1acbc6dbfba263f2901f49802c114161 Mon Sep 17 00:00:00 2001 From: Zhang Huangbin Date: Thu, 17 Dec 2020 13:51:04 +0800 Subject: [PATCH 2/7] Update comment. --- server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.go b/server.go index 8cc3cb2..1858126 100644 --- a/server.go +++ b/server.go @@ -34,7 +34,7 @@ type Server struct { // TCP listener. LMTP bool - Domain string // DEPRECATED + Domain string // DEPRECATED. Keep for backward compatible. MaxRecipients int MaxMessageBytes int MaxLineLength int From 22691ca5ed005ececbe2b2d10ee94c90dc604cc4 Mon Sep 17 00:00:00 2001 From: Zhang Huangbin Date: Thu, 17 Dec 2020 13:52:43 +0800 Subject: [PATCH 3/7] Fix incorrect banner text. --- conn.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conn.go b/conn.go index c0a49e1..d7eba66 100644 --- a/conn.go +++ b/conn.go @@ -934,7 +934,7 @@ func (c *Conn) Reject() { } func (c *Conn) greet() { - c.WriteResponse(220, NoEnhancedCode, fmt.Sprintf("%v ESMTP Service Ready", c.server.Banner)) + c.WriteResponse(220, NoEnhancedCode, c.server.Banner) } func (c *Conn) WriteResponse(code int, enhCode EnhancedCode, text ...string) { From 0ea7f2a1e274b4a219ea01d91ec7a092bc771f6d Mon Sep 17 00:00:00 2001 From: Zhang Huangbin Date: Thu, 17 Dec 2020 14:02:50 +0800 Subject: [PATCH 4/7] Fix unittests. --- server_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server_test.go b/server_test.go index 9de381e..458c66c 100644 --- a/server_test.go +++ b/server_test.go @@ -191,7 +191,7 @@ func testServerGreeted(t *testing.T, fn ...serverConfigureFunc) (be *backend, s be, s, c, scanner = testServer(t, fn...) scanner.Scan() - if scanner.Text() != "220 localhost ESMTP Service Ready" { + if scanner.Text() != "220 ESMTP Service Ready" { t.Fatal("Invalid greeting:", scanner.Text()) } @@ -741,7 +741,7 @@ func testStrictServer(t *testing.T) (s *smtp.Server, c net.Conn, scanner *bufio. scanner = bufio.NewScanner(c) scanner.Scan() - if scanner.Text() != "220 localhost ESMTP Service Ready" { + if scanner.Text() != "220 ESMTP Service Ready" { t.Fatal("Invalid greeting:", scanner.Text()) } From dc7622adf6573876e866fbcf3f47beac4b6f9ca8 Mon Sep 17 00:00:00 2001 From: Zhang Huangbin Date: Thu, 17 Dec 2020 14:13:27 +0800 Subject: [PATCH 5/7] Fix unit tests. --- backendutil/transform_test.go | 2 +- example_test.go | 1 + server_test.go | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/backendutil/transform_test.go b/backendutil/transform_test.go index 74a1d6a..d0badbe 100755 --- a/backendutil/transform_test.go +++ b/backendutil/transform_test.go @@ -142,7 +142,7 @@ func testServerGreeted(t *testing.T, fn ...serverConfigureFunc) (be *backend, s be, s, c, scanner = testServer(t, fn...) scanner.Scan() - if scanner.Text() != "220 localhost ESMTP Service Ready" { + if scanner.Text() != "220 ESMTP Service Ready" { t.Fatal("Invalid greeting:", scanner.Text()) } diff --git a/example_test.go b/example_test.go index 7f5c0c2..10f6a17 100644 --- a/example_test.go +++ b/example_test.go @@ -139,6 +139,7 @@ func ExampleNewServer() { s := smtp.NewServer(be) s.Addr = ":1025" + s.Banner = "localhost ESMTP Server Ready" s.WriteTimeout = 10 * time.Second s.ReadTimeout = 10 * time.Second s.MaxMessageBytes = 1024 * 1024 diff --git a/server_test.go b/server_test.go index 458c66c..ac6daaa 100644 --- a/server_test.go +++ b/server_test.go @@ -171,6 +171,7 @@ func testServer(t *testing.T, fn ...serverConfigureFunc) (be *backend, s *smtp.S be = new(backend) s = smtp.NewServer(be) + s.Banner = "localhost ESMTP Service Ready" s.AllowInsecureAuth = true for _, f := range fn { f(s) @@ -191,7 +192,7 @@ func testServerGreeted(t *testing.T, fn ...serverConfigureFunc) (be *backend, s be, s, c, scanner = testServer(t, fn...) scanner.Scan() - if scanner.Text() != "220 ESMTP Service Ready" { + if scanner.Text() != "220 localhost ESMTP Service Ready" { t.Fatal("Invalid greeting:", scanner.Text()) } From 3aefd335fed22b09af7c65d7dd8dcca849d29201 Mon Sep 17 00:00:00 2001 From: Zhang Huangbin Date: Fri, 18 Dec 2020 22:52:26 +0800 Subject: [PATCH 6/7] Use better text for `QUIT` command. --- conn.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conn.go b/conn.go index d7eba66..0439e2e 100644 --- a/conn.go +++ b/conn.go @@ -140,7 +140,7 @@ func (c *Conn) handle(cmd string, arg string) { case "DATA": c.handleData(arg) case "QUIT": - c.WriteResponse(221, EnhancedCode{2, 0, 0}, "Goodnight and good luck") + c.WriteResponse(221, EnhancedCode{2, 0, 0}, "Bye") c.Close() case "AUTH": if c.server.AuthDisabled { From 16d1d6db47fc9e73a5dd8399503bc0309aa00fc6 Mon Sep 17 00:00:00 2001 From: Zhang Huangbin Date: Tue, 22 Dec 2020 22:24:48 +0800 Subject: [PATCH 7/7] Keep Domain. --- README.md | 1 + backendutil/transform_test.go | 3 ++- cmd/smtp-debug-server/main.go | 1 + example_test.go | 3 ++- server.go | 6 ++++-- server_test.go | 2 +- 6 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index af306d3..15c8bf0 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ func main() { s := smtp.NewServer(be) s.Addr = ":1025" + s.Domain = "localhost" s.ReadTimeout = 10 * time.Second s.WriteTimeout = 10 * time.Second s.MaxMessageBytes = 1024 * 1024 diff --git a/backendutil/transform_test.go b/backendutil/transform_test.go index d0badbe..3c77638 100755 --- a/backendutil/transform_test.go +++ b/backendutil/transform_test.go @@ -122,6 +122,7 @@ func testServer(t *testing.T, fn ...serverConfigureFunc) (be *backend, s *smtp.S TransformData: transformMailReader, } s = smtp.NewServer(tbe) + s.Domain = "localhost" s.AllowInsecureAuth = true for _, f := range fn { f(s) @@ -142,7 +143,7 @@ func testServerGreeted(t *testing.T, fn ...serverConfigureFunc) (be *backend, s be, s, c, scanner = testServer(t, fn...) scanner.Scan() - if scanner.Text() != "220 ESMTP Service Ready" { + if scanner.Text() != "220 localhost ESMTP Service Ready" { t.Fatal("Invalid greeting:", scanner.Text()) } diff --git a/cmd/smtp-debug-server/main.go b/cmd/smtp-debug-server/main.go index 8862d7c..28d9843 100644 --- a/cmd/smtp-debug-server/main.go +++ b/cmd/smtp-debug-server/main.go @@ -51,6 +51,7 @@ func main() { s := smtp.NewServer(&backend{}) s.Addr = addr + s.Domain = "localhost" s.AllowInsecureAuth = true s.Debug = os.Stdout diff --git a/example_test.go b/example_test.go index 10f6a17..6bb881e 100644 --- a/example_test.go +++ b/example_test.go @@ -139,7 +139,8 @@ func ExampleNewServer() { s := smtp.NewServer(be) s.Addr = ":1025" - s.Banner = "localhost ESMTP Server Ready" + s.Domain = "localhost" + s.Banner = s.Domain + " ESMTP Server Ready" s.WriteTimeout = 10 * time.Second s.ReadTimeout = 10 * time.Second s.MaxMessageBytes = 1024 * 1024 diff --git a/server.go b/server.go index 1858126..e3dc078 100644 --- a/server.go +++ b/server.go @@ -34,7 +34,7 @@ type Server struct { // TCP listener. LMTP bool - Domain string // DEPRECATED. Keep for backward compatible. + Domain string MaxRecipients int MaxMessageBytes int MaxLineLength int @@ -108,7 +108,9 @@ func NewServer(be Backend) *Server { } if s.Banner == "" { - s.Banner = "ESMTP Service Ready" + s.Banner = s.Domain + " ESMTP Service Ready" + } else { + s.Banner = s.Domain + " " + s.Banner } return s diff --git a/server_test.go b/server_test.go index ac6daaa..a92f2b7 100644 --- a/server_test.go +++ b/server_test.go @@ -742,7 +742,7 @@ func testStrictServer(t *testing.T) (s *smtp.Server, c net.Conn, scanner *bufio. scanner = bufio.NewScanner(c) scanner.Scan() - if scanner.Text() != "220 ESMTP Service Ready" { + if scanner.Text() != "220 localhost ESMTP Service Ready" { t.Fatal("Invalid greeting:", scanner.Text()) }