From f6f55d4c17d7181590ba11eed32081e875da13d7 Mon Sep 17 00:00:00 2001 From: alemidev Date: Wed, 16 Feb 2022 04:07:34 +0100 Subject: [PATCH] tweaks and fixes --- aiocraft/client.py | 6 ++++++ aiocraft/mc/auth/interface.py | 5 +++-- aiocraft/mc/auth/microsoft.py | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/aiocraft/client.py b/aiocraft/client.py index 7c47572..c70c25c 100644 --- a/aiocraft/client.py +++ b/aiocraft/client.py @@ -95,6 +95,9 @@ class MinecraftClient(CallbacksHolder, Runnable): def connected(self) -> bool: return self.started and self.dispatcher.connected + async def write(self, packet:Packet, wait:bool=False): + await self.dispatcher.write(packet, wait) + def on_connected(self) -> Callable: def wrapper(fun): self.register(ClientEvent.CONNECTED, fun) @@ -173,6 +176,9 @@ class MinecraftClient(CallbacksHolder, Runnable): except AuthException as e: self._logger.error(str(e)) break + except Exception as e: + self._logger.exception("Unexpected error while authenticating") + break try: packet_whitelist = self.callback_keys(filter=Packet) if self.options.use_packet_whitelist else set() await self.dispatcher.connect( diff --git a/aiocraft/mc/auth/interface.py b/aiocraft/mc/auth/interface.py index 39c7e61..caac9ec 100644 --- a/aiocraft/mc/auth/interface.py +++ b/aiocraft/mc/auth/interface.py @@ -23,6 +23,7 @@ class AuthInterface: selectedProfile : GameProfile SESSION_SERVER = "https://sessionserver.mojang.com/session/minecraft" + TIMEOUT = aiohttp.ClientTimeout(total=3) # async def authenticate(self, user:str, pwd:str): # raise NotImplementedError @@ -50,7 +51,7 @@ class AuthInterface: @classmethod async def _post(cls, endpoint:str, **kwargs) -> Dict[str, Any]: - async with aiohttp.ClientSession() as sess: + async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as sess: async with sess.post(endpoint, **kwargs) as res: data = await res.json(content_type=None) if res.status >= 400: @@ -59,7 +60,7 @@ class AuthInterface: @classmethod async def _get(cls, endpoint:str, **kwargs) -> Dict[str, Any]: - async with aiohttp.ClientSession() as sess: + async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as sess: async with sess.get(endpoint, **kwargs) as res: data = await res.json(content_type=None) if res.status >= 400: diff --git a/aiocraft/mc/auth/microsoft.py b/aiocraft/mc/auth/microsoft.py index fe29394..4446ef2 100644 --- a/aiocraft/mc/auth/microsoft.py +++ b/aiocraft/mc/auth/microsoft.py @@ -85,6 +85,7 @@ class MicrosoftAuthenticator(AuthInterface): async def validate(self): prof = await self.fetch_profile() self.selectedProfile = GameProfile(id=prof['id'], name=prof['name']) + logging.info("Session validated : %s", self.selectedProfile) async def authenticate(self, code:str="") -> str: """Authorize Microsoft account""" @@ -106,7 +107,7 @@ class MicrosoftAuthenticator(AuthInterface): headers={ "Content-Type": "application/x-www-form-urlencoded" }, data=urlencode(payload) ) - self.refreshToken = auth_response['refreshToken'] + self.refreshToken = auth_response['refresh_token'] # maybe store expire_in and other stuff too? TODO ms_token = auth_response['access_token']