-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (88 loc) · 1.88 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"github.com/miekg/dns"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run parser.go <input_folder>")
os.Exit(1)
}
inputFolder := os.Args[1]
files, err := os.ReadDir(inputFolder)
if err != nil {
fmt.Println("Error reading directory:", err)
os.Exit(1)
}
var wg sync.WaitGroup
fileChan := make(chan string, 10)
domainChan := make(chan string, 1000)
done := make(chan struct{})
go func() {
outputFile, err := os.Create("domains.txt")
if err != nil {
fmt.Println("Error creating output file:", err)
os.Exit(1)
}
defer outputFile.Close()
domainSet := make(map[string]struct{})
writer := bufio.NewWriter(outputFile)
for domain := range domainChan {
if _, exists := domainSet[domain]; !exists {
domainSet[domain] = struct{}{}
fmt.Fprintln(writer, domain)
}
}
writer.Flush()
close(done)
}()
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for filePath := range fileChan {
parseFile(filePath, domainChan)
}
}()
}
for _, file := range files {
if !file.IsDir() {
fullPath := filepath.Join(inputFolder, file.Name())
fileChan <- fullPath
}
}
close(fileChan)
wg.Wait()
close(domainChan)
<-done
}
func parseFile(filePath string, domainChan chan<- string) {
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
parser := dns.NewZoneParser(file, "", "")
for rr, ok := parser.Next(); ok; rr, ok = parser.Next() {
if rr == nil {
continue
}
domain := rr.Header().Name
if strings.Contains(domain, "*") {
continue
}
domain = strings.TrimSuffix(domain, ".")
domain = strings.ReplaceAll(domain, "_", "")
domainChan <- domain
}
if err := parser.Err(); err != nil && err != io.EOF {
fmt.Println("Error parsing file:", err)
}
}