forked from cloudradar/frontman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcheck.go
327 lines (272 loc) · 8.91 KB
/
webcheck.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package frontman
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
"golang.org/x/net/html"
"github.com/cloudradar-monitoring/frontman/pkg/utils/gzipreader"
)
const maxBodySize = 1 * 1024 * 1024
func getTextFromHTML(data []byte) (text string) {
r := bytes.NewReader(data)
dom := html.NewTokenizer(r)
startToken := dom.Token()
loopDom:
for {
tt := dom.Next()
switch {
case tt == html.ErrorToken:
break loopDom // End of the document, done
case tt == html.StartTagToken:
startToken = dom.Token()
case tt == html.TextToken:
if startToken.Data == "script" {
continue
}
if startToken.Data == "style" {
continue
}
TxtContent := html.UnescapeString(string(dom.Text()))
if len(TxtContent) > 0 {
text += TxtContent
}
}
}
return
}
func (fm *Frontman) newHTTPTransport(ignoreSSLErrors *bool) *http.Transport {
t := &http.Transport{
DisableKeepAlives: true,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 15 * time.Second,
KeepAlive: 0,
DualStack: true,
}).DialContext,
MaxIdleConns: 1,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{},
}
valueProvided := ignoreSSLErrors != nil
if (valueProvided && *ignoreSSLErrors) ||
(!valueProvided && fm.Config.IgnoreSSLErrors) {
t.TLSClientConfig.InsecureSkipVerify = true
t.TLSClientConfig.RootCAs = fm.rootCAs
}
return t
}
func checkBodyReaderMatchesPattern(data []byte, pattern string, expectedPresence string, extractTextFromHTML bool) error {
expectedPresence = strings.ToLower(expectedPresence)
if expectedPresence != "absent" {
expectedPresence = "present"
}
var whereSuffix string
if extractTextFromHTML {
data = []byte(getTextFromHTML(data))
whereSuffix = "in the extracted text"
} else {
whereSuffix = "in the raw HTML"
}
if expectedPresence == "present" && !bytes.Contains(data, []byte(pattern)) {
return fmt.Errorf("pattern expected to be present '%s' not found %s", pattern, whereSuffix)
}
if expectedPresence == "absent" && bytes.Contains(data, []byte(pattern)) {
return fmt.Errorf("pattern expected to be absent '%s' found %s", pattern, whereSuffix)
}
return nil
}
func (check WebCheck) uniqueID() string {
return check.UUID
}
func (check WebCheck) run(fm *Frontman) (*Result, error) {
res := &Result{
Node: fm.Config.NodeName,
CheckType: "webCheck",
CheckUUID: check.UUID,
Check: check.Check,
Timestamp: time.Now().Unix(),
Measurements: make(map[string]interface{}),
}
if check.UUID == "" {
return res, fmt.Errorf("missing checkUuid key")
}
if check.Check.Method == "" {
return res, fmt.Errorf("missing check.method key")
}
if check.Check.URL == "" {
return res, fmt.Errorf("missing check.url key")
}
prefix := fmt.Sprintf("http.%s.", check.Check.Method)
res.Measurements[prefix+"success"] = 0
// In case the webcheck disables redirect following we set maxRedirects to 0
maxRedirects := 0
if !check.Check.DontFollowRedirects {
maxRedirects = fm.Config.HTTPCheckMaxRedirects
}
var httpTransport = fm.newHTTPTransport(check.Check.IgnoreSSLErrors)
httpClient := fm.newClientWithOptions(httpTransport, maxRedirects)
timeout := fm.Config.HTTPCheckTimeout
// set individual timeout in case it is less than in this check
if check.Check.Timeout > 0 && check.Check.Timeout < timeout {
timeout = check.Check.Timeout
}
check.Check.Method = strings.ToUpper(check.Check.Method)
ctx, cancel := context.WithTimeout(context.Background(), secToDuration(timeout))
defer cancel()
url, err := normalizeURLPort(check.Check.URL)
if err != nil {
return res, err
}
req, err := http.NewRequest(check.Check.Method, url, nil)
if err != nil {
return res, err
}
startedConnectionAt := time.Now()
defer func() {
res.Measurements[prefix+"totalTimeSpent_s"] = time.Since(startedConnectionAt).Seconds()
}()
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set("User-Agent", fm.userAgent())
var hostHeader string
for key, val := range check.Check.Headers {
req.Header.Set(key, val)
if hostHeader == "" && strings.ToLower(key) == "host" {
hostHeader = val
}
}
if hostHeader != "" && hostHeader != req.URL.Host {
// most probably we should enable SNI handshake
httpTransport.TLSClientConfig.ServerName = hostHeader
req.Host = hostHeader
}
if check.Check.Method == "POST" && check.Check.PostData != "" {
req.Body = ioutil.NopCloser(strings.NewReader(check.Check.PostData))
// close noop closer to bypass lint warnings
_ = req.Body.Close()
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
var wroteRequestAt time.Time
trace := &httptrace.ClientTrace{
GotFirstResponseByte: func() {
res.Measurements[prefix+"timeToFirstByte_s"] = time.Since(wroteRequestAt).Seconds()
},
}
wroteRequestAt = time.Now()
resp, err := httpClient.Do(req.WithContext(httptrace.WithClientTrace(ctx, trace)))
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return res, fmt.Errorf("timeout exceeded")
}
return res, err
}
defer resp.Body.Close()
// Set the httpStatusCode in case we got a response
res.Measurements[prefix+"httpStatusCode"] = resp.StatusCode
if check.Check.ExpectedHTTPStatus > 0 && resp.StatusCode != check.Check.ExpectedHTTPStatus {
return res, fmt.Errorf("bad status code. Expected %d, got %d", check.Check.ExpectedHTTPStatus, resp.StatusCode)
}
if check.Check.Method != "HEAD" {
if contentLength := resp.Header.Get("Content-Length"); contentLength != "" {
length, err := strconv.ParseInt(contentLength, 10, 64)
if err == nil && length > maxBodySize {
res.Message = fmt.Sprintf("Content-Length too large for checking (%d)", length)
return res, nil
}
}
contentType := resp.Header.Get("Content-Type")
ct := strings.Split(contentType, "/")
if len(ct) >= 2 && ct[0] != "text" {
switch ct[0] {
case "audio", "video", "image", "font":
res.Message = fmt.Sprintf("Content-Type is not readable as text (%s)", contentType)
return res, nil
}
switch contentType {
case "application/octet-stream", "application/ogg", "application/pdf", "application/x-shockwave-flash", "application/zip":
res.Message = fmt.Sprintf("Content-Type is not readable as text (%s)", contentType)
return res, nil
}
}
}
if strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") {
// wrap body reader with gzip reader
resp.Body = &gzipreader.GzipReader{Reader: resp.Body}
}
limitedReader := http.MaxBytesReader(nil, resp.Body, maxBodySize)
data, err := ioutil.ReadAll(limitedReader)
if err != nil {
if err.Error() == "http: request body too large" {
res.Message = fmt.Sprintf("got error while reading full response body: http: request body exceeds the maximum of %dMB", maxBodySize/1024/1024)
} else {
res.Message = fmt.Sprintf("got error while reading response body: %s", err.Error())
}
}
if check.Check.ExpectedPattern != "" {
err = checkBodyReaderMatchesPattern(data, check.Check.ExpectedPattern, check.Check.ExpectedPatternPresence, !check.Check.SearchHTMLSource)
if err != nil {
res.Message = err.Error()
}
}
bytesReceived := len(data)
res.Measurements[prefix+"bytesReceived"] = bytesReceived
secondsSinceRequestWasSent := time.Since(wroteRequestAt).Seconds()
if secondsSinceRequestWasSent > 0 {
// Measure download speed since the request sent
res.Measurements[prefix+"downloadPerformance_bps"] = int64(float64(bytesReceived) / secondsSinceRequestWasSent)
}
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return res, fmt.Errorf("timeout exceeded")
}
return res, nil
}
res.Measurements[prefix+"success"] = 1
return res, nil
}
func (fm *Frontman) newClientWithOptions(transport *http.Transport, maxRedirects int) *http.Client {
client := &http.Client{Transport: transport}
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
// copies headers from previous request (go strips Authorization header on redirects)
lastRequest := via[len(via)-1]
for attr, val := range lastRequest.Header {
if _, ok := req.Header[attr]; !ok {
req.Header[attr] = val
}
}
if maxRedirects <= 0 {
logrus.Println("redirects are not allowed")
return http.ErrUseLastResponse
} else if len(via) > maxRedirects {
logrus.Printf("too many(>%d) redirects", maxRedirects)
return http.ErrUseLastResponse
}
return nil
}
return client
}
// removes ports from URL if it is the default port for given scheme
func normalizeURLPort(u string) (string, error) {
url, err := url.Parse(u)
if err != nil {
return u, err
}
portSeparator := strings.Index(url.Host, ":")
if portSeparator != -1 {
port := url.Host[portSeparator+1:]
if (url.Scheme == "http" && port == "80") || (url.Scheme == "https" && port == "443") {
url.Host = url.Host[0:portSeparator]
}
}
return url.String(), nil
}