-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
219 lines (187 loc) · 5.46 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
package main
import (
"crypto/tls"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"net/http/httptrace"
"os"
"strings"
"time"
"github.com/nallerooth/http-info/cert"
"github.com/nallerooth/http-info/colors"
)
func printLabelValue(indent, label, value string) {
fmt.Printf("%s%-10s: %s\n", indent, label, value)
}
func printReponseHeaders(indent string, headers map[string][]string) {
// Grab longest header name
maxHeaderName := 0
for h := range headers {
if len(h) > maxHeaderName {
maxHeaderName = len(h)
}
}
fmt.Println("Headers")
for k, values := range headers {
if len(values) == 1 {
fmt.Printf("%s%-*s: %s\n", indent, maxHeaderName, k, values[0])
} else {
fmt.Printf("%s%s\n", indent, k)
for i, v := range values {
if i < len(values)-1 {
fmt.Printf("%s ├── %s\n", indent, v)
} else {
fmt.Printf("%s └── %s\n", indent, v)
}
}
}
}
fmt.Println()
}
func transferSize(numBytes int64) string {
units := []string{"B", "KB", "MB", "GB"}
unitIndex := 0
b := float64(numBytes)
for b > 1024 && unitIndex < len(units) {
b /= 1024
unitIndex++
}
if unitIndex == 0 {
return fmt.Sprintf("%d %s", numBytes, units[unitIndex])
}
return fmt.Sprintf("%.2f %s", b, units[unitIndex])
}
func printTLSInfo(cs *tls.ConnectionState) {
indent := "\t"
fmt.Println("Certificates")
printLabelValue(indent, "ServerName", cs.ServerName)
printLabelValue(indent, "Protocol", cs.NegotiatedProtocol)
fmt.Println()
for _, c := range cs.PeerCertificates {
printLabelValue(indent, "Issuer", fmt.Sprintf("%s", c.Issuer))
printLabelValue(indent, "IsCA", fmt.Sprintf("%t", c.IsCA))
if c.IsCA == false {
if len(c.DNSNames) > 1 {
printLabelValue(indent, "DNSNames", c.DNSNames[0])
for _, name := range c.DNSNames[1:] {
printLabelValue(indent, "", name)
}
} else {
printLabelValue(indent, "DNSNames", c.DNSNames[0])
}
}
printLabelValue(indent, "Algorithm", fmt.Sprintf("%s", c.SignatureAlgorithm))
printLabelValue(indent, "NotBefore", fmt.Sprintf("%s", c.NotBefore))
notAfterRemaining := fmt.Sprintf("[ %s ]", cert.CalcRemainingDaysColor(c.NotAfter))
printLabelValue(indent, "NotAfter", fmt.Sprintf("%s %s", c.NotAfter, notAfterRemaining))
fmt.Println()
}
}
func printDNSInfo(ddi *httptrace.DNSDoneInfo) {
indent := "\t"
fmt.Println("DNS")
printLabelValue(indent, "Resolved IPs", "")
for _, ip := range ddi.Addrs {
printLabelValue(indent, "", ip.String())
}
fmt.Println()
}
func timeGet(url string) {
req, _ := http.NewRequest("GET", url, nil)
var start, connect, dns, tlsHandshake time.Time
var tlsConnectionState *tls.ConnectionState
var dnsDoneInfo *httptrace.DNSDoneInfo
var timeDNS, timeConn, timeTLS, timeTTFB time.Duration
indent := "\t"
trace := &httptrace.ClientTrace{
DNSStart: func(dsi httptrace.DNSStartInfo) { dns = time.Now() },
DNSDone: func(ddi httptrace.DNSDoneInfo) {
dnsDoneInfo = &ddi
timeDNS = time.Since(dns)
},
TLSHandshakeStart: func() { tlsHandshake = time.Now() },
TLSHandshakeDone: func(cs tls.ConnectionState, err error) {
tlsConnectionState = &cs
timeTLS = time.Since(tlsHandshake)
},
ConnectStart: func(network, addr string) { connect = time.Now() },
ConnectDone: func(network, addr string, err error) {
timeConn = time.Since(connect)
},
GotFirstResponseByte: func() {
timeTTFB = time.Since(connect)
},
}
start = time.Now()
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
res, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
log.Fatalln("Request Error:", err)
}
defer res.Body.Close()
if dnsDoneInfo.Err != nil {
log.Fatalln("DNS Error: ", dnsDoneInfo.Err)
}
printDNSInfo(dnsDoneInfo)
fmt.Println("Timings")
printLabelValue(indent, "DNS", fmt.Sprintf("%v", timeDNS))
printLabelValue(indent, "Connect", fmt.Sprintf("%v", timeConn))
printLabelValue(indent, "TLS", fmt.Sprintf("%v", timeTLS))
printLabelValue(indent, "TTFB", fmt.Sprintf("%v", timeTTFB))
printLabelValue(indent, "Total", fmt.Sprintf("%v", time.Since(start)))
fmt.Println()
fmt.Println("Transfer")
// Write response to /dev/null and count number of bytes written
dn, err := os.OpenFile(os.DevNull, os.O_WRONLY, os.FileMode(fs.ModeAppend))
if err != nil {
log.Fatalf("Error opening DevNull (%s): %s", os.DevNull, err)
}
defer dn.Close()
numBytes, err := io.Copy(dn, res.Body)
colorFn := colors.White
switch {
case res.StatusCode >= 400:
colorFn = colors.Red
case res.StatusCode >= 300:
colorFn = colors.Yellow
case res.StatusCode >= 200:
colorFn = colors.Green
case res.StatusCode >= 100:
colorFn = colors.Purple
}
printLabelValue(indent, "Status", colorFn(res.Status))
redirects := map[int]bool{
301: true,
302: true,
303: true,
307: true,
}
if redirects[res.StatusCode] && res.Header.Get("Location") != "" {
printLabelValue(indent, "Redirect", colors.Green(res.Header.Get("Location")))
}
printLabelValue(indent, "Bytes", transferSize(numBytes))
printLabelValue(indent, "Compressed", fmt.Sprintf("%t", res.Uncompressed))
if len(res.TransferEncoding) > 0 {
printLabelValue(indent, "Encoding", strings.Join(res.TransferEncoding, ", "))
}
fmt.Println()
// TODO: Check flag if headers are wanted
printReponseHeaders(indent, res.Header)
if tlsConnectionState != nil {
printTLSInfo(tlsConnectionState)
}
}
func main() {
args := os.Args[1:]
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "Usage: %s https://domain.com\n", os.Args[0])
os.Exit(1)
}
url := args[0]
fmt.Println()
timeGet(url)
fmt.Println("Done")
}