Use io.ReadWriteCloser in place of net.TCPConn for SMTP session

This commit is contained in:
Ian Kent 2014-11-23 04:21:53 +00:00
parent c6ae2e15dc
commit fe4748f583
3 changed files with 17 additions and 15 deletions

View file

@ -1603,8 +1603,8 @@ var _bintree = &_bintree_t{nil, map[string]*_bintree_t{
"strutil.js": &_bintree_t{assets_js_strutil_js, map[string]*_bintree_t{}}, "strutil.js": &_bintree_t{assets_js_strutil_js, map[string]*_bintree_t{}},
}}, }},
"templates": &_bintree_t{nil, map[string]*_bintree_t{ "templates": &_bintree_t{nil, map[string]*_bintree_t{
"layout.html": &_bintree_t{assets_templates_layout_html, map[string]*_bintree_t{}},
"index.html": &_bintree_t{assets_templates_index_html, map[string]*_bintree_t{}}, "index.html": &_bintree_t{assets_templates_index_html, map[string]*_bintree_t{}},
"layout.html": &_bintree_t{assets_templates_layout_html, map[string]*_bintree_t{}},
}}, }},
}}, }},
}} }}

View file

@ -3,8 +3,8 @@ package server
// http://www.rfc-editor.org/rfc/rfc5321.txt // http://www.rfc-editor.org/rfc/rfc5321.txt
import ( import (
"io"
"log" "log"
"net"
"strings" "strings"
"github.com/ian-kent/Go-MailHog/mailhog/config" "github.com/ian-kent/Go-MailHog/mailhog/config"
@ -14,18 +14,19 @@ import (
// Session represents a SMTP session using net.TCPConn // Session represents a SMTP session using net.TCPConn
type Session struct { type Session struct {
conn *net.TCPConn conn io.ReadWriteCloser
proto *protocol.Protocol proto *protocol.Protocol
conf *config.Config conf *config.Config
isTLS bool remoteAddress string
line string isTLS bool
line string
} }
// Accept starts a new SMTP session using net.TCPConn // Accept starts a new SMTP session using io.ReadWriteCloser
func Accept(conn *net.TCPConn, conf *config.Config) { func Accept(remoteAddress string, conn io.ReadWriteCloser, conf *config.Config) {
proto := protocol.NewProtocol() proto := protocol.NewProtocol()
proto.Hostname = conf.Hostname proto.Hostname = conf.Hostname
session := &Session{conn, proto, conf, false, ""} session := &Session{conn, proto, conf, remoteAddress, false, ""}
proto.LogHandler = session.logf proto.LogHandler = session.logf
proto.MessageReceivedHandler = session.acceptMessage proto.MessageReceivedHandler = session.acceptMessage
proto.ValidateSenderHandler = session.validateSender proto.ValidateSenderHandler = session.validateSender
@ -60,14 +61,14 @@ func (c *Session) acceptMessage(msg *data.Message) (id string, err error) {
func (c *Session) logf(message string, args ...interface{}) { func (c *Session) logf(message string, args ...interface{}) {
message = strings.Join([]string{"[SMTP %s]", message}, " ") message = strings.Join([]string{"[SMTP %s]", message}, " ")
args = append([]interface{}{c.conn.RemoteAddr()}, args...) args = append([]interface{}{c.remoteAddress}, args...)
log.Printf(message, args...) log.Printf(message, args...)
} }
// Read reads from the underlying net.TCPConn // Read reads from the underlying net.TCPConn
func (c *Session) Read() bool { func (c *Session) Read() bool {
buf := make([]byte, 1024) buf := make([]byte, 1024)
n, err := c.conn.Read(buf) n, err := io.Reader(c.conn).Read(buf)
if n == 0 { if n == 0 {
c.logf("Connection closed by remote host\n") c.logf("Connection closed by remote host\n")
@ -91,7 +92,7 @@ func (c *Session) Read() bool {
if reply != nil { if reply != nil {
c.Write(reply) c.Write(reply)
if reply.Status == 221 { if reply.Status == 221 {
c.conn.Close() io.Closer(c.conn).Close()
} }
} }
@ -105,6 +106,6 @@ func (c *Session) Write(reply *protocol.Reply) {
logText := strings.Replace(l, "\n", "\\n", -1) logText := strings.Replace(l, "\n", "\\n", -1)
logText = strings.Replace(logText, "\r", "\\r", -1) logText = strings.Replace(logText, "\r", "\\r", -1)
c.logf("Sent %d bytes: '%s'", len(l), logText) c.logf("Sent %d bytes: '%s'", len(l), logText)
c.conn.Write([]byte(l)) io.Writer(c.conn).Write([]byte(l))
} }
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"flag" "flag"
"io"
"net" "net"
"os" "os"
@ -122,6 +123,6 @@ func smtp_listen() *net.TCPListener {
} }
defer conn.Close() defer conn.Close()
go smtp.Accept(conn.(*net.TCPConn), conf) go smtp.Accept(conn.(*net.TCPConn).RemoteAddr().String(), io.ReadWriteCloser(conn), conf)
} }
} }