2014-04-16 22:29:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net"
|
2014-04-19 11:19:17 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2014-04-19 11:19:17 +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()
|
|
|
|
|
2014-04-19 11:19:17 +00:00
|
|
|
conf = &mailhog.Config{
|
|
|
|
BindAddr: listen,
|
|
|
|
Hostname: hostname,
|
|
|
|
}
|
2014-04-16 22:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
config()
|
|
|
|
|
2014-04-19 11:19:17 +00:00
|
|
|
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()
|
|
|
|
|
2014-04-19 11:19:17 +00:00
|
|
|
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)
|
|
|
|
}
|