-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
229 lines (194 loc) · 5.05 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"golang.org/x/net/html"
)
func main() {
// Track how long
startTime := time.Now()
var resultTotal uint64
filepath := flag.String("path", "", "The filepath of the names")
workers := flag.Int("workers", 2, "How many workers to run concurrently. (More workers are faster but more prone to rate limiting or bandwith issues)")
sleep := flag.Int("sleep", 100, "Sleep duration between each workers task. (Millisecond)")
// auth := flag.String("auth", "", "authenticity_token for post request to github")
flag.Parse()
var data []byte
var err error
if *filepath == "" {
// Make sure something is being passed to Stdin
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
log.Fatal("Pass a filepath or data on stdin")
}
// If there is something let's read it all
data, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
} else {
data, err = ioutil.ReadFile(*filepath)
if err != nil {
log.Fatal(err)
}
}
// Get auth token
auth, cookie := getAuth()
// Generate the word list from the filepath provided
names, err := splitData(data)
if err != nil {
log.Fatal(err)
}
*workers = verifyWorkerCount(len(names), *workers)
fmt.Printf("Starting github-usercheck. Parsing %d names with %d workers. \n", len(names), *workers)
results := make(chan string)
var wg sync.WaitGroup
wg.Add(*workers)
go func() {
for r := range results {
fmt.Println(r)
// increment the result total
atomic.AddUint64(&resultTotal, 1)
}
close(results)
}()
// Split names among workers
for i := 0; i < *workers; i++ {
go func(i int) {
defer wg.Done()
start, end := calculateLoad(len(names), *workers, i)
for _, n := range names[start:end] {
ok := available(n, auth, cookie)
if ok {
results <- n
time.Sleep(time.Duration(*sleep) * time.Millisecond)
}
}
}(i)
}
wg.Wait()
fmt.Printf("Found %d results in %f seconds \n", resultTotal, time.Since(startTime).Seconds())
}
// verifyWorkerCount if there are fewer names than workers subtract workers
func verifyWorkerCount(totalLoad, workers int) int {
ratio := float64(totalLoad) / float64(workers)
if ratio == float64(int64(ratio)) {
return workers
}
// Make sure there's more work than workers
if ratio < 1 {
for ratio < 1 {
workers--
ratio = float64(totalLoad) / float64(workers)
}
return workers
}
return workers
}
// calculateLoad divides the totalLoad among the number of workers based on turn
func calculateLoad(tasks, workers, turn int) (start, end int) {
load := tasks / workers
// Each turn the start and end index updated based on whose turn it is
start = load * turn
end = load * (turn + 1)
if end < tasks {
end = tasks
}
return
}
// splitData takes a slice of bytes and splits on \n into a slice of strings
func splitData(data []byte) ([]string, error) {
names := []string{}
for _, line := range strings.Split(string(data), "\n") {
// Don't append empty lintes
line = strings.TrimSpace(line)
if line != "" {
names = append(names, line)
}
}
return names, nil
}
// getAuth makes a requst to github, parses the html response for the authenticity_token
// and returns any cookies
func getAuth() (token string, cookies []*http.Cookie) {
c := &http.Client{
Timeout: time.Second * 10,
}
res, err := c.Get("https://github.com/session")
if err != nil {
return "", nil
}
defer res.Body.Close()
doc, err := html.Parse(res.Body)
if err != nil {
return "", nil
}
// Look through html to find authenticity_token
var auth string
var f func(*html.Node)
f = func(n *html.Node) {
if n.Data == "input" {
for _, a := range n.Attr {
if a.Key == "name" {
if a.Val == "authenticity_token" {
// Now find the value of the name
for _, a := range n.Attr {
if a.Key == "value" {
auth = a.Val
}
}
}
break
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
// fmt.Println(res.Header)
cookies = res.Cookies()
return auth, cookies
}
// available check github.com and verifies if the name arg is available
// if there is an auth strig use signup_check form
func available(name, auth string, cookies []*http.Cookie) bool {
c := &http.Client{
Timeout: time.Second * 10,
}
apiURL := "https://github.com"
resource := "/signup_check/username"
data := url.Values{}
data.Add("value", name)
data.Add("authenticity_token", auth)
u, err := url.ParseRequestURI(apiURL)
u.Path = resource
urlStr := fmt.Sprintf("%v", u)
req, err := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))
req.Header.Add("content-type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
req.Header.Add("Referer", "https://github.com/join?source=header-home")
for _, c := range cookies {
req.AddCookie(c)
}
res, err := c.Do(req)
if err != nil {
return false
}
if res.StatusCode != http.StatusOK {
return false
}
return true
}