-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcert.go
More file actions
46 lines (40 loc) · 1.01 KB
/
cert.go
File metadata and controls
46 lines (40 loc) · 1.01 KB
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
package certmon
import (
"crypto/tls"
"crypto/x509"
"net"
"time"
)
type Cert struct {
ID int `json:"id"`
Addr string `json:"addr"`
DNS string `json:"dns"` // for customized DNS resolution, optional
UpdateTime JSONTime `json:"updateTime"`
DaysLeft int `json:"daysLeft"`
UpdateStatus string `json:"updateStatus"`
}
func (c *Cert) Update(rootCAs *x509.CertPool) {
cert, err := CheckCert(c.Addr, c.DNS, rootCAs)
c.UpdateStatus = "ok"
c.UpdateTime = JSONTime(time.Now())
if err != nil {
c.UpdateStatus = err.Error()
return
}
c.DaysLeft = int(time.Until(cert.NotAfter) / (time.Hour * 24))
}
func CheckCert(addr, dns string, rootCAs *x509.CertPool) (*x509.Certificate, error) {
cfg := &tls.Config{
ServerName: dns,
RootCAs: rootCAs,
}
dialer := &net.Dialer{
Timeout: time.Second * 10,
}
conn, err := tls.DialWithDialer(dialer, "tcp", addr, cfg)
if err != nil {
return nil, err
}
cert := conn.ConnectionState().PeerCertificates[0]
return cert, nil
}