-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailbot.go
294 lines (274 loc) · 5.16 KB
/
mailbot.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
// Default settings
var (
DefaultFileName = "crawler" + strconv.FormatInt(time.Now().UnixNano(), 36) + ".log"
)
// Crawler holds the flags and locks
type Crawler struct {
flags struct {
filename string
printToStdout bool
verbose bool
pastebin bool
debian bool
slexy bool
}
file *os.File
}
var blacklist = []string{
"formorer@debian.org",
"user@user",
}
var c = new(Crawler)
func init() {
var err error
flag.StringVar(
&c.flags.filename,
"o",
DefaultFileName,
"File to save the collected mails",
)
flag.BoolVar(
&c.flags.printToStdout,
"stdout",
true,
"Print to stdout",
)
flag.BoolVar(
&c.flags.verbose,
"verbose",
false,
"Verbose mode",
)
flag.BoolVar(
&c.flags.pastebin,
"pastebin",
true,
"Crawl pastebin.com",
)
flag.BoolVar(
&c.flags.debian,
"debian",
true,
"Crawl paste.debian.net",
)
flag.BoolVar(
&c.flags.slexy,
"slexy",
true,
"Crawl slexy.org",
)
flag.Parse()
c.file, err = os.OpenFile(
c.flags.filename,
os.O_APPEND|os.O_WRONLY|os.O_CREATE,
0600,
)
if err != nil {
report(err)
}
}
func main() {
c.Run()
}
// Run runs the crawler
func (c *Crawler) Run() {
var wg = &sync.WaitGroup{}
for {
if c.flags.pastebin {
wg.Add(1)
go c.Pastebin(wg)
}
if c.flags.debian {
wg.Add(1)
go c.Debian(wg)
}
if c.flags.slexy {
wg.Add(1)
go c.Slexy(wg)
}
wg.Wait()
}
}
// GetMail extracts email addresses from text documents
func (c *Crawler) GetMail(page string) {
r := regexp.MustCompile(`[\w]+@[\w.]+`)
mails := r.FindAllString(page, -1)
if mails == nil {
if c.flags.verbose {
report(
errors.New("no mail found"),
)
}
return
}
fresh := FreshFilter(mails)
if len(fresh) != 0 {
return
}
toWrite := strings.Join(fresh, "\n")
c.file.WriteString(toWrite + "\n")
if c.flags.printToStdout {
fmt.Println(toWrite)
}
c.file.Sync()
return
}
// FetchPage fetches/scrapes pages from web URLsl
func (c *Crawler) FetchPage(url string) (string, error) {
if c.flags.verbose {
fmt.Printf("Fetching: %s\n", url)
}
client := &http.Client{}
resp, err := client.Get(url)
if err != nil {
report(err)
return "", err
}
b, err := ioutil.ReadAll(resp.Body)
return string(b), err
}
// Pastebin collects emails from pastebin.com
func (c *Crawler) Pastebin(wg *sync.WaitGroup) {
defer wg.Done()
r := regexp.MustCompile(`class="i_p0" alt="" /><a href="(.*?)">`)
url := "https://pastebin.com/archive"
page, err := c.FetchPage(url)
if err != nil {
report(err)
}
raws := r.FindAllString(page, -1)
if raws == nil {
if c.flags.verbose {
report(errors.New("no raw link"))
}
return
}
for _, v := range raws {
parser := strings.Split(v, `="`)
if len(parser) < 4 {
report(errors.New("can't parse"))
return
}
rawlink := "https://pastebin.com/raw" + strings.Replace(parser[3], `">`, "", -1)
page, err := c.FetchPage(rawlink)
if err != nil {
report(err)
return
}
c.GetMail(page)
}
}
// Debian collects emails from paste.debian.net
func (c *Crawler) Debian(wg *sync.WaitGroup) {
defer wg.Done()
r := regexp.MustCompile(`<li><a href='//paste.debian.net(.*?)'>`)
url := "http://paste.debian.net"
page, err := c.FetchPage(url)
if err != nil {
report(err)
}
raws := r.FindAllString(page, -1)
if raws == nil {
if c.flags.verbose {
report(errors.New("no raw link"))
}
return
}
for _, v := range raws {
parser := strings.Split(v, `<li><a href='//`)
if len(parser) < 2 {
report(errors.New("can't parse"))
return
}
rawlink := "http://" + strings.Replace(parser[1], `'>`, "", -1)
page, err := c.FetchPage(rawlink)
if err != nil {
report(err)
return
}
c.GetMail(page)
}
}
// Slexy collects emails from slexy.org
func (c *Crawler) Slexy(wg *sync.WaitGroup) {
defer wg.Done()
r := regexp.MustCompile(`\/view(.*?)">`)
url := "http://slexy.org/recent"
page, err := c.FetchPage(url)
if err != nil {
report(err)
}
raws := r.FindAllString(page, -1)
if raws == nil {
if c.flags.verbose {
report(errors.New("no raw link"))
}
return
}
for _, v := range raws {
parser := strings.Split(v, `/view`)
if len(parser) < 2 {
report(errors.New("can't parse"))
return
}
rawlink := "http://slexy.org/raw" + strings.Replace(parser[1], `">`, "", -1)
page, err := c.FetchPage(rawlink)
if err != nil {
report(err)
return
}
c.GetMail(page)
}
}
func report(err error) {
fmt.Fprintln(os.Stderr, err)
}
// FreshFilter filters out invalid email addresses
func FreshFilter(mails []string) []string {
var fresh []string
for _, mail := range mails {
var blocked bool
if strings.Contains(mail, ".png") {
continue
}
if strings.Contains(mail, ".gif") {
continue
}
if strings.Contains(mail, ".jpg") {
continue
}
if strings.Contains(mail, "._") {
continue
}
if strings.Contains(mail, "@.") {
continue
}
if !strings.Contains(mail, ".") {
continue
}
for _, black := range blacklist {
if mail == black {
fmt.Println(mail, black)
blocked = true
}
}
if !blocked {
fresh = append(fresh, mail)
}
}
return fresh
}