tweaks and fixes

This commit is contained in:
əlemi 2022-02-16 04:07:34 +01:00
parent e14d27f999
commit f6f55d4c17
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
3 changed files with 11 additions and 3 deletions

View file

@ -95,6 +95,9 @@ class MinecraftClient(CallbacksHolder, Runnable):
def connected(self) -> bool: def connected(self) -> bool:
return self.started and self.dispatcher.connected 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 on_connected(self) -> Callable:
def wrapper(fun): def wrapper(fun):
self.register(ClientEvent.CONNECTED, fun) self.register(ClientEvent.CONNECTED, fun)
@ -173,6 +176,9 @@ class MinecraftClient(CallbacksHolder, Runnable):
except AuthException as e: except AuthException as e:
self._logger.error(str(e)) self._logger.error(str(e))
break break
except Exception as e:
self._logger.exception("Unexpected error while authenticating")
break
try: try:
packet_whitelist = self.callback_keys(filter=Packet) if self.options.use_packet_whitelist else set() packet_whitelist = self.callback_keys(filter=Packet) if self.options.use_packet_whitelist else set()
await self.dispatcher.connect( await self.dispatcher.connect(

View file

@ -23,6 +23,7 @@ class AuthInterface:
selectedProfile : GameProfile selectedProfile : GameProfile
SESSION_SERVER = "https://sessionserver.mojang.com/session/minecraft" SESSION_SERVER = "https://sessionserver.mojang.com/session/minecraft"
TIMEOUT = aiohttp.ClientTimeout(total=3)
# async def authenticate(self, user:str, pwd:str): # async def authenticate(self, user:str, pwd:str):
# raise NotImplementedError # raise NotImplementedError
@ -50,7 +51,7 @@ class AuthInterface:
@classmethod @classmethod
async def _post(cls, endpoint:str, **kwargs) -> Dict[str, Any]: 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: async with sess.post(endpoint, **kwargs) as res:
data = await res.json(content_type=None) data = await res.json(content_type=None)
if res.status >= 400: if res.status >= 400:
@ -59,7 +60,7 @@ class AuthInterface:
@classmethod @classmethod
async def _get(cls, endpoint:str, **kwargs) -> Dict[str, Any]: 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: async with sess.get(endpoint, **kwargs) as res:
data = await res.json(content_type=None) data = await res.json(content_type=None)
if res.status >= 400: if res.status >= 400:

View file

@ -85,6 +85,7 @@ class MicrosoftAuthenticator(AuthInterface):
async def validate(self): async def validate(self):
prof = await self.fetch_profile() prof = await self.fetch_profile()
self.selectedProfile = GameProfile(id=prof['id'], name=prof['name']) self.selectedProfile = GameProfile(id=prof['id'], name=prof['name'])
logging.info("Session validated : %s", self.selectedProfile)
async def authenticate(self, code:str="") -> str: async def authenticate(self, code:str="") -> str:
"""Authorize Microsoft account""" """Authorize Microsoft account"""
@ -106,7 +107,7 @@ class MicrosoftAuthenticator(AuthInterface):
headers={ "Content-Type": "application/x-www-form-urlencoded" }, headers={ "Content-Type": "application/x-www-form-urlencoded" },
data=urlencode(payload) data=urlencode(payload)
) )
self.refreshToken = auth_response['refreshToken'] self.refreshToken = auth_response['refresh_token']
# maybe store expire_in and other stuff too? TODO # maybe store expire_in and other stuff too? TODO
ms_token = auth_response['access_token'] ms_token = auth_response['access_token']