...
1 package probers
2
3 import (
4 "crypto/tls"
5 "fmt"
6 "net/http"
7 "time"
8 )
9
10
11
12 type HTTPProbe struct {
13 url string
14 rcodes []int
15 useragent string
16 insecure bool
17 }
18
19
20
21 func (p HTTPProbe) Name() string {
22 insecure := ""
23 if p.insecure {
24 insecure = "-insecure"
25 }
26 return fmt.Sprintf("%s-%d-%s%s", p.url, p.rcodes, p.useragent, insecure)
27 }
28
29
30 func (p HTTPProbe) Kind() string {
31 return "HTTP"
32 }
33
34
35
36 func (p HTTPProbe) isExpected(received int) bool {
37 for _, c := range p.rcodes {
38 if received == c {
39 return true
40 }
41 }
42 return false
43 }
44
45
46 func (p HTTPProbe) Probe(timeout time.Duration) (bool, time.Duration) {
47 client := http.Client{
48 Timeout: timeout,
49 Transport: &http.Transport{
50 TLSClientConfig: &tls.Config{InsecureSkipVerify: p.insecure},
51 }}
52 req, err := http.NewRequest("GET", p.url, nil)
53 if err != nil {
54 return false, 0
55 }
56 req.Header.Set("User-Agent", p.useragent)
57 start := time.Now()
58
59 resp, err := client.Do(req)
60 if err != nil {
61 return false, time.Since(start)
62 }
63 return p.isExpected(resp.StatusCode), time.Since(start)
64 }
65
View as plain text