forked from aiviaio/okex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
568 lines (496 loc) · 12.2 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package ws
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/drinkthere/okx"
"github.com/drinkthere/okx/events"
"github.com/gorilla/websocket"
"net"
"net/http"
"strings"
"sync"
"time"
)
// ClientWs is the websocket api client
//
// https://www.okx.com/docs-v5/en/#websocket-api
type ClientWs struct {
url map[bool]okx.BaseURL
apiKey string
secretKey []byte
passphrase string
conn map[bool]*websocket.Conn
mu map[bool]*sync.RWMutex
closed map[bool]bool
ctx context.Context
Cancel context.CancelFunc
DoneChan chan interface{}
ErrChan chan *events.Error
SubscribeChan chan *events.Subscribe
UnsubscribeCh chan *events.Unsubscribe
LoginChan chan *events.Login
SuccessChan chan *events.Success
sendChan map[bool]chan []byte
lastTransmit sync.Map
AuthRequested *time.Time
Authorized bool
Private *Private
Public *Public
Trade *Trade
WithIP string
}
const (
redialTick = 2 * time.Second
writeWait = 3 * time.Second
pongWait = 25 * time.Second
PingPeriod = 15 * time.Second
)
// NewClient returns a pointer to a fresh ClientWs
func NewClient(ctx context.Context, apiKey, secretKey, passphrase string, url map[bool]okx.BaseURL) *ClientWs {
ctx, cancel := context.WithCancel(ctx)
c := &ClientWs{
url: url,
apiKey: apiKey,
secretKey: []byte(secretKey),
passphrase: passphrase,
conn: make(map[bool]*websocket.Conn),
closed: make(map[bool]bool),
mu: map[bool]*sync.RWMutex{true: {}, false: {}},
ctx: ctx,
Cancel: cancel,
sendChan: map[bool]chan []byte{true: make(chan []byte, 3), false: make(chan []byte, 3)},
DoneChan: make(chan interface{}, 32),
}
c.Private = NewPrivate(c)
c.Public = NewPublic(c)
c.Trade = NewTrade(c)
now := time.Now()
c.lastTransmit.Store(true, &now)
c.lastTransmit.Store(false, &now)
return c
}
func NewClientWithIP(ctx context.Context, apiKey, secretKey, passphrase string, url map[bool]okx.BaseURL, ip string) *ClientWs {
ctx, cancel := context.WithCancel(ctx)
c := &ClientWs{
url: url,
apiKey: apiKey,
secretKey: []byte(secretKey),
passphrase: passphrase,
conn: make(map[bool]*websocket.Conn),
closed: make(map[bool]bool),
mu: map[bool]*sync.RWMutex{true: {}, false: {}},
ctx: ctx,
Cancel: cancel,
sendChan: map[bool]chan []byte{true: make(chan []byte, 3), false: make(chan []byte, 3)},
DoneChan: make(chan interface{}, 32),
WithIP: ip,
}
c.Private = NewPrivate(c)
c.Public = NewPublic(c)
c.Trade = NewTrade(c)
now := time.Now()
c.lastTransmit.Store(true, &now)
c.lastTransmit.Store(false, &now)
return c
}
// Connect into the server
//
// https://www.okx.com/docs-v5/en/#websocket-api-connect
func (c *ClientWs) Connect(p bool) error {
if c.CheckConnect(p) {
return nil
}
err := c.dial(p)
if err == nil {
return nil
}
ticker := time.NewTicker(redialTick)
defer ticker.Stop()
for {
select {
case <-ticker.C:
err = c.dial(p)
if err == nil {
return nil
}
case <-c.ctx.Done():
return c.handleCancel("connect")
}
}
}
// CheckConnect into the server
func (c *ClientWs) CheckConnect(p bool) bool {
c.mu[p].RLock()
defer c.mu[p].RUnlock()
if c.conn[p] != nil && !c.closed[p] {
return true
}
return false
}
// Login
//
// https://www.okx.com/docs-v5/en/#websocket-api-login
func (c *ClientWs) Login() error {
if c.Authorized {
return nil
}
if c.AuthRequested != nil && time.Since(*c.AuthRequested).Seconds() < 30 {
return nil
}
now := time.Now()
c.AuthRequested = &now
method := http.MethodGet
path := "/users/self/verify"
ts, sign := c.sign(method, path)
args := []map[string]string{
{
"apiKey": c.apiKey,
"passphrase": c.passphrase,
"timestamp": ts,
"sign": sign,
},
}
return c.Send(true, okx.LoginOperation, args)
}
// Subscribe
// Users can choose to subscribe to one or more channels, and the total length of multiple channels cannot exceed 4096 bytes.
//
// https://www.okx.com/docs-v5/en/#websocket-api-subscribe
func (c *ClientWs) Subscribe(p bool, ch []okx.ChannelName, args map[string]string) error {
count := 1
if len(ch) != 0 {
count = len(ch)
}
tmpArgs := make([]map[string]string, count)
tmpArgs[0] = args
for i, name := range ch {
tmpArgs[i] = map[string]string{}
tmpArgs[i]["channel"] = string(name)
for k, v := range args {
tmpArgs[i][k] = v
}
}
return c.Send(p, okx.SubscribeOperation, tmpArgs)
}
// Unsubscribe into channel(s)
//
// https://www.okx.com/docs-v5/en/#websocket-api-unsubscribe
func (c *ClientWs) Unsubscribe(p bool, ch []okx.ChannelName, args map[string]string) error {
tmpArgs := make([]map[string]string, len(ch))
for i, name := range ch {
tmpArgs[i] = make(map[string]string)
tmpArgs[i]["channel"] = string(name)
for k, v := range args {
tmpArgs[i][k] = v
}
}
return c.Send(p, okx.UnsubscribeOperation, tmpArgs)
}
// Send message through either connections
func (c *ClientWs) Send(p bool, op okx.Operation, args []map[string]string, extras ...map[string]string) error {
if op != okx.LoginOperation {
err := c.Connect(p)
if err == nil {
if p {
err = c.WaitForAuthorization()
if err != nil {
return err
}
}
} else {
return err
}
}
data := map[string]interface{}{
"op": op,
"args": args,
}
for _, extra := range extras {
for k, v := range extra {
data[k] = v
}
}
j, err := json.Marshal(data)
if err != nil {
return err
}
c.mu[p].RLock()
c.sendChan[p] <- j
c.mu[p].RUnlock()
return nil
}
// SetChannels to receive certain events on separate channel
func (c *ClientWs) SetChannels(errCh chan *events.Error, subCh chan *events.Subscribe, unSub chan *events.Unsubscribe, lCh chan *events.Login, sCh chan *events.Success) {
c.ErrChan = errCh
c.SubscribeChan = subCh
c.UnsubscribeCh = unSub
c.LoginChan = lCh
c.SuccessChan = sCh
}
// SetErrChannel set error channel
func (c *ClientWs) SetErrChannel(errCh chan *events.Error) {
c.ErrChan = errCh
}
// SetLoginChannel set error channel
func (c *ClientWs) SetLoginChannel(lCh chan *events.Login) {
c.LoginChan = lCh
}
// WaitForAuthorization waits for the auth response and try to log in if it was needed
func (c *ClientWs) WaitForAuthorization() error {
if c.Authorized {
return nil
}
if err := c.Login(); err != nil {
return err
}
ticker := time.NewTicker(time.Millisecond * 300)
defer ticker.Stop()
for range ticker.C {
if c.Authorized {
return nil
}
}
return nil
}
func (c *ClientWs) dial(p bool) error {
c.mu[p].Lock()
var dialer websocket.Dialer
if c.WithIP != "" {
dialer = websocket.Dialer{
NetDial: func(network, addr string) (net.Conn, error) {
localAddr, err := net.ResolveTCPAddr("tcp", c.WithIP+":0") // 替换为您的出口IP地址
if err != nil {
return nil, err
}
d := net.Dialer{
LocalAddr: localAddr,
}
return d.Dial(network, addr)
},
HandshakeTimeout: 45 * time.Second,
EnableCompression: false,
}
} else {
dialer = websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
EnableCompression: false,
}
}
conn, res, err := dialer.Dial(string(c.url[p]), nil)
if err != nil {
var statusCode int
if res != nil {
statusCode = res.StatusCode
}
c.mu[p].Unlock()
return fmt.Errorf("error %d: %w", statusCode, err)
}
defer res.Body.Close()
go func() {
defer func() {
// Cleaning the connection with ws
c.Cancel()
c.mu[p].Lock()
c.conn[p].Close()
c.closed[p] = true
fmt.Printf("receiver connection closed\n")
c.mu[p].Unlock()
}()
err := c.receiver(p)
if err != nil {
if !strings.Contains(err.Error(), "operation cancelled: receiver") {
c.ErrChan <- &events.Error{
Event: "error",
Msg: err.Error(),
}
}
fmt.Printf("receiver error: %v\n", err)
}
}()
go func() {
defer func() {
// Cleaning the connection with ws
c.Cancel()
c.mu[p].Lock()
c.conn[p].Close()
c.closed[p] = true
fmt.Printf("sender connection closed\n")
c.mu[p].Unlock()
}()
err := c.sender(p)
if err != nil {
if !strings.Contains(err.Error(), "operation cancelled: sender") {
c.ErrChan <- &events.Error{
Event: "error",
Msg: err.Error(),
}
}
fmt.Printf("sender error: %v\n", err)
c.Authorized = false
}
}()
c.conn[p] = conn
c.closed[p] = false
c.mu[p].Unlock()
return nil
}
func (c *ClientWs) sender(p bool) error {
ticker := time.NewTicker(time.Millisecond * 300)
defer ticker.Stop()
for {
c.mu[p].RLock()
dataChan := c.sendChan[p]
c.mu[p].RUnlock()
select {
case data := <-dataChan:
c.mu[p].RLock()
err := c.conn[p].SetWriteDeadline(time.Now().Add(writeWait))
if err != nil {
c.mu[p].RUnlock()
return fmt.Errorf("failed to set write deadline for ws connection, error: %w", err)
}
w, err := c.conn[p].NextWriter(websocket.TextMessage)
if err != nil {
c.mu[p].RUnlock()
return fmt.Errorf("failed to get next writer for ws connection, error: %w", err)
}
if _, err = w.Write(data); err != nil {
c.mu[p].RUnlock()
return fmt.Errorf("failed to write data via ws connection, error: %w", err)
}
c.mu[p].RUnlock()
if err := w.Close(); err != nil {
return fmt.Errorf("failed to close ws connection, error: %w", err)
}
case <-ticker.C:
lastTransmitInterface, _ := c.lastTransmit.Load(p)
lastTransmit := lastTransmitInterface.(*time.Time)
c.mu[p].RLock()
if c.conn[p] != nil && (lastTransmit == nil || (lastTransmit != nil && time.Since(*lastTransmit) > PingPeriod)) {
go func() {
c.mu[p].RLock()
c.sendChan[p] <- []byte("ping")
c.mu[p].RUnlock()
}()
}
c.mu[p].RUnlock()
case <-c.ctx.Done():
return c.handleCancel("sender")
}
}
}
func (c *ClientWs) receiver(p bool) error {
for {
select {
case <-c.ctx.Done():
return c.handleCancel("receiver")
default:
c.mu[p].RLock()
err := c.conn[p].SetReadDeadline(time.Now().Add(pongWait))
if err != nil {
c.mu[p].RUnlock()
return fmt.Errorf("failed to set read deadline for ws connection, error: %w", err)
}
mt, data, err := c.conn[p].ReadMessage()
if err != nil {
c.mu[p].RUnlock()
return fmt.Errorf("failed to read message from ws connection, error: %v\n", err)
}
c.mu[p].RUnlock()
now := time.Now()
c.lastTransmit.Store(p, &now)
if mt == websocket.TextMessage && string(data) != "pong" {
e := &events.Basic{}
if err := json.Unmarshal(data, e); err != nil {
return fmt.Errorf("failed to unmarshall message from ws, error: %w", err)
}
go c.process(data, e)
}
}
}
}
func (c *ClientWs) sign(method, path string) (string, string) {
t := time.Now().UTC().Unix()
ts := fmt.Sprint(t)
s := ts + method + path
p := []byte(s)
h := hmac.New(sha256.New, c.secretKey)
h.Write(p)
return ts, base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func (c *ClientWs) handleCancel(msg string) error {
go func() {
c.DoneChan <- msg
}()
return fmt.Errorf("operation cancelled: %s", msg)
}
// TODO: break each case into a separate function
func (c *ClientWs) process(data []byte, e *events.Basic) bool {
switch e.Event {
case "error":
e := events.Error{}
_ = json.Unmarshal(data, &e)
go func() {
if c.ErrChan != nil {
c.ErrChan <- &e
}
}()
return true
case "subscribe":
e := events.Subscribe{}
_ = json.Unmarshal(data, &e)
if c.SubscribeChan != nil {
c.SubscribeChan <- &e
}
return true
case "unsubscribe":
e := events.Unsubscribe{}
_ = json.Unmarshal(data, &e)
go func() {
if c.UnsubscribeCh != nil {
c.UnsubscribeCh <- &e
}
}()
return true
case "login":
if time.Since(*c.AuthRequested).Seconds() > 30 {
c.AuthRequested = nil
_ = c.Login()
break
}
c.Authorized = true
e := events.Login{}
_ = json.Unmarshal(data, &e)
go func() {
if c.LoginChan != nil {
c.LoginChan <- &e
}
}()
return true
}
if c.Private.Process(data, e) {
return true
}
if c.Public.Process(data, e) {
return true
}
if e.ID != "" {
if e.Code != 0 {
ee := *e
ee.Event = "error"
return c.process(data, &ee)
}
e := events.Success{}
_ = json.Unmarshal(data, &e)
if c.SuccessChan != nil {
c.SuccessChan <- &e
}
return true
}
return false
}