-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
307 lines (256 loc) · 10.1 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"bufio"
"encoding/csv"
//"fmt"
"os"
"os/exec"
"strings"
"sync"
"github.com/schollz/progressbar/v3"
"regexp"
"path/filepath"
"flag"
"log"
)
func digNS(domain string, mu *sync.Mutex) map[string][]string {
nsRegex := regexp.MustCompile(`\S+\s+IN\s+NS\s+(\S+)`) // 匹配 NS 记录的正则表达式
// 执行 dig 查询 NS 记录,并获取 ADDITIONAL 部分的 A 和 AAAA 记录
cmd := exec.Command("dig", domain, "NS", "+additional")
output, err := cmd.Output()
if err != nil {
log.Printf("[Error] Failed to execute dig command for domain %s: %v\n", domain, err)
return nil
}
nsRecords := make(map[string][]string)
scanner := bufio.NewScanner(strings.NewReader(string(output)))
parsingAdditional := false
for scanner.Scan() {
line := scanner.Text()
// 找到 ADDITIONAL SECTION
if strings.Contains(line, ";; ADDITIONAL SECTION:") {
parsingAdditional = true
//continue
}
// 解析 ANSWER 部分中的 NS 记录
if nsRegex.MatchString(line){
matches := nsRegex.FindStringSubmatch(line)
if len(matches) > 1 && !strings.Contains(line, "9.18.18-0ubuntu0.22.04.2-Ubuntu") {
ns := matches[1]
//println(ns,":",line)
nsRecords[ns] = []string{} // 初始化 NS 记录
}
}
// 解析 ADDITIONAL 部分中的 A 和 AAAA 记录
if parsingAdditional {
log.Printf("[DEBUG] Additional.\n")
if strings.Contains(line, " IN A ") || strings.Contains(line, " IN AAAA ") {
parts := strings.Fields(line)
if len(parts) >= 5 {
ns := parts[0]
ip := parts[4]
if _, exists := nsRecords[ns]; exists {
nsRecords[ns] = append(nsRecords[ns], ip)
log.Printf("[DEBUG] Found IP %s for NS %s of domain %s\n", ip, ns, domain)
}
}
}
}
}
return nsRecords
}
func digIP(ns string, mu *sync.Mutex) ([]string, []string) {
var ipv4Records []string
var ipv6Records []string
// 查询 A 记录(IPv4)
cmd := exec.Command("dig", ns, "A", "+short")
output, err := cmd.Output()
if err != nil {
log.Printf("[Error] Failed to execute dig command for A record of NS %s: %v\n", ns, err)
} else {
scanner := bufio.NewScanner(strings.NewReader(string(output)))
for scanner.Scan() {
ipRecord := strings.TrimSpace(scanner.Text())
//println(ns, ", ", ipRecord)
if ipRecord != "" {
ipv4Records = append(ipv4Records, ipRecord)
}else {
ipv4Records = append(ipv4Records, "")
}
}
}
// 查询 AAAA 记录(IPv6)
cmd = exec.Command("dig", ns, "AAAA", "+short")
output, err = cmd.Output()
if err != nil {
log.Printf("[Error] Failed to execute dig command for AAAA record of NS %s: %v\n", ns, err)
} else {
scanner := bufio.NewScanner(strings.NewReader(string(output)))
for scanner.Scan() {
ipRecord := strings.TrimSpace(scanner.Text())
if ipRecord != "" {
ipv6Records = append(ipv6Records, ipRecord)
} else {
ipv6Records = append(ipv6Records, "")
}
}
}
return ipv4Records, ipv6Records
}
func processDomain(domain string, mu *sync.Mutex, resultsIPv4 *[][]string, resultsIPv6 *[][]string, bar *progressbar.ProgressBar, sem chan struct{}) {
defer func() { <-sem }() // 释放一个占用的通道
nsRecords := digNS(domain, mu)
var domainNSIPv4List [][]string
var domainNSIPv6List [][]string
if len(nsRecords) > 0 {
for ns := range nsRecords {
//println(ns)
ipv4Records, ipv6Records := digIP(ns, mu) // 查询 IPv4 和 IPv6 地址
//println("124: ", ns,": ",ipv4Records)
if len(ipv4Records) == 0 {
domainNSIPv4List = append(domainNSIPv4List, []string{domain, ns, ""}) // 保存空 IP 的记录
} else {
for _, ip := range ipv4Records {
//println("126: ", ns)
domainNSIPv4List = append(domainNSIPv4List, []string{domain, ns, ip}) // 保存 IPv4 地址
}
}
if len(ipv6Records) == 0 {
domainNSIPv6List = append(domainNSIPv4List, []string{domain, ns, ""}) // 保存空 IP 的记录
} else {
for _, ip := range ipv6Records {
domainNSIPv6List = append(domainNSIPv6List, []string{domain, ns, ip}) // 保存 IPv6 地址
}
}
}
} else {
domainNSIPv4List = append(domainNSIPv4List, []string{domain, "", ""}) // 没有 NS 记录
domainNSIPv6List = append(domainNSIPv6List, []string{domain, "", ""})
}
mu.Lock()
*resultsIPv4 = append(*resultsIPv4, domainNSIPv4List...)
*resultsIPv6 = append(*resultsIPv6, domainNSIPv6List...)
mu.Unlock()
bar.Add(1)
}
func processFile(filename string, resultsIPv4 chan<- [][]string, resultsIPv6 chan<- [][]string, maxThreads int, cacheMutex *sync.Mutex, wgMain *sync.WaitGroup) {
defer wgMain.Done()
file, err := os.Open(filename)
if err != nil {
log.Println("Error opening file:", err)
return
}
defer file.Close()
reader := csv.NewReader(file)
domains, err := reader.ReadAll()
if err != nil {
log.Println("Error reading CSV:", err)
return
}
var wg sync.WaitGroup
var mu sync.Mutex
chunkResultsIPv4 := make([][]string, 0)
chunkResultsIPv6 := make([][]string, 0)
bar := progressbar.NewOptions(len(domains)-1,
progressbar.OptionSetDescription("Processing domains in "+filename),
progressbar.OptionSetPredictTime(true),
progressbar.OptionShowCount(),
progressbar.OptionFullWidth(),
progressbar.OptionShowIts(),
)
sem := make(chan struct{}, maxThreads)
for _, domain := range domains[1:] { // 跳过标题行
wg.Add(1)
sem <- struct{}{}
go func(domain string) {
defer wg.Done()
processDomain(domain, &mu, &chunkResultsIPv4, &chunkResultsIPv6, bar, sem)
}(domain[0])
}
wg.Wait()
bar.Finish()
log.Printf("[INFO] Sending %d IPv4 records and %d IPv6 records to results channels from file %s\n", len(chunkResultsIPv4), len(chunkResultsIPv6), filename)
resultsIPv4 <- chunkResultsIPv4 // 确保所有 IPv4 结果发送到通道
resultsIPv6 <- chunkResultsIPv6 // 确保所有 IPv6 结果发送到通道
}
func main() {
FilePath := flag.String("dir", "chunk-data", "The directory containing chunk files")
flag.Parse() // 解析命令行参数
fileNameWithExt := filepath.Base(*FilePath)
// 使用命令行传入的目录路径
//files, err := filepath.Glob(filepath.Join(*chunkDir, "chunk*.csv"))
files := []string{*FilePath}
errorLogFile, err := os.OpenFile("output/error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Failed to open error log file: %v", err)
}
defer errorLogFile.Close()
log.SetOutput(errorLogFile)
resultsIPv4 := make(chan [][]string)
resultsIPv6 := make(chan [][]string)
finalResultsIPv4 := make([][]string, 0)
finalResultsIPv6 := make([][]string, 0)
var cacheMutex sync.Mutex // cacheMutex 虽然不再需要 cache,但仍需要锁来处理并发
var wgMain sync.WaitGroup
var finalResultsMutex sync.Mutex
var resultsWaitGroup sync.WaitGroup
resultsWaitGroup.Add(2) // 等待两个通道的处理完成
// 处理 IPv4 结果
go func() {
defer resultsWaitGroup.Done()
for res := range resultsIPv4 {
finalResultsMutex.Lock()
log.Printf("[INFO] Received %d IPv4 records, processing...\n", len(res))
finalResultsIPv4 = append(finalResultsIPv4, res...)
log.Printf("[INFO] Total IPv4 records in finalResults: %d\n", len(finalResultsIPv4))
finalResultsMutex.Unlock()
}
}()
// 处理 IPv6 结果
go func() {
defer resultsWaitGroup.Done()
for res := range resultsIPv6 {
finalResultsMutex.Lock()
log.Printf("[INFO] Received %d IPv6 records, processing...\n", len(res))
finalResultsIPv6 = append(finalResultsIPv6, res...)
log.Printf("[INFO] Total IPv6 records in finalResults: %d\n", len(finalResultsIPv6))
finalResultsMutex.Unlock()
}
}()
maxThreads := 10
for _, file := range files {
wgMain.Add(1)
go processFile(file, resultsIPv4, resultsIPv6, maxThreads, &cacheMutex, &wgMain) // 移除 cache 参数
}
wgMain.Wait() // 等待所有文件处理完成
close(resultsIPv4) // 确保所有 IPv4 数据传输完成后再关闭通道
close(resultsIPv6) // 确保所有 IPv6 数据传输完成后再关闭通道
resultsWaitGroup.Wait() // 等待所有数据接收完毕
// 写入 IPv4 结果
log.Printf("[INFO] Writing %d IPv4 results to file\n", len(finalResultsIPv4))
outFileIPv4Path := "output/" + fileNameWithExt+"_IPv4.csv"
outFileIPv4, err := os.Create(outFileIPv4Path)
if err != nil {
log.Println("Error creating IPv4 output file:", err)
return
}
defer outFileIPv4.Close()
writerIPv4 := csv.NewWriter(outFileIPv4)
defer writerIPv4.Flush()
writerIPv4.Write([]string{"domain", "NS", "IP"})
writerIPv4.WriteAll(finalResultsIPv4)
// 写入 IPv6 结果
log.Printf("[INFO] Writing %d IPv6 results to file\n", len(finalResultsIPv6))
outFileIPv6Path := "output/" + fileNameWithExt+"_IPv6.csv"
outFileIPv6, err := os.Create(outFileIPv6Path)
if err != nil {
log.Println("Error creating IPv6 output file:", err)
return
}
defer outFileIPv6.Close()
writerIPv6 := csv.NewWriter(outFileIPv6)
defer writerIPv6.Flush()
writerIPv6.Write([]string{"domain", "NS", "IP"})
writerIPv6.WriteAll(finalResultsIPv6)
log.Println("[INFO] Finished writing results to output/auth-ns-ipv4.csv and output/auth-ns-ipv6.csv\n")
}