Repository navigation
Expand file tree
/
Copy pathhttpprobe.go
More file actions
193 lines (182 loc) · 7.21 KB
/
Copy pathhttpprobe.go
File metadata and controls
193 lines (182 loc) · 7.21 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
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
package main
// The built-in HTTP(S) prober: time to first byte of a request.
//
// For each of `packets` probes it opens a NEW connection, sends one request and
// stops the clock when the response HEADERS arrive. That is DNS + TCP connect + TLS
// handshake + the server's own processing: the number a visitor feels. The body is
// never read. A probe counts as received when the status is acceptable.
//
// The same code is built into fogping and vrf-ping, so one set of tests describes
// all three. (fogping registers it with its prober registry; pingping has no
// registry, so config.go, probe.go and targetsio.go name the type directly.)
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"
)
const typeHTTP = "http"
// httpSpecs are the options of the http type, in the order the form shows them.
var httpSpecs = []optionSpec{
{Key: "scheme", Label: "Scheme", Kind: optChoice, Choices: []string{"https", "http"}, Default: "https",
Help: "https verifies the certificate; there is deliberately no option to switch that off"},
{Key: "method", Label: "Method", Kind: optChoice, Choices: []string{"GET", "HEAD"}, Default: "GET"},
{Key: "path", Label: "Path", Kind: optString, Default: "/", MaxLen: 200,
Help: "Starts with /, for example /health. A query string is allowed: /ping?src=pingping"},
{Key: "host_header", Label: "Host header", Kind: optString, MaxLen: 253,
Help: "Ask for a different site on the same address (virtual hosting). Empty = the target's host."},
{Key: "expect_status", Label: "Expected status", Kind: optInt, Min: 0, Max: 599, Default: 0,
Help: "0 = any status below 400 counts as up. Otherwise only exactly this status does."},
{Key: "follow_redirects", Label: "Follow redirects", Kind: optBool,
Help: "Off: a 301 is the answer. On: follow up to 5 redirects and time the whole chain."},
}
// validateHTTP holds the rules only this prober knows. fogping checks the types
// and ranges the option specs declare; it cannot know that a path starts with "/".
func validateHTTP(t *TargetCfg) error {
if t.Port < 1 || t.Port > 65535 {
return fmt.Errorf("http target needs a port 1-65535")
}
if !validURLHost(t.Host) {
return fmt.Errorf("http host must be a hostname or an IP address")
}
if p := t.OptString("path"); !validURLPath(p) {
return fmt.Errorf("option \"path\" must start with / and hold no spaces, # or control characters")
}
if h := t.OptString("host_header"); h != "" && !validURLHost(h) {
return fmt.Errorf("option \"host_header\" must be a hostname or an IP address")
}
return nil
}
// validURLPath: starts with "/", and every character is printable and not a space
// or "#". The path is split at "?" and the query part is placed into the URL as
// written (url.URL does not escape it), so what may appear in it has to be checked
// here: a space would corrupt the request line, and "#" would start a fragment that
// silently drops the rest. Control characters are refused by the options layer and
// again by net/http, but this prober does not rely on either.
func validURLPath(p string) bool {
if !strings.HasPrefix(p, "/") {
return false
}
for _, r := range p {
if r <= ' ' || r == 0x7f || r == '#' {
return false
}
}
return true
}
// validURLHost is a WHITELIST: letters, digits, dot, hyphen, underscore, colon
// (IPv6). The host comes from a user, and the general host check only refuses
// spaces. A host such as "good.example/@evil.example" would, glued into a URL,
// make the probe talk to a DIFFERENT server. Whenever text becomes part of a
// structured string (URL, SQL, command line), check it against what it may be
// first; listing what is allowed is safer than listing what is dangerous.
func validURLHost(h string) bool {
if h == "" || len(h) > 253 {
return false
}
for _, r := range h {
switch {
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9',
r == '.', r == '-', r == '_', r == ':':
default:
return false
}
}
return true
}
// httpURL builds the URL with net/url rather than by concatenation, so the path is
// escaped properly. net.JoinHostPort adds the brackets an IPv6 address needs.
func httpURL(t TargetCfg) string {
u := url.URL{Scheme: t.OptString("scheme"), Host: net.JoinHostPort(t.Host, fmt.Sprint(t.Port))}
// The path may carry a query string: split it ourselves instead of letting
// url.URL escape the "?".
if p, q, found := strings.Cut(t.OptString("path"), "?"); found {
u.Path, u.RawQuery = p, q
} else {
u.Path = p
}
return u.String()
}
// httpStatusOK decides whether a status means "the service answered properly".
func httpStatusOK(got, expect int) bool {
if expect == 0 {
return got < 400
}
return got == expect
}
// httpRound sends `packets` requests, `gap` apart. A request that fails, times out
// or returns an unacceptable status has no sample: that is what packet loss IS to
// fogping. A 500 counts as loss because the service is not working, although the
// network is fine.
func httpRound(ctx context.Context, t TargetCfg, packets int, gap, timeout time.Duration) (Round, error) {
r := Round{T: time.Now().Unix(), S: packets}
follow := t.OptBool("follow_redirects")
client := &http.Client{
Timeout: timeout,
Transport: &http.Transport{
// Every probe opens a fresh connection. With reuse, probes 2..N would
// skip DNS, TCP and TLS and measure only the server: a much smaller
// number than probe 1.
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12}, // certificates ARE verified
Proxy: nil, // never a proxy taken from the environment
// A server chooses how much it sends before the body. Bound it: the default is
// 1 MB per probe, from a machine we do not control.
MaxResponseHeaderBytes: 64 << 10,
},
CheckRedirect: func(_ *http.Request, via []*http.Request) error {
if !follow {
return http.ErrUseLastResponse // the redirect itself is the answer
}
if len(via) >= 5 {
return errors.New("stopped after 5 redirects")
}
return nil
},
}
target, expect := httpURL(t), t.OptInt("expect_status")
for i := 0; i < packets; i++ {
if i > 0 {
select {
case <-time.After(gap):
case <-ctx.Done():
return r, ctx.Err()
}
}
if ms, ok := httpOnce(ctx, client, t, target, expect); ok {
r.R++
r.MS = append(r.MS, ms)
} else if ctx.Err() != nil {
return r, ctx.Err() // stopped mid-round: the caller drops the round
}
}
return r, nil
}
func httpOnce(ctx context.Context, client *http.Client, t TargetCfg, target string, expect int) (float64, bool) {
req, err := http.NewRequestWithContext(ctx, t.OptString("method"), target, nil)
if err != nil {
return 0, false
}
if h := t.OptString("host_header"); h != "" {
req.Host = h // the Host header is a field of the request, not a header map entry
}
req.Header.Set("User-Agent", "pingping-http/1")
start := time.Now()
resp, err := client.Do(req) // returns as soon as the headers are in
elapsed := time.Since(start)
if err != nil {
return 0, false
}
// The body is never read: draining a large download would cost time and
// bandwidth for nothing.
resp.Body.Close()
if !httpStatusOK(resp.StatusCode, expect) {
return 0, false
}
return float64(elapsed.Microseconds()) / 1000.0, true
}