-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck_handler.go
112 lines (93 loc) · 2.64 KB
/
check_handler.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
type checkerFunc func(*Checker, *Check)
var userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
// statusChecker check username by response status code
func statusChecker(checker *Checker, check *Check) {
// res, err := http.Get(check.ProbeUrl())
var client = checker.CreateClient()
req, err := http.NewRequest("GET", check.ProbeURL(), nil)
req.Header.Set("User-Agent", userAgent)
res, err := client.Do(req)
if err != nil {
// Check failed
if checker.conf.Verbose {
check.errorMsg = err.Error()
} else {
check.errorMsg = "Request Error"
}
check.failed = true
checker.results <- check
return
}
defer res.Body.Close()
check.found = res.StatusCode == 200
checker.results <- check
}
// bodyChecker unsure username by searching given text in page content
func bodyChecker(searchText string) checkerFunc {
return func(checker *Checker, check *Check) {
// res, err := http.Get(check.ProbeUrl())
var client = checker.CreateClient()
req, err := http.NewRequest("GET", check.ProbeURL(), nil)
req.Header.Set("User-Agent", userAgent)
res, err := client.Do(req)
if err != nil {
// Check failed
if checker.conf.Verbose {
check.errorMsg = err.Error()
} else {
check.errorMsg = "Request Error"
}
check.failed = true
checker.results <- check
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
check.failed = true
checker.results <- check
return
}
check.found = !strings.Contains(string(body), searchText)
checker.results <- check
}
}
// catchByRedirectUrl check username by redirected page url
func redirectChecker(redirectUrlTemplate string) checkerFunc {
return func(checker *Checker, check *Check) {
var client = checker.CreateClient()
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
req, err := http.NewRequest("GET", check.ProbeURL(), nil)
req.Header.Set("User-Agent", userAgent)
res, err := client.Do(req)
if err != nil {
// Check failed
if checker.conf.Verbose {
check.errorMsg = err.Error()
} else {
check.errorMsg = "Request Error"
}
// check.errorMsg = "Request Error"
check.failed = true
checker.results <- check
return
}
var errorURL string
if strings.Contains(redirectUrlTemplate, "%s") {
errorURL = fmt.Sprintf(redirectUrlTemplate, check.username)
} else {
errorURL = redirectUrlTemplate
}
check.found = !strings.Contains(res.Header.Get("Location"), errorURL)
checker.results <- check
}
}