/
tcp.go
174 lines (167 loc) · 4.23 KB
/
tcp.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package polling
// TCP/HTTP(S)/TLSのポーリングを行う。
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"strings"
"time"
"github.com/twsnmp/twsnmpfk/datastore"
)
func doPollingTCP(pe *datastore.PollingEnt) {
n := datastore.GetNode(pe.NodeID)
if n == nil {
return
}
ok := false
var rTime int64
for i := 0; !ok && i <= pe.Retry; i++ {
startTime := time.Now().UnixNano()
conn, err := net.DialTimeout("tcp", n.IP+":"+pe.Params, time.Duration(pe.Timeout)*time.Second)
endTime := time.Now().UnixNano()
if err != nil {
pe.Result["error"] = fmt.Sprintf("%v", err)
continue
}
defer conn.Close()
rTime = endTime - startTime
ok = true
}
pe.Result["rtt"] = float64(rTime)
if ok {
setPollingState(pe, "normal")
} else {
setPollingState(pe, pe.Level)
}
}
func doPollingTLS(pe *datastore.PollingEnt) {
n := datastore.GetNode(pe.NodeID)
if n == nil {
setPollingError("tls", pe, fmt.Errorf("node not found"))
return
}
mode := pe.Mode
if mode == "" {
mode = "verify"
}
target := pe.Params
if target == "" {
target = n.IP + ":443"
} else if !strings.Contains(target, ":") {
target = n.IP + ":" + target
}
script := pe.Script
conf := &tls.Config{
InsecureSkipVerify: true,
}
switch mode {
case "verify":
conf.InsecureSkipVerify = false
case "version":
if strings.Contains(script, "1.0") {
conf.MaxVersion = tls.VersionTLS10
} else if strings.Contains(script, "1.1") {
conf.MinVersion = tls.VersionTLS11
conf.MaxVersion = tls.VersionTLS11
} else if strings.Contains(script, "1.2") {
conf.MinVersion = tls.VersionTLS12
conf.MaxVersion = tls.VersionTLS12
} else if strings.Contains(script, "1.3") {
conf.MinVersion = tls.VersionTLS13
conf.MaxVersion = tls.VersionTLS13
}
}
d := &net.Dialer{
Timeout: time.Duration(pe.Timeout) * time.Second,
}
ok := false
var rTime int64
var cs tls.ConnectionState
lr := make(map[string]string)
for i := 0; !ok && i <= pe.Retry; i++ {
startTime := time.Now().UnixNano()
conn, err := tls.DialWithDialer(d, "tcp", target, conf)
endTime := time.Now().UnixNano()
if err != nil {
lr["error"] = fmt.Sprintf("%v", err)
continue
}
defer conn.Close()
rTime = endTime - startTime
cs = conn.ConnectionState()
ok = true
}
pe.Result["rtt"] = float64(rTime)
if ok {
getTLSConnectioStateInfo(pe, n.Name, &cs)
if mode == "expire" {
var d int
if _, err := fmt.Sscanf(script, "%d", &d); err == nil && d > 0 {
a := strings.SplitN(target, ":", 2)
cert := getServerCert(a[0], &cs)
if cert != nil {
na := cert.NotAfter.Unix()
pe.Result["notafter"] = cert.NotAfter.Format("2006/01/02")
pe.Result["issuer"] = cert.Issuer.String()
pe.Result["subject"] = cert.Subject.String()
ct := time.Now().AddDate(0, 0, d).Unix()
if ct > na {
ok = false
}
} else {
ok = false
}
}
}
}
if (ok && !strings.Contains(script, "!")) || (!ok && strings.Contains(script, "!")) {
setPollingState(pe, "normal")
} else {
setPollingState(pe, pe.Level)
}
}
func getServerCert(host string, cs *tls.ConnectionState) *x509.Certificate {
for _, cl := range cs.VerifiedChains {
for _, c := range cl {
if c.VerifyHostname(host) == nil {
return c
}
}
}
for _, c := range cs.PeerCertificates {
if c.VerifyHostname(host) == nil {
return c
}
}
return nil
}
func getTLSConnectioStateInfo(pe *datastore.PollingEnt, host string, cs *tls.ConnectionState) {
switch cs.Version {
case 0x0300: //tls.VersionSSL30 : ワーニングがでる
pe.Result["version"] = "SSLv3"
case tls.VersionTLS10:
pe.Result["version"] = "TLSv1.0"
case tls.VersionTLS11:
pe.Result["version"] = "TLSv1.1"
case tls.VersionTLS12:
pe.Result["version"] = "TLSv1.2"
case tls.VersionTLS13:
pe.Result["version"] = "TLSv1.3"
default:
pe.Result["version"] = "Unknown"
}
id := fmt.Sprintf("%04x", cs.CipherSuite)
pe.Result["cipherSuite"] = id
if len(cs.VerifiedChains) > 0 {
pe.Result["valid"] = "true"
} else {
pe.Result["valid"] = "false"
}
if cert := getServerCert(host, cs); cert != nil {
pe.Result["issuer"] = cert.Issuer.String()
pe.Result["subject"] = cert.Subject.String()
pe.Result["notAfter"] = cert.NotAfter.Format("2006/01/02")
pe.Result["subjectKeyID"] = fmt.Sprintf("%x", cert.SubjectKeyId)
}
}