MailHog/main.go

53 lines
1 KiB
Go
Raw Normal View History

2014-04-16 22:29:23 +00:00
package main
import (
"flag"
"log"
"net"
"github.com/ian-kent/MailHog/mailhog"
2014-04-16 22:59:25 +00:00
"github.com/ian-kent/MailHog/mailhog/smtp"
2014-04-16 22:29:23 +00:00
)
var conf *mailhog.Config
2014-04-16 22:29:23 +00:00
func config() {
2014-04-16 22:59:25 +00:00
var listen, hostname string
2014-04-16 22:29:23 +00:00
flag.StringVar(&listen, "listen", "0.0.0.0:1025", "Bind interface and port, e.g. 0.0.0.0:1025 or just :1025")
2014-04-16 22:59:25 +00:00
flag.StringVar(&hostname, "hostname", "mailhog.example", "Hostname for EHLO/HELO response, e.g. mailhog.example")
2014-04-16 22:29:23 +00:00
flag.Parse()
conf = &mailhog.Config{
BindAddr: listen,
Hostname: hostname,
}
2014-04-16 22:29:23 +00:00
}
func main() {
config()
ln := listen(conf.BindAddr)
2014-04-16 22:29:23 +00:00
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("Error accepting connection: %s\n", err)
continue
}
defer conn.Close()
go smtp.StartSession(conn.(*net.TCPConn), conf)
2014-04-16 22:29:23 +00:00
}
}
func listen(bind string) (*net.TCPListener) {
log.Printf("Binding to address: %s\n", bind)
ln, err := net.Listen("tcp", bind)
if err != nil {
log.Fatalf("Error listening on socket: %s\n", err)
}
return ln.(*net.TCPListener)
}