Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Read and Write timeouts. #33

Merged
merged 11 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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])
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ func ExampleNewServer() {

s.Addr = ":1025"
s.Domain = "localhost"
s.MaxIdleSeconds = 300
s.WriteTimeout = 300
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably use saner values here. Something like 10 * time.Second.

s.ReadTimeout = 300
s.MaxMessageBytes = 1024 * 1024
s.MaxRecipients = 50
s.AllowInsecureAuth = true
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/emersion/go-sasl v0.0.0-20161116183048-7e096a0a6197 h1:rDJPbyliyym8ZL/Wt71kdolp6yaD4fLIQz638E6JEt0=
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this file? I should add it to .gitignore

github.com/emersion/go-sasl v0.0.0-20161116183048-7e096a0a6197/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k=
4 changes: 3 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net"
"sync"
"time"

"github.com/emersion/go-sasl"
)
Expand All @@ -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.
Expand Down