-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchecker.go
111 lines (93 loc) · 2.15 KB
/
checker.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
package main
import (
"fmt"
"net/http"
"net/url"
"regexp"
"sync"
)
type resultChan chan *Check
type Check struct {
site *Site
username string
found bool // Keeps username is found or not on the website
failed bool // Keeps check is success or not
errorMsg string
}
type CheckConfig struct {
Verbose bool
ProxyURL *url.URL
}
type Checker struct {
username string
sites []Site
results resultChan
wg *sync.WaitGroup
conf *CheckConfig
}
// ProfileURL return profile url of username
func (c *Check) ProfileURL() string {
return fmt.Sprintf(c.site.profileURL, c.username)
}
// ProbeURL return page which using for check existance of username
func (c *Check) ProbeURL() string {
if c.site.probeURL != "" {
return fmt.Sprintf(c.site.probeURL, c.username)
}
return fmt.Sprintf(c.site.profileURL, c.username)
}
func newChecker(username string, sites *[]Site, proxyURL *url.URL, verbose bool) *Checker {
return &Checker{
username: username,
results: make(resultChan),
sites: *sites,
wg: &sync.WaitGroup{},
conf: &CheckConfig{
Verbose: verbose,
ProxyURL: proxyURL,
},
}
}
// Check start checker goroutine for site
func (c *Checker) Check() {
for i := 0; len(c.sites) > i; i++ {
c.wg.Add(1)
check := &Check{
username: c.username,
site: &sites[i],
}
go c.checkSite(check)
}
c.wg.Wait()
close(c.results)
}
// Results gives check result channel
func (c *Checker) Results() resultChan {
return c.results
}
func (c *Checker) checkSite(check *Check) {
defer c.wg.Done()
if check.site.regexCheck != "" {
match, _ := regexp.MatchString(check.site.regexCheck, c.username)
if !match {
check.errorMsg = "Illegal username format!"
c.results <- check
return
}
}
check.site.checkerFn(c, check)
}
func (checker *Checker) CreateClient() *http.Client {
var client = &http.Client{}
if *checker.conf.ProxyURL != (url.URL{}) {
//adding the proxy settings to the Transport object
transport := &http.Transport{
Proxy: http.ProxyURL(checker.conf.ProxyURL),
}
//adding the Transport object to the http Client
client = &http.Client{
Transport: transport,
}
}
return client
}