diff --git a/README.md b/README.md index 14f7f7d..73687bc 100755 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ import ( "io" "io/ioutil" "log" + "time" "github.com/emersion/go-smtp" ) @@ -115,7 +116,8 @@ func main() { s.Addr = ":1025" s.Domain = "localhost" - s.MaxIdleSeconds = 300 + s.ReadTimeout = 10 * time.Second + s.WriteTimeout = 10 * time.Second s.MaxMessageBytes = 1024 * 1024 s.MaxRecipients = 50 s.AllowInsecureAuth = true diff --git a/conn.go b/conn.go index 51e638d..587af43 100644 --- a/conn.go +++ b/conn.go @@ -201,7 +201,7 @@ func (c *Conn) handleGreet(enhanced bool, arg string) { } if c.authAllowed() { authCap := "AUTH" - for name, _ := range c.server.auths { + for name := range c.server.auths { authCap += " " + name } @@ -471,19 +471,11 @@ func (c *Conn) greet() { c.WriteResponse(220, fmt.Sprintf("%v ESMTP Service Ready", c.server.Domain)) } -// Calculate the next read or write deadline based on MaxIdleSeconds. -func (c *Conn) nextDeadline() time.Time { - if c.server.MaxIdleSeconds == 0 { - return time.Time{} // No deadline - } - - return time.Now().Add(time.Duration(c.server.MaxIdleSeconds) * time.Second) -} - func (c *Conn) WriteResponse(code int, text ...string) { // TODO: error handling - - c.conn.SetDeadline(c.nextDeadline()) + if c.server.WriteTimeout != 0 { + c.conn.SetWriteDeadline(time.Now().Add(c.server.WriteTimeout)) + } for i := 0; i < len(text)-1; i++ { c.text.PrintfLine("%v-%v", code, text[i]) @@ -493,8 +485,10 @@ func (c *Conn) WriteResponse(code int, text ...string) { // Reads a line of input func (c *Conn) ReadLine() (string, error) { - if err := c.conn.SetReadDeadline(c.nextDeadline()); err != nil { - return "", err + if c.server.ReadTimeout != 0 { + if err := c.conn.SetReadDeadline(time.Now().Add(c.server.ReadTimeout)); err != nil { + return "", err + } } return c.text.ReadLine() diff --git a/example_test.go b/example_test.go index 6d0428f..35c5046 100644 --- a/example_test.go +++ b/example_test.go @@ -11,6 +11,7 @@ import ( "io/ioutil" "log" "strings" + "time" "github.com/emersion/go-sasl" "github.com/emersion/go-smtp" @@ -139,7 +140,8 @@ func ExampleNewServer() { s.Addr = ":1025" s.Domain = "localhost" - s.MaxIdleSeconds = 300 + s.WriteTimeout = 10 * time.Second + s.ReadTimeout = 10 * time.Second s.MaxMessageBytes = 1024 * 1024 s.MaxRecipients = 50 s.AllowInsecureAuth = true diff --git a/server.go b/server.go index 8381fe5..2d4b403 100755 --- a/server.go +++ b/server.go @@ -6,6 +6,7 @@ import ( "io" "net" "sync" + "time" "github.com/emersion/go-sasl" ) @@ -27,11 +28,12 @@ type Server struct { Domain string MaxRecipients int - MaxIdleSeconds int MaxMessageBytes int AllowInsecureAuth bool Strict bool Debug io.Writer + ReadTimeout time.Duration + WriteTimeout time.Duration // If set, the AUTH command will not be advertised and authentication // attempts will be rejected. This setting overrides AllowInsecureAuth.