Skip to content

Commit

Permalink
client: Implement command timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
foxcpp authored and emersion committed Sep 13, 2020
1 parent f804d2f commit c20b441
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
30 changes: 29 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/textproto"
"strconv"
"strings"
"time"

"github.com/emersion/go-sasl"
)
Expand All @@ -38,6 +39,12 @@ type Client struct {
didHello bool // whether we've said HELO/EHLO/LHLO
helloError error // the error from the hello
rcpts []string // recipients accumulated for the current session

// Time to wait for command responses (this includes 3xx reply to DATA).
CommandTimeout time.Duration

// Time to wait for responses after final dot.
SubmissionTimeout time.Duration
}

// Dial returns a new Client connected to an SMTP server at addr.
Expand Down Expand Up @@ -89,7 +96,20 @@ func NewClient(conn net.Conn, host string) (*Client, error) {
return nil, err
}
_, isTLS := conn.(*tls.Conn)
c := &Client{Text: text, conn: conn, serverName: host, localName: "localhost", tls: isTLS}
c := &Client{
Text: text,
conn: conn,
serverName: host,
localName: "localhost",
tls: isTLS,
// As recommended by RFC 5321. For DATA command reply (3xx one) RFC
// recommends a slightly shorter timeout but we do not bother
// differentiating these.
CommandTimeout: 5 * time.Minute,
// 10 minutes + 2 minute buffer in case the server is doing transparent
// forwarding and also follows recommended timeouts.
SubmissionTimeout: 12 * time.Minute,
}
return c, nil
}

Expand Down Expand Up @@ -142,6 +162,9 @@ func (c *Client) Hello(localName string) error {
// cmd is a convenience function that sends a command and returns the response
// textproto.Error returned by c.Text.ReadResponse is converted into SMTPError.
func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, string, error) {
c.conn.SetDeadline(time.Now().Add(c.CommandTimeout))
defer c.conn.SetDeadline(time.Time{})

id, err := c.Text.Cmd(format, args...)
if err != nil {
return 0, "", err
Expand Down Expand Up @@ -174,6 +197,7 @@ func (c *Client) ehlo() error {
if c.lmtp {
cmd = "LHLO"
}

_, msg, err := c.cmd(250, "%s %s", cmd, c.localName)
if err != nil {
return err
Expand Down Expand Up @@ -375,6 +399,10 @@ type dataCloser struct {

func (d *dataCloser) Close() error {
d.WriteCloser.Close()

d.c.conn.SetDeadline(time.Now().Add(d.c.SubmissionTimeout))
defer d.c.conn.SetDeadline(time.Time{})

expectedResponses := len(d.c.rcpts)
if d.c.lmtp {
for expectedResponses > 0 {
Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestBasic(t *testing.T) {
bcmdbuf := bufio.NewWriter(&cmdbuf)
var fake faker
fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf)
c := &Client{Text: textproto.NewConn(fake), localName: "localhost"}
c := &Client{Text: textproto.NewConn(fake), conn: fake, localName: "localhost"}

if err := c.helo(); err != nil {
t.Fatalf("HELO failed: %s", err)
Expand Down Expand Up @@ -912,7 +912,7 @@ func TestLMTP(t *testing.T) {
bcmdbuf := bufio.NewWriter(&cmdbuf)
var fake faker
fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf)
c := &Client{Text: textproto.NewConn(fake), lmtp: true}
c := &Client{Text: textproto.NewConn(fake), conn: fake, lmtp: true}

if err := c.Hello("localhost"); err != nil {
t.Fatalf("LHLO failed: %s", err)
Expand Down Expand Up @@ -997,7 +997,7 @@ func TestLMTPData(t *testing.T) {
bcmdbuf := bufio.NewWriter(&cmdbuf)
var fake faker
fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf)
c := &Client{Text: textproto.NewConn(fake), lmtp: true}
c := &Client{Text: textproto.NewConn(fake), conn: fake, lmtp: true}

if err := c.Hello("localhost"); err != nil {
t.Fatalf("LHLO failed: %s", err)
Expand Down

0 comments on commit c20b441

Please sign in to comment.