-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
136 lines (111 loc) · 2.65 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
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"unicode/utf8"
)
var (
client *http.Client
wg sync.WaitGroup
requestMethod string
userAgent string
concurrency int
delay int
maxSize = int64(1024000)
sleepDuration time.Duration
)
func main() {
flag.StringVar(&requestMethod, "X", "HEAD", "The HTTP Request method. For the fastest, use HEAD.")
flag.StringVar(&userAgent, "user-agent", "Mozilla", "The HTTP User Agent.")
flag.IntVar(&concurrency, "t", 50, "The number of threads to use.")
flag.IntVar(&delay, "delay", 0, "The number miliseconds to delay threads.")
flag.Parse()
var input io.Reader
input = os.Stdin
if flag.NArg() > 0 {
file, err := os.Open(flag.Arg(0))
if err != nil {
fmt.Printf("failed to open file: %s\n", err)
os.Exit(1)
}
input = file
}
sc := bufio.NewScanner(input)
client = &http.Client{
Transport: &http.Transport{
MaxIdleConns: concurrency,
MaxIdleConnsPerHost: concurrency,
MaxConnsPerHost: concurrency,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
Timeout: 5 * time.Second,
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
},
}
semaphore := make(chan bool, concurrency)
for sc.Scan() {
raw := sc.Text()
wg.Add(1)
semaphore <- true
go func(raw string) {
defer wg.Done()
u, err := url.ParseRequestURI(raw)
if err != nil {
<-semaphore
return
}
resp, ws, err := fetchURL(u)
if err != nil {
<-semaphore
return
}
if resp.StatusCode <= 300 || resp.StatusCode >= 500 {
fmt.Printf("%d %d %d %s %s\n", resp.StatusCode, resp.ContentLength, ws, strings.ReplaceAll(resp.Header.Get("Content-Type"), " ", ""), u.String())
}
if delay > 0 {
sleepDuration = time.Duration(delay)
time.Sleep(sleepDuration * time.Millisecond)
}
<-semaphore
}(raw)
// <-semaphore
}
wg.Wait()
if sc.Err() != nil {
fmt.Printf("error: %s\n", sc.Err())
}
}
func fetchURL(u *url.URL) (*http.Response, int, error) {
wordsSize := 0
req, err := http.NewRequest(requestMethod, u.String(), nil)
if err != nil {
return nil, 0, err
}
req.Header.Set("User-Agent", userAgent)
resp, err := client.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
if resp.ContentLength <= maxSize {
if respbody, err := ioutil.ReadAll(resp.Body); err == nil {
resp.ContentLength = int64(utf8.RuneCountInString(string(respbody)))
wordsSize = len(strings.Split(string(respbody), " "))
}
}
io.Copy(ioutil.Discard, resp.Body)
return resp, wordsSize, err
}