-
Notifications
You must be signed in to change notification settings - Fork 159
/
smtp_by_api_yahoo.go
178 lines (161 loc) · 4.41 KB
/
smtp_by_api_yahoo.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
package emailverifier
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"regexp"
"strings"
"time"
)
const (
SIGNUP_PAGE = "https://login.yahoo.com/account/create?specId=yidregsimplified&lang=en-US&src=&done=https%3A%2F%2Fwww.yahoo.com&display=login"
SIGNUP_API = "https://login.yahoo.com/account/module/create?validateField=userId"
// USER_AGENT Fake one to use in API requests
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
)
// Check yahoo email exists by their login & registration page.
// See https://login.yahoo.com
// See https://login.yahoo.com/account/create
func newYahooAPIVerifier(client *http.Client) smtpAPIVerifier {
if client == nil {
client = http.DefaultClient
}
return yahoo{
client: client,
}
}
type yahoo struct {
client *http.Client
}
type yahooValidateReq struct {
Domain, Username, Acrumb, SessionIndex string
Cookies []*http.Cookie
}
type yahooErrorResp struct {
Errors []errItem `json:"errors"`
}
type errItem struct {
Name string `json:"name"`
Error string `json:"error"`
}
func (y yahoo) isSupported(host string) bool {
// FIXME Is this `contains` too lenient?
return strings.Contains(host, "yahoo")
}
func (y yahoo) check(domain, username string) (*SMTP, error) {
cookies, signUpPageRespBytes, err := y.toSignUpPage()
if err != nil {
return nil, err
}
if len(cookies) == 0 {
return nil, errors.New("yahoo check by api, no cookies")
}
acrumb := getAcrumb(cookies)
if acrumb == "" {
return nil, errors.New("yahoo check by api, no acrumb")
}
sessionIndex := getSessionIndex(signUpPageRespBytes)
if sessionIndex == "" {
return nil, errors.New("yahoo check by api, no sessionIndex")
}
yahooErrResp, err := y.sendValidateRequest(yahooValidateReq{
Domain: domain,
Username: username,
Acrumb: acrumb,
SessionIndex: sessionIndex,
Cookies: cookies,
})
if err != nil {
return nil, err
}
usernameExists := checkUsernameExists(yahooErrResp)
return &SMTP{
HostExists: true,
Deliverable: usernameExists,
}, nil
}
func getSessionIndex(respBytes []byte) string {
re := regexp.MustCompile(`value="([^"]+)" name="sessionIndex"`)
match := re.FindSubmatch(respBytes)
if len(match) > 1 {
return string(match[1])
}
return ""
}
func checkUsernameExists(resp yahooErrorResp) bool {
for _, item := range resp.Errors {
if item.Name == "userId" && item.Error == "IDENTIFIER_EXISTS" {
return true
}
}
return false
}
func (y yahoo) sendValidateRequest(req yahooValidateReq) (yahooErrorResp, error) {
var res yahooErrorResp
data, err := json.Marshal(struct {
Acrumb string `json:"acrumb"`
SpecId string `json:"specId"`
Yid string `json:"userId"`
SessionIndex string `json:"sessionIndex"`
YidDomain string `json:"yidDomain"`
}{
Acrumb: req.Acrumb,
SpecId: "yidregsimplified",
Yid: req.Username,
SessionIndex: req.SessionIndex,
YidDomain: req.Domain,
})
if err != nil {
return res, err
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
request, err := http.NewRequestWithContext(ctx, http.MethodPost, SIGNUP_API, bytes.NewReader(data))
if err != nil {
return res, err
}
for _, c := range req.Cookies {
request.AddCookie(c)
}
request.Header.Add("X-Requested-With", "XMLHttpRequest")
request.Header.Add("Content-Type", "application/json; charset=UTF-8")
resp, err := y.client.Do(request)
if err != nil {
return res, err
}
defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return res, err
}
return res, json.Unmarshal(respBytes, &res)
}
func (y yahoo) toSignUpPage() ([]*http.Cookie, []byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
request, err := http.NewRequestWithContext(ctx, http.MethodGet, SIGNUP_PAGE, nil)
if err != nil {
return nil, nil, err
}
request.Header.Add("User-Agent", USER_AGENT)
resp, err := y.client.Do(request)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
return resp.Cookies(), respBytes, err
}
func getAcrumb(cookies []*http.Cookie) string {
for _, c := range cookies {
re := regexp.MustCompile(`s=(?P<acrumb>[^;^&]*)`)
match := re.FindStringSubmatch(c.Value)
if len(match) > 1 {
return match[1]
}
}
return ""
}