Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ログに情報追加 #90

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions server/auth/mackey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=[% X]", 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 {
Expand Down
6 changes: 3 additions & 3 deletions server/binary/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ 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)
if !ok {
return nil, xerrors.Errorf("invalid msg")
data, err := auth.ValidateMsgHMAC(hmac, data)
if err != nil {
return nil, xerrors.Errorf("invalid msg hmac: %w", err)
}

if len(data) < 1 {
Expand Down
2 changes: 1 addition & 1 deletion server/game/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
Loading