-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsfwdetector.go
176 lines (151 loc) · 5.96 KB
/
nsfwdetector.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
package main
import (
"bufio"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/rix4uni/nsfwdetector/banner"
)
func checkNSFW(content string, keywords []string) []string {
var matchedKeywords []string
for _, keyword := range keywords {
if strings.Contains(content, keyword) {
matchedKeywords = append(matchedKeywords, keyword)
}
}
return matchedKeywords
}
func checkURL(url string, keywords []string, timeout int, wg *sync.WaitGroup) {
defer wg.Done()
// Create an HTTP client with a timeout
client := &http.Client{
Timeout: time.Duration(timeout) * time.Second,
}
// Try both https:// and http:// if the protocol is missing
if !strings.HasPrefix(url, "https://") && !strings.HasPrefix(url, "http://") {
// Try https:// first
logrus.Debugf("Trying https:// for URL: %s", url)
tryURLs := []string{"https://" + url, "http://" + url}
for _, tryURL := range tryURLs {
resp, err := client.Get(tryURL)
if err != nil {
logrus.Warnf("Error fetching URL %s: %v", tryURL, err)
continue // Try the next protocol
}
defer resp.Body.Close()
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logrus.Warnf("Error reading response body for %s: %v", tryURL, err)
continue
}
// Check for any matched keywords
matchedKeywords := checkNSFW(string(body), keywords)
// Format the matched keywords as a comma-separated string
if len(matchedKeywords) > 0 {
logrus.Infof("%s [%s]", tryURL, strings.Join(matchedKeywords, ", "))
} else {
logrus.Infof("%s []", tryURL)
}
return // Exit the loop after successfully processing one URL
}
logrus.Warnf("Both protocols failed for %s, skipping.", url)
return
}
// If URL has a valid protocol, continue fetching
resp, err := client.Get(url)
if err != nil {
logrus.Warnf("Error fetching URL %s: %v", url, err)
return
}
defer resp.Body.Close()
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logrus.Warnf("Error reading response body for %s: %v", url, err)
return
}
// Check for any matched keywords
matchedKeywords := checkNSFW(string(body), keywords)
// Format the matched keywords as a comma-separated string
if len(matchedKeywords) > 0 {
logrus.Infof("%s [%s]", url, strings.Join(matchedKeywords, ", "))
} else {
logrus.Infof("%s []", url)
}
}
func main() {
// Initialize the flags
concurrency := pflag.IntP("concurrency", "c", 50, "Number of concurrent workers")
timeout := pflag.IntP("timeout", "t", 30, "Timeout for each HTTP request in seconds")
verbose := pflag.Bool("verbose", false, "Enable verbose logging")
wordlist := pflag.StringP("wordlist", "w", "keywords.txt", "Path to the file containing keywords to check")
silent := pflag.Bool("silent", false, "silent mode.")
versionFlag := pflag.Bool("version", false, "Print the version of the tool and exit.")
// Set the logging level based on the verbose flag
logrus.SetOutput(os.Stdout)
if *verbose {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
// Parse the command-line arguments
pflag.Parse()
if *versionFlag {
banner.PrintBanner()
banner.PrintVersion()
return
}
if !*silent {
banner.PrintBanner()
}
// Read the keywords from the wordlist file
file, err := os.Open(*wordlist)
if err != nil {
logrus.Fatal("Error opening keyword file:", err)
}
defer file.Close()
var keywords []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
keywords = append(keywords, scanner.Text())
}
if err := scanner.Err(); err != nil {
logrus.Fatal("Error reading keyword file:", err)
}
// Read URLs from standard input
var urls []string
scanner = bufio.NewScanner(os.Stdin)
for scanner.Scan() {
url := scanner.Text()
if url != "" {
urls = append(urls, url)
}
}
if err := scanner.Err(); err != nil {
logrus.Fatal("Error reading input:", err)
}
// Create a wait group to manage concurrent workers
var wg sync.WaitGroup
sem := make(chan struct{}, *concurrency) // Semaphore to limit the number of concurrent workers
// Process the URLs concurrently
for _, url := range urls {
wg.Add(1)
// Acquire a slot in the semaphore
sem <- struct{}{}
go func(url string) {
defer func() {
// Release the slot in the semaphore
<-sem
}()
checkURL(url, keywords, *timeout, &wg)
}(url)
}
// Wait for all workers to finish
wg.Wait()
}