From 5ff4f83e7ba7f0a011203f78e53b435e9b21ec77 Mon Sep 17 00:00:00 2001 From: Daisuke MAKIUCHI Date: Tue, 5 May 2026 16:00:25 +0900 Subject: [PATCH] =?UTF-8?q?game=E3=81=A7Client.hmac=E3=81=AErace=E3=82=92?= =?UTF-8?q?=E5=9B=9E=E9=81=BF=E3=81=99=E3=82=8B=E3=81=9F=E3=82=81peer?= =?UTF-8?q?=E3=81=9D=E3=82=8C=E3=81=9E=E3=82=8C=E3=81=8Chash.Hash=E3=82=92?= =?UTF-8?q?=E6=8C=81=E3=81=A4=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/game/client.go | 7 ++----- server/game/peer.go | 7 ++++++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/server/game/client.go b/server/game/client.go index 0e64e8e..c97c05e 100644 --- a/server/game/client.go +++ b/server/game/client.go @@ -1,9 +1,6 @@ package game import ( - "crypto/hmac" - "crypto/sha1" - "hash" "sync" "time" @@ -44,7 +41,7 @@ type Client struct { received bool authKey string - hmac hash.Hash + macKey string logger log.Logger @@ -85,7 +82,7 @@ func newClient(info *pb.ClientInfo, macKey string, room IRoom, isPlayer bool) (* renewPeer: make(chan struct{}, 1), authKey: RandomHex(room.ClientConf().AuthKeyLen), - hmac: hmac.New(sha1.New, []byte(macKey)), + macKey: macKey, logger: room.Logger().With(log.KeyClient, info.Id), diff --git a/server/game/peer.go b/server/game/peer.go index b9a3320..cc20b15 100644 --- a/server/game/peer.go +++ b/server/game/peer.go @@ -2,7 +2,10 @@ package game import ( "context" + "crypto/hmac" + "crypto/sha1" "errors" + "hash" "net" "sync" "time" @@ -30,6 +33,7 @@ type Peer struct { client *Client conn *websocket.Conn msgCh chan binary.Msg + hmac hash.Hash done chan struct{} detached chan struct{} @@ -45,6 +49,7 @@ func NewPeer(ctx context.Context, cli *Client, conn *websocket.Conn, lastEvSeq i client: cli, conn: conn, msgCh: make(chan binary.Msg), + hmac: hmac.New(sha1.New, []byte(cli.macKey)), done: make(chan struct{}), detached: make(chan struct{}), @@ -223,7 +228,7 @@ loop: } metrics.MessageRecv.Add(1) - msg, err := binary.UnmarshalMsg(p.client.hmac, data) + msg, err := binary.UnmarshalMsg(p.hmac, data) if err != nil { p.client.logger.Errorf("peer UnmarshalMsg (%v, %p): %+v", p.client.Id, p, err) p.closeWithMessage(websocket.CloseInvalidFramePayloadData, err.Error())