-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisposable_email.go
81 lines (65 loc) · 1.8 KB
/
disposable_email.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
package validators
import (
"github.com/asaskevich/govalidator"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"strings"
"sync"
"time"
)
var (
mux_disp sync.Mutex
cache_disp []string
)
func IsDisposableEmailProvider(domain string) bool {
mux_disp.Lock()
if len(cache_disp) == 0 {
// Backup list file:
// https://raw.githubusercontent.com/zierric/validators/master/data/disposable_email_blocklist.conf
req, err := http.NewRequest("GET", "https://raw.githubusercontent.com/disposable-email-domains/disposable-email-domains/master/disposable_email_blocklist.conf", nil)
if err != nil {
panic(errors.Wrap(err, "IsDisposableEmail"))
}
resp, err := (&http.Client{
Timeout: 10 * time.Second,
}).Do(req)
if err != nil {
panic(errors.Wrap(err, "IsDisposableEmail"))
}
b, err := ioutil.ReadAll(resp.Body)
if err := resp.Body.Close(); err != nil {
panic(errors.Wrap(err, "IsDisposableEmail"))
}
lines := strings.Split(string(b), "\n")
for _, l := range lines {
if len(strings.Split(l, ".")) == 0 {
panic(errors.WithMessage(errors.New("invalid domain name"), "IsDisposableEmail"))
}
cache_disp = append(cache_disp, strings.TrimSpace(l))
}
if len(cache_disp) < 2 {
panic(errors.WithMessage(errors.New("no domains loaded"), "IsDisposableEmail"))
}
}
mux_disp.Unlock()
for _, v := range cache_disp {
if v == domain {
return true
}
}
return false
}
func IsDisposableEmail(email string) (bool, error) {
if !govalidator.IsEmail(email) {
return false, errors.WithMessage(errors.New("invalid email format"), "IsDisposableEmail")
}
e := strings.Split(email, "@")
if len(e) != 2 {
return false, errors.WithMessage(errors.New("invalid email format"), "IsDisposableEmail")
}
if IsDisposableEmailProvider(e[1]) {
return true, nil
}
return false, nil
}