-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
119 lines (101 loc) · 2.69 KB
/
main.go
File metadata and controls
119 lines (101 loc) · 2.69 KB
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
package main
import (
"context"
"fmt"
"net"
"os"
"sort"
"strings"
"sync"
"time"
)
type DNSServer struct {
Name string
City string
State string
IP string
}
var dnsServers = []DNSServer{
{"Cloudflare", "San Francisco", "CA", "1.1.1.1"},
{"Google", "Mountain View", "CA", "8.8.8.8"},
{"Quad9", "Berkeley", "CA", "9.9.9.9"},
{"OpenDNS", "San Francisco", "CA", "208.67.222.222"},
{"Cloudflare Secondary", "Los Angeles", "CA", "1.0.0.1"},
{"Level3", "Broomfield", "CO", "209.244.0.3"},
{"Verisign", "Reston", "VA", "64.6.64.6"},
{"Oracle Dyn", "Manchester", "NH", "216.146.35.35"},
{"CenturyLink", "Monroe", "LA", "205.171.3.65"},
{"SafeDNS", "New York", "NY", "195.46.39.39"},
{"Google Secondary", "Council Bluffs", "IA", "8.8.4.4"},
{"AdGuard", "Chicago", "IL", "94.140.14.14"},
{"CleanBrowsing", "Los Angeles", "CA", "185.228.168.9"},
{"OpenDNS Secondary", "Seattle", "WA", "208.67.220.220"},
{"Quad9 Secondary", "Miami", "FL", "149.112.112.112"},
}
type Result struct {
Server DNSServer
Success bool
IPs []string
Error error
}
func checkDNS(hostname string, server DNSServer, timeout time.Duration) Result {
resolver := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: timeout,
}
return d.DialContext(ctx, network, server.IP+":53")
},
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ips, err := resolver.LookupHost(ctx, hostname)
return Result{
Server: server,
Success: err == nil && len(ips) > 0,
IPs: ips,
Error: err,
}
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s <hostname>\n", os.Args[0])
os.Exit(1)
}
hostname := os.Args[1]
timeout := 2 * time.Second
fmt.Printf("\nChecking DNS resolution for: %s\n", hostname)
fmt.Println("=" + strings.Repeat("=", 50))
var wg sync.WaitGroup
results := make(chan Result, len(dnsServers))
for _, server := range dnsServers {
wg.Add(1)
go func(srv DNSServer) {
defer wg.Done()
results <- checkDNS(hostname, srv, timeout)
}(server)
}
go func() {
wg.Wait()
close(results)
}()
// Collect all results
var allResults []Result
for result := range results {
allResults = append(allResults, result)
}
// Sort results by city name for consistent output
sort.Slice(allResults, func(i, j int) bool {
return allResults[i].Server.City < allResults[j].Server.City
})
// Display results
for _, result := range allResults {
icon := "❌"
if result.Success {
icon = "✅"
}
location := fmt.Sprintf("%s, %s", result.Server.City, result.Server.State)
fmt.Printf("%s %-25s (%s)\n", icon, location, result.Server.Name)
}
}