-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyhttptool.go
125 lines (103 loc) · 2.52 KB
/
myhttptool.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
package main
import (
"crypto/md5"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
// Limiting default number of goroutines to prevent exhausting local resources.
const defaultLimit = 10
// validateURL validates the string is a valid url or not
func validateURL(val string) bool {
_, err := url.ParseRequestURI(val)
if err != nil {
fmt.Println("Invalid URL: ", val, "with error:-", err)
return false
}
return true
}
// addHttpsToDomains adds https to domains to create a valid url
func addHttpToDomains(domains []string) []string {
var urlBuilder strings.Builder
var urls []string
for _, domain := range domains {
if strings.HasPrefix(domain, "http://") || strings.HasPrefix(domain, "https://") {
if validateURL(domain) {
urls = append(urls, domain)
}
} else {
urlBuilder.WriteString("http://")
urlBuilder.WriteString(domain)
if validateURL(urlBuilder.String()) {
urls = append(urls, urlBuilder.String())
}
urlBuilder.Reset()
}
}
return urls
}
// isCommandValid checks if given command is valid or not
func isCommandValid(parallel int, urls []string) bool {
if parallel <= 0 || len(urls) == 0 {
return false
}
return true
}
// getHashOfUrls process Url's response and return corresponding Hash
func getHashOfUrls(url string) (err error) {
res, err := http.Get(url)
if err != nil {
return err
}
body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
return err
}
text := string(body)
hash := fmt.Sprintf("%x", md5.Sum([]byte(text)))
fmt.Println(url + " " + hash)
return nil
}
// runCommand runs the given command
func runCommand(job <-chan string, result chan bool) {
for url := range job {
err := getHashOfUrls(url)
if err != nil {
log.Fatalln("Error: ", err)
}
//Sending message through channel
result <- true
}
}
func main() {
parallel := flag.Int("parallel", defaultLimit, "To limit the number of parallel requests")
flag.Parse()
args := flag.Args()
if !isCommandValid(*parallel, args) {
log.Fatalln("Invalid command! Please check sample commands in README file")
}
urls := addHttpToDomains(args)
jobs := make(chan string, len(urls))
result := make(chan bool, len(urls))
if *parallel > len(urls) {
*parallel = len(urls)
}
//Running Parallel commands using multiple goroutines
for p := 0; p < *parallel; p++ {
go runCommand(jobs, result)
}
//Sending through channel
for _, url := range urls {
jobs <- url
}
close(jobs)
//Recieving through channel
for i := 0; i < len(urls); i++ {
<-result
}
}