-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
82 lines (73 loc) · 2.11 KB
/
main.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/myzie/burrow"
)
func main() {
var timeoutDur time.Duration
var maxResponseBytes, maxRetries int64
var allowedContentTypes string
var target, proxy, method string
flag.StringVar(&target, "url", "", "URL to send a request to")
flag.StringVar(&proxy, "proxy", "", "URL of the proxy to use")
flag.Int64Var(&maxResponseBytes, "max-response-bytes", 0, "Maximum response body size")
flag.DurationVar(&timeoutDur, "timeout", 0, "Timeout")
flag.Int64Var(&maxRetries, "retries", 0, "Maximum retries")
flag.StringVar(&allowedContentTypes, "allowed-content-types", "", "Allowed content types")
flag.Parse()
allowedContentTypesList := strings.Split(allowedContentTypes, ",")
opts := []burrow.ClientOption{
burrow.WithProxyURL(proxy),
burrow.WithRetries(int(maxRetries)),
burrow.WithRetryableCodes([]int{404}),
burrow.WithCallback(func(ctx context.Context, proxyResponse *burrow.Response) {
fmt.Printf("proxy response: %+v\n", proxyResponse)
}),
}
if maxResponseBytes > 0 {
opts = append(opts, burrow.WithMaxResponseBytes(maxResponseBytes))
}
if timeoutDur > 0 {
opts = append(opts, burrow.WithTimeout(timeoutDur))
}
if len(allowedContentTypesList) > 0 {
opts = append(opts, burrow.WithAllowedContentTypes(allowedContentTypesList))
}
client := burrow.NewClient(opts...)
req, err := http.NewRequest(method, target, nil)
if err != nil {
fmt.Println("failed to create request:", err)
os.Exit(1)
}
resp, err := client.Do(req)
if err != nil {
var proxyErr *burrow.ProxyError
if errors.As(err, &proxyErr) {
fmt.Println("proxy error:", proxyErr.Message)
fmt.Println("proxy error type:", proxyErr.Type)
os.Exit(1)
}
fmt.Println("unknown error:", err)
os.Exit(1)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("unexpected status code: %d\n", resp.StatusCode)
os.Exit(1)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("failed to read response body:", err)
os.Exit(1)
}
fmt.Println("================")
fmt.Println(string(body))
}