-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
470 lines (426 loc) · 14 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
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"
"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
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
}
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"
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, runCheckResultCh chan<- RunCheckResult)
}
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 waitTCPServer(address string) {
time.Sleep(100 * time.Millisecond)
for {
_, err := net.Dial("tcp", address)
if err == nil {
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() {
waitTCPServer(httpAddress)
// TODO: wait UDP server for HTTP/3
finishCh <- struct{}{}
}()
<-finishCh
return
}
func prepareServerUrl(config *Config, runCheckResultCh chan<- RunCheckResult) (serverUrl string, ok bool, stopServerIfNeed func()) {
if config.ServerSchemalessUrl == "" {
var stopServer func()
var resultErrors []ResultError
serverUrl, stopServer, resultErrors = prepareServer(config)
if len(resultErrors) != 0 {
runCheckResultCh <- 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() {}
}
func checkProtocol(resp *http.Response, expectedProto Protocol) []ResultError {
var resultErrors []ResultError
var versionOk bool
switch expectedProto {
case ProtocolHttp1_0, ProtocolHttp1_0_tls:
versionOk = resp.Proto == "HTTP/1.0"
case ProtocolHttp1_1, ProtocolHttp1_1_tls:
versionOk = resp.Proto == "HTTP/1.1"
case ProtocolH2, ProtocolH2c:
versionOk = resp.Proto == "HTTP/2.0"
case ProtocolH3:
versionOk = resp.Proto == "HTTP/3.0"
}
if !versionOk {
resultErrors = append(resultErrors, NewError(fmt.Sprintf("expected %s but %s", expectedProto, resp.Proto), nil))
}
shouldUseTls := protocolUsesTls(expectedProto)
if shouldUseTls && resp.TLS == nil {
resultErrors = append(resultErrors, NewError("should use TLS but not used", nil))
}
if !shouldUseTls && resp.TLS != nil {
resultErrors = append(resultErrors, NewError("should not use TLS but used", nil))
}
return resultErrors
}
func sendOrGetAndCheck(httpClient *http.Client, req *http.Request, protocol Protocol, runCheckResultCh chan<- RunCheckResult) (*http.Response, bool) {
resp, err := httpClient.Do(req)
if err != nil {
runCheckResultCh <- NewRunCheckResultWithOneError(NewError(fmt.Sprintf("failed to %s", req.Method), err))
return nil, false
}
if resultErrors := checkProtocol(resp, protocol); len(resultErrors) != 0 {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameProtocol, Errors: resultErrors}
}
if resp.StatusCode != 200 {
runCheckResultCh <- NewRunCheckResultWithOneError(NotOkStatusError(resp.StatusCode))
return nil, false
}
return resp, true
}
func checkContentTypeForwarding(getResp *http.Response, expectedContentType string, runCheckResultCh chan<- RunCheckResult) {
receivedContentType := getResp.Header.Get("Content-Type")
if receivedContentType == expectedContentType {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameContentTypeForwarding}
} else {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameContentTypeForwarding, Errors: []ResultError{ContentTypeMismatchError(expectedContentType, receivedContentType)}}
}
}
func checkXRobotsTag(getResp *http.Response, runCheckResultCh chan<- RunCheckResult) {
receivedXRobotsTag := getResp.Header.Get("X-Robots-Tag")
if receivedXRobotsTag == "none" {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameXRobotsTagNone}
} else {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameXRobotsTagNone, Warnings: []ResultWarning{XRobotsTagNoneWarning(receivedXRobotsTag)}}
}
}
func checkTransferForReusePath(config *Config, url string, runCheckResultCh chan<- RunCheckResult) {
getHttpClient := newHTTPClient(config.Protocol, config.TlsSkipVerifyCert)
defer getHttpClient.CloseIdleConnections()
postHttpClient := newHTTPClient(config.Protocol, config.TlsSkipVerifyCert)
defer postHttpClient.CloseIdleConnections()
bodyString := "message for reuse"
getRespCh := make(chan *http.Response)
go func() {
getReq, err := http.NewRequest("GET", url, nil)
if err != nil {
runCheckResultCh <- NewRunCheckResultWithOneError(NewError("failed to create GET request", err))
return
}
getResp, getOk := sendOrGetAndCheck(getHttpClient, getReq, config.Protocol, runCheckResultCh)
if !getOk {
return
}
getRespCh <- getResp
}()
postFinishedCh := make(chan struct{})
go func() {
postReq, err := http.NewRequest("POST", url, strings.NewReader(bodyString))
if err != nil {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameReusePath, Errors: []ResultError{NewError("failed to create POST request", err)}}
return
}
_, postOk := sendOrGetAndCheck(getHttpClient, postReq, config.Protocol, runCheckResultCh)
if !postOk {
return
}
postFinishedCh <- struct{}{}
}()
getResp := <-getRespCh
bodyBytes, err := io.ReadAll(getResp.Body)
if err != nil {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameReusePath, Errors: []ResultError{NewError("failed to read up", err)}}
return
}
if string(bodyBytes) != bodyString {
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameReusePath, Errors: []ResultError{NewError("message different", nil)}}
return
}
runCheckResultCh <- RunCheckResult{SubCheckName: SubCheckNameReusePath}
<-postFinishedCh
}
func runCheck(c *Check, config *Config, resultCh chan<- Result) {
runCheckResultCh := make(chan RunCheckResult)
go func() {
c.run(config, 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)
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
}