-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
332 lines (308 loc) · 8.8 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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/nextdns/diag/traceroute"
"github.com/nextdns/nextdns/host"
)
type Report struct {
Contact string `json:",omitempty"`
HasV6 bool
Resolvers []string
Test Test
ULLPrimary *Ping `json:",omitempty"`
ULLSecondary *Ping `json:",omitempty"`
ULLPrimary6 *Ping `json:",omitempty"`
ULLSecondary6 *Ping `json:",omitempty"`
Primary *Ping `json:",omitempty"`
Secondary *Ping `json:",omitempty"`
Primary6 *Ping `json:",omitempty"`
Secondary6 *Ping `json:",omitempty"`
Top []Ping `json:",omitempty"`
ULLPrimaryTraceroute []traceroute.Hop `json:",omitempty"`
ULLSecondaryTraceroute []traceroute.Hop `json:",omitempty"`
ULLPrimaryTraceroute6 []traceroute.Hop `json:",omitempty"`
ULLSecondaryTraceroute6 []traceroute.Hop `json:",omitempty"`
PrimaryTraceroute []traceroute.Hop `json:",omitempty"`
SecondaryTraceroute []traceroute.Hop `json:",omitempty"`
PrimaryTraceroute6 []traceroute.Hop `json:",omitempty"`
SecondaryTraceroute6 []traceroute.Hop `json:",omitempty"`
}
type Test struct {
Status string
Protocol string `json:",omitempty"`
Client string `json:",omitempty"`
Resolver string `json:",omitempty"`
SrcIP string `json:",omitempty"`
DestIP string `json:",omitempty"`
Server string `json:",omitempty"`
}
func (p Test) String() string {
var sb strings.Builder
fmt.Fprintf(&sb, "status: %s\n", p.Status)
fmt.Fprintf(&sb, "client: %s\n", p.Client)
if p.Protocol != "" {
fmt.Fprintf(&sb, "protocol: %s\n", p.Protocol)
fmt.Fprintf(&sb, "dest IP: %s\n", p.DestIP)
fmt.Fprintf(&sb, "server: %s", p.Server)
} else {
fmt.Fprintf(&sb, "resolver: %s", p.Resolver)
}
return sb.String()
}
type Ping struct {
Pop string `json:",omitempty"`
Protocol int
RTT time.Duration
}
func (p Ping) String() string {
if p.Protocol == 6 {
return fmt.Sprintf("%s (IPv6): %s", p.Pop, p.RTT)
}
return fmt.Sprintf("%s: %s", p.Pop, p.RTT)
}
type RouterTarget struct {
IPs []string
}
func main() {
if runtime.GOOS == "windows" {
fmt.Println("")
fmt.Println("Welcome to NextDNS network diagnostic tool.")
fmt.Println("")
fmt.Println("This tool will capture latency and routing information regarding")
fmt.Println("the connectivity of your network with NextDNS.")
fmt.Println("")
fmt.Println("The source code of this tool is available at https://github.com/nextdns/diag")
fmt.Println("")
fmt.Println("Do you want to continue? (press enter to accept)")
fmt.Scanln()
}
var r Report
if r.Resolvers = host.DNS(); len(r.Resolvers) > 0 {
fmt.Println("Resolvers: ", strings.Join(r.Resolvers, ", "))
net.DefaultResolver.PreferGo = true
d := &net.Dialer{}
net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
return d.DialContext(ctx, network, net.JoinHostPort(r.Resolvers[0], "53"))
}
}
r.HasV6 = hasIPv6()
r.Test = test()
r.ULLPrimary = pop("ultra low latency primary IPv4", "ipv4.dns1.nextdns.io")
r.ULLSecondary = pop("ultra low latency secondary IPv4", "ipv4.dns2.nextdns.io")
r.Primary = pop("anycast primary IPv4", "45.90.28.0")
r.Secondary = pop("anycast secondary IPv4", "45.90.30.0")
if r.HasV6 {
r.ULLPrimary6 = pop("ultra low latency primary IPv6", "ipv6.dns1.nextdns.io")
r.ULLSecondary6 = pop("ultra low latency secondary IPv6", "ipv6.dns2.nextdns.io")
r.Primary6 = pop("anycast primary IPv6", "2a07:a8c0::")
r.Secondary6 = pop("anycast secondary IPv6", "2a07:a8c1::")
}
r.Top = pings(r.HasV6)
r.ULLPrimaryTraceroute = trace("ultra low latency primary IPv4", "ipv4.dns1.nextdns.io")
r.ULLSecondaryTraceroute = trace("ultra low latency secondary IPv4", "ipv4.dns2.nextdns.io")
r.PrimaryTraceroute = trace("anycast primary IPv4", "45.90.28.0")
r.SecondaryTraceroute = trace("anycast secondary IPv4", "45.90.30.0")
if r.HasV6 {
r.ULLPrimaryTraceroute6 = trace("ultra low latency primary IPv6", "ipv6.dns1.nextdns.io")
r.ULLSecondaryTraceroute6 = trace("ultra low latency secondary IPv6", "ipv6.dns2.nextdns.io")
r.PrimaryTraceroute6 = trace("anycast primary IPv6", "2a07:a8c0::")
r.SecondaryTraceroute6 = trace("anycast secondary IPv6", "2a07:a8c1::")
}
fmt.Print("Do you want to send this report? [Y/n]: ")
var resp string
fmt.Scanln(&resp)
if resp != "" && resp[0] != 'y' && resp[0] != 'Y' {
return
}
fmt.Print("Optional email in case we need additional info: ")
fmt.Scanln(&r.Contact)
b, err := json.Marshal(r)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Print("Posting...\r")
req, _ := http.NewRequest("POST", "https://api.nextdns.io/diagnostic", bytes.NewBuffer(b))
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("Post unsuccessful: %v\n", err)
fmt.Println("Please report this issue on https://github.com/nextdns/diag")
os.Exit(1)
}
if res.StatusCode != http.StatusOK {
fmt.Printf("Post unsuccessful: status %d\n", res.StatusCode)
_, _ = io.Copy(os.Stderr, res.Body)
os.Exit(1)
}
result := struct {
ID string
}{}
j := json.NewDecoder(res.Body)
_ = j.Decode(&result)
fmt.Printf("Posted: https://nextdns.io/diag/%s\n", result.ID)
if runtime.GOOS == "windows" {
fmt.Scanln()
}
}
func hasIPv6() bool {
fmt.Println("Testing IPv6 connectivity")
c, err := net.Dial("tcp", "[2620:fe::fe]:443")
if c != nil {
c.Close()
}
v6 := err == nil
fmt.Printf(indent("available: %v\n"), v6)
return v6
}
func trace(name string, dest string) []traceroute.Hop {
ip := net.ParseIP(dest)
if ip == nil {
ips, err := net.DefaultResolver.LookupIP(context.Background(), "ip", dest)
if err != nil {
fmt.Printf(indent("Traceroute error: %v\n"), err)
return nil
}
if len(ips) == 0 {
fmt.Print(indent("Traceroute error: no IP for host\n"))
return nil
}
ip = ips[0]
}
fmt.Printf("Traceroute for %s (%s)\n", name, ip)
var t traceroute.Tracer
c := make(chan traceroute.Hop)
var hops []traceroute.Hop
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for hop := range c {
hops = append(hops, hop)
fmt.Println(indent(hop.String()))
}
}()
err := t.Trace(context.Background(), ip, c)
if err != nil {
fmt.Printf(indent("error: %v\n"), err)
}
close(c)
wg.Wait()
return hops
}
func test() Test {
fmt.Println("Fetching https://test.nextdns.io")
req, _ := http.NewRequest("GET", "https://test.nextdns.io", nil)
req.Header.Set("User-Agent", "curl")
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf(indent("Fetch error: %v\n"), err)
return Test{}
}
defer res.Body.Close()
var t Test
j := json.NewDecoder(res.Body)
if err := j.Decode(&t); err != nil {
fmt.Printf(indent("Cannot decode response: %v\n"), err)
}
if t.Client == "" {
t.Client, t.SrcIP = t.SrcIP, ""
}
fmt.Println(indent(t.String()))
return t
}
func pop(name, target string) *Ping {
fmt.Printf("Fetching PoP name for %s (%s)\n", name, target)
req, _ := http.NewRequest("GET", "https://dns.nextdns.io/info", nil)
cl := http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network string, addr string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, network, net.JoinHostPort(target, "443"))
},
},
}
res, err := cl.Do(req)
if err != nil {
fmt.Printf("Fetch error: %v\n", err)
return &Ping{
Pop: "err: " + err.Error(),
Protocol: 0,
RTT: 0,
}
}
defer res.Body.Close()
var p Ping
j := json.NewDecoder(res.Body)
if err := j.Decode(&p); err != nil {
fmt.Printf(indent("Cannot decode response: %v\n"), err)
}
p.RTT *= 1000
fmt.Println(indent(p.String()))
return &p
}
func pings(v6 bool) []Ping {
fmt.Println("Pinging PoPs")
res, err := http.Get("https://router.nextdns.io/?limit=10&stack=dual")
if err != nil {
fmt.Printf(indent("error: %v\n"), err)
return nil
}
defer res.Body.Close()
var targets []RouterTarget
j := json.NewDecoder(res.Body)
if err := j.Decode(&targets); err != nil {
fmt.Printf(indent("Cannot decode response: %v\n"), err)
return nil
}
c := make(chan Ping)
var total int
for _, t := range targets {
for _, ip := range t.IPs {
if !v6 && strings.IndexByte(ip, ':') != -1 {
continue
}
total++
go func(ip string) {
c <- ping(ip)
}(ip)
}
}
var ps []Ping
for ; total > 0; total-- {
if p := <-c; p.Pop != "" {
fmt.Println(indent(p.String()))
ps = append(ps, p)
}
}
return ps
}
func ping(ip string) (p Ping) {
p.Protocol = 4
if net.ParseIP(ip).To4() == nil {
p.Protocol = 6
}
res, err := http.Get("http://" + net.JoinHostPort(ip, "80") + "/info")
if err != nil {
return p
}
defer res.Body.Close()
j := json.NewDecoder(res.Body)
_ = j.Decode(&p)
p.RTT *= 1000
return p
}
func indent(s string) string {
return " " + strings.TrimRight(strings.ReplaceAll(s, "\n", "\n "), " ")
}