Make Auth asynchronous

This commit is contained in:
Dan 2018-06-18 13:07:02 +02:00
parent 9a5ce0fe2d
commit 8049c9129b

View file

@ -67,14 +67,14 @@ class Auth:
b.seek(20) # Skip auth_key_id (8), message_id (8) and message_length (4) b.seek(20) # Skip auth_key_id (8), message_id (8) and message_length (4)
return Object.read(b) return Object.read(b)
def send(self, data: Object): async def send(self, data: Object):
data = self.pack(data) data = self.pack(data)
self.connection.send(data) await self.connection.send(data)
response = BytesIO(self.connection.recv()) response = BytesIO(await self.connection.recv())
return self.unpack(response) return self.unpack(response)
def create(self): async def create(self):
""" """
https://core.telegram.org/mtproto/auth_key https://core.telegram.org/mtproto/auth_key
https://core.telegram.org/mtproto/samples-auth_key https://core.telegram.org/mtproto/samples-auth_key
@ -89,12 +89,12 @@ class Auth:
try: try:
log.info("Start creating a new auth key on DC{}".format(self.dc_id)) log.info("Start creating a new auth key on DC{}".format(self.dc_id))
self.connection.connect() await self.connection.connect()
# Step 1; Step 2 # Step 1; Step 2
nonce = int.from_bytes(urandom(16), "little", signed=True) nonce = int.from_bytes(urandom(16), "little", signed=True)
log.debug("Send req_pq: {}".format(nonce)) log.debug("Send req_pq: {}".format(nonce))
res_pq = self.send(functions.ReqPqMulti(nonce)) res_pq = await self.send(functions.ReqPqMulti(nonce))
log.debug("Got ResPq: {}".format(res_pq.server_nonce)) log.debug("Got ResPq: {}".format(res_pq.server_nonce))
log.debug("Server public key fingerprints: {}".format(res_pq.server_public_key_fingerprints)) log.debug("Server public key fingerprints: {}".format(res_pq.server_public_key_fingerprints))
@ -138,7 +138,7 @@ class Auth:
# Step 5. TODO: Handle "server_DH_params_fail". Code assumes response is ok # Step 5. TODO: Handle "server_DH_params_fail". Code assumes response is ok
log.debug("Send req_DH_params") log.debug("Send req_DH_params")
server_dh_params = self.send( server_dh_params = await self.send(
functions.ReqDHParams( functions.ReqDHParams(
nonce, nonce,
server_nonce, server_nonce,
@ -198,7 +198,7 @@ class Auth:
encrypted_data = AES.ige256_encrypt(data_with_hash, tmp_aes_key, tmp_aes_iv) encrypted_data = AES.ige256_encrypt(data_with_hash, tmp_aes_key, tmp_aes_iv)
log.debug("Send set_client_DH_params") log.debug("Send set_client_DH_params")
set_client_dh_params_answer = self.send( set_client_dh_params_answer = await self.send(
functions.SetClientDHParams( functions.SetClientDHParams(
nonce, nonce,
server_nonce, server_nonce,