fix AIORedis v2.0.0 #2

This commit is contained in:
AllinolCP 2021-08-01 10:56:50 +07:00
parent 239b0dad7b
commit b46d56acad
5 changed files with 35 additions and 32 deletions

View File

@ -45,10 +45,12 @@ async def get_server_presence(p, pdata):
if await p.server.redis.scard(server_key): if await p.server.redis.scard(server_key):
async with p.server.db.transaction(): async with p.server.db.transaction():
buddies = BuddyList.select('buddy_id').where(BuddyList.penguin_id == pdata.id).gino.iterate() buddies = BuddyList.select('buddy_id').where(BuddyList.penguin_id == pdata.id).gino.iterate()
tr = p.server.redis.multi_exec()
async for buddy_id, in buddies: async with p.server.redis.pipeline(transaction=True) as tr:
tr.sismember(server_key, buddy_id) async for buddy_id, in buddies:
online_buddies = await tr.execute() tr.sismember(server_key, buddy_id)
online_buddies = await tr.execute()
if any(online_buddies): if any(online_buddies):
buddy_worlds.append(str(int(server_id))) buddy_worlds.append(str(int(server_id)))

View File

@ -41,10 +41,10 @@ async def handle_login(p, credentials: Credentials):
p.logger.info(f'{username} failed to login: incorrect password') p.logger.info(f'{username} failed to login: incorrect password')
if await p.server.redis.exists(flood_key): if await p.server.redis.exists(flood_key):
tr = p.server.redis.multi_exec() async with p.server.redis.pipeline(transaction=True) as tr:
tr.incr(flood_key) tr.incr(flood_key)
tr.expire(flood_key, p.server.config.login_failure_timer) tr.expire(flood_key, p.server.config.login_failure_timer)
failure_count, _ = await tr.execute() failure_count, _ = await tr.execute()
if failure_count >= p.server.config.login_failure_limit: if failure_count >= p.server.config.login_failure_limit:
return await p.send_error_and_disconnect(150) return await p.send_error_and_disconnect(150)
@ -100,10 +100,11 @@ async def handle_login(p, credentials: Credentials):
login_key = Crypto.hash(random_key[::-1]) login_key = Crypto.hash(random_key[::-1])
confirmation_hash = Crypto.hash(os.urandom(24)) confirmation_hash = Crypto.hash(os.urandom(24))
tr = p.server.redis.multi_exec()
tr.setex(f'{data.username}.lkey', p.server.config.auth_ttl, login_key) async with p.server.redis.pipeline(transaction=True) as tr:
tr.setex(f'{data.username}.ckey', p.server.config.auth_ttl, confirmation_hash) tr.setex(f'{data.username}.lkey', p.server.config.auth_ttl, login_key)
await tr.execute() tr.setex(f'{data.username}.ckey', p.server.config.auth_ttl, confirmation_hash)
await tr.execute()
world_populations, buddy_presence = await get_server_presence(p, data) world_populations, buddy_presence = await get_server_presence(p, data)

View File

@ -41,11 +41,11 @@ async def world_login(p, data):
@handlers.allow_once @handlers.allow_once
@handlers.depends_on_packet(XMLPacket('verChk'), XMLPacket('rndK')) @handlers.depends_on_packet(XMLPacket('verChk'), XMLPacket('rndK'))
async def handle_login(p, credentials: WorldCredentials): async def handle_login(p, credentials: WorldCredentials):
tr = p.server.redis.multi_exec() async with p.server.redis.pipeline(transaction=True) as tr:
tr.get(f'{credentials.username}.lkey') tr.get(f'{credentials.username}.lkey')
tr.get(f'{credentials.username}.ckey') tr.get(f'{credentials.username}.ckey')
tr.delete(f'{credentials.username}.lkey', f'{credentials.username}.ckey') tr.delete(f'{credentials.username}.lkey', f'{credentials.username}.ckey')
login_key, confirmation_hash, _ = await tr.execute() login_key, confirmation_hash, _ = await tr.execute()
if login_key is None or confirmation_hash is None: if login_key is None or confirmation_hash is None:
return await p.close() return await p.close()
@ -69,10 +69,10 @@ async def handle_login(p, credentials: WorldCredentials):
@handlers.allow_once @handlers.allow_once
@handlers.depends_on_packet(XMLPacket('verChk'), XMLPacket('rndK')) @handlers.depends_on_packet(XMLPacket('verChk'), XMLPacket('rndK'))
async def handle_legacy_login(p, credentials: Credentials): async def handle_legacy_login(p, credentials: Credentials):
tr = p.server.redis.multi_exec() async with p.server.redis.pipeline(transaction=True) as tr:
tr.get(f'{credentials.username}.lkey') tr.get(f'{credentials.username}.lkey')
tr.delete(f'{credentials.username}.lkey', f'{credentials.username}.ckey') tr.delete(f'{credentials.username}.lkey', f'{credentials.username}.ckey')
login_key, _ = await tr.execute() login_key, _ = await tr.execute()
try: try:
login_key = login_key.decode() login_key = login_key.decode()

View File

@ -12,10 +12,10 @@ async def handle_join_redemption_server_vanilla(p, credentials: str, confirmatio
if login_key != p.login_key: if login_key != p.login_key:
return await p.close() return await p.close()
tr = p.server.redis.multi_exec() async with p.server.redis.pipeline(transaction=True) as tr:
tr.setex(f'{username}.lkey', p.server.config.auth_ttl, login_key) tr.setex(f'{username}.lkey', p.server.config.auth_ttl, login_key)
tr.setex(f'{username}.ckey', p.server.config.auth_ttl, confirmation_hash) tr.setex(f'{username}.ckey', p.server.config.auth_ttl, confirmation_hash)
await tr.execute() await tr.execute()
redeemed_books = await PenguinRedemptionBook.query.where(PenguinRedemptionBook.penguin_id == p.id).gino.all() redeemed_books = await PenguinRedemptionBook.query.where(PenguinRedemptionBook.penguin_id == p.id).gino.all()
await p.send_xt('rjs', ','.join(str(redeemed_book.book_id) for redeemed_book in redeemed_books), 'houdini', await p.send_xt('rjs', ','.join(str(redeemed_book.book_id) for redeemed_book in redeemed_books), 'houdini',
@ -28,9 +28,9 @@ async def handle_join_redemption_server_legacy(p, _, login_key: str):
if login_key != p.login_key: if login_key != p.login_key:
return await p.close() return await p.close()
tr = p.server.redis.multi_exec() async with p.server.redis.pipeline(transaction=True) as tr:
tr.setex(f'{p.username}.lkey', p.server.config.auth_ttl, login_key) tr.setex(f'{p.username}.lkey', p.server.config.auth_ttl, login_key)
await tr.execute() await tr.execute()
redeemed_books = await PenguinRedemptionBook.query.where(PenguinRedemptionBook.penguin_id == p.id).gino.all() redeemed_books = await PenguinRedemptionBook.query.where(PenguinRedemptionBook.penguin_id == p.id).gino.all()
await p.send_xt('rjs', ','.join(str(redeemed_book.book_id) for redeemed_book in redeemed_books), 'houdini', await p.send_xt('rjs', ','.join(str(redeemed_book.book_id) for redeemed_book in redeemed_books), 'houdini',

View File

@ -37,10 +37,10 @@ async def handle_send_book_answer(p, book: int, question_id: int, answer: str):
redemption_attempts_key = f'{p.id}.redemption_attempts' redemption_attempts_key = f'{p.id}.redemption_attempts'
if await p.server.redis.exists(redemption_attempts_key): if await p.server.redis.exists(redemption_attempts_key):
tr = p.server.redis.multi_exec() async with p.server.redis.pipeline(transaction=True) as tr:
tr.incr(redemption_attempts_key) tr.incr(redemption_attempts_key)
tr.expire(redemption_attempts_key, p.server.config.login_failure_timer) tr.expire(redemption_attempts_key, p.server.config.login_failure_timer)
failure_count, _ = await tr.execute() failure_count, _ = await tr.execute()
if failure_count >= 5: if failure_count >= 5:
return await p.send_error(713) return await p.send_error(713)