diff --git a/.gitignore b/.gitignore deleted file mode 100644 index a1338d6..0000000 --- a/.gitignore +++ /dev/null @@ -1,14 +0,0 @@ -# Binaries for programs and plugins -*.exe -*.dll -*.so -*.dylib - -# Test binary, build with `go test -c` -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE -*.out - -# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 -.glide/ diff --git a/go.mod b/go.mod index a720f16..12c79d0 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.14 require ( github.com/fatih/color v1.9.0 - github.com/mhale/smtpd v0.0.0-20181125220505-3c4c908952b8 + github.com/mhale/smtpd v0.0.0-20200509114310-d7a07f752336 ) diff --git a/go.sum b/go.sum index e2f6dda..1f4d02d 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= -github.com/mhale/smtpd v0.0.0-20181125220505-3c4c908952b8 h1:DuLRJOD3tr0rbrwDXXw5mw8YRPl70y8RbFpUtCjzOkU= -github.com/mhale/smtpd v0.0.0-20181125220505-3c4c908952b8/go.mod h1:qqKwvL5sfYgFxcMy96Kjx3TCorMfDaQBvmEL2nvdidc= +github.com/mhale/smtpd v0.0.0-20200509114310-d7a07f752336 h1:Rp+Y5NAgnPvY7FeVNPMRZdiEQzrHTw0cL+cK9AU1Gow= +github.com/mhale/smtpd v0.0.0-20200509114310-d7a07f752336/go.mod h1:qqKwvL5sfYgFxcMy96Kjx3TCorMfDaQBvmEL2nvdidc= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/smtpdump.go b/smtpdump.go index 5a9e8be..1117810 100644 --- a/smtpdump.go +++ b/smtpdump.go @@ -19,19 +19,22 @@ import ( ) var ( - addr = flag.String("addr", "127.0.0.1:2525", "Listen address:port") - cert = flag.String("cert", "", "PEM-encoded certificate") - color = flag.Bool("color", true, "color debug output") - extension = flag.String("extension", "eml", "Saved file extension") - hostname string - pkey = flag.String("key", "", "PEM-encoded private key") - output = flag.String("output", "", "Output directory (default to current directory)") - minTLS11 = flag.Bool("tls11", false, "accept TLSv1.1 as a minimum") - minTLS12 = flag.Bool("tls12", false, "accept TLSv1.2 as a minimum") - minTLS13 = flag.Bool("tls13", false, "accept TLSv1.3 as a minimum") - verbose = flag.Bool("verbose", false, "verbose output") + addr = flag.String("addr", "127.0.0.1:2525", "Listen address:port") + cert = flag.String("cert", "", "PEM-encoded certificate") + color = flag.Bool("color", true, "color debug output") + discard = flag.Bool("discard", false, "discard incoming messages") + extension = flag.String("extension", "eml", "Saved file extension") + output = flag.String("output", "", "Output directory (default to current directory)") + minTLS11 = flag.Bool("tls11", false, "accept TLSv1.1 as a minimum") + minTLS12 = flag.Bool("tls12", false, "accept TLSv1.2 as a minimum") + minTLS13 = flag.Bool("tls13", false, "accept TLSv1.3 as a minimum") + pkey = flag.String("key", "", "PEM-encoded private key") + verbose = flag.Bool("verbose", false, "verbose output") + readPrintf = c.New(c.FgGreen).Printf writePrintf = c.New(c.FgCyan).Printf + + hostname string ) func init() { @@ -71,11 +74,18 @@ func main() { log.Fatalln(err) } + var handler smtpd.Handler + if *discard { + handler = discardHandler(*verbose) + } else { + handler = outputHandler(*output, *extension, *verbose) + } + srv := &smtpd.Server{ Addr: *addr, Appname: "SMTPDump", AuthHandler: authHandler, - Handler: outputHandler(*output, *extension, *verbose), + Handler: handler, LogRead: func(_, _, line string) { line = strings.Replace(line, "\n", "\n ", -1) _, _ = readPrintf(" %s\n", line) @@ -121,6 +131,22 @@ func authHandler(_ net.Addr, _ string, username []byte, password []byte, _ []byt return true, nil } +func discardHandler(verbose bool) smtpd.Handler { + return func(origin net.Addr, from string, to []string, data []byte) { + if verbose { + msg, err := mail.ReadMessage(bytes.NewReader(data)) + if err != nil { + log.Println(err) + + return + } + subject := msg.Header.Get("Subject") + + log.Printf("Received mail from %q with subject %q\n", from, subject) + } + } +} + // outputHandler is called when a new message is received by the server. func outputHandler(output, ext string, verbose bool) smtpd.Handler { return func(origin net.Addr, from string, to []string, data []byte) {