From 411bf02905e91d8cf82459492e728a0a24f3820e Mon Sep 17 00:00:00 2001 From: Daisuke MAKIUCHI Date: Wed, 10 Jul 2024 15:56:42 +0900 Subject: [PATCH 1/3] add log info --- server/binary/msg.go | 6 +++--- server/game/client.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/server/binary/msg.go b/server/binary/msg.go index 03ee46e8..3b2288b9 100644 --- a/server/binary/msg.go +++ b/server/binary/msg.go @@ -136,10 +136,10 @@ func BuildRegularMsgFrame(t MsgType, seq int, payload []byte, hmac hash.Hash) [] } // ParseMsg parse binary data to Msg struct -func UnmarshalMsg(hmac hash.Hash, data []byte) (Msg, error) { - data, ok := auth.ValidateMsgHMAC(hmac, data) +func UnmarshalMsg(hmac hash.Hash, alldata []byte) (Msg, error) { + data, ok := auth.ValidateMsgHMAC(hmac, alldata) if !ok { - return nil, xerrors.Errorf("invalid msg") + return nil, xerrors.Errorf("invalid msg hmac (len=%v)", len(alldata)) } if len(data) < 1 { diff --git a/server/game/client.go b/server/game/client.go index a1e4616a..e519ecb1 100644 --- a/server/game/client.go +++ b/server/game/client.go @@ -351,7 +351,7 @@ func (c *Client) DetachPeer(p *Peer) { if c.peer != p { return // すでにdetach済み } - c.logger.Infof("detach peer: %v peer=%p", c.Id, p) + c.logger.Infof("detach peer: %v peer=%p lastMsg=%v", c.Id, p, c.msgSeqNum) c.peer.Detached() go c.drainMsg(c.peer.MsgCh()) From fff7bbe24e05f2f4664bfbfa16c701be28bfd6cb Mon Sep 17 00:00:00 2001 From: Daisuke MAKIUCHI Date: Thu, 11 Jul 2024 13:33:19 +0900 Subject: [PATCH 2/3] msg validation error --- server/auth/mackey.go | 10 +++++++--- server/binary/msg.go | 8 ++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/server/auth/mackey.go b/server/auth/mackey.go index 50e0e897..88dbd5ee 100644 --- a/server/auth/mackey.go +++ b/server/auth/mackey.go @@ -69,13 +69,17 @@ func EncryptMACKey(appKey, macKey string) (string, error) { } // ValidateMsgHMAC validates the hmac of a websocket message. -func ValidateMsgHMAC(mac hash.Hash, data []byte) ([]byte, bool) { +func ValidateMsgHMAC(mac hash.Hash, data []byte) ([]byte, error) { dlen := len(data) - mac.Size() if dlen < 0 { - return nil, false + return nil, xerrors.Errorf("data=%v", data) } data, h := data[:dlen], data[dlen:] - return data, hmac.Equal(h, CalculateMsgHMAC(mac, data)) + hash := CalculateMsgHMAC(mac, data) + if !hmac.Equal(h, hash) { + return nil, xerrors.Errorf("hash=(%v, %v)", h, hash) + } + return data, nil } func CalculateMsgHMAC(mac hash.Hash, data []byte) []byte { diff --git a/server/binary/msg.go b/server/binary/msg.go index 3b2288b9..8142e4ce 100644 --- a/server/binary/msg.go +++ b/server/binary/msg.go @@ -136,10 +136,10 @@ func BuildRegularMsgFrame(t MsgType, seq int, payload []byte, hmac hash.Hash) [] } // ParseMsg parse binary data to Msg struct -func UnmarshalMsg(hmac hash.Hash, alldata []byte) (Msg, error) { - data, ok := auth.ValidateMsgHMAC(hmac, alldata) - if !ok { - return nil, xerrors.Errorf("invalid msg hmac (len=%v)", len(alldata)) +func UnmarshalMsg(hmac hash.Hash, data []byte) (Msg, error) { + data, err := auth.ValidateMsgHMAC(hmac, data) + if err != nil { + return nil, xerrors.Errorf("invalid msg hmac: %w", err) } if len(data) < 1 { From 5c9707d5e7548da90ff4631f81c9458c6cd08246 Mon Sep 17 00:00:00 2001 From: Daisuke MAKIUCHI Date: Wed, 17 Jul 2024 16:15:18 +0900 Subject: [PATCH 3/3] change err msg format --- server/auth/mackey.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/auth/mackey.go b/server/auth/mackey.go index 88dbd5ee..bb8d9027 100644 --- a/server/auth/mackey.go +++ b/server/auth/mackey.go @@ -72,7 +72,7 @@ func EncryptMACKey(appKey, macKey string) (string, error) { func ValidateMsgHMAC(mac hash.Hash, data []byte) ([]byte, error) { dlen := len(data) - mac.Size() if dlen < 0 { - return nil, xerrors.Errorf("data=%v", data) + return nil, xerrors.Errorf("data=[% X]", data) } data, h := data[:dlen], data[dlen:] hash := CalculateMsgHMAC(mac, data)