-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnameservers.go
69 lines (55 loc) · 1.31 KB
/
nameservers.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
package main
import (
"fmt"
"net"
"sync"
"sync/atomic"
)
func resolveNameserver(ns string) (string, error) {
ips, err := net.LookupIP(ns)
if err != nil {
return "", fmt.Errorf("Failed to resolve nameserver: %v", err)
}
if len(ips) == 0 {
return "", fmt.Errorf("No IP addresses found for nameserver: %s", ns)
}
return ips[0].String(), nil
}
func worker(id int, jobs <-chan string, results chan<- map[string]string, wg *sync.WaitGroup) {
defer wg.Done()
for ns := range jobs {
ip, err := resolveNameserver(ns)
if err != nil {
results <- map[string]string{ns: ""}
} else {
results <- map[string]string{ns: ip}
}
atomic.AddInt32(&progress, int32(1))
}
}
func resolveNameservers(nameservers []string, numWorkers int) map[string]string {
atomic.StoreInt32(&progress, int32(0))
atomic.StoreInt32(&total, int32(len(nameservers)))
resultsMap := make(map[string]string)
jobs := make(chan string, len(nameservers))
results := make(chan map[string]string, len(nameservers))
var wg sync.WaitGroup
for i := 1; i <= numWorkers; i++ {
wg.Add(1)
go worker(i, jobs, results, &wg)
}
for _, ns := range nameservers {
jobs <- ns
}
close(jobs)
go func() {
wg.Wait()
close(results)
}()
for result := range results {
for ns, ip := range result {
resultsMap[ip] = ns
}
}
return resultsMap
}