-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (88 loc) · 2.5 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"os"
"time"
)
type CertInfo struct {
Host string
Status string
ResponseTime time.Duration
ResolvedIP string
IssuedTo string
IssuedOrganization string
IssuerCountry string
IssuerCN string
IssuerOrganization string
CertSN string
CertSHA1 string
CertAlgorithm string
CertVersion int
CertSANs []string
CertExpired bool
CertValid bool
ValidFrom string
ValidUntil string
ValidityDays int
DaysLeft int
ValidDaysToExpire int
}
func main() {
var server = "akihito.uz"
var domainName = "akihito.uz"
port := "443"
if addr, err := net.LookupIP(server); err == nil {
server = addr[0].String()
}
hostname := server
server += ":" + port
cert, err := getCert(server, hostname)
if err != nil {
fmt.Println("Error getting cert: ", err)
os.Exit(1)
}
var CertInfo = &CertInfo{
Host: domainName,
Status: "OK",
ResponseTime: time.Duration(0),
ResolvedIP: server,
IssuedOrganization: IsEmpty(cert.Subject.Organization),
IssuerCountry: cert.Issuer.Country[0],
IssuerCN: cert.Issuer.CommonName,
IssuerOrganization: cert.Issuer.Organization[0],
CertSN: cert.SerialNumber.String(),
CertSHA1: fmt.Sprintf("%x", cert.SignatureAlgorithm.String()),
CertAlgorithm: cert.SignatureAlgorithm.String(),
CertVersion: cert.Version,
CertSANs: cert.DNSNames,
CertExpired: cert.NotAfter.Before(time.Now()),
CertValid: cert.NotBefore.Before(time.Now()) && cert.NotAfter.After(time.Now()),
ValidFrom: cert.NotBefore.Format("02.01.2006"),
ValidUntil: cert.NotAfter.Format("02.01.2006"),
ValidityDays: int(cert.NotAfter.Sub(cert.NotBefore).Hours() / 24),
DaysLeft: int(cert.NotAfter.Sub(time.Now()).Hours() / 24),
ValidDaysToExpire: int(cert.NotAfter.Sub(time.Now()).Hours() / 24),
}
fmt.Println(CertInfo.IssuedOrganization)
}
func getCert(server, hostname string) (*x509.Certificate, error) {
conf := &tls.Config{
InsecureSkipVerify: true,
ServerName: hostname,
}
conn, err := tls.Dial("tcp", server, conf)
if err != nil {
return nil, err
}
defer conn.Close()
return conn.ConnectionState().PeerCertificates[0], nil
}
func IsEmpty(s any) string {
if len(s.([]string)) == 0 {
return "N/A"
}
return s.(string)
}