-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
357 lines (295 loc) · 7.79 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package nacos
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
)
const (
contentType = "application/x-www-form-urlencoded;charset=utf-8"
loginApi = "/nacos/v1/auth/login"
getApi = "/nacos/v2/cs/config"
putApi = "/nacos/v2/cs/config"
)
type AuthType string
const (
AccessTokenAuth AuthType = "AccessTokenAuth"
AccessKeyAuth AuthType = "AccessKeyAuth"
)
type Client struct {
httpClient *http.Client
endpoint string
accessToken string
tokenTTL time.Time
username string
password string
accessKeyId string
accessKeySecret string
authType AuthType
logger *slog.Logger
pollTime time.Duration
lock sync.RWMutex
}
type Option func(c *Client)
func WithAccessTokenAuth(username, password string) Option {
return func(c *Client) {
c.username = username
c.password = password
c.authType = AccessTokenAuth
}
}
func WithAccessKeyAuth(accessKeyId, accessKeySecret string) Option {
return func(c *Client) {
c.accessKeyId = accessKeyId
c.accessKeySecret = accessKeySecret
c.authType = AccessKeyAuth
}
}
func WithPullTime(t time.Duration) Option {
return func(c *Client) {
c.pollTime = t
}
}
func WithHttpClient(h *http.Client) Option {
return func(c *Client) {
c.httpClient = h
}
}
func WithLogger(l *slog.Logger) Option {
return func(c *Client) {
c.logger = l
}
}
type getResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data string `json:"data"`
}
type putResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data bool `json:"data"`
}
type loginResponse struct {
AccessToken string `json:"accessToken"`
TokenTTL int `json:"tokenTtl"`
GlobalAdmin bool `json:"globalAdmin"`
}
func NewClient(endpoint string, options ...func(c *Client)) *Client {
nc := &Client{
endpoint: endpoint,
httpClient: http.DefaultClient,
lock: sync.RWMutex{},
logger: slog.Default(),
pollTime: 10 * time.Second,
}
for _, option := range options {
option(nc)
}
if nc.authType == AccessKeyAuth && (nc.accessKeyId == "" || nc.accessKeySecret == "") {
panic("nacos AccessKey auth access_key or access_key_secret is empty")
}
if nc.authType == AccessTokenAuth && (nc.username == "" || nc.password == "") {
panic("nacos AccessToken auth username or password is empty")
}
return nc
}
func (n *Client) login() error {
if n.authType != AccessTokenAuth {
return nil
}
n.logger.Debug(fmt.Sprintf("nacos login server:[%s:%s]", n.endpoint, n.username))
v := url.Values{}
v.Add("username", n.username)
v.Add("password", n.password)
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", n.endpoint, loginApi), strings.NewReader(v.Encode()))
if err != nil {
return err
}
req.Header.Add("Content-Type", contentType)
resp, err := n.httpClient.Do(req)
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
bb, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("nacos login fail:%s", string(bb))
}
loginResp := &loginResponse{}
if err := json.Unmarshal(bb, loginResp); err != nil {
return err
}
n.accessToken = loginResp.AccessToken
n.tokenTTL = time.Now().Add(time.Duration(loginResp.TokenTTL-600) * time.Second)
return nil
}
func (n *Client) getAccessToken() {
if n.authType != AccessTokenAuth {
return
}
if n.tokenTTL.After(time.Now()) {
return
}
n.lock.Lock()
defer n.lock.Unlock()
err := n.login()
if err != nil {
log.Panicf("[nacos] login error:%s", err.Error())
}
}
func (n *Client) Put(namespace, group, dataId string, content string) error {
n.logger.Debug(fmt.Sprintf("nacos get config:[namespace:%s,group:%s,dataId:%s]", namespace, group, dataId))
v := url.Values{}
v.Add("tenant", namespace)
v.Add("namespaceId", namespace)
v.Add("group", group)
v.Add("dataId", dataId)
v.Add("content", content)
if n.authType == AccessTokenAuth {
n.getAccessToken()
v.Add("accessToken", n.accessToken)
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", n.endpoint, putApi), strings.NewReader(v.Encode()))
if err != nil {
return err
}
timeStamp := strconv.FormatInt(time.Now().UnixMilli(), 10)
req.Header.Add("Content-Type", contentType)
req.Header.Add("Timestamp", timeStamp)
if n.authType == AccessKeyAuth {
req.Header.Add("Spas-AccessKey", n.accessKeyId)
req.Header.Add("Spas-Signature", signSha1(namespace+"+"+group+"+"+timeStamp, n.accessKeySecret))
}
resp, err := n.httpClient.Do(req)
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
bb, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("nacos body read fail:%w", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("nacos put fail:%s", string(bb))
}
ret := &putResponse{}
err = json.Unmarshal(bb, ret)
if err != nil {
return fmt.Errorf("nacos response unmarshal fail:%w", err)
}
if ret.Code != 0 {
return fmt.Errorf("nacos put fail:%s", string(bb))
}
return nil
}
func (n *Client) Get(namespace, group, dataId string) (string, error) {
n.logger.Debug(fmt.Sprintf("nacos get config:[namespace:%s,group:%s,dataId:%s]", namespace, group, dataId))
v := url.Values{}
v.Add("tenant", namespace)
v.Add("namespaceId", namespace)
v.Add("group", group)
v.Add("dataId", dataId)
if n.authType == AccessTokenAuth {
n.getAccessToken()
v.Add("accessToken", n.accessToken)
}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s%s?", n.endpoint, getApi)+v.Encode(), nil)
if err != nil {
return "", err
}
timeStamp := strconv.FormatInt(time.Now().UnixMilli(), 10)
req.Header.Add("Content-Type", contentType)
req.Header.Add("Timestamp", timeStamp)
if n.authType == AccessKeyAuth {
req.Header.Add("Spas-AccessKey", n.accessKeyId)
req.Header.Add("Spas-Signature", signSha1(namespace+"+"+group+"+"+timeStamp, n.accessKeySecret))
}
resp, err := n.httpClient.Do(req)
if err != nil {
return "", err
}
defer func() {
_ = resp.Body.Close()
}()
bb, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("nacos body read fail:%w", err)
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("nacos get fail:%s", string(bb))
}
ret := &getResponse{}
err = json.Unmarshal(bb, ret)
if err != nil || ret.Data == "" {
return string(bb), nil
}
if ret.Code != 0 {
return "", fmt.Errorf("nacos get fail:%s", string(bb))
}
return ret.Data, nil
}
func (n *Client) ListenAsync(namespace, group, dataId string, fn func(cnf string)) {
ret, err := n.Get(namespace, group, dataId)
if err != nil {
panic(err)
}
contentMd5 := md5string(ret)
go func() {
t1 := time.NewTicker(3600 * time.Second)
t2 := time.NewTicker(n.pollTime)
for {
select {
// token到期刷新
case <-t1.C:
if err := n.login(); err != nil {
n.logger.Error("[nacos] login error", slog.Any("error", err))
}
// 每60秒监听配置
case <-t2.C:
update, err := n.Get(namespace, group, dataId)
if err != nil {
n.logger.Error("[nacos] listen error", slog.Any("error", err))
continue
}
newMd5 := md5string(update)
if newMd5 != contentMd5 {
n.logger.Info(fmt.Sprintf("nacos listen refresh:[namespace:%s,group:%s,dataId:%s,md5:%s,newMd5:%s]", namespace, group, dataId, contentMd5, newMd5))
contentMd5 = newMd5
fn(update)
}
}
}
}()
}
func md5string(text string) string {
algorithm := md5.New()
algorithm.Write([]byte(text))
return hex.EncodeToString(algorithm.Sum(nil))
}
func signSha1(encryptText, encryptKey string) string {
// hmac ,use sha1
key := []byte(encryptKey)
mac := hmac.New(sha1.New, key)
mac.Write([]byte(encryptText))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}