forked from KevinGong2013/wechat
/
wechat.go
294 lines (240 loc) · 6.05 KB
/
wechat.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 wechat
import (
"bytes"
"crypto/tls"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
var logger = log.WithFields(log.Fields{
"module": "wechat",
})
const httpOK = `200`
// BaseRequest is a base for all wx api request.
type BaseRequest struct {
XMLName xml.Name `xml:"error" json:"-"`
Ret int `xml:"ret" json:"-"`
Message string `xml:"message" json:"-"`
Wxsid string `xml:"wxsid" json:"Sid"`
Skey string `xml:"skey"`
DeviceID string `xml:"-"`
Wxuin int64 `xml:"wxuin" json:"Uin"`
PassTicket string `xml:"pass_ticket" json:"-"`
}
// Caller is a interface, All response need implement this.
type Caller interface {
IsSuccess() bool
Error() error
}
// Response is a wrapper.
type Response struct {
BaseResponse *BaseResponse
}
// IsSuccess flag this request is success or failed.
func (response *Response) IsSuccess() bool {
return response.BaseResponse.Ret == 0
}
// response's error msg.
func (response *Response) Error() error {
return fmt.Errorf("error message:[%s]", response.BaseResponse.ErrMsg)
}
// BaseResponse for all api resp.
type BaseResponse struct {
Ret int
ErrMsg string
}
// Configure ...
type Configure struct {
Processor UUIDProcessor
Debug bool
CachePath string
UniqueGroupMember bool
version string
}
// DefaultConfigure create default configuration
func DefaultConfigure() *Configure {
return &Configure{
Processor: new(defaultUUIDProcessor),
Debug: true,
UniqueGroupMember: true,
CachePath: `.wechat/debug`,
version: `1.0.1-rc1`,
}
}
func (c *Configure) contactCachePath() string {
return filepath.Join(c.CachePath, `contact-cache.json`)
}
func (c *Configure) baseInfoCachePath() string {
return filepath.Join(c.CachePath, `basic-info-cache.json`)
}
func (c *Configure) cookieCachePath() string {
return filepath.Join(c.CachePath, `cookie-cache.json`)
}
func (c *Configure) httpDebugPath(url *url.URL) string {
ps := strings.Split(url.Path, `/`)
lastP := strings.Split(ps[len(ps)-1], `?`)[0][5:]
return c.CachePath + `/` + lastP
}
// WeChat container a default http client and base request.
type WeChat struct {
Client *http.Client
BaseURL string
BaseRequest *BaseRequest
MySelf Contact
IsLogin bool
conf *Configure
evtStream *evtStream
cache *cache
syncKey map[string]interface{}
syncHost string
retryTimes time.Duration
loginState chan int // -1 登录失败 1登录成功
}
// NewWeChat is designed for Create a new Wechat instance.
func newWeChat(conf *Configure) (*WeChat, error) {
if _, err := os.Stat(conf.CachePath); err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(conf.CachePath, os.ModePerm)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
client, err := newClient()
if err != nil {
return nil, err
}
baseReq := new(BaseRequest)
baseReq.Ret = 1
baseReq.DeviceID = `e999471493880231`
wechat := &WeChat{
Client: client,
BaseRequest: baseReq,
evtStream: newEvtStream(),
IsLogin: false,
retryTimes: time.Duration(0),
loginState: make(chan int),
conf: conf,
cache: newCache(),
}
return wechat, nil
}
func newClient() (*http.Client, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
transport := http.Transport{
Dial: (&net.Dialer{
Timeout: 1 * time.Minute,
}).Dial,
TLSHandshakeTimeout: 1 * time.Minute,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := &http.Client{
Transport: &transport,
Jar: jar,
Timeout: 1 * time.Minute,
}
return client, nil
}
// NewBot is start point for wx bot.
func NewBot(conf *Configure) (*WeChat, error) {
if conf == nil {
conf = DefaultConfigure()
}
wechat, err := newWeChat(conf)
if err != nil {
return nil, err
}
wechat.evtStream.init()
go func() {
for {
ls := <-wechat.loginState
event := Event{
Path: `/login`,
From: `Wechat`,
To: `End`,
Data: ls,
Time: time.Now().Unix(),
}
wechat.evtStream.serverEvt <- event
}
}()
wechat.keepAlive()
if conf.Debug {
log.SetLevel(log.DebugLevel)
}
return wechat, nil
}
// ExecuteRequest is designed for perform http request
func (wechat *WeChat) ExecuteRequest(req *http.Request, call Caller) error {
filename := wechat.conf.httpDebugPath(req.URL)
if wechat.conf.Debug {
reqData, _ := httputil.DumpRequestOut(req, false)
createFile(filename+`_req.json`, reqData, false)
c, _ := json.Marshal(wechat.Client.Jar.Cookies(req.URL))
createFile(filename+`_req.json`, c, true)
}
resp, err := wechat.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
reader := resp.Body.(io.Reader)
if wechat.conf.Debug {
data, e := ioutil.ReadAll(reader)
if e != nil {
return e
}
createFile(filename+`_resp.json`, data, true)
reader = bytes.NewReader(data)
}
if err = json.NewDecoder(reader).Decode(call); err != nil {
return err
}
if !call.IsSuccess() {
return call.Error()
}
wechat.refreshBaseInfo()
wechat.refreshCookieCache(resp.Cookies())
return nil
}
// Execute a http request by default http client.
func (wechat *WeChat) Execute(path string, body io.Reader, call Caller) error {
method := "GET"
if body != nil {
method = "POST"
}
req, err := http.NewRequest(method, path, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
req.Header.Set(`User-Agent`, `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0.2 Safari/602.3.12`)
return wechat.ExecuteRequest(req, call)
}
// PassTicketKV return a string like `pass_ticket=1234s`
func (wechat *WeChat) PassTicketKV() string {
return fmt.Sprintf(`pass_ticket=%s`, wechat.BaseRequest.PassTicket)
}
// SkeyKV return a string like `skey=1234s`
func (wechat *WeChat) SkeyKV() string {
return fmt.Sprintf(`skey=%s`, wechat.BaseRequest.Skey)
}