Skip to content
Open
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
12 changes: 9 additions & 3 deletions server/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ func joinRoomWs(conn *websocket.Conn, r *http.Request) {
}

client := &RoomClient{
conn: conn,
outbox: make(chan []byte, 256),
key: serverSecurity.NewClientKey(),
conn: conn,
outbox: make(chan []byte, 256),
key: serverSecurity.NewClientKey(),
// 2^32-1, the biggest number uint32 can hold
// will overflow to 0 when incremented to check the counter (cnt + 1)
// allowing 0 to be the first packet counter
counter: 4294967295,
}

if session, ok := clients.LoadOK(pd.Uuid); ok {
Expand Down Expand Up @@ -224,10 +228,12 @@ func (c *RoomClient) processMsgs(msg []byte) (errs []error) {
}

if !serverSecurity.VerifySignature(c.key, msg) {
c.disconnect()
return append(errs, errors.New("bad signature"))
}

if !serverSecurity.VerifyCounter(&c.counter, msg) {
c.disconnect()
return append(errs, errors.New("bad counter"))
}

Expand Down
2 changes: 1 addition & 1 deletion server/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *Security) VerifySignature(clientKey uint32, msg []byte) bool {
}

func (s *Security) VerifyCounter(counter *uint32, msg []byte) bool {
if cnt := binary.BigEndian.Uint32(msg[4:]); *counter < cnt {
if cnt := binary.BigEndian.Uint32(msg[4:]); *counter == cnt+1 {
*counter = cnt
return true
}
Expand Down