This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenroot.go
85 lines (71 loc) · 1.41 KB
/
genroot.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
//go:build ignore
// +build ignore
// This program generates docs/roots. It can be invoked by running
// go generate
package main
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"sort"
"strings"
"github.com/miekg/dns"
)
type ips struct {
ip4 net.IP
ip6 net.IP
}
var myroots = map[string]*ips{}
const (
url = "https://www.internic.net/domain/named.root"
rootsfile = "doc/roots"
)
func trimDot(s string) string {
if strings.HasSuffix(s, ".") {
s = s[:len(s)-1]
}
return s
}
func main() {
rsp, _ := http.Get(url)
defer rsp.Body.Close()
xroots, _ := ioutil.ReadAll(rsp.Body)
zp := dns.NewZoneParser(strings.NewReader(string(xroots)), "", "")
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
dom := trimDot(strings.ToLower(rr.Header().Name))
if dom != "" {
switch tt := rr.(type) {
case *dns.A:
z4, ok := myroots[dom]
if !ok {
z4 = &ips{}
}
z4.ip4 = tt.A
myroots[dom] = z4
case *dns.AAAA:
z6, ok := myroots[dom]
if !ok {
z6 = &ips{}
}
z6.ip6 = tt.AAAA
myroots[dom] = z6
}
}
}
f, err := os.Create(rootsfile)
if err != nil {
fmt.Println(err)
}
defer f.Close()
sortedKeys := make([]string, 0, len(myroots))
for k := range myroots {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
for _, k := range sortedKeys {
f.WriteString(k + " " + myroots[k].ip4.String() + " " + myroots[k].ip6.String() + "\n")
}
f.Sync()
}