-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnojunk.go
208 lines (180 loc) · 4.86 KB
/
nojunk.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"gopkg.in/yaml.v2"
)
type Config struct {
Blacklist []string `yaml:"blacklist"`
}
func getConfigPath() string {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Error getting user home directory: %v", err)
}
return filepath.Join(homeDir, ".config.yaml")
}
func loadConfig() Config {
configPath := getConfigPath()
// Check if config file exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
log.Printf("YAML file not found. Creating default config at %s\n", configPath)
defaultConfig := Config{
Blacklist: []string{
"otf", "woff2", "js", "ttf", "woff", "eot",
"svg", "png", "jpg", "jpeg", "gif", "bmp",
"css", "webp", "tiff", "heic", "heif",
},
}
file, err := os.Create(configPath)
if err != nil {
log.Fatalf("Failed to create YAML file: %v", err)
}
defer file.Close()
encoder := yaml.NewEncoder(file)
if err := encoder.Encode(defaultConfig); err != nil {
log.Fatalf("Failed to write default config to YAML file: %v", err)
}
log.Printf("Default config written to %s\n", configPath)
return defaultConfig
}
// Load config from YAML file
file, err := os.Open(configPath)
if err != nil {
log.Fatalf("Error loading YAML file: %v", err)
}
defer file.Close()
var config Config
decoder := yaml.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
log.Fatalf("Error parsing YAML file: %v", err)
}
return config
}
func filterURLs(urls []string, blacklist []string) []string {
var cleanURLs []string
for _, url := range urls {
// Remove query parameters to get the file part of the URL
re := regexp.MustCompile(`(\?[^\s]*)|(#.*)$`)
urlWithoutParams := re.ReplaceAllString(url, "")
// Extract file extension (handling case insensitivity)
ext := strings.ToLower(filepath.Ext(urlWithoutParams))
// Check if the extension is in the blacklist
excluded := false
for _, pattern := range blacklist {
if ext == "."+strings.ToLower(pattern) {
excluded = true
break
}
}
if !excluded {
cleanURLs = append(cleanURLs, url)
}
}
return cleanURLs
}
func readURLsFromInput() []string {
var urls []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
url := strings.TrimSpace(scanner.Text())
if url != "" {
urls = append(urls, url)
}
}
if err := scanner.Err(); err != nil {
log.Fatalf("Error reading from stdin: %v", err)
}
return urls
}
func readURLsFromFile(filePath string) []string {
file, err := os.Open(filePath)
if err != nil {
log.Fatalf("Error opening input file: %v", err)
}
defer file.Close()
var urls []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
url := strings.TrimSpace(scanner.Text())
if url != "" {
urls = append(urls, url)
}
}
if err := scanner.Err(); err != nil {
log.Fatalf("Error reading from file: %v", err)
}
return urls
}
func saveURLsToFile(urls []string, filePath string) {
file, err := os.Create(filePath)
if err != nil {
log.Fatalf("Error creating output file: %v", err)
}
defer file.Close()
for _, url := range urls {
_, err := file.WriteString(url + "\n")
if err != nil {
log.Fatalf("Error writing to output file: %v", err)
}
}
}
func printUsage() {
fmt.Println("Usage: nojunk [OPTIONS]")
fmt.Println("A program to filter URLs based on a blacklist from a YAML configuration file.")
fmt.Println("Author : sudomode | LinkedIn : https://www.linkedin.com/in/0xsudomode ")
fmt.Println("")
fmt.Println("Options:")
fmt.Println(" -i <file> Input file containing URLs (one per line)")
fmt.Println(" -o <file> Output file to save filtered URLs")
fmt.Println("")
fmt.Println("If no input or output file is provided, the program reads from stdin and writes to stdout.")
}
func isInputFromPipe() bool {
// Check if there's input from stdin (a pipe is used)
fi, err := os.Stdin.Stat()
if err != nil {
log.Fatalf("Error checking stdin: %v", err)
}
// If the file mode is named pipe or has data, it's a pipe.
return fi.Mode()&os.ModeNamedPipe != 0
}
func main() {
// Load config if it exists or create it if it doesn't
config := loadConfig()
// Check if the program is run without parameters and is not piped input
if len(os.Args) == 1 && !isInputFromPipe() {
// Display usage and exit if no parameters are provided
printUsage()
os.Exit(1)
}
var urls []string
inputFile := ""
outputFile := ""
// Check for input and output options
for i, arg := range os.Args {
if arg == "-i" && i+1 < len(os.Args) {
inputFile = os.Args[i+1]
} else if arg == "-o" && i+1 < len(os.Args) {
outputFile = os.Args[i+1]
}
}
if inputFile != "" {
urls = readURLsFromFile(inputFile)
} else {
urls = readURLsFromInput()
}
cleanURLs := filterURLs(urls, config.Blacklist)
if outputFile != "" {
saveURLsToFile(cleanURLs, outputFile)
} else {
for _, url := range cleanURLs {
fmt.Println(url)
}
}
}