-
Notifications
You must be signed in to change notification settings - Fork 384
/
main.go
539 lines (463 loc) · 14.2 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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package main
import (
"context"
"crypto/tls"
"encoding/pem"
"flag"
"fmt"
"io"
"log"
"mime"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"os"
"path"
"runtime"
"sort"
"strconv"
"strings"
"time"
"github.com/fatih/color"
)
const (
httpsTemplate = `` +
` DNS Lookup TCP Connection TLS Handshake Server Processing Content Transfer` + "\n" +
`[%s | %s | %s | %s | %s ]` + "\n" +
` | | | | |` + "\n" +
` namelookup:%s | | | |` + "\n" +
` connect:%s | | |` + "\n" +
` pretransfer:%s | |` + "\n" +
` starttransfer:%s |` + "\n" +
` total:%s` + "\n"
httpTemplate = `` +
` DNS Lookup TCP Connection Server Processing Content Transfer` + "\n" +
`[ %s | %s | %s | %s ]` + "\n" +
` | | | |` + "\n" +
` namelookup:%s | | |` + "\n" +
` connect:%s | |` + "\n" +
` starttransfer:%s |` + "\n" +
` total:%s` + "\n"
)
var (
// Command line flags.
httpMethod string
postBody string
followRedirects bool
onlyHeader bool
insecure bool
httpHeaders headers
saveOutput bool
outputFile string
showVersion bool
clientCertFile string
fourOnly bool
sixOnly bool
// number of redirects followed
redirectsFollowed int
version = "devel" // for -v flag, updated during the release process with -ldflags=-X=main.version=...
)
const maxRedirects = 10
func init() {
flag.StringVar(&httpMethod, "X", "GET", "HTTP method to use")
flag.StringVar(&postBody, "d", "", "the body of a POST or PUT request; from file use @filename")
flag.BoolVar(&followRedirects, "L", false, "follow 30x redirects")
flag.BoolVar(&onlyHeader, "I", false, "don't read body of request")
flag.BoolVar(&insecure, "k", false, "allow insecure SSL connections")
flag.Var(&httpHeaders, "H", "set HTTP header; repeatable: -H 'Accept: ...' -H 'Range: ...'")
flag.BoolVar(&saveOutput, "O", false, "save body as remote filename")
flag.StringVar(&outputFile, "o", "", "output file for body")
flag.BoolVar(&showVersion, "v", false, "print version number")
flag.StringVar(&clientCertFile, "E", "", "client cert file for tls config")
flag.BoolVar(&fourOnly, "4", false, "resolve IPv4 addresses only")
flag.BoolVar(&sixOnly, "6", false, "resolve IPv6 addresses only")
flag.Usage = usage
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] URL\n\n", os.Args[0])
fmt.Fprintln(os.Stderr, "OPTIONS:")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "ENVIRONMENT:")
fmt.Fprintln(os.Stderr, " HTTP_PROXY proxy for HTTP requests; complete URL or HOST[:PORT]")
fmt.Fprintln(os.Stderr, " used for HTTPS requests if HTTPS_PROXY undefined")
fmt.Fprintln(os.Stderr, " HTTPS_PROXY proxy for HTTPS requests; complete URL or HOST[:PORT]")
fmt.Fprintln(os.Stderr, " NO_PROXY comma-separated list of hosts to exclude from proxy")
}
func printf(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(color.Output, format, a...)
}
func grayscale(code color.Attribute) func(string, ...interface{}) string {
return color.New(code + 232).SprintfFunc()
}
func main() {
flag.Parse()
if showVersion {
fmt.Printf("%s %s (runtime: %s)\n", os.Args[0], version, runtime.Version())
os.Exit(0)
}
if fourOnly && sixOnly {
fmt.Fprintf(os.Stderr, "%s: Only one of -4 and -6 may be specified\n", os.Args[0])
os.Exit(-1)
}
args := flag.Args()
if len(args) != 1 {
flag.Usage()
os.Exit(2)
}
if (httpMethod == "POST" || httpMethod == "PUT") && postBody == "" {
log.Fatal("must supply post body using -d when POST or PUT is used")
}
if onlyHeader {
httpMethod = "HEAD"
}
url := parseURL(args[0])
visit(url)
}
// readClientCert - helper function to read client certificate
// from pem formatted file
func readClientCert(filename string) []tls.Certificate {
if filename == "" {
return nil
}
var (
pkeyPem []byte
certPem []byte
)
// read client certificate file (must include client private key and certificate)
certFileBytes, err := os.ReadFile(filename)
if err != nil {
log.Fatalf("failed to read client certificate file: %v", err)
}
for {
block, rest := pem.Decode(certFileBytes)
if block == nil {
break
}
certFileBytes = rest
if strings.HasSuffix(block.Type, "PRIVATE KEY") {
pkeyPem = pem.EncodeToMemory(block)
}
if strings.HasSuffix(block.Type, "CERTIFICATE") {
certPem = pem.EncodeToMemory(block)
}
}
cert, err := tls.X509KeyPair(certPem, pkeyPem)
if err != nil {
log.Fatalf("unable to load client cert and key pair: %v", err)
}
return []tls.Certificate{cert}
}
func parseURL(uri string) *url.URL {
if !strings.Contains(uri, "://") && !strings.HasPrefix(uri, "//") {
uri = "//" + uri
}
url, err := url.Parse(uri)
if err != nil {
log.Fatalf("could not parse url %q: %v", uri, err)
}
if url.Scheme == "" {
url.Scheme = "http"
if !strings.HasSuffix(url.Host, ":80") {
url.Scheme += "s"
}
}
return url
}
func headerKeyValue(h string) (string, string) {
i := strings.Index(h, ":")
if i == -1 {
log.Fatalf("Header '%s' has invalid format, missing ':'", h)
}
return strings.TrimRight(h[:i], " "), strings.TrimLeft(h[i:], " :")
}
func dialContext(network string) func(ctx context.Context, network, addr string) (net.Conn, error) {
return func(ctx context.Context, _, addr string) (net.Conn, error) {
return (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: false,
}).DialContext(ctx, network, addr)
}
}
// visit visits a url and times the interaction.
// If the response is a 30x, visit follows the redirect.
func visit(url *url.URL) {
req := newRequest(httpMethod, url, postBody)
var t0, t1, t2, t3, t4, t5, t6 time.Time
trace := &httptrace.ClientTrace{
DNSStart: func(_ httptrace.DNSStartInfo) { t0 = time.Now() },
DNSDone: func(_ httptrace.DNSDoneInfo) { t1 = time.Now() },
ConnectStart: func(_, _ string) {
if t1.IsZero() {
// connecting to IP
t1 = time.Now()
}
},
ConnectDone: func(net, addr string, err error) {
if err != nil {
log.Fatalf("unable to connect to host %v: %v", addr, err)
}
t2 = time.Now()
printf("\n%s%s\n", color.GreenString("Connected to "), color.CyanString(addr))
},
GotConn: func(_ httptrace.GotConnInfo) { t3 = time.Now() },
GotFirstResponseByte: func() { t4 = time.Now() },
TLSHandshakeStart: func() { t5 = time.Now() },
TLSHandshakeDone: func(_ tls.ConnectionState, _ error) { t6 = time.Now() },
}
req = req.WithContext(httptrace.WithClientTrace(context.Background(), trace))
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ForceAttemptHTTP2: true,
}
switch {
case fourOnly:
tr.DialContext = dialContext("tcp4")
case sixOnly:
tr.DialContext = dialContext("tcp6")
}
switch url.Scheme {
case "https":
host, _, err := net.SplitHostPort(req.Host)
if err != nil {
host = req.Host
}
tr.TLSClientConfig = &tls.Config{
ServerName: host,
InsecureSkipVerify: insecure,
Certificates: readClientCert(clientCertFile),
MinVersion: tls.VersionTLS12,
}
}
client := &http.Client{
Transport: tr,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// always refuse to follow redirects, visit does that
// manually if required.
return http.ErrUseLastResponse
},
}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("failed to read response: %v", err)
}
// Print SSL/TLS version which is used for connection
connectedVia := "plaintext"
if resp.TLS != nil {
switch resp.TLS.Version {
case tls.VersionTLS12:
connectedVia = "TLSv1.2"
case tls.VersionTLS13:
connectedVia = "TLSv1.3"
}
}
printf("\n%s %s\n", color.GreenString("Connected via"), color.CyanString("%s", connectedVia))
bodyMsg := readResponseBody(req, resp)
resp.Body.Close()
t7 := time.Now() // after read body
if t0.IsZero() {
// we skipped DNS
t0 = t1
}
// print status line and headers
printf("\n%s%s%s\n", color.GreenString("HTTP"), grayscale(14)("/"), color.CyanString("%d.%d %s", resp.ProtoMajor, resp.ProtoMinor, resp.Status))
names := make([]string, 0, len(resp.Header))
for k := range resp.Header {
names = append(names, k)
}
sort.Sort(headers(names))
for _, k := range names {
printf("%s %s\n", grayscale(14)(k+":"), color.CyanString(strings.Join(resp.Header[k], ",")))
}
if bodyMsg != "" {
printf("\n%s\n", bodyMsg)
}
fmta := func(d time.Duration) string {
return color.CyanString("%7dms", int(d/time.Millisecond))
}
fmtb := func(d time.Duration) string {
return color.CyanString("%-9s", strconv.Itoa(int(d/time.Millisecond))+"ms")
}
colorize := func(s string) string {
v := strings.Split(s, "\n")
v[0] = grayscale(16)(v[0])
return strings.Join(v, "\n")
}
fmt.Println()
switch url.Scheme {
case "https":
printf(colorize(httpsTemplate),
fmta(t1.Sub(t0)), // dns lookup
fmta(t2.Sub(t1)), // tcp connection
fmta(t6.Sub(t5)), // tls handshake
fmta(t4.Sub(t3)), // server processing
fmta(t7.Sub(t4)), // content transfer
fmtb(t1.Sub(t0)), // namelookup
fmtb(t2.Sub(t0)), // connect
fmtb(t3.Sub(t0)), // pretransfer
fmtb(t4.Sub(t0)), // starttransfer
fmtb(t7.Sub(t0)), // total
)
case "http":
printf(colorize(httpTemplate),
fmta(t1.Sub(t0)), // dns lookup
fmta(t3.Sub(t1)), // tcp connection
fmta(t4.Sub(t3)), // server processing
fmta(t7.Sub(t4)), // content transfer
fmtb(t1.Sub(t0)), // namelookup
fmtb(t3.Sub(t0)), // connect
fmtb(t4.Sub(t0)), // starttransfer
fmtb(t7.Sub(t0)), // total
)
}
if followRedirects && isRedirect(resp) {
loc, err := resp.Location()
if err != nil {
if err == http.ErrNoLocation {
// 30x but no Location to follow, give up.
return
}
log.Fatalf("unable to follow redirect: %v", err)
}
redirectsFollowed++
if redirectsFollowed > maxRedirects {
log.Fatalf("maximum number of redirects (%d) followed", maxRedirects)
}
visit(loc)
}
}
func isRedirect(resp *http.Response) bool {
return resp.StatusCode > 299 && resp.StatusCode < 400
}
func newRequest(method string, url *url.URL, body string) *http.Request {
req, err := http.NewRequest(method, url.String(), createBody(body))
if err != nil {
log.Fatalf("unable to create request: %v", err)
}
for _, h := range httpHeaders {
k, v := headerKeyValue(h)
if strings.EqualFold(k, "host") {
req.Host = v
continue
}
req.Header.Add(k, v)
}
return req
}
func createBody(body string) io.Reader {
if strings.HasPrefix(body, "@") {
filename := body[1:]
f, err := os.Open(filename)
if err != nil {
log.Fatalf("failed to open data file %s: %v", filename, err)
}
return f
}
return strings.NewReader(body)
}
// getFilenameFromHeaders tries to automatically determine the output filename,
// when saving to disk, based on the Content-Disposition header.
// If the header is not present, or it does not contain enough information to
// determine which filename to use, this function returns "".
func getFilenameFromHeaders(headers http.Header) string {
// if the Content-Disposition header is set parse it
if hdr := headers.Get("Content-Disposition"); hdr != "" {
// pull the media type, and subsequent params, from
// the body of the header field
mt, params, err := mime.ParseMediaType(hdr)
// if there was no error and the media type is attachment
if err == nil && mt == "attachment" {
if filename := params["filename"]; filename != "" {
return filename
}
}
}
// return an empty string if we were unable to determine the filename
return ""
}
// readResponseBody consumes the body of the response.
// readResponseBody returns an informational message about the
// disposition of the response body's contents.
func readResponseBody(req *http.Request, resp *http.Response) string {
if isRedirect(resp) || req.Method == http.MethodHead {
return ""
}
w := io.Discard
msg := color.CyanString("Body discarded")
if saveOutput || outputFile != "" {
filename := outputFile
if saveOutput {
// try to get the filename from the Content-Disposition header
// otherwise fall back to the RequestURI
if filename = getFilenameFromHeaders(resp.Header); filename == "" {
filename = path.Base(req.URL.RequestURI())
}
if filename == "/" {
log.Fatalf("No remote filename; specify output filename with -o to save response body")
}
}
f, err := os.Create(filename)
if err != nil {
log.Fatalf("unable to create file %s: %v", filename, err)
}
defer f.Close()
w = f
msg = color.CyanString("Body read")
}
if _, err := io.Copy(w, resp.Body); err != nil && w != io.Discard {
log.Fatalf("failed to read response body: %v", err)
}
return msg
}
type headers []string
func (h headers) String() string {
var o []string
for _, v := range h {
o = append(o, "-H "+v)
}
return strings.Join(o, " ")
}
func (h *headers) Set(v string) error {
*h = append(*h, v)
return nil
}
func (h headers) Len() int { return len(h) }
func (h headers) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h headers) Less(i, j int) bool {
a, b := h[i], h[j]
// server always sorts at the top
if a == "Server" {
return true
}
if b == "Server" {
return false
}
endtoend := func(n string) bool {
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1
switch n {
case "Connection",
"Keep-Alive",
"Proxy-Authenticate",
"Proxy-Authorization",
"TE",
"Trailers",
"Transfer-Encoding",
"Upgrade":
return false
default:
return true
}
}
x, y := endtoend(a), endtoend(b)
if x == y {
// both are of the same class
return a < b
}
return x
}