-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostshift.go
169 lines (142 loc) · 3.76 KB
/
postshift.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
package tmpemail
import (
"context"
"encoding/json"
"errors"
"fmt"
"golang.org/x/net/proxy"
"log"
"net"
"net/http"
"time"
)
type PostShift struct {
TmpEmailConf
email string
key string
conf *TmpEmailConf
ctx context.Context
}
func (t *PostShift) Create(conf *TmpEmailConf) ITmpEmail {
t.conf = conf
return t
}
func (t *PostShift) NewRegistration() error {
if body, err := t.getResponse("https://post-shift.ru/api.php?action=new&type=json"); err != nil {
log.Printf("Регистрация нового email. Ошибка:\n %q \n", err.Error())
return err
} else {
tmp := map[string]interface{}{}
if err := json.Unmarshal(body, &tmp); err != nil {
return fmt.Errorf("Регистрация нового email. Ошибка сериализации json: %q \n", err.Error())
}
if e, ok := tmp["error"]; ok {
return errors.New(e.(string))
}
t.email = tmp["email"].(string)
t.key = tmp["key"].(string)
t.conf.Result <- &Result{
Email: t.email,
}
// запускаем горутину что б она проверяла входящие письма
//if confirm {
// if t.conf.Activation == nil {
// return errors.New("Должна быть задана функция активации")
// }
// t.ctx, _ = context.WithTimeout(context.Background(), t.conf.Timeout)
//
// go t.watcherMail()
//} else {
// t.deleteEmail()
// close(t.conf.Result)
//}
}
return nil
}
func (t *PostShift) watcherMail() {
tick := time.NewTicker(time.Second * 2)
defer tick.Stop()
checked := map[int]bool{}
//FOR:
for range tick.C {
if t.readInBox(checked) {
t.deleteEmail()
t.conf.Result <- &Result{
Email: t.email,
}
close(t.conf.Result)
break
}
if errors.Is(t.ctx.Err(), context.DeadlineExceeded) {
t.deleteEmail()
t.conf.Result <- &Result{
Error: errors.New("Прервано по таймауту"),
}
close(t.conf.Result)
break
}
//select {
//case <-t.ctx.Done():
// break FOR
//case <-tick.C:
//}
}
}
func (t *PostShift) readInBox(checked map[int]bool) (result bool) {
// EAFP
defer func() {
if err := recover(); err != nil {
result = false
}
}()
if body, err := t.getResponse(fmt.Sprintf("https://post-shift.ru/api.php?action=getlist&key=%v&type=json", t.key)); err == nil {
tmp := []map[string]interface{}{}
if err := json.Unmarshal(body, &tmp); err != nil {
return false
}
for _, body := range tmp {
if from, ok := body["from"]; ok {
id := int(body["id"].(float64))
if !checked[id] {
checked[id] = true
return t.readEmail(from.(string), id)
}
}
}
}
return false
}
func (t *PostShift) httpClient(timeout time.Duration) *http.Client {
httpTransport := &http.Transport{}
if t.conf.Proxy != nil {
httpTransport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
select {
case <-ctx.Done():
return nil, nil
default:
}
dialer, err := proxy.SOCKS5("tcp", t.conf.Proxy.address+":"+t.conf.Proxy.port, nil, proxy.Direct)
if err != nil {
//logrus.WithField("Прокси", net_.PROXY_ADDR).Errorf("Ошибка соединения с прокси: %q", err)
return nil, err
}
return dialer.Dial(network, addr)
}
}
return &http.Client{
Timeout: timeout,
Transport: httpTransport,
}
}
func (t *PostShift) readEmail(from string, id int) bool {
if body, err := t.getResponse(fmt.Sprintf("https://post-shift.ru/api.php?action=getmail&key=%v&id=%d", t.key, id)); err == nil {
return t.conf.Activation(from, string(body))
}
return false
}
func (t *PostShift) deleteEmail() {
t.getResponse(fmt.Sprintf("https://post-shift.ru/api.php?action=delete&key=%v", t.key))
}
func (t *PostShift) DeleteAllEmail() {
t.getResponse("https://post-shift.ru/api.php?action=deleteall")
}