-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
395 lines (355 loc) · 11.2 KB
/
check.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package check
import (
"context"
"crypto/tls"
"fmt"
"github.com/nwtgck/piping-server-check/http10_round_tripper"
"github.com/nwtgck/piping-server-check/util"
"github.com/quic-go/quic-go/http3"
"go.uber.org/atomic"
"golang.org/x/net/http2"
"io"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"sync"
"syscall"
"time"
)
type Protocol string
const (
ProtocolHttp1_0 = Protocol("http1.0")
ProtocolHttp1_0_tls = Protocol("http1.0-tls")
ProtocolHttp1_1 = Protocol("http1.1")
ProtocolHttp1_1_tls = Protocol("http1.1-tls")
ProtocolH2 = Protocol("h2")
ProtocolH2c = Protocol("h2c")
ProtocolH3 = Protocol("h3")
)
type Config struct {
RunServerCmd []string
HealthCheckPath string
ServerSchemalessUrl string
Protocol Protocol
TlsSkipVerifyCert bool
Concurrency uint
SenderResponseBeforeReceiverTimeout time.Duration
FirstByteCheckTimeout time.Duration
GetResponseReceivedTimeout time.Duration
GetReqWroteRequestWaitForH3 time.Duration // because httptrace not supported: https://github.com/quic-go/quic-go/issues/3342
TransferBytePerSec int
SortedTransferSpans []time.Duration
WaitDurationAfterSenderCancel time.Duration
WaitDurationBetweenReceiverWroteRequestAndCancel time.Duration
WaitDurationAfterReceiverCancel time.Duration
}
func protocolUsesTls(protocol Protocol) bool {
switch protocol {
case ProtocolHttp1_0_tls, ProtocolHttp1_1_tls, ProtocolH2, ProtocolH3:
return true
default:
return false
}
}
func newHTTPClient(protocol Protocol, tlsSkipVerifyCert bool) *http.Client {
tlsConfig := &tls.Config{InsecureSkipVerify: tlsSkipVerifyCert}
// TODO: impl
switch protocol {
case ProtocolHttp1_0, ProtocolHttp1_0_tls:
return &http.Client{
Transport: &http10_round_tripper.Http10RoundTripper{
TLSClientConfig: tlsConfig,
},
}
case ProtocolHttp1_1, ProtocolHttp1_1_tls:
return &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}
case ProtocolH2c:
return &http.Client{
// (base: https://github.com/thrawn01/h2c-golang-example/tree/cafa2960ca5df81100b61a1785b37f5ed749b87d)
Transport: &http2.Transport{
AllowHTTP: true,
TLSClientConfig: tlsConfig,
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(network, addr)
},
},
}
case ProtocolH2:
return &http.Client{
Transport: &http2.Transport{
TLSClientConfig: tlsConfig,
},
}
case ProtocolH3:
return &http.Client{
Transport: &http3.RoundTripper{
TLSClientConfig: tlsConfig,
},
}
}
return nil
}
type ResultError struct {
Message string `json:"message"`
}
func NewError(message string, err error) ResultError {
if err == nil {
return ResultError{Message: message}
}
return ResultError{Message: fmt.Sprintf("%s: %+v", message, err)}
}
func FailedToGetPortError() ResultError {
return ResultError{Message: "failed to get port"}
}
func FailedToRunServerError(err error) ResultError {
return ResultError{Message: fmt.Sprintf("failed to run server: %+v", err)}
}
func NotOkStatusError(status int) ResultError {
return ResultError{Message: fmt.Sprintf("not OK status: %d", status)}
}
func ContentTypeMismatchError(expectedContentType string, actualContentType string) ResultError {
return ResultError{Message: fmt.Sprintf("Content-Type should be %s but found %s", expectedContentType, actualContentType)}
}
type ResultWarning struct {
Message string `json:"message"`
}
func NewWarning(message string, err error) ResultWarning {
if err == nil {
return ResultWarning{Message: message}
}
return ResultWarning{Message: fmt.Sprintf("%s: %+v", message, err)}
}
func XRobotsTagNoneWarning(actualValue string) ResultWarning {
return ResultWarning{Message: fmt.Sprintf("X-Robots-Tag: none is recommeded but found '%+v'", actualValue)}
}
type Result struct {
// result name can be "<check name>.<subcheck name>" or "<check name>"
Name string `json:"name"`
Protocol Protocol `json:"protocol"`
Message string `json:"message,omitempty"`
OkForJson *bool `json:"ok,omitempty"`
Errors []ResultError `json:"errors,omitempty"`
Warnings []ResultWarning `json:"warnings,omitempty"`
}
// Subcheck name is top-level. The same subcheck names in different checks should be the same meaning.
const (
SubCheckNameProtocol = "protocol"
SubCheckNameSenderResponseBeforeReceiver = "sender_response_before_receiver"
SubCheckNameSamePathSenderRejection = "same_path_sender_rejection"
SubCheckNameContentTypeForwarding = "content_type_forwarding"
SubCheckNameContentDispositionForwarding = "content_disposition_forwarding"
SubCheckNameXRobotsTagNone = "x_robots_tag_none"
SubCheckNameTransferred = "transferred"
SubCheckNameReusePath = "reuse_path"
SubCheckNamePartialTransfer = "partial_transfer"
)
type RunCheckResult struct {
// empty string is ok
SubCheckName string
Message string
Errors []ResultError
Warnings []ResultWarning
}
func NewRunCheckResultWithOneError(resultError ResultError) RunCheckResult {
return RunCheckResult{Errors: []ResultError{resultError}}
}
type Check struct {
Name string
run func(config *Config, reporter RunCheckReporter)
}
type RunCheckReporter struct {
ch chan<- RunCheckResult
closed *atomic.Bool
}
func NewRunCheckReporter(ch chan<- RunCheckResult) RunCheckReporter {
return RunCheckReporter{ch: ch, closed: atomic.NewBool(false)}
}
func (r *RunCheckReporter) Report(result RunCheckResult) {
if r.closed.Load() {
return
}
r.ch <- result
}
func (r *RunCheckReporter) Close() {
r.closed.Store(true)
close(r.ch)
}
func getCheckName() string {
counter, _, _, success := runtime.Caller(1)
if !success {
panic(fmt.Errorf("failed to run runtime.Caller()"))
}
functionName := runtime.FuncForPC(counter).Name()
index := strings.LastIndex(functionName, ".")
return functionName[index+1:]
}
func startServer(cmd []string, httpPort string, httpsPort string) (c *exec.Cmd, stdout io.ReadCloser, stderr io.ReadCloser, err error) {
c = exec.Command(cmd[0], cmd[1:]...)
c.Env = append(os.Environ(), "HTTP_PORT="+httpPort, "HTTPS_PORT="+httpsPort)
stdout, err = c.StdoutPipe()
if err != nil {
return
}
stderr, err = c.StderrPipe()
if err != nil {
return
}
err = c.Start()
return
}
func waitHTTPServer(httpClient *http.Client, healthCheckUrl string) {
time.Sleep(100 * time.Millisecond)
for {
resp, err := httpClient.Get(healthCheckUrl)
if err == nil && (200 <= resp.StatusCode && resp.StatusCode < 300) {
return
}
time.Sleep(1 * time.Second)
}
}
// To avoid port already used error
var prepareServerMutex = new(sync.Mutex)
func prepareServer(config *Config) (serverUrl string, stopSerer func(), resultErrors []ResultError) {
prepareServerMutex.Lock()
defer prepareServerMutex.Unlock()
httpPort, err := util.GetTCPPort()
if err != nil {
resultErrors = append(resultErrors, FailedToGetPortError())
return
}
httpsPort, err := util.GetTCPAndUDPPort()
if err != nil {
resultErrors = append(resultErrors, FailedToGetPortError())
return
}
cmd, _, stderr, err := startServer(config.RunServerCmd, httpPort, httpsPort)
if err != nil {
resultErrors = append(resultErrors, FailedToRunServerError(err))
return
}
finishCh := make(chan struct{})
go func() {
stderrStringCh := make(chan string)
go func() {
var buf [2048]byte
// err can be ignored because empty bytes will be an empty string
n, _ := io.ReadFull(stderr, buf[:])
stderrString := string(buf[:n])
stderrStringCh <- stderrString
}()
err := cmd.Wait()
stderrString := <-stderrStringCh
if err != nil {
resultErrors = append(resultErrors, NewError(fmt.Sprintf("%+v, stderr: %s", err, stderrString), err))
}
finishCh <- struct{}{}
}()
stopSerer = func() {
cmd.Process.Signal(syscall.SIGTERM)
}
serverPort := httpPort
if protocolUsesTls(config.Protocol) {
serverPort = httpsPort
}
httpAddress := net.JoinHostPort("localhost", serverPort)
if protocolUsesTls(config.Protocol) {
serverUrl = "https://" + httpAddress
} else {
serverUrl = "http://" + httpAddress
}
go func() {
client := newHTTPClient(config.Protocol, true /* always skip verification for health check */)
defer client.CloseIdleConnections()
waitHTTPServer(client, serverUrl+config.HealthCheckPath)
finishCh <- struct{}{}
}()
<-finishCh
return
}
func prepareServerUrl(config *Config, reporter RunCheckReporter) (serverUrl string, ok bool, stopServerIfNeed func()) {
if config.ServerSchemalessUrl == "" {
var stopServer func()
var resultErrors []ResultError
serverUrl, stopServer, resultErrors = prepareServer(config)
if len(resultErrors) != 0 {
reporter.Report(RunCheckResult{Errors: resultErrors})
return
}
return serverUrl, true, stopServer
}
if protocolUsesTls(config.Protocol) {
serverUrl = "https:" + config.ServerSchemalessUrl
} else {
serverUrl = "http:" + config.ServerSchemalessUrl
}
return serverUrl, true, func() {}
}
// Use this function when Go standard HTTP library automatically attach it.
func ensureContentLengthExits(req *http.Request) {
if req.ContentLength <= 0 {
panic(fmt.Errorf("Content-Length not found in %v", req))
}
}
func runCheck(c *Check, config *Config, resultCh chan<- Result) {
runCheckResultCh := make(chan RunCheckResult)
go func() {
c.run(config, NewRunCheckReporter(runCheckResultCh))
}()
for runCheckResult := range runCheckResultCh {
var result Result
if runCheckResult.SubCheckName == "" {
result.Name = c.Name
} else {
result.Name = c.Name + "." + runCheckResult.SubCheckName
}
result.Message = runCheckResult.Message
result.Errors = runCheckResult.Errors
result.Warnings = runCheckResult.Warnings
result.Protocol = config.Protocol
if len(result.Errors) == 0 {
result.OkForJson = new(bool)
*result.OkForJson = true
}
resultCh <- result
}
}
func RunChecks(checks []Check, commonConfig *Config, protocols []Protocol) <-chan Result {
if commonConfig.Concurrency < 1 {
panic("concurrency should be >= 1")
}
ch := make(chan Result)
resultChForRunCheckCh := make(chan (<-chan Result), commonConfig.Concurrency-1)
go func() {
for resultChForRunCheck := range resultChForRunCheckCh {
for result := range resultChForRunCheck {
ch <- result
}
}
close(ch)
}()
go func() {
for _, c := range checks {
for _, protocol := range protocols {
var resultChForRunCheck chan Result
resultChForRunCheck = make(chan Result, 128 /* subcheck waits if buffer size is less than the number of subchecks */)
resultChForRunCheckCh <- resultChForRunCheck
config := *commonConfig
config.Protocol = protocol
go func(c Check, config Config) {
// TODO: timeout for runCheck considering long-time check
runCheck(&c, &config, resultChForRunCheck)
close(resultChForRunCheck)
}(c, config)
}
}
close(resultChForRunCheckCh)
}()
return ch
}