forked from amir-the-h/okex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
416 lines (402 loc) · 9.61 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
package ws
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/amir-the-h/okex"
"github.com/amir-the-h/okex/events"
"github.com/gorilla/websocket"
"net/http"
"sync"
"time"
)
// ClientWs is the websocket api client
//
// https://www.okex.com/docs-v5/en/#websocket-api
type ClientWs struct {
Cancel context.CancelFunc
DoneChan chan interface{}
StructuredEventChan chan interface{}
RawEventChan chan *events.Basic
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
url map[bool]okex.BaseURL
conn map[bool]*websocket.Conn
apiKey string
secretKey []byte
passphrase string
lastTransmit map[bool]*time.Time
mu map[bool]*sync.RWMutex
AuthRequested *time.Time
Authorized bool
Private *Private
Public *Public
Trade *Trade
ctx context.Context
}
const (
redialTick = 2 * time.Second
writeWait = 3 * time.Second
pongWait = 30 * time.Second
PingPeriod = (pongWait * 8) / 10
)
// NewClient returns a pointer to a fresh ClientWs
func NewClient(ctx context.Context, apiKey, secretKey, passphrase string, url map[bool]okex.BaseURL) *ClientWs {
ctx, cancel := context.WithCancel(ctx)
c := &ClientWs{
apiKey: apiKey,
secretKey: []byte(secretKey),
passphrase: passphrase,
ctx: ctx,
Cancel: cancel,
url: url,
sendChan: map[bool]chan []byte{true: make(chan []byte, 3), false: make(chan []byte, 3)},
DoneChan: make(chan interface{}),
StructuredEventChan: make(chan interface{}),
RawEventChan: make(chan *events.Basic),
conn: make(map[bool]*websocket.Conn),
lastTransmit: make(map[bool]*time.Time),
mu: map[bool]*sync.RWMutex{true: {}, false: {}},
}
c.Private = NewPrivate(c)
c.Public = NewPublic(c)
c.Trade = NewTrade(c)
return c
}
// Connect into the server
//
// https://www.okex.com/docs-v5/en/#websocket-api-connect
func (c *ClientWs) Connect(p bool) error {
if c.conn[p] != nil {
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")
}
}
}
// Login
//
// https://www.okex.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, okex.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.okex.com/docs-v5/en/#websocket-api-subscribe
func (c *ClientWs) Subscribe(p bool, ch []okex.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, okex.SubscribeOperation, tmpArgs)
}
// Unsubscribe into channel(s)
//
// https://www.okex.com/docs-v5/en/#websocket-api-unsubscribe
func (c *ClientWs) Unsubscribe(p bool, ch []okex.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, okex.UnsubscribeOperation, tmpArgs)
}
// Send message through either connections
func (c *ClientWs) Send(p bool, op okex.Operation, args []map[string]string, extras ...map[string]string) error {
if op != okex.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.sendChan[p] <- j
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
}
// 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()
conn, res, err := websocket.DefaultDialer.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() {
err := c.receiver(p)
if err != nil {
fmt.Printf("receiver error: %v\n", err)
}
}()
go func() {
err := c.sender(p)
if err != nil {
fmt.Printf("sender error: %v\n", err)
}
}()
c.conn[p] = conn
c.mu[p].Unlock()
return nil
}
func (c *ClientWs) sender(p bool) error {
ticker := time.NewTicker(time.Millisecond * 300)
defer ticker.Stop()
for {
select {
case data := <-c.sendChan[p]:
c.mu[p].RLock()
err := c.conn[p].SetWriteDeadline(time.Now().Add(writeWait))
if err != nil {
c.mu[p].RUnlock()
return err
}
w, err := c.conn[p].NextWriter(websocket.TextMessage)
if err != nil {
c.mu[p].RUnlock()
return err
}
if _, err = w.Write(data); err != nil {
c.mu[p].RUnlock()
return err
}
now := time.Now()
c.lastTransmit[p] = &now
c.mu[p].RUnlock()
if err := w.Close(); err != nil {
return err
}
case <-ticker.C:
if c.conn[p] != nil && (c.lastTransmit[p] == nil || (c.lastTransmit[p] != nil && time.Since(*c.lastTransmit[p]) > PingPeriod)) {
go func() {
c.sendChan[p] <- []byte("ping")
}()
}
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 err
}
mt, data, err := c.conn[p].ReadMessage()
if err != nil {
c.mu[p].RUnlock()
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
return c.conn[p].Close()
}
return err
}
c.mu[p].RUnlock()
now := time.Now()
c.mu[p].Lock()
c.lastTransmit[p] = &now
c.mu[p].Unlock()
if mt == websocket.TextMessage && string(data) != "pong" {
e := &events.Basic{}
if err := json.Unmarshal(data, &e); err != nil {
return err
}
go func() {
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() {
c.ErrChan <- &e
}()
return true
case "subscribe":
e := events.Subscribe{}
_ = json.Unmarshal(data, &e)
go func() {
if c.SubscribeChan != nil {
c.SubscribeChan <- &e
}
c.StructuredEventChan <- e
}()
return true
case "unsubscribe":
e := events.Unsubscribe{}
_ = json.Unmarshal(data, &e)
go func() {
if c.UnsubscribeCh != nil {
c.UnsubscribeCh <- &e
}
c.StructuredEventChan <- 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
}
c.StructuredEventChan <- 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)
go func() {
if c.SuccessChan != nil {
c.SuccessChan <- &e
}
c.StructuredEventChan <- e
}()
return true
}
go func() { c.RawEventChan <- e }()
return false
}