Skip to content

Commit

Permalink
🐛 Bugfix: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lupinthe14th committed Jul 3, 2019
1 parent 4ed7488 commit b41f20f
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"crypto/tls"
"fmt"
"net"
"net/smtp"
"os"
"sync"
Expand All @@ -16,7 +17,15 @@ func init() {
}

func startTLSConnectionState(host, port string) (state tls.ConnectionState, err error) {
conn, err := smtp.Dial(fmt.Sprint(host, ":", port))
addr := fmt.Sprint(host, ":", port)
// Dial the tcp connection
_, err = net.DialTimeout("tcp", addr, 10*time.Second)
if err != nil {
log.Errorf("net dial: %s", err)
return state, err
}
// Dial the SMTP server
conn, err := smtp.Dial(addr)
if err != nil {
log.Errorf("smtp: dial: %s", err)
return state, err
Expand All @@ -28,7 +37,16 @@ func startTLSConnectionState(host, port string) (state tls.ConnectionState, err
}

func tlsConnectionState(host, port string) (state tls.ConnectionState, err error) {
conn, err := tls.Dial("tcp", fmt.Sprint(host, ":", port), &tls.Config{})
addr := fmt.Sprint(host, ":", port)
// Dial the tcp connection
_, err = net.DialTimeout("tcp", addr, 10*time.Second)
if err != nil {
log.Errorf("net dial: %s", err)
return state, err
}

// Dial the tls connection
conn, err := tls.Dial("tcp", addr, &tls.Config{})
if err != nil {
log.Errorf("tls: dial: %s", err)
return state, err
Expand All @@ -48,13 +66,15 @@ func statePeerCertificateExpireDate(host, port string) (expireTime time.Time, er
state, err = startTLSConnectionState(host, port)
if err != nil {
log.Errorf("startTLS connection state: %s", err)
return expireTime, err
}
log.Debugf("startTLS connection state: %v", state)
default:
log.Debugf("case: %v", port)
state, err = tlsConnectionState(host, port)
if err != nil {
log.Errorf("TLS connection state: %s", err)
return expireTime, err
}
log.Debugf("TLS connection state: %v", state)
}
Expand Down

0 comments on commit b41f20f

Please sign in to comment.