From 472d6bd19bf6a23eea8ab2090d6e205cef9f2067 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 18 Dec 2016 02:37:58 -0500 Subject: [PATCH] Add more logging to debug production issues. --- pop3/conn.go | 7 ++----- smtp/conn.go | 33 +++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/pop3/conn.go b/pop3/conn.go index 6b5fe4c..55fc01a 100644 --- a/pop3/conn.go +++ b/pop3/conn.go @@ -54,12 +54,9 @@ func AcceptConnection(netConn net.Conn, po PostOffice, log zap.Logger) { for { conn.line, err = conn.tp.ReadLine() if err != nil { - if err == io.EOF { - break - } conn.log.Error("ReadLine()", zap.Error(err)) - conn.err("did't catch that") - continue + conn.tp.Close() + break } var cmd string diff --git a/smtp/conn.go b/smtp/conn.go index 075ca21..33bca29 100644 --- a/smtp/conn.go +++ b/smtp/conn.go @@ -4,7 +4,6 @@ import ( "crypto/rand" "crypto/tls" "fmt" - "io" "net" "net/mail" "net/textproto" @@ -45,7 +44,7 @@ type connection struct { rcptTo []mail.Address } -func AcceptConnection(netConn net.Conn, server Server, log zap.Logger) error { +func AcceptConnection(netConn net.Conn, server Server, log zap.Logger) { conn := connection{ server: server, tp: textproto.NewConn(netConn), @@ -55,21 +54,19 @@ func AcceptConnection(netConn net.Conn, server Server, log zap.Logger) error { state: stateNew, } - var err error - conn.writeReply(220, fmt.Sprintf("%s ESMTP [%s] (mailpopbox)", server.Name(), netConn.LocalAddr())) for { + var err error conn.line, err = conn.tp.ReadLine() if err != nil { - if err == io.EOF { - break - } conn.log.Error("ReadLine()", zap.Error(err)) - conn.writeReply(500, "line too long") - continue + conn.tp.Close() + break } + conn.log.Info("ReadLine()", zap.String("line", conn.line)) + var cmd string if _, err = fmt.Sscanf(conn.line, "%s", &cmd); err != nil { conn.reply(ReplyBadSyntax) @@ -109,8 +106,6 @@ func AcceptConnection(netConn net.Conn, server Server, log zap.Logger) error { conn.writeReply(500, "unrecognized command") } } - - return err } func (conn *connection) reply(reply ReplyLine) error { @@ -118,11 +113,19 @@ func (conn *connection) reply(reply ReplyLine) error { } func (conn *connection) writeReply(code int, msg string) error { + conn.log.Info("writeReply", zap.Int("code", code)) + var err error if len(msg) > 0 { - return conn.tp.PrintfLine("%d %s", code, msg) + err = conn.tp.PrintfLine("%d %s", code, msg) } else { - return conn.tp.PrintfLine("%d", code) + err = conn.tp.PrintfLine("%d", code) } + if err != nil { + conn.log.Error("writeReply", + zap.Int("code", code), + zap.Error(err)) + } + return err } // parsePath parses out either a forward-, reverse-, or return-path from the @@ -262,7 +265,9 @@ func (conn *connection) doDATA() { data, err := conn.tp.ReadDotBytes() if err != nil { - conn.log.Error("failed to ReadDotBytes()", zap.Error(err)) + conn.log.Error("failed to ReadDotBytes()", + zap.Error(err), + zap.String("bytes", fmt.Sprintf("%x", data))) conn.writeReply(552, "transaction failed") return } -- 2.43.5