-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (165 loc) · 3.5 KB
/
main.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
package main
import (
"compress/gzip"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/cheggaaa/pb"
"zf-analysis/zoneparse"
"zf-analysis/zoneparse/comparse"
)
var (
loadDone = make(chan bool)
inputChan = make(chan string)
work sync.WaitGroup
zones []ZoneInfo
directory = flag.String("directory", "", "directory with zone files")
verbose = flag.Bool("verbose", false, "enable verbose logging")
pbar = flag.Bool("progress", false, "enable progress bar")
parallel = flag.Uint("parallel", 2, "number of zones to process in parallel")
)
type ZoneInfo struct {
SOA string
Count uint
}
func v(format string, v ...interface{}) {
if *verbose {
log.Printf(format, v...)
}
}
func checkFlags() {
flag.Parse()
if len(*directory) == 0 {
log.Printf("must pass directory (e.g. /data/domains/2019/02/01/)")
goto FlagError
}
if *parallel < 1 {
log.Printf("parallel must be positive")
goto FlagError
}
return
FlagError:
flag.PrintDefaults()
os.Exit(1)
}
func loadFilesToProcess(files []string) {
for _, file := range files {
work.Add(1)
inputChan <- file
}
loadDone <- true
}
func worker(bar *pb.ProgressBar) {
for {
file, more := <-inputChan
if more {
if *pbar {
bar.Increment()
} else {
log.Printf("Processing zone %s", file)
}
makeDomainsFile(file)
work.Done()
} else {
// done
return
}
}
}
func makeDomainsFile(zonefile string) {
// Special case com.zone file
if strings.Contains(zonefile, "com.zone.gz") {
soa, count := comparse.Parse(zonefile)
zones = append(zones, ZoneInfo{
SOA: soa,
Count: count,
})
return
}
stream, err := os.Open(zonefile)
if err != nil {
log.Printf("ERR: %s not found; skipping", zonefile)
return
}
defer stream.Close()
gz, err := gzip.NewReader(stream)
if err != nil {
log.Fatal(err)
}
defer gz.Close()
var record zoneparse.Record
scanner := zoneparse.NewScanner(gz)
stuff := make(map[string]struct{})
var zone ZoneInfo
for {
err := scanner.Next(&record)
if err != nil {
if err == io.EOF {
break
}
//log.Println(err)
continue
}
v("a '%s' Record for domain/subdomain '%s'\n",
record.Type,
record.DomainName,
)
if fmt.Sprintf("%s", record.Type) == "SOA" {
zone.SOA = record.DomainName
}
stuff[strings.TrimRight(record.DomainName, ".")] = struct{}{}
}
zone.Count = uint(len(stuff))
zones = append(zones, zone)
outputFile, err := os.Create(strings.TrimSuffix(zonefile, ".gz") + "_domains.gz")
if err != nil {
log.Fatal(err)
}
gzw := gzip.NewWriter(outputFile)
defer gzw.Close()
for elem := range stuff {
_, _ = gzw.Write([]byte(elem + "\n"))
}
stuff = nil
// Yes, forcing gc locks program, but worth the time delay for memory save.
// some zone file can be quite large.
runtime.GC()
}
func writeStatsFile() {
f, err := os.Create(*directory + "stats")
if err != nil {
log.Fatal(err)
}
defer f.Close()
for _, zone := range zones {
f.WriteString(fmt.Sprintf("SOA: %20s\tNum.Domains: %d\n", zone.SOA, zone.Count))
}
f.Sync()
}
func main() {
checkFlags()
matches, err := filepath.Glob(*directory + "*.txt.gz")
if err != nil {
log.Fatal(err)
}
// add com and org
matches = append(matches, []string{*directory + "com.zone.gz", *directory + "org.zone.gz"}...)
bar := pb.New(len(matches))
if *pbar {
bar.Start()
}
go loadFilesToProcess(matches)
v("starting %d parallel processing", *parallel)
for i := uint(0); i < *parallel; i++ {
go worker(bar)
}
<-loadDone
work.Wait()
writeStatsFile()
}