-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
244 lines (198 loc) · 4.77 KB
/
file.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"gopkg.in/yaml.v3"
)
var wordlists = "lists.yaml"
var wordlistCache = ".cached_wordlists/"
var combinedFilename = "combined.txt"
func processFile(filePath string, linesChan chan<- string) error {
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if regexp.MustCompile(`^[a-zA-Z0-9-]+$`).MatchString(line) && !strings.Contains(line, " ") {
line = strings.ToLower(line)
if line != "" {
linesChan <- line
}
}
}
return scanner.Err()
}
func writeToFile(filePath string, lines []string) error {
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer file.Close()
writer := bufio.NewWriter(file)
for _, line := range lines {
if _, err := writer.WriteString(line + "\n"); err != nil {
return fmt.Errorf("failed to write to output file: %w", err)
}
}
return writer.Flush()
}
func readFile(filePath string, lineChannel chan<- string) {
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lineChannel <- scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
}
}
func downloadSecLists() {
if _, err := os.Stat(wordlistCache); os.IsNotExist(err) {
err := os.Mkdir(wordlistCache, os.ModePerm)
if err != nil {
fmt.Println("Error creating directory:", err)
return
}
}
secListSources := readListsFile(wordlists)
fetch(secListSources)
}
func readListsFile(filename string) []string {
file, err := os.Open(filename)
if err != nil {
fmt.Println("Error opening file:", err)
return nil
}
defer file.Close()
decoder := yaml.NewDecoder(file)
var list []string
err = decoder.Decode(&list)
if err != nil {
log.Fatalf("Error decoding YAML: %v", err)
}
return list
}
func directoryExists(dirName string) bool {
_, err := os.Stat(dirName)
if err != nil {
if os.IsNotExist(err) {
return false
}
return false
}
return true
}
func countLinesInDirectory(directory string) (int, error) {
totalLines := 0
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
lines, err := countLinesInFile(path)
if err != nil {
return err
}
totalLines += lines
}
return nil
})
if err != nil {
return 0, fmt.Errorf("failed to count lines: %w", err)
}
return totalLines, nil
}
func countLinesInFile(filePath string) (int, error) {
file, err := os.Open(filePath)
if err != nil {
return 0, fmt.Errorf("failed to open file %s: %w", filePath, err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
lineCount := 0
for scanner.Scan() {
lineCount++
}
if err := scanner.Err(); err != nil {
return 0, fmt.Errorf("error reading file %s: %w", filePath, err)
}
return lineCount, nil
}
func listFiles(directory string) ([]string, error) {
var files []string
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
relPath, err := filepath.Rel(directory, path)
if err != nil {
return err
}
files = append(files, relPath)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to list files: %w", err)
}
return files, err
}
type FileWriter struct {
filename string
mu sync.Mutex
}
func NewFileWriter(filename string) *FileWriter {
return &FileWriter{filename: filename}
}
func (fw *FileWriter) WriteToFile(data string) error {
fw.mu.Lock()
defer fw.mu.Unlock()
file, err := os.OpenFile(fw.filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(data)
return err
}
func stringInFile(subdomain string, outputFile *os.File) (bool, error) {
scanner := bufio.NewScanner(outputFile)
// Reset file pointer to the beginning
if _, err := outputFile.Seek(0, 0); err != nil {
return false, err
}
for scanner.Scan() {
if strings.TrimSpace(scanner.Text()) == subdomain {
return true, nil
}
}
if err := scanner.Err(); err != nil {
return false, err
}
return false, nil
}
func downloadAndValidateWordlists() {
fmt.Println("[↓] Downloading wordlists mentioned in " + wordlists)
downloadSecLists()
fmt.Println("\n[⟳] Validating downloaded wordlists...")
removeDuplicatesFromSecLists()
fmt.Println("\n[✓] Processing complete!")
lines, _ := countLinesInDirectory(wordlistCache)
fmt.Println("\n[+] Wordlist cache count: " + strconv.Itoa(lines) + " items")
}