zhou.xiaoning пре 2 година
родитељ
комит
f440fb79ae
3 измењених фајлова са 23 додато и 8 уклоњено
  1. 7 0
      client/client.go
  2. 2 1
      initialize/rabbitmq.go
  3. 14 7
      service/asign/sign.go

+ 7 - 0
client/client.go

@@ -14,6 +14,7 @@ import (
 
 	"github.com/gorilla/websocket"
 	"github.com/mitchellh/mapstructure"
+	"go.uber.org/zap"
 )
 
 var Clients map[int]*Client // key:SessionID
@@ -96,6 +97,8 @@ func (r *Client) WriteQuoteWsBuf(buf []byte) (err error) {
 func (r *Client) WriteTradeWsBuf(buf []byte) (err error) {
 	if r.wsTradeConn != nil {
 		r.tradeWriteChan <- buf
+	} else {
+		global.M2A_LOG.Info("WriteTradeWsBuf == nil")
 	}
 
 	return
@@ -272,6 +275,8 @@ func (r *Client) SetTradeWebSocket(ws *websocket.Conn) (err error) {
 	// 开始推送客户端信息循环
 	go r.writeClientWsTradeMessage()
 
+	global.M2A_LOG.Info("交易长连客户端接入", zap.Any("LoginID", r.LoginID), zap.Any("SessionID", r.SessionID))
+
 	return
 }
 
@@ -313,10 +318,12 @@ func (r *Client) writeClientWsTradeMessage() {
 		buf, ok := <-r.tradeWriteChan
 		if !ok {
 			// 通道已关闭
+			global.M2A_LOG.Info("writeClientWsTradeMessage 通道已关闭", zap.Any("LoginID", r.LoginID), zap.Any("SessionID", r.SessionID))
 			return
 		}
 		err := r.wsTradeConn.WriteMessage(websocket.BinaryMessage, buf)
 		if err != nil {
+			global.M2A_LOG.Info("writeClientWsTradeMessage 向客户端发送信息时发生错误", zap.Any("LoginID", r.LoginID), zap.Any("SessionID", r.SessionID))
 			return
 		}
 	}

+ 2 - 1
initialize/rabbitmq.go

@@ -180,6 +180,7 @@ func (t *MQProc) onNtf(funcode uint32, sessionId uint32, bytes *[]byte) {
 		// 获取目标客户
 		clients, err = accountSrv.GetClientsByAccountID(*p.AccountID)
 		if err != nil {
+			global.M2A_LOG.Info(fmt.Sprintf("接收头寸变化通知时获取不到对应Client,AccountID:%v", *p.AccountID))
 			return
 		}
 	case global.OrderDealedNtf: // 委托单成交通知
@@ -254,7 +255,7 @@ func (t *MQProc) onNtf(funcode uint32, sessionId uint32, bytes *[]byte) {
 			c.WriteTradeWsBuf(b)
 
 			// 给客户端通知
-			global.M2A_LOG.Info("[S->C]", zap.Any("ntf", funcode), zap.Any("clients", c.LoginID), zap.Any("len", len(b)))
+			global.M2A_LOG.Info("[S->C]给客户端通知", zap.Any("ntf", funcode), zap.Any("clients", c.LoginID), zap.Any("SessionID", c.SessionID), zap.Any("len", len(b)))
 		}
 	}
 }

+ 14 - 7
service/asign/sign.go

@@ -2,10 +2,12 @@ package asign
 
 import (
 	"crypto"
+	"crypto/md5"
 	"crypto/rand"
 	"crypto/rsa"
 	"crypto/sha1"
 	"crypto/x509"
+	"encoding/hex"
 	"encoding/pem"
 	"errors"
 	"fmt"
@@ -26,7 +28,7 @@ import (
 //	4、签名算法:
 //	    4.1、将上述3所属的bizData(json字符串),按照阿拉伯字母排序(如:{"ba":1,"ac":2}--->{"ac":2,"ba":1}),
 //	    4.2、将4.1排序后的字符串,将【bizData+md5(bizData)+ appId + timestatmp】拼接后利用RSA非对称加密算法(SHA1withRSA),计算出最后的签名sign,对其base64编码,放入head的key(sign)中。
-func GetSignature(bizData map[string]interface{}, appId string, privateKeyPEM string) (signatureBase64 string, timestamp string, err error) {
+func GetSignature(bizData string, appId string, privateKeyPEM string) (signatureBase64 string, timestamp string, err error) {
 	timestamp = strconv.FormatInt(time.Now().UnixMilli(), 10)
 	// Parse the privateKeyPEM into an RSA private key
 	privateKeyBlock, _ := pem.Decode([]byte(privateKeyPEM))
@@ -39,8 +41,13 @@ func GetSignature(bizData map[string]interface{}, appId string, privateKeyPEM st
 		return
 	}
 
+	// md5(bizData)
+	m := md5.New()
+	m.Write([]byte(bizData))
+	bdMd5Hx := hex.EncodeToString(m.Sum(nil))
+
 	// Message to be signed
-	message := ""
+	message := bizData + bdMd5Hx + appId + timestamp
 
 	// Sign the message using SHA1withRSA
 	signature, err := signMessage(message, privateKey)
@@ -60,8 +67,8 @@ func signMessage(message string, privateKey *rsa.PrivateKey) ([]byte, error) {
 }
 
 // verifySignature verifies the given signature for the message using SHA1withRSA
-func verifySignature(message string, signature []byte, publicKey *rsa.PublicKey) bool {
-	hashed := sha1.Sum([]byte(message))
-	err := rsa.VerifyPKCS1v15(publicKey, crypto.SHA1, hashed[:], signature)
-	return err == nil
-}
+// func verifySignature(message string, signature []byte, publicKey *rsa.PublicKey) bool {
+// 	hashed := sha1.Sum([]byte(message))
+// 	err := rsa.VerifyPKCS1v15(publicKey, crypto.SHA1, hashed[:], signature)
+// 	return err == nil
+// }