forked from cloudrkt/domainchecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
138 lines (113 loc) · 2.74 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
// Check domain response code and time to load.
package main
import (
"bufio"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
)
func main() {
filePtr := flag.String("f", "", "Add file containing URL's")
timeOutPtr := flag.Int("t", 10, "Add timeout in seconds")
concurrentPtr := flag.Int("c", 10,
"Add concurrent checks in "+
"units 1 -> N [Keep in mind that a value to high will saturate "+
"your connection and produce inacurate results]")
flag.Parse()
// If flags set to unreasonable values then exit.
if *concurrentPtr <= 0 || *timeOutPtr <= 0 {
flag.PrintDefaults()
os.Exit(1)
}
argDomain := flag.Args()
domains := []string{}
totalCount := 0
if len(argDomain) > 0 {
totalCount = len(argDomain)
for _, v := range argDomain {
domains = append(domains, v)
}
} else {
if *filePtr == "" {
flag.PrintDefaults()
os.Exit(1)
}
domains, totalCount = read(*filePtr)
}
result := checker(domains, timeOutPtr, concurrentPtr)
var goodCounter, badCounter int
var badStatus []string
start := time.Now()
for status := range result {
if strings.HasPrefix(status, "200") {
fmt.Println(status)
goodCounter++
} else {
badStatus = append(badStatus, status)
badCounter++
}
}
timeElapsed := time.Since(start)
for _, status := range badStatus {
fmt.Println(status)
}
fmt.Printf("Checked %v domains (succes:%v bad:%v) - took: %v \n",
totalCount,
goodCounter,
badCounter,
timeElapsed,
)
}
func read(file string) ([]string, int) {
var count int
domains := []string{}
fileHandle, err := os.Open(file)
if err != nil {
log.Fatal(err)
}
defer fileHandle.Close()
scanner := bufio.NewScanner(fileHandle)
for scanner.Scan() {
domains = append(domains, scanner.Text())
count++
}
return domains, count
}
func checker(domains []string, timeOutPtr, concurrentPtr *int) chan string {
out := make(chan string)
done := make(chan bool)
sem := make(chan bool, *concurrentPtr)
check := func(domain string) {
sem <- true
defer func() { <-sem }()
client := http.Client{
Timeout: time.Duration(time.Duration(*timeOutPtr) * time.Second),
}
req, _ := http.NewRequest("GET", fmt.Sprintf("http://www.%v", domain), nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
start := time.Now()
resp, err := client.Do(req)
elapsed := time.Since(start)
if err != nil {
out <- fmt.Sprintf("ERR, %v, Error: %v", domain, err)
done <- true
} else {
out <- fmt.Sprintf("%v, %v, took: %v", resp.Status, domain, elapsed)
done <- true
}
}
for _, v := range domains {
go check(v)
}
go func() {
for i := 0; i < len(domains); i++ {
<-done
}
close(out)
}()
return out
}