-
Notifications
You must be signed in to change notification settings - Fork 35
/
files.go
192 lines (178 loc) · 4.31 KB
/
files.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
package build
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"github.com/wsxiaoys/terminal/color"
"golang.org/x/net/idna"
)
// BaseDir specifies the base working directory that contains the ZoneDB flat-file database.
var BaseDir = "."
// ReadZones reads the zone database from the local filesystem.
func ReadZones() (zones map[string]*Zone, errs []error) {
zones, errs = ReadZonesFile()
errs = append(errs, ReadMetadata(zones)...)
return
}
// ReadZonesFile reads and parses the zones.txt file.
func ReadZonesFile() (zones map[string]*Zone, errs []error) {
path := filepath.Join(BaseDir, "zones.txt")
f, err := os.Open(path)
if err != nil {
LogFatal(err)
}
defer f.Close()
zones = make(map[string]*Zone)
var line int
s := bufio.NewScanner(f)
for s.Scan() {
line++
d := s.Text()
n := Normalize(d)
if n != d {
err := fmt.Errorf("invalid domain name %s at %s:%d", d, path, line)
errs = append(errs, err)
LogError(err)
continue
}
if d == "" {
err := fmt.Errorf("blank line at %s:%d", path, line)
errs = append(errs, err)
LogWarning(err)
continue
}
if _, ok := zones[d]; ok {
err := fmt.Errorf("duplicate zone %s at %s:%d", d, path, line)
errs = append(errs, err)
LogWarning(err)
continue
}
z := &Zone{Domain: d}
zones[z.Domain] = z
pd := z.ParentDomain()
if pd != "" {
p, ok := zones[pd]
if !ok {
err := fmt.Errorf("missing parent %s for subdomain %s at %s:%d", pd, d, path, line)
errs = append(errs, err)
LogWarning(err)
continue
}
p.subdomains = append(p.subdomains, d)
}
}
if err := s.Err(); err != nil {
errs = append(errs, err)
LogError(err)
}
for _, z := range zones {
sort.Strings(z.subdomains)
}
color.Fprintf(os.Stderr, "@{.}Read %d zones\n", len(zones))
return
}
// ReadMetadata reads JSON files for each zone in the metadata directory.
func ReadMetadata(zones map[string]*Zone) (errs []error) {
dir := filepath.Join(BaseDir, "metadata")
paths, _ := filepath.Glob(filepath.Join(dir, "*.json"))
var read int
for _, path := range paths {
// Ensure filename equals ASCII/punycode domain name
base := filepath.Base(path)
ext := filepath.Ext(base)
di := strings.TrimSuffix(base, ext)
d, err := idna.ToUnicode(di)
if err != nil {
errs = append(errs, err)
LogError(err)
continue
}
// Ensure the domain exists in zones.txt
z, ok := zones[d]
if !ok {
err = fmt.Errorf("domain not found in zones.txt: %s", base)
errs = append(errs, err)
LogWarning(err)
continue
}
// Parse the JSON metadata
f, err := os.Open(path)
if err != nil {
err = fmt.Errorf("cannot load %s: %s", base, err)
errs = append(errs, err)
LogError(err)
continue
}
dec := json.NewDecoder(f)
err = dec.Decode(z)
f.Close()
if err != nil && err != io.EOF {
err = fmt.Errorf("unable to parse %s: %s", base, err)
errs = append(errs, err)
LogError(err)
continue
}
read++
// Ensure domain name matches
if z.Domain != d {
err = fmt.Errorf("domain %s doesn’t match filename (expected %s): %s", z.Domain, z.ASCII(), base)
errs = append(errs, err)
LogError(err)
continue
}
// Transition on load
z.transition()
}
color.Fprintf(os.Stderr, "@{.}Read %d metadata files\n", read)
return
}
// WriteZonesFile writes the zones.txt file.
func WriteZonesFile(zones map[string]*Zone) error {
domains := SortedDomains(zones)
path := filepath.Join(BaseDir, "zones.txt")
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(strings.Join(domains, "\n"))
if err != nil {
return err
}
color.Fprintf(os.Stderr, "@{.}Wrote %d zones\n", len(domains))
return nil
}
// WriteMetadata writes the metadata/*.json files.
func WriteMetadata(zones map[string]*Zone, delete bool) error {
var wrote, deleted int
for _, z := range zones {
z.Normalize()
path := filepath.Join(BaseDir, "metadata", z.ASCII()+".json")
if delete || !z.HasMetadata() {
err := os.Remove(path)
if err == nil {
deleted++
}
continue
}
f, err := os.Create(path)
if err != nil {
return err
}
b, err := json.MarshalIndent(&z, "", "\t")
if err != nil {
return err
}
f.Write(b)
f.Write([]byte("\n"))
f.Close()
wrote++
}
color.Fprintf(os.Stderr, "@{.}Wrote %d metadata files, deleted %d\n", wrote, deleted)
return nil
}