Skip to content

Commit

Permalink
Add ability to discard incoming email
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Woodbeck committed May 18, 2020
1 parent 9694690 commit a2fcb5f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
14 changes: 0 additions & 14 deletions .gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
50 changes: 38 additions & 12 deletions smtpdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a2fcb5f

Please sign in to comment.