-
Notifications
You must be signed in to change notification settings - Fork 35
/
iana_special.go
94 lines (80 loc) · 2.11 KB
/
iana_special.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
package build
import (
"encoding/csv"
"io"
"os"
"strings"
"github.com/wsxiaoys/terminal/color"
)
const (
// https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml
ianaSpecialUseURL = "https://www.iana.org/assignments/special-use-domain-names/special-use-domain.csv"
ietfDataTrackerBaseURL = "https://datatracker.ietf.org/doc/"
)
// FetchSpecialUseDomainsFromIANA fetches special use domains from the IANA website.
func FetchSpecialUseDomainsFromIANA(zones map[string]*Zone, addNew bool) error {
res, err := Fetch(ianaSpecialUseURL)
if err != nil {
return err
}
defer res.Body.Close()
r := csv.NewReader(res.Body)
cols, err := r.Read()
if err != nil {
return err
}
var zonesAdded, zonesModified int
for {
row, err := r.Read()
if err == io.EOF {
break
} else if err != nil {
return err
}
rec := make(map[string]string)
for i := range cols {
rec[cols[i]] = row[i]
}
// Mangle reference into IETF data tracker URL
ref := strings.Trim(rec["Reference"], "[]")
ref = strings.Replace(ref, "RFC-ietf-", "draft-", 1)
infoURL := ietfDataTrackerBaseURL + strings.ToLower(ref)
domain := Normalize(rec["Name"])
d := domain
for d != "" {
var added, modified bool
z := zones[d]
if z == nil {
z = &Zone{Domain: d}
if addNew {
zones[d] = z
added = true
}
}
// Fix up metadata
if !z.HasMetadata() || z.ParentDomain() != "" {
if z.RegistryOperator != "IANA" {
z.RegistryOperator = "IANA"
modified = true
}
if z.InfoURL != infoURL && d == domain {
z.InfoURL = infoURL
modified = true
}
if z.AddTags("infrastructure", "closed") != 0 {
modified = true
}
}
if added {
zonesAdded++
color.Fprintf(os.Stderr, "@{g}New zone @{g!}%s@{.}\t%s\n", d, infoURL)
} else if modified && zones[d] != nil {
zonesModified++
color.Fprintf(os.Stderr, "@{y}Modified @{y!}%s@{.}\t%s\n", d, infoURL)
}
d = z.ParentDomain()
}
}
color.Fprintf(os.Stderr, "@{.}Added %d and modified %d special domain (infrastructure) zone(s)\n", zonesAdded, zonesModified)
return nil
}