-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (56 loc) · 1.41 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"regexp"
"runtime"
"sync"
)
func worker(domains chan string, wg *sync.WaitGroup) {
defer wg.Done()
for domain := range domains {
response, err := http.Get(domain)
if err != nil {
log.Fatalln(err)
}
if err == nil {
log.Println(fmt.Sprintf("[%s] Status %d", domain, response.StatusCode))
} else {
log.Println(fmt.Sprintf("[%s] Error %s", domain, err.Error()))
}
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
var threadsCount int
flag.IntVar(&threadsCount, "threads", 15, "Count of used threads (goroutines)")
flag.Parse()
domains := make(chan string, 2)
for _, domain := range flag.Args() {
u, err := url.ParseRequestURI(domain)
if err != nil {
var validURL = regexp.MustCompile(`^[a-zA-z]*:\/\/`)
if !validURL.MatchString(domain) {
domains <- fmt.Sprintf("http://%s", domain)
} else {
log.Println(fmt.Sprintf("[%s] Status 400 - Error %s", domain, err.Error()))
}
} else if u.Scheme == "" || u.Host == "" {
log.Println(fmt.Sprintf("[%s] Status 400 - Must be an absolute URL", domain))
} else if u.Scheme != "http" && u.Scheme != "https" {
log.Println(fmt.Sprintf("[%s] Status 400 - Scheme must be HTTP or HTTPS", domain))
} else {
domains <- u.String()
}
}
close(domains)
wg := new(sync.WaitGroup)
for i := 0; i < threadsCount; i++ {
wg.Add(1)
go worker(domains, wg)
}
wg.Wait()
}