From: Christian Heller Date: Sat, 5 Dec 2020 05:44:38 +0000 (+0100) Subject: Re-instate (better) flood protection. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/do_day?a=commitdiff_plain;h=089f09f75ae957e2483da10f0dbeac299217688c;p=plomrogue2 Re-instate (better) flood protection. --- diff --git a/plomrogue/commands.py b/plomrogue/commands.py index dc2e6ea..f2e479e 100644 --- a/plomrogue/commands.py +++ b/plomrogue/commands.py @@ -36,6 +36,7 @@ def cmd_SPAWN_POINT(game, big_yx, little_yx): cmd_SPAWN_POINT.argtypes = 'yx_tuple yx_tuple:nonneg' def cmd_LOGIN(game, nick, connection_id): + # TODO filter newlines for t in [t for t in game.things if t.type_ == 'Player' and t.name == nick]: raise GameError('name already in use') if game.get_player(connection_id): diff --git a/plomrogue/io.py b/plomrogue/io.py index 8cba18d..438b8b6 100644 --- a/plomrogue/io.py +++ b/plomrogue/io.py @@ -16,8 +16,8 @@ class GameIO(): def loop(self, q): """Handle commands coming through queue q, run game, send results back. - As basic flood protection, Only accepts one command per connection per - 1/100 of a second (currently commented out). + As basic flood protection, only accepts ten commands per connection per + 1/10 of a second. """ import time @@ -25,13 +25,14 @@ class GameIO(): while True: try: connection_id, command = q.get(timeout=0.001) - - # FIXME: this would catch the init command flood - #if connection_id in potential_flooders: - # if int(time.time() * 100) == potential_flooders[connection_id]: - # continue - #potential_flooders[connection_id] = int(time.time() * 100) - + now = int(time.time() * 10) + if connection_id in potential_flooders and \ + potential_flooders[connection_id][0] == now: + if potential_flooders[connection_id][1] > 10: + continue + potential_flooders[connection_id][1] += 1 + else: + potential_flooders[connection_id] = [now, 1] self.handle_input(command, connection_id) except queue.Empty: self.game.run_tick()