-
Notifications
You must be signed in to change notification settings - Fork 0
/
https.go
52 lines (47 loc) · 1.05 KB
/
https.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
package libtower
import (
"context"
"crypto/tls"
"net"
"time"
)
const DefaultHTTPSPort = "443"
// HTTPS type
type HTTPS struct {
Host string
Port string
Timeout time.Duration
Start time.Time
End time.Time
Duration time.Duration
}
// HTTPSCheck checks tls certificate is valid
func (hs *HTTPS) HTTPSCheck(ctx context.Context) (bool, time.Time, error) {
if hs.Port == "" {
hs.Port = DefaultHTTPSPort
}
address := hs.Host + ":" + hs.Port
dialer := net.Dialer{
Timeout: hs.Timeout,
}
hs.Start = time.Now()
conn, err := tls.DialWithDialer(&dialer, "tcp", address, &tls.Config{
InsecureSkipVerify: false,
})
hs.End = time.Now()
hs.Duration = hs.End.Sub(hs.Start)
if err != nil {
return false, time.Time{}, err
}
if conn != nil {
defer conn.Close()
var NotAfter = conn.ConnectionState().PeerCertificates[0].NotAfter
for _, cert := range conn.ConnectionState().PeerCertificates {
if cert.NotAfter.Before(NotAfter) {
NotAfter = cert.NotAfter
}
}
return true, NotAfter, nil
}
return false, time.Time{}, nil
}