author | Alberto Bertogli
<albertito@blitiri.com.ar> 2016-10-01 13:19:44 UTC |
committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2016-10-09 23:51:04 UTC |
parent | d97a4047c160823669e7f05231f4d250de39752a |
cmd/smtp-check/smtp-check.go | +70 | -0 |
diff --git a/cmd/smtp-check/smtp-check.go b/cmd/smtp-check/smtp-check.go new file mode 100644 index 0000000..49b5971 --- /dev/null +++ b/cmd/smtp-check/smtp-check.go @@ -0,0 +1,70 @@ +// smtp-check is a command-line too for checking SMTP setups. +package main + +import ( + "crypto/tls" + "flag" + "log" + "net" + "net/smtp" + + "blitiri.com.ar/go/chasquid/internal/tlsconst" + + "golang.org/x/net/idna" +) + +var ( + port = flag.String("port", "smtp", + "port to use for connecting to the MX servers") +) + +func main() { + flag.Parse() + + domain := flag.Arg(0) + if domain == "" { + log.Fatal("Use: smtp-check <domain>") + } + + domain, err := idna.ToASCII(domain) + if err != nil { + log.Fatal("IDNA conversion failed: %v", err) + } + + mxs, err := net.LookupMX(domain) + if err != nil { + log.Fatalf("MX lookup: %v", err) + } + + if len(mxs) == 0 { + log.Fatalf("MX lookup returned no results") + } + + for _, mx := range mxs { + log.Printf("=== Testing MX: %2d %s", mx.Pref, mx.Host) + + c, err := smtp.Dial(mx.Host + ":" + *port) + if err != nil { + log.Fatal(err) + } + + config := &tls.Config{ + // Expect the server to have a certificate valid for the MX + // we're connecting to. + ServerName: mx.Host, + } + err = c.StartTLS(config) + if err != nil { + log.Fatal(err) + } + + cstate, _ := c.TLSConnectionState() + log.Printf("%s - %s", tlsconst.VersionName(cstate.Version), + tlsconst.CipherSuiteName(cstate.CipherSuite)) + + log.Printf("") + c.Close() + } + + log.Printf("=== Success") +}