/
main.go
140 lines (120 loc) · 3.41 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
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
package main
import (
"fmt"
"os"
"strconv"
"github.com/cheggaaa/pb"
)
type latency struct {
TTFB int64 `json:"ttfb" yaml:"ttfb"`
Connect int64 `json:"connect" yaml:"connect"`
TLSHandshake int64 `json:"tls_handshake" yaml:"tls_handshake"`
}
type result struct {
Proxy string `json:"proxy" yaml:"proxy"`
Endpoint string `json:"endpoint" yaml:"endpoint"`
StatusCode int `json:"status_code" yaml:"status_code"`
ResponseBody string `json:"response_body omitempty" yaml:"response_body,omitempty"`
Latency latency `json:"latency" yaml:"latency"`
Err error `json:"error" yaml:"error"`
}
type report struct {
Success int `json:"success" yaml:"success"`
Fail int `json:"fail" yaml:"fail"`
AverageTTFB int64 `json:"average_ttfb" yaml:"average_ttfb"`
Results []*result `json:"results" yaml:"results"`
}
var (
results []*result
bar *pb.ProgressBar
output = "plaintext"
proxyFile = ""
singleProxy = ""
testURL = "https://example.com"
delay = 50
maxThreads = 10
includeResponseBody = false
)
func init() {
for k, v := range os.Args {
// Handle arguments that don't require a parameter
switch v {
case "--include-response-body":
includeResponseBody = true
case "--help", "-h":
showHelp()
return
}
// Handle Arguments that require a parameter
if len(os.Args) < k+2 {
break
}
switch v {
case "--output":
setOutput(os.Args[k+1])
case "--file":
proxyFile = os.Args[k+1]
case "--proxy":
singleProxy = os.Args[k+1]
case "--endpoint":
testURL = os.Args[k+1]
case "--max-threads":
var err error
maxThreads, err = strconv.Atoi(os.Args[k+1])
if err != nil {
maxThreads = 10
break
}
if maxThreads < 1 {
panic("invalid value for --max-threads")
}
case "--delay":
var err error
delay, err = strconv.Atoi(os.Args[k+1])
if err != nil {
delay = 50
break
}
if delay < 0 {
panic("invalid value for delay")
}
}
}
if proxyFile == "" && singleProxy == "" {
panic("Use --file or --proxy to specify the proxy server(s) that you want to test")
}
}
func main() {
if singleProxy != "" {
testProxies([]string{singleProxy})
return
}
testProxiesFromFile(proxyFile)
}
// setOutput validates and sets output format selection.
func setOutput(outputType string) {
switch outputType {
case "json", "yaml", "plaintext":
output = outputType
default:
panic("invalid value for --output. supported types are 'json', 'yaml', and 'plaintext'")
}
}
// showHelp displays the cli help menu.
func showHelp() {
fmt.Print(`
Usage:
proxysampler [OPTIONS]
Application Options:
--output {json|yaml|plaintext} Default is plaintext.
--include-response-body Include response bodies in JSON/YAML output. Disabled by default.
--file {/path/to/file.txt} Relative/absolute path to a file containing a list of proxies.
--proxy {proxy info} Test a single proxy. Example: https://proxyuser:proxypass@packetstream.io:31111
--endpoint {https://example.com} The endpoint that you want to use for testing.
--max-threads {10} Number of concurrent threads to use for testing proxy. Default is 10.
--delay {50} Delay in ms between each request. Default is 50.
Help Options
--help, -h Show this screen.
`)
os.Exit(0)
}