Merge branch 'master' into asyncio

This commit is contained in:
Lonami Exo
2017-10-21 15:45:56 +02:00
20 changed files with 343 additions and 694 deletions

View File

@@ -42,7 +42,7 @@ async def _do_authentication(connection):
req_pq_request = ReqPqRequest(
nonce=int.from_bytes(os.urandom(16), 'big', signed=True)
)
await sender.send(req_pq_request.to_bytes())
await sender.send(bytes(req_pq_request))
with BinaryReader(await sender.receive()) as reader:
req_pq_request.on_response(reader)
@@ -60,12 +60,12 @@ async def _do_authentication(connection):
p, q = rsa.get_byte_array(min(p, q)), rsa.get_byte_array(max(p, q))
new_nonce = int.from_bytes(os.urandom(32), 'little', signed=True)
pq_inner_data = PQInnerData(
pq_inner_data = bytes(PQInnerData(
pq=rsa.get_byte_array(pq), p=p, q=q,
nonce=res_pq.nonce,
server_nonce=res_pq.server_nonce,
new_nonce=new_nonce
).to_bytes()
))
# sha_digest + data + random_bytes
cipher_text, target_fingerprint = None, None
@@ -90,7 +90,7 @@ async def _do_authentication(connection):
public_key_fingerprint=target_fingerprint,
encrypted_data=cipher_text
)
await sender.send(req_dh_params.to_bytes())
await sender.send(bytes(req_dh_params))
# Step 2 response: DH Exchange
with BinaryReader(await sender.receive()) as reader:
@@ -138,12 +138,12 @@ async def _do_authentication(connection):
gab = pow(g_a, b, dh_prime)
# Prepare client DH Inner Data
client_dh_inner = ClientDHInnerData(
client_dh_inner = bytes(ClientDHInnerData(
nonce=res_pq.nonce,
server_nonce=res_pq.server_nonce,
retry_id=0, # TODO Actual retry ID
g_b=rsa.get_byte_array(gb)
).to_bytes()
))
client_dh_inner_hashed = sha1(client_dh_inner).digest() + client_dh_inner
@@ -156,7 +156,7 @@ async def _do_authentication(connection):
server_nonce=res_pq.server_nonce,
encrypted_data=client_dh_encrypted,
)
await sender.send(set_client_dh.to_bytes())
await sender.send(bytes(set_client_dh))
# Step 3 response: Complete DH Exchange
with BinaryReader(await sender.receive()) as reader:

View File

@@ -39,7 +39,7 @@ class MtProtoSender:
self._logger = logging.getLogger(__name__)
# Message IDs that need confirmation
self._need_confirmation = []
self._need_confirmation = set()
# Requests (as msg_id: Message) sent waiting to be received
self._pending_receive = {}
@@ -74,7 +74,7 @@ class MtProtoSender:
# Pack everything in the same container if we need to send AckRequests
if self._need_confirmation:
messages.append(
TLMessage(self.session, MsgsAck(self._need_confirmation))
TLMessage(self.session, MsgsAck(list(self._need_confirmation)))
)
self._need_confirmation.clear()
@@ -125,7 +125,7 @@ class MtProtoSender:
plain_text = \
struct.pack('<QQ', self.session.salt, self.session.id) \
+ message.to_bytes()
+ bytes(message)
msg_key = utils.calc_msg_key(plain_text)
key_id = struct.pack('<Q', self.session.auth_key.key_id)
@@ -174,7 +174,7 @@ class MtProtoSender:
"""
# TODO Check salt, session_id and sequence_number
self._need_confirmation.append(msg_id)
self._need_confirmation.add(msg_id)
code = reader.read_int(signed=False)
reader.seek(-4)
@@ -217,7 +217,7 @@ class MtProtoSender:
r = self._pop_request_of_type(msg_id, LogOutRequest)
if r:
r.result = True # Telegram won't send this value
r.confirm_received()
r.confirm_received.set()
self._logger.debug('Message ack confirmed', r)
return True
@@ -261,7 +261,7 @@ class MtProtoSender:
def _clear_all_pending(self):
for r in self._pending_receive.values():
r.confirm_received.set()
r.request.confirm_received.set()
self._pending_receive.clear()
async def _handle_pong(self, msg_id, sequence, reader):
@@ -303,6 +303,7 @@ class MtProtoSender:
self.session.salt = struct.unpack(
'<Q', struct.pack('<q', bad_salt.new_server_salt)
)[0]
self.session.save()
request = self._pop_request(bad_salt.bad_msg_id)
if request:
@@ -411,6 +412,11 @@ class MtProtoSender:
async def _handle_gzip_packed(self, msg_id, sequence, reader, state):
self._logger.debug('Handling gzip packed data')
with BinaryReader(GzipPacked.read(reader)) as compressed_reader:
# We are reentering process_msg, which seemingly the same msg_id
# to the self._need_confirmation set. Remove it from there first
# to avoid any future conflicts (i.e. if we "ignore" messages
# that we are already aware of, see 1a91c02 and old 63dfb1e)
self._need_confirmation -= {msg_id}
return await self._process_msg(msg_id, sequence, compressed_reader, state)
# endregion