-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
202 lines (174 loc) · 3.86 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"regexp"
"strings"
"sync"
"time"
)
func main() {
var concurrency int
flag.IntVar(&concurrency, "c", 20, "set the concurrency level")
var to int
flag.IntVar(&to, "t", 10000, "timeout (milliseconds)")
var filename string
flag.StringVar(&filename, "l", "", "filename location")
var verbose bool
flag.BoolVar(&verbose, "v", false, "output errors to stderr")
flag.Parse()
// make an actual time.Duration out of the timeout
timeout := time.Duration(to * 1000000)
var tr = &http.Transport{
MaxIdleConns: 30,
IdleConnTimeout: time.Second,
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: time.Second,
}).DialContext,
}
re := func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
client := &http.Client{
Transport: tr,
CheckRedirect: re,
Timeout: timeout,
}
urls := make(chan string)
var wg sync.WaitGroup
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
for url := range urls {
if is(client, url) {
//fmt.Println(url)
continue
}
if verbose {
fmt.Fprintf(os.Stderr, "failed: %s\n", url)
}
}
wg.Done()
}()
}
if filename != "" {
file, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read input: %s\n", err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
domain := strings.ToLower(scanner.Text())
// and http
if !strings.Contains(domain, "http") {
urls <- "https://" + domain
urls <- "http://" + domain
}else{
urls <- domain
}
}
close(urls)
} else {
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
domain := strings.ToLower(sc.Text())
// and http://
if !strings.Contains(domain, "http") {
urls <- "https://" + domain
urls <- "http://" + domain
}else {
urls <- domain
}
}
close(urls)
if err := sc.Err();err != nil{
fmt.Fprintf(os.Stderr, "failed to read input: %s\n", err)
}
}
wg.Wait()
fmt.Println("over")
}
func is(client *http.Client, urls string) bool {
//GET
req, err := http.NewRequest("GET", urls, nil)
if err != nil {
return false
}
req.Header.Add("Connection", "close")
req.Close = true
resp, err := client.Do(req)
if resp != nil || resp.StatusCode != 200{
if(scan(resp,urls,"GET")){
return true
}
defer resp.Body.Close()
}
//POST
reqpost, errp := http.NewRequest("POST",urls,nil)
if errp != nil{
return false
}
reqpost.Header.Add("Connection", "close")
reqpost.Close = true
reqpostp,err := client.Do(reqpost)
if reqpostp != nil{
if(scan(reqpostp,urls,"POST")){
return true
}
}
if err!= nil{
return false
}
return true
}
func Gettype(str string) string {
if strings.Contains(str,"json"){
return "json"
}else if(strings.Contains(str,"text/plain")){
return "text"
}else if(strings.Contains(str,"text/html")){
return "html"
}else if(strings.Contains(str,"application/javascript")){
return "javascript"
}else{
return "unkonw"
}
}
func findtitle(str string) string {
matcheds := regexp.MustCompile("<title>[\\s\\S]*?</title>")
s := matcheds.FindString(str)
return s
}
func scan(resp *http.Response,url string,method string) bool{
if resp.StatusCode == 200{
body,_ := ioutil.ReadAll(resp.Body)
str := string(body)
Type := resp.Header.Get("Content-Type")
Type = Gettype(Type)
switch Type{
case "html":
title := findtitle(str)
fmt.Println(url+" "+method+" "+resp.Status+" "+"HTML"+" "+title)
default:
if len(str) > 50{
fmt.Println(url+" "+method+" "+resp.Status+" "+Type+" "+ str[0:50])
}else {
fmt.Println(url+" "+method+" "+resp.Status+" "+Type+" "+ str)
}
}
return true
}else {
fmt.Println(url+" "+resp.Status)
return false
}
return false
}