2016-08-28 14:43:00 +03:00
|
|
|
# This file is based on TLSharp
|
|
|
|
# https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/Network/MtProtoSender.cs
|
2016-08-26 13:58:53 +03:00
|
|
|
import re
|
|
|
|
import zlib
|
|
|
|
from time import sleep
|
|
|
|
|
2016-09-04 13:42:11 +03:00
|
|
|
import utils
|
|
|
|
from crypto import AES
|
|
|
|
from utils import BinaryWriter, BinaryReader
|
|
|
|
from tl.types import MsgsAck
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
class MtProtoSender:
|
2016-08-28 14:43:00 +03:00
|
|
|
"""MTProto Mobile Protocol sender (https://core.telegram.org/mtproto/description)"""
|
2016-08-26 13:58:53 +03:00
|
|
|
def __init__(self, transport, session):
|
2016-08-28 14:43:00 +03:00
|
|
|
self.transport = transport
|
|
|
|
self.session = session
|
|
|
|
self.need_confirmation = [] # Message IDs that need confirmation
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
def generate_sequence(self, confirmed):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Generates the next sequence number, based on whether it was confirmed yet or not"""
|
2016-08-26 13:58:53 +03:00
|
|
|
if confirmed:
|
2016-08-28 14:43:00 +03:00
|
|
|
result = self.session.sequence * 2 + 1
|
|
|
|
self.session.sequence += 1
|
2016-08-26 13:58:53 +03:00
|
|
|
return result
|
|
|
|
else:
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.session.sequence * 2
|
|
|
|
|
|
|
|
# region Send and receive
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
# TODO In TLSharp, this was async. Should this be?
|
2016-08-26 13:58:53 +03:00
|
|
|
def send(self, request):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Sends the specified MTProtoRequest, previously sending any message which needed confirmation"""
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
# First check if any message needs confirmation, if this is the case, send an "AckRequest"
|
|
|
|
if self.need_confirmation:
|
|
|
|
msgs_ack = MsgsAck(self.need_confirmation)
|
2016-08-26 13:58:53 +03:00
|
|
|
with BinaryWriter() as writer:
|
2016-08-28 14:43:00 +03:00
|
|
|
msgs_ack.on_send(writer)
|
|
|
|
self.send_packet(writer.get_bytes(), msgs_ack)
|
2016-08-26 13:58:53 +03:00
|
|
|
del self.need_confirmation[:]
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
# Then send our packed request
|
2016-08-26 13:58:53 +03:00
|
|
|
with BinaryWriter() as writer:
|
|
|
|
request.on_send(writer)
|
|
|
|
self.send_packet(writer.get_bytes(), request)
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
# And update the saved session
|
|
|
|
self.session.save()
|
|
|
|
|
|
|
|
def receive(self, request):
|
|
|
|
"""Receives the specified MTProtoRequest ("fills in it" the received data)"""
|
|
|
|
while not request.confirm_received:
|
|
|
|
message, remote_msg_id, remote_sequence = self.decode_msg(self.transport.receive().body)
|
|
|
|
|
|
|
|
with BinaryReader(message) as reader:
|
|
|
|
self.process_msg(remote_msg_id, remote_sequence, reader, request)
|
|
|
|
|
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Low level processing
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
def send_packet(self, packet, request):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Sends the given packet bytes with the additional information of the original request"""
|
|
|
|
request.msg_id = self.session.get_new_msg_id()
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
# First calculate the ciphered bit
|
2016-08-26 13:58:53 +03:00
|
|
|
with BinaryWriter() as writer:
|
2016-08-28 14:43:00 +03:00
|
|
|
writer.write_long(self.session.salt, signed=False)
|
|
|
|
writer.write_long(self.session.id, signed=False)
|
2016-08-27 12:59:23 +03:00
|
|
|
writer.write_long(request.msg_id)
|
2016-08-26 13:58:53 +03:00
|
|
|
writer.write_int(self.generate_sequence(request.confirmed))
|
|
|
|
writer.write_int(len(packet))
|
|
|
|
writer.write(packet)
|
|
|
|
|
2016-09-04 13:42:11 +03:00
|
|
|
msg_key = utils.calc_msg_key(writer.get_bytes())
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-09-04 13:42:11 +03:00
|
|
|
key, iv = utils.calc_key(self.session.auth_key.key, msg_key, True)
|
2016-09-04 12:07:18 +03:00
|
|
|
cipher_text = AES.encrypt_ige(writer.get_bytes(), key, iv)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
# And then finally send the packet
|
2016-08-26 13:58:53 +03:00
|
|
|
with BinaryWriter() as writer:
|
2016-09-04 12:07:18 +03:00
|
|
|
writer.write_long(self.session.auth_key.key_id, signed=False)
|
2016-08-26 13:58:53 +03:00
|
|
|
writer.write(msg_key)
|
|
|
|
writer.write(cipher_text)
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
self.transport.send(writer.get_bytes())
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
def decode_msg(self, body):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Decodes an received encrypted message body bytes"""
|
2016-08-26 13:58:53 +03:00
|
|
|
message = None
|
2016-08-28 14:43:00 +03:00
|
|
|
remote_msg_id = None
|
2016-08-26 13:58:53 +03:00
|
|
|
remote_sequence = None
|
|
|
|
|
|
|
|
with BinaryReader(body) as reader:
|
|
|
|
if len(body) < 8:
|
2016-09-04 12:07:18 +03:00
|
|
|
raise BufferError("Can't decode packet ({})".format(body))
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
# TODO Check for both auth key ID and msg_key correctness
|
|
|
|
remote_auth_key_id = reader.read_long()
|
|
|
|
msg_key = reader.read(16)
|
|
|
|
|
2016-09-04 13:42:11 +03:00
|
|
|
key, iv = utils.calc_key(self.session.auth_key.data, msg_key, False)
|
2016-09-04 12:07:18 +03:00
|
|
|
plain_text = AES.decrypt_ige(reader.read(len(body) - reader.tell_position()), key, iv)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
with BinaryReader(plain_text) as plain_text_reader:
|
|
|
|
remote_salt = plain_text_reader.read_long()
|
|
|
|
remote_session_id = plain_text_reader.read_long()
|
2016-08-28 14:43:00 +03:00
|
|
|
remote_msg_id = plain_text_reader.read_long()
|
2016-08-26 13:58:53 +03:00
|
|
|
remote_sequence = plain_text_reader.read_int()
|
|
|
|
msg_len = plain_text_reader.read_int()
|
|
|
|
message = plain_text_reader.read(msg_len)
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
return message, remote_msg_id, remote_sequence
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def process_msg(self, msg_id, sequence, reader, request):
|
|
|
|
"""Processes and handles a Telegram message"""
|
2016-08-26 13:58:53 +03:00
|
|
|
# TODO Check salt, session_id and sequence_number
|
2016-08-28 14:43:00 +03:00
|
|
|
self.need_confirmation.append(msg_id)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
code = reader.read_int(signed=False)
|
|
|
|
reader.seek(-4)
|
|
|
|
|
|
|
|
if code == 0x73f1f8dc: # Container
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.handle_container(msg_id, sequence, reader, request)
|
2016-08-26 13:58:53 +03:00
|
|
|
if code == 0x7abe77ec: # Ping
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.handle_ping(msg_id, sequence, reader)
|
2016-08-26 13:58:53 +03:00
|
|
|
if code == 0x347773c5: # pong
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.handle_pong(msg_id, sequence, reader)
|
2016-08-26 13:58:53 +03:00
|
|
|
if code == 0xae500895: # future_salts
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.handle_future_salts(msg_id, sequence, reader)
|
2016-08-26 13:58:53 +03:00
|
|
|
if code == 0x9ec20908: # new_session_created
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.handle_new_session_created(msg_id, sequence, reader)
|
2016-08-26 13:58:53 +03:00
|
|
|
if code == 0x62d6b459: # msgs_ack
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.handle_msgs_ack(msg_id, sequence, reader)
|
2016-08-26 13:58:53 +03:00
|
|
|
if code == 0xedab447b: # bad_server_salt
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.handle_bad_server_salt(msg_id, sequence, reader, request)
|
2016-08-26 13:58:53 +03:00
|
|
|
if code == 0xa7eff811: # bad_msg_notification
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.handle_bad_msg_notification(msg_id, sequence, reader)
|
2016-08-26 13:58:53 +03:00
|
|
|
if code == 0x276d3ec6: # msg_detailed_info
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.hangle_msg_detailed_info(msg_id, sequence, reader)
|
2016-08-26 13:58:53 +03:00
|
|
|
if code == 0xf35c6d01: # rpc_result
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.handle_rpc_result(msg_id, sequence, reader, request)
|
2016-08-26 13:58:53 +03:00
|
|
|
if code == 0x3072cfa1: # gzip_packed
|
2016-08-28 14:43:00 +03:00
|
|
|
return self.handle_gzip_packed(msg_id, sequence, reader, request)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
if (code == 0xe317af7e or
|
2016-08-28 14:43:00 +03:00
|
|
|
code == 0xd3f45784 or
|
|
|
|
code == 0x2b2fbd4e or
|
|
|
|
code == 0x78d4dec1 or
|
|
|
|
code == 0x725b04c3 or
|
|
|
|
code == 0x74ae4240):
|
|
|
|
return self.handle_update(msg_id, sequence, reader)
|
|
|
|
|
|
|
|
print('Unknown message: {}'.format(hex(msg_id)))
|
2016-08-26 13:58:53 +03:00
|
|
|
return False
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Message handling
|
|
|
|
|
|
|
|
def handle_update(self, msg_id, sequence, reader):
|
2016-08-26 13:58:53 +03:00
|
|
|
return False
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def handle_container(self, msg_id, sequence, reader, request):
|
2016-08-26 13:58:53 +03:00
|
|
|
code = reader.read_int(signed=False)
|
|
|
|
size = reader.read_int()
|
|
|
|
for _ in range(size):
|
|
|
|
inner_msg_id = reader.read_long(signed=False)
|
|
|
|
inner_sequence = reader.read_int()
|
|
|
|
inner_length = reader.read_int()
|
|
|
|
begin_position = reader.tell_position()
|
|
|
|
try:
|
2016-08-28 14:43:00 +03:00
|
|
|
if not self.process_msg(inner_msg_id, sequence, reader, request):
|
2016-08-26 13:58:53 +03:00
|
|
|
reader.set_position(begin_position + inner_length)
|
|
|
|
|
|
|
|
except:
|
|
|
|
reader.set_position(begin_position + inner_length)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def handle_ping(self, msg_id, sequence, reader):
|
2016-08-26 13:58:53 +03:00
|
|
|
return False
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def handle_pong(self, msg_id, sequence, reader):
|
2016-08-26 13:58:53 +03:00
|
|
|
return False
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def handle_future_salts(self, msg_id, sequence, reader):
|
2016-08-26 13:58:53 +03:00
|
|
|
code = reader.read_int(signed=False)
|
|
|
|
request_id = reader.read_long(signed=False)
|
|
|
|
reader.seek(-12)
|
|
|
|
|
|
|
|
raise NotImplementedError("Handle future server salts function isn't implemented.")
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def handle_new_session_created(self, msg_id, sequence, reader):
|
2016-08-26 13:58:53 +03:00
|
|
|
return False
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def handle_msgs_ack(self, msg_id, sequence, reader):
|
2016-08-26 13:58:53 +03:00
|
|
|
return False
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def handle_bad_server_salt(self, msg_id, sequence, reader, mtproto_request):
|
2016-08-26 13:58:53 +03:00
|
|
|
code = reader.read_int(signed=False)
|
|
|
|
bad_msg_id = reader.read_long(signed=False)
|
|
|
|
bad_msg_seq_no = reader.read_int()
|
|
|
|
error_code = reader.read_int()
|
|
|
|
new_salt = reader.read_long(signed=False)
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
self.session.salt = new_salt
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
# Resend
|
|
|
|
self.send(mtproto_request)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def handle_bad_msg_notification(self, msg_id, sequence, reader):
|
2016-08-26 13:58:53 +03:00
|
|
|
code = reader.read_int(signed=False)
|
|
|
|
request_id = reader.read_long(signed=False)
|
|
|
|
request_sequence = reader.read_int()
|
|
|
|
error_code = reader.read_int()
|
|
|
|
|
|
|
|
if error_code == 16:
|
|
|
|
raise RuntimeError("msg_id too low (most likely, client time is wrong it would be worthwhile to "
|
|
|
|
"synchronize it using msg_id notifications and re-send the original message "
|
|
|
|
"with the “correct” msg_id or wrap it in a container with a new msg_id if the "
|
|
|
|
"original message had waited too long on the client to be transmitted)")
|
|
|
|
if error_code == 17:
|
|
|
|
raise RuntimeError("msg_id too high (similar to the previous case, the client time has to be "
|
|
|
|
"synchronized, and the message re-sent with the correct msg_id)")
|
|
|
|
if error_code == 18:
|
|
|
|
raise RuntimeError("Incorrect two lower order msg_id bits (the server expects client message msg_id "
|
|
|
|
"to be divisible by 4)")
|
|
|
|
if error_code == 19:
|
|
|
|
raise RuntimeError("Container msg_id is the same as msg_id of a previously received message "
|
|
|
|
"(this must never happen)")
|
|
|
|
if error_code == 20:
|
|
|
|
raise RuntimeError("Message too old, and it cannot be verified whether the server has received a "
|
|
|
|
"message with this msg_id or not")
|
|
|
|
if error_code == 32:
|
|
|
|
raise RuntimeError("msg_seqno too low (the server has already received a message with a lower "
|
|
|
|
"msg_id but with either a higher or an equal and odd seqno)")
|
|
|
|
if error_code == 33:
|
|
|
|
raise RuntimeError("msg_seqno too high (similarly, there is a message with a higher msg_id but with "
|
|
|
|
"either a lower or an equal and odd seqno)")
|
|
|
|
if error_code == 34:
|
|
|
|
raise RuntimeError("An even msg_seqno expected (irrelevant message), but odd received")
|
|
|
|
if error_code == 35:
|
|
|
|
raise RuntimeError("Odd msg_seqno expected (relevant message), but even received")
|
|
|
|
if error_code == 48:
|
|
|
|
raise RuntimeError("Incorrect server salt (in this case, the bad_server_salt response is received with "
|
|
|
|
"the correct salt, and the message is to be re-sent with it)")
|
|
|
|
if error_code == 64:
|
|
|
|
raise RuntimeError("Invalid container")
|
|
|
|
|
|
|
|
raise NotImplementedError('This should never happen!')
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def hangle_msg_detailed_info(self, msg_id, sequence, reader):
|
2016-08-26 13:58:53 +03:00
|
|
|
return False
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def handle_rpc_result(self, msg_id, sequence, reader, mtproto_request):
|
2016-08-26 13:58:53 +03:00
|
|
|
code = reader.read_int(signed=False)
|
|
|
|
request_id = reader.read_long(signed=False)
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
if request_id == mtproto_request.msg_id:
|
2016-08-26 13:58:53 +03:00
|
|
|
mtproto_request.confirm_received = True
|
|
|
|
|
|
|
|
inner_code = reader.read_int(signed=False)
|
|
|
|
if inner_code == 0x2144ca19: # RPC Error
|
|
|
|
error_code = reader.read_int()
|
|
|
|
error_msg = reader.tgread_string()
|
|
|
|
|
|
|
|
if error_msg.startswith('FLOOD_WAIT_'):
|
|
|
|
seconds = int(re.search(r'\d+', error_msg).group(0))
|
|
|
|
print('Should wait {}s. Sleeping until then.')
|
|
|
|
sleep(seconds)
|
|
|
|
|
|
|
|
elif error_msg.startswith('PHONE_MIGRATE_'):
|
|
|
|
dc_index = int(re.search(r'\d+', error_msg).group(0))
|
2016-09-04 12:07:18 +03:00
|
|
|
raise ConnectionError('Your phone number is registered to {} DC. Please update settings. '
|
2016-08-26 13:58:53 +03:00
|
|
|
'See https://github.com/sochix/TLSharp#i-get-an-error-migrate_x '
|
|
|
|
'for details.'.format(dc_index))
|
|
|
|
else:
|
|
|
|
raise ValueError(error_msg)
|
|
|
|
|
|
|
|
elif inner_code == 0x3072cfa1: # GZip packed
|
|
|
|
try:
|
|
|
|
packed_data = reader.tgread_bytes()
|
|
|
|
unpacked_data = zlib.decompress(packed_data)
|
|
|
|
|
|
|
|
with BinaryReader(unpacked_data) as compressed_reader:
|
|
|
|
mtproto_request.on_response(compressed_reader)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
|
|
|
reader.seek(-4)
|
|
|
|
mtproto_request.on_response(reader)
|
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
def handle_gzip_packed(self, msg_id, sequence, reader, mtproto_request):
|
2016-08-26 13:58:53 +03:00
|
|
|
code = reader.read_int(signed=False)
|
|
|
|
packed_data = reader.tgread_bytes()
|
|
|
|
unpacked_data = zlib.decompress(packed_data)
|
|
|
|
|
|
|
|
with BinaryReader(unpacked_data) as compressed_reader:
|
2016-08-28 14:43:00 +03:00
|
|
|
self.process_msg(msg_id, sequence, compressed_reader, mtproto_request)
|
|
|
|
|
|
|
|
# endregion
|
|
|
|
pass
|