-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (162 loc) · 4.23 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
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"sync"
"time"
)
// Get create a get request
func Get(query, url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", "GoodBoy")
req.Header.Add("Accept", "*/*")
req.Header.Add("Cache-Control", "no-cache")
// req.Header.Add("Accept-Encoding", "gzip, deflate")
req.Header.Add("Connection", "keep-alive")
req.Header.Add("cache-control", "no-cache")
req.URL.RawQuery = query
trt := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
}
client := &http.Client{Transport: trt}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func main() {
username := flag.String("u", "", "Username want to find mails")
flag.Parse()
if *username == "" {
fmt.Println("-u username is required")
os.Exit(1)
}
fmt.Println(*username)
user := *username
findFromNPM(user)
findFromRecentCommits(user)
findFromRecentActivity(user)
}
func findFromNPM(user string) {
fmt.Println("-----Emails on npm-----")
userURL := "https://registry.npmjs.org/-/user/org.couchdb.user:" + user
q := url.Values{}
byteBody, err := Get(q.Encode(), userURL)
if err != nil {
fmt.Println("Error when do get request to npm api")
return
}
// regular expression pattern
regE := regexp.MustCompile(`"(email)":"([^"]+)"`)
fmt.Println(regE.FindAllString(string(byteBody), -1))
}
func findFromRecentCommits(user string) {
fmt.Println("-----Emails from recent commits-----")
userURL := "https://api.github.com/users/" + user + "/events"
byteBody, err := Get("", userURL)
if err != nil {
fmt.Println("Error when do get request to github api")
return
}
// regular expression pattern
reg := regexp.MustCompile(`"(email)":"([^"]+)"`)
stringArr := unique(reg.FindAllString(string(byteBody), -1))
for i := 0; i < len(stringArr); i++ {
tmp := strings.ReplaceAll(stringArr[i], `"email":"`, "")
fmt.Println(tmp[:len(tmp)-1])
}
}
func unique(stringSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range stringSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func getAllRepos(user string) ([]string, error) {
userURL := "https://api.github.com/users/" + user + "/repos"
q := url.Values{}
q.Add("type", "owner")
q.Add("sort", "updated")
byteBody, err := Get(q.Encode(), userURL)
if err != nil {
return nil, err
}
var results []map[string]interface{}
json.Unmarshal(byteBody, &results)
var allRepo []string
for _, result := range results {
allRepo = append(allRepo, result["name"].(string))
}
return allRepo, nil
}
func findFromRecentActivity(user string) {
fmt.Println("-----Emails from owned-repo recent activity-----")
repos, err := getAllRepos(user)
if err != nil {
fmt.Println("Error when get owned-repo recent activity")
return
}
for _, repo := range repos {
go findFromRepo(user, repo)
}
wg.Wait()
for i := 0; i < len(emailFromRepos); i++ {
fmt.Println(emailFromRepos[i])
}
}
var wg sync.WaitGroup
var emailFromRepos []string
var mutex = &sync.Mutex{}
func findFromRepo(user, repo string) {
wg.Add(1)
defer wg.Done()
userURL := "https://api.github.com/repos/" + user + "/" + repo + "/commits"
byteBody, err := Get("", userURL)
if err != nil {
// fmt.Println("Error when get details owned-repo recent activity")
return
}
// regular expression pattern
reg := regexp.MustCompile(`"(email)":"([^"]+)"`)
stringArr := unique(reg.FindAllString(string(byteBody), -1))
for i := 0; i < len(stringArr); i++ {
tmp := strings.ReplaceAll(stringArr[i], `"email":"`, "")
mutex.Lock()
emailFromRepos = appendIfMissing(emailFromRepos, tmp[:len(tmp)-1])
mutex.Unlock()
}
}
// It's simple and obvious and will be fast for small lists.
func appendIfMissing(slice []string, i string) []string {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}