From 3f1dd2a2f135f4dd88664fb0de39ffdfd2f8f2cb Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 2 Dec 2019 23:18:23 +0000 Subject: [PATCH] Catch ChecklistError & CooldownError on every listener call --- houdini/handlers/__init__.py | 58 ++++++++++++++++++++++-------------- houdini/spheniscidae.py | 32 +++++++------------- 2 files changed, 46 insertions(+), 44 deletions(-) diff --git a/houdini/handlers/__init__.py b/houdini/handlers/__init__.py index 7970b3b..519a151 100644 --- a/houdini/handlers/__init__.py +++ b/houdini/handlers/__init__.py @@ -3,8 +3,9 @@ import enum import itertools from types import FunctionType -from houdini.converters import _listener, _ArgumentDeserializer, get_converter, do_conversion, _ConverterContext -from houdini.cooldown import _Cooldown, _CooldownMapping, BucketType +from houdini.converters import _listener, _ArgumentDeserializer, get_converter, \ + do_conversion, _ConverterContext, ChecklistError +from houdini.cooldown import _Cooldown, _CooldownMapping, BucketType, CooldownError from houdini import plugins, _AbstractManager, get_package_modules @@ -73,14 +74,19 @@ class _XTListener(_Listener): self.pre_login = kwargs.get('pre_login') async def __call__(self, p, packet_data): - if not self.pre_login and not p.joined_world: - await p.close() - raise AuthorityError(f'{p} tried sending XT packet before authentication!') + try: + if not self.pre_login and not p.joined_world: + await p.close() + raise AuthorityError(f'{p} tried sending XT packet before authentication!') - await super()._check_cooldown(p) - super()._check_list(p) + await super()._check_cooldown(p) + super()._check_list(p) - await super().__call__(p, packet_data) + await super().__call__(p, packet_data) + except CooldownError: + p.logger.debug(f'{p} tried to send a packet during a cooldown') + except ChecklistError: + p.logger.debug(f'{p} sent a packet without meeting checklist requirements') class _XMLListener(_Listener): @@ -89,27 +95,35 @@ class _XMLListener(_Listener): super().__init__(*args, **kwargs) async def __call__(self, p, packet_data): - await super()._check_cooldown(p) - super()._check_list(p) + try: + await super()._check_cooldown(p) + super()._check_list(p) - handler_call_arguments = [self.instance, p] if self.instance is not None else [p] + handler_call_arguments = [self.instance, p] if self.instance is not None else [p] - ctx = _ConverterContext(None, None, packet_data, p) - for ctx.component in itertools.islice(self._signature, len(handler_call_arguments), len(self._signature)): - if ctx.component.default is not ctx.component.empty: - handler_call_arguments.append(ctx.component.default) - elif ctx.component.kind == ctx.component.POSITIONAL_OR_KEYWORD: - converter = get_converter(ctx.component) + ctx = _ConverterContext(None, None, packet_data, p) + for ctx.component in itertools.islice(self._signature, len(handler_call_arguments), len(self._signature)): + if ctx.component.default is not ctx.component.empty: + handler_call_arguments.append(ctx.component.default) + elif ctx.component.kind == ctx.component.POSITIONAL_OR_KEYWORD: + converter = get_converter(ctx.component) - handler_call_arguments.append(await do_conversion(converter, ctx)) - return await self.callback(*handler_call_arguments) + handler_call_arguments.append(await do_conversion(converter, ctx)) + return await self.callback(*handler_call_arguments) + except CooldownError: + p.logger.debug(f'{p} tried to send a packet during a cooldown') + except ChecklistError: + p.logger.debug(f'{p} sent a packet without meeting checklist requirements') class _DummyListener(_Listener): async def __call__(self, p, *_): - super()._check_list(p) - handler_call_arguments = [self.instance, p] if self.instance is not None else [p] - return await self.callback(*handler_call_arguments) + try: + super()._check_list(p) + handler_call_arguments = [self.instance, p] if self.instance is not None else [p] + return await self.callback(*handler_call_arguments) + except ChecklistError: + p.logger.debug(f'{p} sent a packet without meeting checklist requirements') class _ListenerManager(_AbstractManager): diff --git a/houdini/spheniscidae.py b/houdini/spheniscidae.py index 48e47ad..be7b531 100644 --- a/houdini/spheniscidae.py +++ b/houdini/spheniscidae.py @@ -7,8 +7,6 @@ from xml.etree.cElementTree import Element, SubElement, tostring from houdini.constants import ClientType from houdini.handlers import AuthorityError, DummyEventPacket -from houdini.converters import ChecklistError -from houdini.cooldown import CooldownError class Spheniscidae: @@ -142,27 +140,21 @@ class Spheniscidae: async def _client_connected(self): self.logger.info(f'Client {self.peer_name} connected') - try: - dummy_event = DummyEventPacket('connected') - if dummy_event in self.server.dummy_event_listeners: - dummy_event_listeners = self.server.dummy_event_listeners[dummy_event] - for listener in dummy_event_listeners: - await listener(self) - except ChecklistError: - self.logger.debug(f'{self} sent a packet without meeting checklist requirements') + dummy_event = DummyEventPacket('connected') + if dummy_event in self.server.dummy_event_listeners: + dummy_event_listeners = self.server.dummy_event_listeners[dummy_event] + for listener in dummy_event_listeners: + await listener(self) async def _client_disconnected(self): del self.server.peers_by_ip[self.peer_name] self.logger.info(f'Client {self.peer_name} disconnected') - try: - dummy_event = DummyEventPacket('disconnected') - if dummy_event in self.server.dummy_event_listeners: - dummy_event_listeners = self.server.dummy_event_listeners[dummy_event] - for listener in dummy_event_listeners: - await listener(self) - except ChecklistError: - self.logger.debug(f'{self} sent a packet without meeting checklist requirements') + dummy_event = DummyEventPacket('disconnected') + if dummy_event in self.server.dummy_event_listeners: + dummy_event_listeners = self.server.dummy_event_listeners[dummy_event] + for listener in dummy_event_listeners: + await listener(self) async def __data_received(self, data): data = data.decode()[:-1] @@ -173,10 +165,6 @@ class Spheniscidae: await self.__handle_xt_data(data) except AuthorityError: self.logger.debug(f'{self} tried to send game packet before authentication') - except CooldownError: - self.logger.debug(f'{self} tried to send a packet during a cooldown') - except ChecklistError: - self.logger.debug(f'{self} sent a packet without meeting checklist requirements') async def run(self): await self._client_connected()