-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
370 lines (306 loc) · 7.92 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
/*
CDN Test is a program intended to be distributed to the users of a service,
in order to help debug connection issues to a CDN (which frequently seem to
involve ISPs with congested routes)
It is currently based mainly around the debugging of CloudFront requests,
which helpfully already provide a source of helpful information:
http://d7uri8nf7uskq.cloudfront.net/CustomerTesting.html
Rather than require a user to be quite technically competent and install or use
multiple tools, cdntest aims to reduce the work required by the end user to
downloading a single tool, and running it with an endpoint file to download,
then producing output in a file (and on the console) that the user can attach
to a report.
*/
package main
import (
"bufio"
"crypto/md5"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"time"
"github.com/urfave/cli"
)
const ipify = "https://api.ipify.org"
var transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
ResponseHeaderTimeout: time.Second * 10,
}
// Use a custom http client to ensure sensible timeouts
var DefaultClient = &http.Client{
Timeout: 10 * time.Second,
Transport: transport,
}
func getPort(scheme string) (int, error) {
switch scheme {
case "http":
return 80, nil
case "https":
return 443, nil
default:
return 0, fmt.Errorf("Expected http or https: \"%v\"", scheme)
}
}
func getMyPublicIP() (string, error) {
response, err := DefaultClient.Get(ipify)
if err != nil {
return "", fmt.Errorf(
"Could not contact %v to get your public IP: %v",
ipify,
err,
)
}
defer response.Body.Close()
b, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf(
"Could not contact %v to get your public IP: %v",
ipify,
err,
)
}
return string(b), nil
}
func main() {
app := cli.NewApp()
app.Author = "Daniel Robert Speed"
app.Usage = `
Used for testing HTTP(S) endpoints for their SSL certificates and performance.
See: https://github.com/Redundancy/cdntest
`
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "ip",
Usage: "Use an explicit IP address rather than DNS provided",
},
cli.StringFlag{
Name: "cname",
Usage: "Use an alternative DNS entry to lookup the IP",
},
cli.StringFlag{
Name: "out",
Usage: "Save the content body to a file",
},
}
app.Action = func(c *cli.Context) error {
if c.NArg() != 1 {
return fmt.Errorf("Need a URL argument")
}
return testHTTP(options{
url: c.Args()[0],
ip: c.String("ip"),
cname: c.String("cname"),
outfile: c.String("out"),
})
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
}
type options struct {
url string
ip string
cname string
outfile string
}
func testHTTP(o options) error {
cdnURL, err := url.Parse(o.url)
if err != nil {
return fmt.Errorf("Could not parse url: \"%v\" - %v\n", cdnURL, err)
}
port, err := getPort(cdnURL.Scheme)
if err != nil {
return err
}
fmt.Println("Running CDN test at", time.Now())
myPublicIP, err := getMyPublicIP()
if err != nil {
return err
}
fmt.Println("My public IP is:", myPublicIP)
var addrs []string
host := cdnURL.Host
if o.cname != "" {
host = o.cname
}
beforeLookup := time.Now()
addrs, err = net.LookupHost(host)
fmt.Printf("Host lookup of %v took %v\n", host, time.Now().Sub(beforeLookup))
if err != nil {
return fmt.Errorf("Could not look up host: %v", err)
}
fmt.Println("DNS returned:", addrs)
/*
resolver-identity.cloudfront.net reflects back the IP of the dns server that
looked it up.
*/
resolverIdentities, _ := net.LookupHost("resolver-identity.cloudfront.net")
if len(resolverIdentities) > 0 {
resolver := ""
if resolverNames, _ := net.LookupAddr(resolverIdentities[0]); len(resolverNames) > 0 {
resolver = resolverNames[0]
}
fmt.Printf(
"Your DNS resolver is %v (%v)\n",
resolver,
resolverIdentities[0],
)
} else {
fmt.Printf("Could not identify your DNS resolver\n")
}
request, _ := http.NewRequest("GET", cdnURL.String(), nil)
// We cannot use http.Transport because it does not provide hooks
// to get the timing metrics that we require
// TODO: Proxy support based on environment variables
var conn net.Conn
var dialErr error
ip := addrs[0]
if o.ip != "" {
ip = o.ip
}
beforeDialTime := time.Now()
conn, dialErr = net.DialTimeout(
"tcp",
fmt.Sprintf("%v:%v", ip, port),
10*time.Second,
)
afterDialTime := time.Now()
defer func() {
if conn != nil {
conn.Close()
}
}()
if dialErr != nil {
return fmt.Errorf("Could not contact server: %v", dialErr)
}
fmt.Println(
"Connected to",
conn.RemoteAddr().String(),
"in",
afterDialTime.Sub(beforeDialTime),
)
if cdnURL.Scheme == "https" {
config := &tls.Config{
ServerName: cdnURL.Host,
}
beforeHandshakeTime := time.Now()
tlsCon := tls.Client(conn, config)
tlsCon.SetDeadline(time.Now().Add(10 * time.Second))
if hsErr := tlsCon.Handshake(); hsErr != nil {
fmt.Println("SSL handshake error:", hsErr)
}
fmt.Println("TLS handshake in", time.Now().Sub(beforeHandshakeTime))
if e := tlsCon.VerifyHostname(cdnURL.Host); e != nil {
fmt.Println("SSL was unable to verify the hostname!", e)
}
printableCertChain(
tlsCon.ConnectionState().PeerCertificates,
).Write(os.Stdout)
conn = tlsCon
}
request.WriteProxy(conn)
beforeRead := time.Now()
response, err := http.ReadResponse(bufio.NewReader(conn), request)
if err != nil || response == nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
return fmt.Errorf(
"Timeout reading response from %v",
cdnURL.String(),
)
}
return fmt.Errorf(
"Did not get a response (after establishing connection), server behind loadbalancer / CDN may be down: %v",
err,
)
}
if response.Body != nil {
defer response.Body.Close()
}
fmt.Println("Response was", response.Status)
if response.Body != nil {
var bodyWriters []io.Writer
md5 := md5.New()
var writtenBytes int64
bodyWriters = append(bodyWriters, md5)
if o.outfile != "" {
f, openFileErr := os.OpenFile(o.outfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if openFileErr != nil {
return fmt.Errorf("Unable to open %v for writing: %v", o.outfile, openFileErr)
}
defer func() {
errClose := f.Close()
if err != nil {
fmt.Println(errClose)
return
}
fmt.Println("Wrote", o.outfile)
}()
bodyWriters = append(bodyWriters, f)
}
writtenBytes, err = io.Copy(io.MultiWriter(bodyWriters...), response.Body)
if err != nil {
return fmt.Errorf("Error reading content body: %v", err)
}
timeTaken := time.Now().Sub(beforeRead)
bps := bytesPerSecond((float64(time.Second) / float64(timeTaken)) * float64(writtenBytes))
fmt.Printf(
"Reading %v bytes took %v (%v)\n",
writtenBytes,
timeTaken,
bps,
)
fmt.Printf("MD5 of Content: %x\n", md5.Sum(nil))
}
if response.Header.Get("X-Amz-Cf-Id") != "" {
fmt.Println("Amazon CloudFront Request ID:", response.Header.Get("X-Amz-Cf-Id"))
// lookup AWS DNS server
nameserver, _ := net.LookupHost("identity.cloudfront.net")
names, _ := net.LookupAddr(nameserver[0])
fmt.Printf(
"The CloudFront DNS server you query is %v (%v)\n",
names[0],
nameserver[0],
)
}
return nil
}
type printableCertChain []*x509.Certificate
func (cc printableCertChain) Write(o io.Writer) {
fmt.Fprintln(o, "SSL Certificate Information")
for _, cert := range cc {
if cert.IsCA {
fmt.Fprintf(
o,
"\t%v - Valid Until: %v\n",
cert.Subject.CommonName,
cert.NotAfter,
)
} else {
fmt.Fprintf(
o,
"\t%v - Issued by: %v, Valid Until: %v\n",
cert.Subject.CommonName,
cert.Issuer.CommonName,
cert.NotAfter,
)
}
}
}
type bytesPerSecond int64
func (i bytesPerSecond) String() string {
switch {
case i < 1000:
return fmt.Sprintf("%v bytes/s", int64(i))
case i < 1000*1000:
return fmt.Sprintf("%v KiB/s", int64(i)/1000)
default:
return fmt.Sprintf("%v MiB/s", int64(i)/1000000)
}
}