-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
189 lines (157 loc) · 4.14 KB
/
client.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
package mopan
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"runtime"
"strings"
"sync/atomic"
"github.com/go-resty/resty/v2"
"github.com/hashicorp/go-version"
)
type RestyOption func(request *resty.Request)
type Json map[string]any
func NewMoClientWithAuthorization(authorization string) *MoClient {
return NewMoClient().SetAuthorization(authorization)
}
func NewMoClientWithRestyClient(client *resty.Client) *MoClient {
return &MoClient{
Client: client,
DeviceInfo: DefaultDeviceInfo,
}
}
func NewMoClient() *MoClient {
return &MoClient{
Client: resty.New(),
DeviceInfo: DefaultDeviceInfo,
}
}
type MoClient struct {
Authorization string
DeviceInfo DeviceInfo
Client *resty.Client
onAuthorizationExpired func(err error) error
flag int32
}
// 当Token失效时回调
func (c *MoClient) SetOnAuthorizationExpired(f func(err error) error) *MoClient {
c.onAuthorizationExpired = f
return c
}
func (c *MoClient) SetDeviceInfo(info *DeviceInfo) *MoClient {
if info != nil {
c.DeviceInfo = *info
}
return c
}
func (c *MoClient) GetDeviceInfo() DeviceInfo {
return c.DeviceInfo
}
func (c *MoClient) SetAuthorization(authorization string) *MoClient {
if !strings.HasPrefix(authorization, "Bearer") {
authorization = "Bearer " + authorization
}
c.Authorization = authorization
return c
}
func (c *MoClient) SetClient(client *http.Client) *MoClient {
c.Client = resty.NewWithClient(client)
return c
}
func (c *MoClient) SetRestyClient(client *resty.Client) *MoClient {
c.Client = client
return c
}
func (c *MoClient) SetProxy(proxy string) *MoClient {
c.Client.SetProxy(proxy)
return c
}
func (c *MoClient) request(url string, data Json, resp any, option ...RestyOption) ([]byte, error) {
secretKey := GetSecretKey()
headers := map[string]string{
"Authorization": c.Authorization,
"remoteInfo": c.DeviceInfo.Encrypt(secretKey),
}
ver := version.Must(version.NewVersion(c.DeviceInfo.MpVersion))
if ver.GreaterThanOrEqual(version.Must(version.NewVersion("1.1.202"))) {
encryptedKey := MustRsaEncryptBase64Str(secretKey, PublicKeyV2)
headers["version"] = c.DeviceInfo.MpVersion
headers["encrypted-key"] = encryptedKey
} else {
encryptedKey := MustRsaEncryptBase64Str(secretKey, PublicKeyV1)
headers["encrypted-key"] = encryptedKey
}
req := c.Client.R().SetHeaders(headers)
if data != nil {
req.SetHeader("Content-Type", "application/json")
temp, err := c.Client.JSONMarshal(data)
if err != nil {
return nil, err
}
enc, _ := AesEncryptBase64(temp, []byte(secretKey))
req.SetBody(enc)
}
for _, opt := range option {
opt(req)
}
resp_, err := req.Post(url)
if err != nil {
return nil, err
}
body := resp_.Body()
// 解密数据
if bytes.HasPrefix(body, []byte{'"'}) && bytes.HasSuffix(body, []byte{'"'}) {
body, err = AesDecryptBase64(bytes.Trim(body, "\""), []byte(secretKey))
if err != nil {
return nil, err
}
}
var result Resp
c.Client.JSONUnmarshal(body, &result)
if resp_.StatusCode() == http.StatusUnauthorized {
result.Code = 401
}
if result.Code != 200 {
return nil, &result
}
if resp != nil {
if err := c.Client.JSONUnmarshal(result.Data, &resp); err != nil {
return nil, err
}
}
return result.Data, nil
}
func (c *MoClient) Request(url string, data Json, resp any, option ...RestyOption) ([]byte, error) {
v, err := c.request(url, data, resp, option...)
if err != nil {
if err, ok := err.(*Resp); ok {
// 401 错误处理
if err.Code == 401 && c.onAuthorizationExpired != nil {
if atomic.CompareAndSwapInt32(&c.flag, 0, 1) {
err2 := c.onAuthorizationExpired(err)
atomic.SwapInt32(&c.flag, 0)
if err2 != nil {
return nil, errors.Join(err, err2)
}
}
for atomic.LoadInt32(&c.flag) != 0 {
runtime.Gosched()
}
return c.request(url, data, resp, option...)
}
}
return nil, err
}
return v, nil
}
type Resp struct {
Code int64 `json:"code"`
Message string `json:"message"`
Data json.RawMessage `json:"data"`
Status bool `json:"status"`
}
func (r *Resp) Error() string {
return fmt.Sprintf("Code:%d, Message:%s", r.Code, r.Message)
}