-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·182 lines (158 loc) · 4.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
"sync"
"github.com/fatih/color"
)
const (
Red = "\033[31m"
Green = "\033[32m"
Reset = "\033[0m"
)
func printLogo() {
logo := `
) ( (
( /( )\ ) )\ )
)\()) (()/( (()/(
((_)\ /(_)) /(_))
((_) (_))_|(_))
/ * \ | |* / __|
| (_) || **| \** \
\___/ |_| |___/
`
fmt.Print(color.RedString(logo))
}
func matchesDomain(domain string, rule string) bool {
rule = strings.TrimSpace(rule)
if strings.HasPrefix(rule, "*.") {
wildcard := rule[2:]
return strings.HasSuffix(domain, wildcard)
} else if strings.HasSuffix(rule, "/*") {
baseDomain := rule[:len(rule)-2]
return domain == baseDomain || strings.HasPrefix(domain, baseDomain+"/")
} else {
return domain == rule
}
}
func isAllowed(domain string, allowed []string) bool {
if len(allowed) == 0 {
return true
}
for _, rule := range allowed {
if matchesDomain(domain, rule) {
return true
}
}
return false
}
func isDisallowed(domain string, disallowed []string) bool {
for _, rule := range disallowed {
fmt.Printf("Checking domain '%s' against rule '%s'\n", domain, rule)
if matchesDomain(domain, rule) {
fmt.Printf("Match found: '%s' is disallowed by rule '%s'\n", domain, rule)
return true
}
}
return false
}
func readLines(filePath string) ([]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func writeLines(lines []string, filePath string) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range lines {
fmt.Fprintln(w, line)
}
return w.Flush()
}
func filterDomains(subdomains []string, allowed []string, disallowed []string, results chan<- string, wg *sync.WaitGroup) {
defer wg.Done()
for _, subdomain := range subdomains {
if isDisallowed(subdomain, disallowed) {
fmt.Println(color.RedString("Removed (Disallowed): %s", subdomain))
} else if !isAllowed(subdomain, allowed) {
fmt.Println(color.RedString("Removed (Not Allowed): %s", subdomain))
} else {
fmt.Println(color.GreenString("Retained: %s", subdomain))
results <- subdomain
}
}
}
func main() {
subdomainsFile := flag.String("IL", "", "Path to the subdomains file")
allowedDomains := flag.String("a", "", "Allowed domains (comma-separated)")
disallowedDomains := flag.String("d", "", "Disallowed domains (comma-separated)")
outputFile := flag.String("o", "", "Path to the output file")
flag.Parse()
if *subdomainsFile == "" || *outputFile == "" {
fmt.Println("Usage: ofc -IL <subdomains_file> -a <allowed_domains> -d <disallowed_domains> -o <output_file>")
return
}
printLogo()
subdomains, err := readLines(*subdomainsFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading subdomains file: %v\n", err)
return
}
var allowed []string
if *allowedDomains != "" {
allowed = strings.Split(*allowedDomains, ",")
}
var disallowed []string
if *disallowedDomains != "" {
disallowed, err = readLines(*disallowedDomains)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading disallowed domains file: %v\n", err)
return
}
}
fmt.Println("Disallowed rules:")
for _, rule := range disallowed {
fmt.Printf("- %s\n", rule)
}
results := make(chan string, len(subdomains))
var wg sync.WaitGroup
numWorkers := 4
chunkSize := (len(subdomains) + numWorkers - 1) / numWorkers
for i := 0; i < len(subdomains); i += chunkSize {
end := i + chunkSize
if end > len(subdomains) {
end = len(subdomains)
}
wg.Add(1)
go filterDomains(subdomains[i:end], allowed, disallowed, results, &wg)
}
go func() {
wg.Wait()
close(results)
}()
var filteredSubdomains []string
for subdomain := range results {
filteredSubdomains = append(filteredSubdomains, subdomain)
}
err = writeLines(filteredSubdomains, *outputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing output file: %v\n", err)
return
}
fmt.Println(color.GreenString("\nProcessing complete!"))
}