-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (105 loc) · 2.68 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
package main
import (
"bufio"
"fmt"
"github.com/alecthomas/kingpin/v2"
"log"
urllib "net/url"
"os"
"path/filepath"
"runtime"
"sync"
"url-tester/lib/url"
"url-tester/lib/worker"
)
var (
app = kingpin.New("url-tester", "Reports not available URLs by sending HEAD requests to them.")
file = app.Flag("file", "Test all urls contained in a file, separated by a new line").Short('f').String()
link = app.Arg("url", "URL to send a HEAD request to").String()
)
// Must be called after parsing arguments
func getUrls() ([]string, error) {
var urls []string
if *file == "" && *link == "" {
app.Fatalf("No urls passed, see --help")
}
if *link != "" {
argURL, err := url.SanitizeURL(*link)
if err != nil {
urls = append(urls, argURL)
} else {
return nil, fmt.Errorf("invalid url %s: %s", *link, err)
}
}
if *file == "" {
return urls, nil
}
path, err := filepath.Abs(*file)
if err != nil {
return nil, fmt.Errorf("invalid filename %s: %s", *file, err)
}
fileinfo, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("file %s does not exist: %s", path, err)
}
if fileinfo.IsDir() {
return nil, fmt.Errorf("given path %s is a directory", path)
}
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("error opening file: %s", err)
}
defer file.Close()
urls, err = parseURLFile(file, urls)
if err != nil {
return nil, fmt.Errorf("error parsing provided file: %s", err)
}
return urls, nil
}
func parseURLFile(fileHandler *os.File, result []string) ([]string, error) {
scanner := bufio.NewScanner(fileHandler)
originalWriter := log.Writer()
defer log.SetOutput(originalWriter)
log.SetOutput(os.Stderr)
for scanner.Scan() {
rawURL := scanner.Text()
url, err := url.SanitizeURL(rawURL)
if err == nil {
result = append(result, url)
} else {
log.Printf("WARNING: error ocurred during parsing of url `%s`, ignoring: %s", rawURL, err)
}
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading file: %s", err)
}
return result, nil
}
func main() {
kingpin.MustParse(app.Parse(os.Args[1:]))
urls, err := getUrls()
if err != nil {
log.Fatal("Error parsing URL file:", err)
}
var wg sync.WaitGroup
wg.Add(len(urls))
numWorkers := runtime.NumCPU() * 2
results := make(chan worker.HeadResult, len(urls))
url := make(chan string, len(urls))
// Setup workers
for i := 0; i < numWorkers; i++ {
go worker.Worker(url, results, &wg)
}
// Send urls to them
for _, u := range urls {
url <- u
}
close(url)
// When all urls are checked, it is safe to close.
wg.Wait()
close(results)
for r := range results {
u, _ := urllib.QueryUnescape(r.URL)
fmt.Printf("Status '%d': '%s'\n", r.StatusCode, u)
}
}