exchange: update maskkey handling

This commit is contained in:
TonyQ 2021-12-23 01:16:08 +08:00
parent bcbf7c3f3b
commit d7ac645253
4 changed files with 15 additions and 15 deletions

View File

@ -1,2 +1 @@
package bbgo package bbgo

View File

@ -2,12 +2,12 @@ package binance
import ( import (
"context" "context"
"github.com/c9s/bbgo/pkg/util"
"math/rand" "math/rand"
"net" "net"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
@ -419,7 +419,7 @@ func (s *Stream) connect(ctx context.Context) error {
return err return err
} }
log.Infof("listen key is created: %s", MaskKey(listenKey)) log.Infof("listen key is created: %s", util.MaskKey(listenKey))
} }
// when in public mode, the listen key is an empty string // when in public mode, the listen key is an empty string
@ -496,7 +496,7 @@ func (s *Stream) listenKeyKeepAlive(ctx context.Context, listenKey string) {
defer func() { defer func() {
log.Debugf("keepalive worker stopped") log.Debugf("keepalive worker stopped")
if err := s.invalidateListenKey(context.Background(), listenKey); err != nil { if err := s.invalidateListenKey(context.Background(), listenKey); err != nil {
log.WithError(err).Errorf("invalidate listen key error: %v key: %s", err, MaskKey(listenKey)) log.WithError(err).Errorf("invalidate listen key error: %v key: %s", err, util.MaskKey(listenKey))
} }
}() }()
@ -514,12 +514,12 @@ func (s *Stream) listenKeyKeepAlive(ctx context.Context, listenKey string) {
} else { } else {
switch err.(type) { switch err.(type) {
case net.Error: case net.Error:
log.WithError(err).Errorf("listen key keep-alive network error: %v key: %s", err, MaskKey(listenKey)) log.WithError(err).Errorf("listen key keep-alive network error: %v key: %s", err, util.MaskKey(listenKey))
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
continue continue
default: default:
log.WithError(err).Errorf("listen key keep-alive unexpected error: %v key: %s", err, MaskKey(listenKey)) log.WithError(err).Errorf("listen key keep-alive unexpected error: %v key: %s", err, util.MaskKey(listenKey))
s.Reconnect() s.Reconnect()
return return
@ -637,7 +637,7 @@ func (s *Stream) read(ctx context.Context) {
func (s *Stream) invalidateListenKey(ctx context.Context, listenKey string) (err error) { func (s *Stream) invalidateListenKey(ctx context.Context, listenKey string) (err error) {
// should use background context to invalidate the user stream // should use background context to invalidate the user stream
log.Infof("closing listen key: %s", MaskKey(listenKey)) log.Infof("closing listen key: %s", util.MaskKey(listenKey))
if s.IsMargin { if s.IsMargin {
if s.IsIsolatedMargin { if s.IsIsolatedMargin {
@ -657,7 +657,7 @@ func (s *Stream) invalidateListenKey(ctx context.Context, listenKey string) (err
} }
if err != nil { if err != nil {
log.WithError(err).Errorf("error deleting listen key: %s", MaskKey(listenKey)) log.WithError(err).Errorf("error deleting listen key: %s", util.MaskKey(listenKey))
return err return err
} }
@ -676,8 +676,3 @@ func (s *Stream) Close() error {
s.ConnLock.Unlock() s.ConnLock.Unlock()
return err return err
} }
func MaskKey(key string) string {
maskKey := key[0:5]
return maskKey + strings.Repeat("*", len(key)-1-5)
}

View File

@ -182,7 +182,7 @@ func NewStream(client *okexapi.RestClient) *Stream {
}, },
} }
log.Infof("sending login request: %+v", op) log.Infof("sending okex login request")
err := stream.Conn.WriteJSON(op) err := stream.Conn.WriteJSON(op)
if err != nil { if err != nil {
log.WithError(err).Errorf("can not send login message") log.WithError(err).Errorf("can not send login message")
@ -241,7 +241,6 @@ func (s *Stream) connect(ctx context.Context) error {
url = okexapi.PrivateWebSocketURL url = okexapi.PrivateWebSocketURL
} }
conn, err := s.StandardStream.Dial(url) conn, err := s.StandardStream.Dial(url)
if err != nil { if err != nil {
return err return err

View File

@ -1,5 +1,7 @@
package util package util
import "strings"
func StringSliceContains(slice []string, needle string) bool { func StringSliceContains(slice []string, needle string) bool {
for _, s := range slice { for _, s := range slice {
if s == needle { if s == needle {
@ -9,3 +11,8 @@ func StringSliceContains(slice []string, needle string) bool {
return false return false
} }
func MaskKey(key string) string {
maskKey := key[0:5]
return maskKey + strings.Repeat("*", len(key)-1-5)
}