From d7ac6452533f9768b0ca51cc79801179517b0e1d Mon Sep 17 00:00:00 2001 From: TonyQ Date: Thu, 23 Dec 2021 01:16:08 +0800 Subject: [PATCH] exchange: update maskkey handling --- pkg/bbgo/string.go | 1 - pkg/exchange/binance/stream.go | 19 +++++++------------ pkg/exchange/okex/stream.go | 3 +-- pkg/util/string.go | 7 +++++++ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/bbgo/string.go b/pkg/bbgo/string.go index 920078f66..f30d11b65 100644 --- a/pkg/bbgo/string.go +++ b/pkg/bbgo/string.go @@ -1,2 +1 @@ package bbgo - diff --git a/pkg/exchange/binance/stream.go b/pkg/exchange/binance/stream.go index 50f308eae..2a898b581 100644 --- a/pkg/exchange/binance/stream.go +++ b/pkg/exchange/binance/stream.go @@ -2,12 +2,12 @@ package binance import ( "context" + "github.com/c9s/bbgo/pkg/util" "math/rand" "net" "net/http" "os" "strconv" - "strings" "sync" "time" @@ -419,7 +419,7 @@ func (s *Stream) connect(ctx context.Context) error { 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 @@ -496,7 +496,7 @@ func (s *Stream) listenKeyKeepAlive(ctx context.Context, listenKey string) { defer func() { log.Debugf("keepalive worker stopped") 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 { switch err.(type) { 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) continue 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() return @@ -637,7 +637,7 @@ func (s *Stream) read(ctx context.Context) { func (s *Stream) invalidateListenKey(ctx context.Context, listenKey string) (err error) { // 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.IsIsolatedMargin { @@ -657,7 +657,7 @@ func (s *Stream) invalidateListenKey(ctx context.Context, listenKey string) (err } 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 } @@ -676,8 +676,3 @@ func (s *Stream) Close() error { s.ConnLock.Unlock() return err } - -func MaskKey(key string) string { - maskKey := key[0:5] - return maskKey + strings.Repeat("*", len(key)-1-5) -} diff --git a/pkg/exchange/okex/stream.go b/pkg/exchange/okex/stream.go index a2cb44c76..011c25df4 100644 --- a/pkg/exchange/okex/stream.go +++ b/pkg/exchange/okex/stream.go @@ -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) if err != nil { log.WithError(err).Errorf("can not send login message") @@ -241,7 +241,6 @@ func (s *Stream) connect(ctx context.Context) error { url = okexapi.PrivateWebSocketURL } - conn, err := s.StandardStream.Dial(url) if err != nil { return err diff --git a/pkg/util/string.go b/pkg/util/string.go index d4e446ffa..053e16516 100644 --- a/pkg/util/string.go +++ b/pkg/util/string.go @@ -1,5 +1,7 @@ package util +import "strings" + func StringSliceContains(slice []string, needle string) bool { for _, s := range slice { if s == needle { @@ -9,3 +11,8 @@ func StringSliceContains(slice []string, needle string) bool { return false } + +func MaskKey(key string) string { + maskKey := key[0:5] + return maskKey + strings.Repeat("*", len(key)-1-5) +}