-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconnect.go
75 lines (64 loc) · 1.67 KB
/
connect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package ssl
import (
"crypto/tls"
"crypto/x509"
"fmt"
"math"
"net"
"strings"
"sync"
"time"
"github.com/gammazero/workerpool"
)
// NewSSLConnect ...
func NewSSLConnect(warnDays, alertDays int, wg *sync.WaitGroup, wp *workerpool.WorkerPool, r chan<- CertificateStatusResult) *SSLConnect {
return &SSLConnect{warnDays: warnDays, alertDays: alertDays, wg: wg, wp: wp, results: r}
}
// Connect ... Connects to the SSL host
func (sc *SSLConnect) Connect(h SSLHost) {
defer sc.wg.Done()
d := &net.Dialer{
Timeout: time.Millisecond * time.Duration(2000),
}
conn, err := tls.DialWithDialer(d, "tcp", h.Host+":"+h.Port, &tls.Config{
InsecureSkipVerify: true, ServerName: h.Host})
if err != nil {
sc.results <- CertificateStatusResult{Host: h.Host, Port: h.Port, Err: err}
return
}
defer conn.Close()
pc := conn.ConnectionState().PeerCertificates
if len(pc) == 0 {
sc.results <- CertificateStatusResult{Err: fmt.Errorf("no peer certificates received")}
return
}
r := sc.validateCertificate(pc[0])
r.Host = strings.ToLower(h.Host)
r.Port = h.Port
sc.results <- r
}
func (sc *SSLConnect) validateCertificate(cert *x509.Certificate) CertificateStatusResult {
exp := cert.NotAfter
days := int(math.Ceil(time.Until(exp).Hours() / 24))
var status ExpirationStatus
switch {
case days < sc.warnDays && days > sc.alertDays:
status = Warn
case days < sc.alertDays && days >= 0:
status = Alert
case days > sc.warnDays:
status = Valid
case days < 0:
fallthrough
default:
status = Expired
}
return CertificateStatusResult{
Subject: cert.Subject.CommonName,
NotAfter: cert.NotAfter,
Ca: cert.IsCA,
Err: nil,
Status: status,
Days: days,
}
}