Skip to content

Commit

Permalink
feat(#267): Fixed evil race condition in bs_rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed Nov 12, 2024
1 parent e520121 commit 177b018
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 25 deletions.
23 changes: 8 additions & 15 deletions pkg/server/pokete/msg/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import (
"context"
"fmt"

"github.com/lxgr-linux/pokete/server/pokete/poke"
"github.com/lxgr-linux/pokete/server/pokete/troll"

"github.com/lxgr-linux/pokete/server/pokete/msg/map_info"

"github.com/lxgr-linux/pokete/bs_rpc/msg"
"github.com/lxgr-linux/pokete/server/config"
pctx "github.com/lxgr-linux/pokete/server/context"
error2 "github.com/lxgr-linux/pokete/server/pokete/msg/error"
"github.com/lxgr-linux/pokete/server/pokete/msg/map_info"
"github.com/lxgr-linux/pokete/server/pokete/troll"
"github.com/lxgr-linux/pokete/server/pokete/user"
"github.com/lxgr-linux/pokete/server/pokete/users"
)
Expand All @@ -21,9 +18,8 @@ const HandshakeType msg.Type = "pokete.handshake"

type Handshake struct {
msg.BaseMsg
UserName string `json:"user_name"`
Version string `json:"version"`
Pokes []poke.Instance `json:"pokes"`
User user.User `json:"user"`
Version string `json:"version"`
}

func (h Handshake) GetType() msg.Type {
Expand All @@ -40,17 +36,14 @@ func (h Handshake) CallForResponse(ctx context.Context) (msg.Body, error) {

position := getStartPosition(cfg)

err := troll.CheckPokes(res.BaseAssets.Pokes, h.Pokes)
err := troll.CheckPokes(res.BaseAssets.Pokes, h.User.Pokes)
if err != nil {
return error2.NewInvalidPoke(err), err
}

newUser := user.User{
Name: h.UserName,
Client: client,
Position: position,
Pokes: h.Pokes,
}
newUser := h.User
newUser.Client = client
newUser.Position = position

if h.Version != cfg.ClientVersion {
return error2.NewVersionMismatch(cfg.ClientVersion), fmt.Errorf("connection closed")
Expand Down
1 change: 1 addition & 0 deletions pkg/server/pokete/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type User struct {
Position Position `json:"position"`
Client *bs_rpc.Client `json:"client"` // TODO: Maybe remove
Pokes []poke.Instance `json:"pokes"`
Items map[string]uint `json:"items"`
}
6 changes: 4 additions & 2 deletions pokete.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,14 @@ def set_args(self, _si):
mvp.movemap.add_obs()

def set(self, x, y):
if super().set(x, y) == 0:
if (ret := super().set(x, y)) == 0:
self.update_server_pos()
return ret

def add(self, _map, x, y):
if super().add(_map, x, y) == 0:
if (ret := super().add(_map, x, y)) == 0:
self.update_server_pos()
return ret

def update_server_pos(self):
if modeProvider.mode == Mode.MULTI:
Expand Down
13 changes: 10 additions & 3 deletions pokete_classes/multiplayer/communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pokete_classes.multiplayer.exceptions import ConnectionException, \
VersionMismatchException, UserPresentException, InvalidPokeException
from pokete_classes.multiplayer.msg import player, position, error, map_info, fight
from pokete_classes.multiplayer.msg.position.update import UpdateDict
from pokete_classes.multiplayer.msg.position.update import Position, UpdateDict
from pokete_classes.multiplayer.pc_manager import pc_manager


Expand Down Expand Up @@ -69,9 +69,14 @@ def handshake(
"""Sends and handles the handshake with the server"""
resp = self.client.call_for_response(
msg.Handshake({
"user_name": user_name,
"user": {
"name": user_name,
"pokes": [p.dict() for p in ctx.figure.pokes],
"items": ctx.figure.inv,
"client": None,
"position": {"map": "", "x":0, "y": 0} # Null position
},
"version": version,
"pokes": [p.dict() for p in ctx.figure.pokes]
}))
match resp.get_type():
case error.VERSION_MISMATCH_TYPE:
Expand All @@ -98,6 +103,8 @@ def handshake(
ctx.figure.y = pos["y"]
pc_manager.waiting_users = data["users"]
return data["greeting_text"]
case _:
assert False, resp.get_type()

def request_fight(self, name: str) -> bool | None:
resp = self.client.call_for_response(fight.Request({"name": name}))
Expand Down
1 change: 1 addition & 0 deletions pokete_classes/multiplayer/menu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Contains classes ralated to the mode choosing meni"""

import logging
import sys

import scrap_engine as se
Expand Down
4 changes: 2 additions & 2 deletions pokete_classes/multiplayer/msg/handshake.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import bs_rpc
from pokete_classes.asset_service.resources.base import PokeDict
from pokete_classes.multiplayer.msg.player import player


class HandshakeData(TypedDict):
user_name: str
user: player.User
version: str
pokes: list[PokeDict]


class Handshake(bs_rpc.Body):
Expand Down
2 changes: 1 addition & 1 deletion pokete_classes/multiplayer/msg/player/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class User(TypedDict):
name: str
positon: Position
position: Position
client: None
pokes: list[PokeDict]
items: dict[str, int]
Expand Down
2 changes: 0 additions & 2 deletions pokete_classes/multiplayer/pc_manager/remote_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from ...color import Color
from ...interactions import Interactor, MultiTextChooseBox
from ...landscape import MapInteract
#from ... import movemap as mvp


class RemotePlayer(se.Object, MapInteract, Interactor):
"""A remote player
Expand Down

0 comments on commit 177b018

Please sign in to comment.