From 33fe81427397abf61acda9a9e640d34c1699a15f Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 12 Oct 2019 00:14:09 +0100 Subject: [PATCH] Remove duplicate code in world auth handlers --- houdini/handlers/login/world.py | 86 +++++++++++++-------------------- 1 file changed, 34 insertions(+), 52 deletions(-) diff --git a/houdini/handlers/login/world.py b/houdini/handlers/login/world.py index 913451d..32cfa9c 100644 --- a/houdini/handlers/login/world.py +++ b/houdini/handlers/login/world.py @@ -14,17 +14,40 @@ handle_version_check = login.handle_version_check handle_random_key = login.handle_random_key +async def world_login(p, data): + if len(p.server.penguins_by_id) >= p.server.server_config['Capacity']: + return await p.send_error_and_disconnect(103) + + if data is None: + return await p.send_error_and_disconnect(100) + + if not data.active: + return await p.close() + + if data.permaban: + return await p.close() + + active_ban = await Ban.query.where((Ban.penguin_id == data.id) & (Ban.expires >= datetime.now())).gino.scalar() + if active_ban is not None: + return await p.close() + + if data.id in p.server.penguins_by_id: + await p.server.penguins_by_id[data.id].close() + + p.logger.info(f'{data.username} logged in successfully') + + p.data = data + await p.send_xt('l') + + @handlers.handler(XMLPacket('login'), client=ClientType.Vanilla) @handlers.allow_once @handlers.depends_on_packet(XMLPacket('verChk'), XMLPacket('rndK')) async def handle_login(p, credentials: WorldCredentials): - if len(p.server.penguins_by_id) >= p.server.server_config['Capacity']: - return await p.send_error_and_disconnect(103) - tr = p.server.redis.multi_exec() - tr.get('{}.lkey'.format(credentials.username)) - tr.get('{}.ckey'.format(credentials.username)) - tr.delete('{}.lkey'.format(credentials.username), '{}.ckey'.format(credentials.username)) + tr.get(f'{credentials.username}.lkey') + tr.get(f'{credentials.username}.ckey') + tr.delete(f'{credentials.username}.lkey', f'{credentials.username}.ckey') login_key, confirmation_hash, _ = await tr.execute() if login_key is None or confirmation_hash is None: @@ -41,40 +64,18 @@ async def handle_login(p, credentials: WorldCredentials): data = await Penguin.get(credentials.id) - if data is None: - return await p.send_error_and_disconnect(100) - - if not data.active: - return await p.close() - - if data.permaban: - return await p.close() - - active_ban = await Ban.query.where((Ban.penguin_id == data.id) & (Ban.expires >= datetime.now())).gino.scalar() - if active_ban is not None: - return await p.close() - - if data.id in p.server.penguins_by_id: - await p.server.penguins_by_id[data.id].close() - - p.logger.info('{} logged in successfully'.format(data.username)) - - p.data = data p.login_key = login_key - await p.send_xt('l') + await world_login(p, data) @handlers.handler(XMLPacket('login'), client=ClientType.Legacy) @handlers.allow_once @handlers.depends_on_packet(XMLPacket('verChk'), XMLPacket('rndK')) async def handle_legacy_login(p, credentials: Credentials): - if len(p.server.penguins_by_id) >= p.server.server_config['Capacity']: - return await p.send_error_and_disconnect(103) - tr = p.server.redis.multi_exec() - tr.get('{}.lkey'.format(credentials.username)) - tr.delete('{}.lkey'.format(credentials.username), '{}.ckey'.format(credentials.username)) - login_key, _ = await tr.execute() + tr.get(f'{credentials.username}.lkey') + tr.delete(f'{credentials.username}.lkey', '{credentials.username}.ckey') + login_key, _ = await tr.execute() login_key = login_key.decode() login_hash = Crypto.encrypt_password(login_key + config.client['AuthStaticKey']) + login_key @@ -84,24 +85,5 @@ async def handle_legacy_login(p, credentials: Credentials): data = await Penguin.query.where(Penguin.username == credentials.username).gino.first() - if data is None: - return await p.send_error_and_disconnect(100) - - if not data.active: - return await p.close() - - if data.permaban: - return await p.close() - - active_ban = await Ban.query.where((Ban.penguin_id == data.id) & (Ban.expires >= datetime.now())).gino.scalar() - if active_ban is not None: - return await p.close() - - if data.id in p.server.penguins_by_id: - await p.server.penguins_by_id[data.id].close() - - p.logger.info('{} logged in successfully'.format(data.username)) - - p.data = data p.login_key = login_key - await p.send_xt('l') + await world_login(p, data)