2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
This module contains the class used to communicate with Telegram's servers
|
|
|
|
encrypting every packet, and relies on a valid AuthKey in the used Session.
|
|
|
|
"""
|
2016-09-05 19:35:12 +03:00
|
|
|
import gzip
|
2017-09-04 18:18:33 +03:00
|
|
|
import logging
|
2018-01-07 01:43:40 +03:00
|
|
|
from threading import Lock
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-05-21 14:02:54 +03:00
|
|
|
from .. import helpers as utils
|
2017-09-22 14:32:00 +03:00
|
|
|
from ..errors import (
|
|
|
|
BadMessageError, InvalidChecksumError, BrokenAuthKeyError,
|
|
|
|
rpc_message_to_error
|
|
|
|
)
|
2017-09-27 22:01:20 +03:00
|
|
|
from ..extensions import BinaryReader
|
2017-09-28 12:59:24 +03:00
|
|
|
from ..tl import TLMessage, MessageContainer, GzipPacked
|
2017-05-21 14:02:54 +03:00
|
|
|
from ..tl.all_tlobjects import tlobjects
|
2018-01-30 20:32:42 +03:00
|
|
|
from ..tl.functions.auth import LogOutRequest
|
2017-10-12 22:09:09 +03:00
|
|
|
from ..tl.types import (
|
2018-01-30 20:32:42 +03:00
|
|
|
MsgsAck, Pong, BadServerSalt, BadMsgNotification, FutureSalts,
|
2017-10-12 22:09:09 +03:00
|
|
|
MsgNewDetailedInfo, NewSessionCreated, MsgDetailedInfo
|
|
|
|
)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__ = logging.getLogger(__name__)
|
2017-05-20 12:49:09 +03:00
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
class MtProtoSender:
|
2017-09-02 22:27:11 +03:00
|
|
|
"""MTProto Mobile Protocol sender
|
2017-09-30 12:49:38 +03:00
|
|
|
(https://core.telegram.org/mtproto/description).
|
|
|
|
|
|
|
|
Note that this class is not thread-safe, and calling send/receive
|
|
|
|
from two or more threads at the same time is undefined behaviour.
|
|
|
|
Rationale: a new connection should be spawned to send/receive requests
|
|
|
|
in parallel, so thread-safety (hence locking) isn't needed.
|
2017-09-02 22:27:11 +03:00
|
|
|
"""
|
|
|
|
|
2017-09-21 14:43:33 +03:00
|
|
|
def __init__(self, session, connection):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Initializes a new MTProto sender.
|
|
|
|
|
|
|
|
:param session:
|
|
|
|
the Session to be used with this sender. Must contain the IP and
|
|
|
|
port of the server, salt, ID, and AuthKey,
|
|
|
|
:param connection:
|
|
|
|
the Connection to be used.
|
2017-09-02 22:27:11 +03:00
|
|
|
"""
|
2016-08-28 14:43:00 +03:00
|
|
|
self.session = session
|
2017-09-21 14:43:33 +03:00
|
|
|
self.connection = connection
|
2016-09-07 12:36:34 +03:00
|
|
|
|
2017-09-27 22:01:20 +03:00
|
|
|
# Message IDs that need confirmation
|
2017-10-20 23:07:45 +03:00
|
|
|
self._need_confirmation = set()
|
2017-09-27 22:01:20 +03:00
|
|
|
|
|
|
|
# Requests (as msg_id: Message) sent waiting to be received
|
|
|
|
self._pending_receive = {}
|
2016-09-06 19:54:49 +03:00
|
|
|
|
2018-01-07 01:43:40 +03:00
|
|
|
# Multithreading
|
|
|
|
self._send_lock = Lock()
|
|
|
|
|
2017-04-29 12:07:32 +03:00
|
|
|
def connect(self):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""Connects to the server."""
|
2017-10-01 20:26:20 +03:00
|
|
|
self.connection.connect(self.session.server_address, self.session.port)
|
2017-02-19 17:20:21 +03:00
|
|
|
|
2017-06-19 10:58:03 +03:00
|
|
|
def is_connected(self):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Determines whether the sender is connected or not.
|
|
|
|
|
|
|
|
:return: true if the sender is connected.
|
|
|
|
"""
|
2017-08-28 22:23:31 +03:00
|
|
|
return self.connection.is_connected()
|
2017-06-19 10:58:03 +03:00
|
|
|
|
2016-09-09 12:47:37 +03:00
|
|
|
def disconnect(self):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""Disconnects from the server."""
|
2017-09-03 10:56:10 +03:00
|
|
|
self.connection.close()
|
2017-09-21 14:43:33 +03:00
|
|
|
self._need_confirmation.clear()
|
|
|
|
self._clear_all_pending()
|
2016-09-09 12:47:37 +03:00
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
# region Send and receive
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-09-25 21:52:27 +03:00
|
|
|
def send(self, *requests):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Sends the specified TLObject(s) (which must be requests),
|
|
|
|
and acknowledging any message which needed confirmation.
|
2016-09-10 12:01:03 +03:00
|
|
|
|
2017-11-30 15:20:51 +03:00
|
|
|
:param requests: the requests to be sent.
|
|
|
|
"""
|
2017-09-25 21:52:27 +03:00
|
|
|
# Finally send our packed request(s)
|
2017-09-28 12:59:24 +03:00
|
|
|
messages = [TLMessage(self.session, r) for r in requests]
|
2017-09-27 22:01:20 +03:00
|
|
|
self._pending_receive.update({m.msg_id: m for m in messages})
|
|
|
|
|
2018-02-12 15:40:00 +03:00
|
|
|
__log__.debug('Sending requests with IDs: %s', ', '.join(
|
|
|
|
'{}: {}'.format(m.request.__class__.__name__, m.msg_id)
|
|
|
|
for m in messages
|
|
|
|
))
|
|
|
|
|
2017-10-06 12:36:39 +03:00
|
|
|
# Pack everything in the same container if we need to send AckRequests
|
|
|
|
if self._need_confirmation:
|
|
|
|
messages.append(
|
2017-10-20 23:07:45 +03:00
|
|
|
TLMessage(self.session, MsgsAck(list(self._need_confirmation)))
|
2017-10-06 12:36:39 +03:00
|
|
|
)
|
|
|
|
self._need_confirmation.clear()
|
|
|
|
|
2017-09-27 22:01:20 +03:00
|
|
|
if len(messages) == 1:
|
|
|
|
message = messages[0]
|
2017-09-25 21:52:27 +03:00
|
|
|
else:
|
2017-09-28 12:59:24 +03:00
|
|
|
message = TLMessage(self.session, MessageContainer(messages))
|
2017-10-22 14:13:49 +03:00
|
|
|
# On bad_msg_salt errors, Telegram will reply with the ID of
|
|
|
|
# the container and not the requests it contains, so in case
|
|
|
|
# this happens we need to know to which container they belong.
|
|
|
|
for m in messages:
|
|
|
|
m.container_msg_id = message.msg_id
|
2017-09-25 21:52:27 +03:00
|
|
|
|
2017-09-27 22:01:20 +03:00
|
|
|
self._send_message(message)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-10-06 12:36:39 +03:00
|
|
|
def _send_acknowledge(self, msg_id):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""Sends a message acknowledge for the given msg_id."""
|
2017-10-06 12:36:39 +03:00
|
|
|
self._send_message(TLMessage(self.session, MsgsAck([msg_id])))
|
2017-06-09 11:46:39 +03:00
|
|
|
|
2017-09-07 19:49:08 +03:00
|
|
|
def receive(self, update_state):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Receives a single message from the connected endpoint.
|
|
|
|
|
|
|
|
This method returns nothing, and will only affect other parts
|
|
|
|
of the MtProtoSender such as the updates callback being fired
|
|
|
|
or a pending request being confirmed.
|
2017-09-02 22:27:11 +03:00
|
|
|
|
2017-11-30 15:20:51 +03:00
|
|
|
Any unhandled object (likely updates) will be passed to
|
|
|
|
update_state.process(TLObject).
|
2017-09-07 19:49:08 +03:00
|
|
|
|
2017-11-30 15:20:51 +03:00
|
|
|
:param update_state:
|
|
|
|
the UpdateState that will process all the received
|
|
|
|
Update and Updates objects.
|
2017-09-02 22:27:11 +03:00
|
|
|
"""
|
2017-09-30 12:49:38 +03:00
|
|
|
try:
|
|
|
|
body = self.connection.recv()
|
|
|
|
except (BufferError, InvalidChecksumError):
|
|
|
|
# TODO BufferError, we should spot the cause...
|
|
|
|
# "No more bytes left"; something wrong happened, clear
|
|
|
|
# everything to be on the safe side, or:
|
|
|
|
#
|
|
|
|
# "This packet should be skipped"; since this may have
|
|
|
|
# been a result for a request, invalidate every request
|
|
|
|
# and just re-invoke them to avoid problems
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.exception('Error while receiving server response. '
|
|
|
|
'%d pending request(s) will be ignored',
|
|
|
|
len(self._pending_receive))
|
2017-09-30 12:49:38 +03:00
|
|
|
self._clear_all_pending()
|
|
|
|
return
|
2016-09-09 12:47:37 +03:00
|
|
|
|
2017-09-07 22:32:46 +03:00
|
|
|
message, remote_msg_id, remote_seq = self._decode_msg(body)
|
2017-09-02 19:27:22 +03:00
|
|
|
with BinaryReader(message) as reader:
|
2017-09-07 19:49:08 +03:00
|
|
|
self._process_msg(remote_msg_id, remote_seq, reader, update_state)
|
2017-09-02 19:27:22 +03:00
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Low level processing
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-09-27 22:01:20 +03:00
|
|
|
def _send_message(self, message):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Sends the given encrypted through the network.
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-11-30 15:20:51 +03:00
|
|
|
:param message: the TLMessage to be sent.
|
|
|
|
"""
|
2018-01-07 01:43:40 +03:00
|
|
|
with self._send_lock:
|
|
|
|
self.connection.send(utils.pack_message(self.session, message))
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-05-29 18:06:48 +03:00
|
|
|
def _decode_msg(self, body):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Decodes the body of the payload received from the network.
|
|
|
|
|
|
|
|
:param body: the body to be decoded.
|
|
|
|
:return: a tuple of (decoded message, remote message id, remote seq).
|
|
|
|
"""
|
2018-01-06 03:55:11 +03:00
|
|
|
if len(body) < 8:
|
|
|
|
if body == b'l\xfe\xff\xff':
|
|
|
|
raise BrokenAuthKeyError()
|
|
|
|
else:
|
|
|
|
raise BufferError("Can't decode packet ({})".format(body))
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
with BinaryReader(body) as reader:
|
2018-01-06 03:55:11 +03:00
|
|
|
return utils.unpack_message(self.session, reader)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-09-07 19:49:08 +03:00
|
|
|
def _process_msg(self, msg_id, sequence, reader, state):
|
2017-06-11 18:53:53 +03:00
|
|
|
"""
|
2017-11-30 15:20:51 +03:00
|
|
|
Processes the message read from the network inside reader.
|
2016-09-09 12:47:37 +03:00
|
|
|
|
2017-11-30 15:20:51 +03:00
|
|
|
:param msg_id: the ID of the message.
|
|
|
|
:param sequence: the sequence of the message.
|
|
|
|
:param reader: the BinaryReader that contains the message.
|
|
|
|
:param state: the current UpdateState.
|
|
|
|
:return: true if the message was handled correctly, false otherwise.
|
|
|
|
"""
|
2016-08-26 13:58:53 +03:00
|
|
|
# TODO Check salt, session_id and sequence_number
|
2017-10-20 23:07:45 +03:00
|
|
|
self._need_confirmation.add(msg_id)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
code = reader.read_int(signed=False)
|
|
|
|
reader.seek(-4)
|
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
# These are a bit of special case, not yet generated by the code gen
|
2017-09-04 18:10:04 +03:00
|
|
|
if code == 0xf35c6d01: # rpc_result, (response of an RPC call)
|
2017-12-20 19:45:40 +03:00
|
|
|
__log__.debug('Processing Remote Procedure Call result')
|
2017-06-11 15:58:16 +03:00
|
|
|
return self._handle_rpc_result(msg_id, sequence, reader)
|
2016-09-10 19:05:20 +03:00
|
|
|
|
2017-10-12 17:40:59 +03:00
|
|
|
if code == MessageContainer.CONSTRUCTOR_ID:
|
2017-12-20 19:45:40 +03:00
|
|
|
__log__.debug('Processing container result')
|
2017-09-07 19:49:08 +03:00
|
|
|
return self._handle_container(msg_id, sequence, reader, state)
|
2017-05-29 21:41:03 +03:00
|
|
|
|
2017-10-12 17:40:59 +03:00
|
|
|
if code == GzipPacked.CONSTRUCTOR_ID:
|
2017-12-20 19:45:40 +03:00
|
|
|
__log__.debug('Processing gzipped result')
|
2017-09-07 19:49:08 +03:00
|
|
|
return self._handle_gzip_packed(msg_id, sequence, reader, state)
|
2017-05-29 21:41:03 +03:00
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
if code not in tlobjects:
|
|
|
|
__log__.warning(
|
|
|
|
'Unknown message with ID %d, data left in the buffer %s',
|
|
|
|
hex(code), repr(reader.get_bytes()[reader.tell_position():])
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
obj = reader.tgread_object()
|
|
|
|
__log__.debug('Processing %s result', type(obj).__name__)
|
|
|
|
|
|
|
|
if isinstance(obj, Pong):
|
|
|
|
return self._handle_pong(msg_id, sequence, obj)
|
2017-05-29 21:41:03 +03:00
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
if isinstance(obj, BadServerSalt):
|
|
|
|
return self._handle_bad_server_salt(msg_id, sequence, obj)
|
2016-09-09 12:47:37 +03:00
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
if isinstance(obj, BadMsgNotification):
|
|
|
|
return self._handle_bad_msg_notification(msg_id, sequence, obj)
|
2017-10-12 22:09:09 +03:00
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
if isinstance(obj, MsgDetailedInfo):
|
|
|
|
return self._handle_msg_detailed_info(msg_id, sequence, obj)
|
2017-10-12 22:09:09 +03:00
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
if isinstance(obj, MsgNewDetailedInfo):
|
|
|
|
return self._handle_msg_new_detailed_info(msg_id, sequence, obj)
|
2017-10-12 22:09:09 +03:00
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
if isinstance(obj, NewSessionCreated):
|
|
|
|
return self._handle_new_session_created(msg_id, sequence, obj)
|
|
|
|
|
|
|
|
if isinstance(obj, MsgsAck): # may handle the request we wanted
|
2017-09-29 14:58:15 +03:00
|
|
|
# Ignore every ack request *unless* when logging out, when it's
|
|
|
|
# when it seems to only make sense. We also need to set a non-None
|
|
|
|
# result since Telegram doesn't send the response for these.
|
2017-12-20 19:45:40 +03:00
|
|
|
for msg_id in obj.msg_ids:
|
2017-09-29 14:58:15 +03:00
|
|
|
r = self._pop_request_of_type(msg_id, LogOutRequest)
|
|
|
|
if r:
|
|
|
|
r.result = True # Telegram won't send this value
|
2017-10-17 01:39:04 +03:00
|
|
|
r.confirm_received.set()
|
2017-04-11 10:52:44 +03:00
|
|
|
|
2017-06-11 18:53:53 +03:00
|
|
|
return True
|
2016-09-26 14:13:11 +03:00
|
|
|
|
2018-01-30 20:32:42 +03:00
|
|
|
if isinstance(obj, FutureSalts):
|
|
|
|
r = self._pop_request(obj.req_msg_id)
|
|
|
|
if r:
|
|
|
|
r.result = obj
|
|
|
|
r.confirm_received.set()
|
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
# If the object isn't any of the above, then it should be an Update.
|
|
|
|
self.session.process_entities(obj)
|
|
|
|
if state:
|
|
|
|
state.process(obj)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
return True
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Message handling
|
|
|
|
|
2017-09-27 22:01:20 +03:00
|
|
|
def _pop_request(self, msg_id):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Pops a pending **request** from self._pending_receive.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the message that belongs to the request.
|
|
|
|
:return: the request, or None if it wasn't found.
|
2017-09-03 11:50:55 +03:00
|
|
|
"""
|
2017-09-27 22:01:20 +03:00
|
|
|
message = self._pending_receive.pop(msg_id, None)
|
|
|
|
if message:
|
|
|
|
return message.request
|
2017-09-03 11:50:55 +03:00
|
|
|
|
2017-09-29 14:58:15 +03:00
|
|
|
def _pop_request_of_type(self, msg_id, t):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Pops a pending **request** from self._pending_receive.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the message that belongs to the request.
|
|
|
|
:param t: the type of the desired request.
|
|
|
|
:return: the request matching the type t, or None if it wasn't found.
|
2017-09-29 14:58:15 +03:00
|
|
|
"""
|
|
|
|
message = self._pending_receive.get(msg_id, None)
|
2017-10-12 19:03:10 +03:00
|
|
|
if message and isinstance(message.request, t):
|
2017-09-29 14:58:15 +03:00
|
|
|
return self._pending_receive.pop(msg_id).request
|
|
|
|
|
2017-10-22 14:13:49 +03:00
|
|
|
def _pop_requests_of_container(self, container_msg_id):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Pops pending **requests** from self._pending_receive.
|
|
|
|
|
|
|
|
:param container_msg_id: the ID of the container.
|
|
|
|
:return: the requests that belong to the given container. May be empty.
|
2017-10-22 14:13:49 +03:00
|
|
|
"""
|
|
|
|
msgs = [msg for msg in self._pending_receive.values()
|
|
|
|
if msg.container_msg_id == container_msg_id]
|
|
|
|
|
|
|
|
requests = [msg.request for msg in msgs]
|
|
|
|
for msg in msgs:
|
|
|
|
self._pending_receive.pop(msg.msg_id, None)
|
|
|
|
return requests
|
|
|
|
|
2017-09-21 14:43:33 +03:00
|
|
|
def _clear_all_pending(self):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Clears all pending requests, and flags them all as received.
|
|
|
|
"""
|
2017-09-27 22:01:20 +03:00
|
|
|
for r in self._pending_receive.values():
|
2017-10-18 16:34:04 +03:00
|
|
|
r.request.confirm_received.set()
|
2017-09-21 14:43:33 +03:00
|
|
|
self._pending_receive.clear()
|
|
|
|
|
2017-10-22 14:13:49 +03:00
|
|
|
def _resend_request(self, msg_id):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Re-sends the request that belongs to a certain msg_id. This may
|
|
|
|
also be the msg_id of a container if they were sent in one.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the request to be resent.
|
2017-10-22 14:13:49 +03:00
|
|
|
"""
|
|
|
|
request = self._pop_request(msg_id)
|
|
|
|
if request:
|
|
|
|
return self.send(request)
|
|
|
|
requests = self._pop_requests_of_container(msg_id)
|
|
|
|
if requests:
|
|
|
|
return self.send(*requests)
|
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
def _handle_pong(self, msg_id, sequence, pong):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Handles a Pong response.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the message.
|
|
|
|
:param sequence: the sequence of the message.
|
|
|
|
:param reader: the reader containing the Pong.
|
|
|
|
:return: true, as it always succeeds.
|
|
|
|
"""
|
2017-10-11 22:09:09 +03:00
|
|
|
request = self._pop_request(pong.msg_id)
|
2017-09-03 11:50:55 +03:00
|
|
|
if request:
|
2017-10-11 22:09:09 +03:00
|
|
|
request.result = pong
|
2017-09-02 21:41:00 +03:00
|
|
|
request.confirm_received.set()
|
2017-01-19 17:54:28 +03:00
|
|
|
|
2017-06-11 18:53:53 +03:00
|
|
|
return True
|
2017-01-19 17:54:28 +03:00
|
|
|
|
2017-09-07 19:49:08 +03:00
|
|
|
def _handle_container(self, msg_id, sequence, reader, state):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Handles a MessageContainer response.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the message.
|
|
|
|
:param sequence: the sequence of the message.
|
|
|
|
:param reader: the reader containing the MessageContainer.
|
|
|
|
:return: true, as it always succeeds.
|
|
|
|
"""
|
2017-09-25 21:52:27 +03:00
|
|
|
for inner_msg_id, _, inner_len in MessageContainer.iter_read(reader):
|
2016-08-26 13:58:53 +03:00
|
|
|
begin_position = reader.tell_position()
|
2016-09-06 19:54:49 +03:00
|
|
|
|
2017-05-29 21:41:03 +03:00
|
|
|
# Note that this code is IMPORTANT for skipping RPC results of
|
|
|
|
# lost requests (i.e., ones from the previous connection session)
|
2017-07-26 17:10:45 +03:00
|
|
|
try:
|
2017-09-07 19:49:08 +03:00
|
|
|
if not self._process_msg(inner_msg_id, sequence, reader, state):
|
2017-09-25 21:52:27 +03:00
|
|
|
reader.set_position(begin_position + inner_len)
|
2017-07-26 17:10:45 +03:00
|
|
|
except:
|
|
|
|
# If any error is raised, something went wrong; skip the packet
|
2017-09-25 21:52:27 +03:00
|
|
|
reader.set_position(begin_position + inner_len)
|
2017-07-26 17:10:45 +03:00
|
|
|
raise
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-06-11 18:53:53 +03:00
|
|
|
return True
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
def _handle_bad_server_salt(self, msg_id, sequence, bad_salt):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Handles a BadServerSalt response.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the message.
|
|
|
|
:param sequence: the sequence of the message.
|
|
|
|
:param reader: the reader containing the BadServerSalt.
|
|
|
|
:return: true, as it always succeeds.
|
|
|
|
"""
|
2017-12-03 23:10:22 +03:00
|
|
|
self.session.salt = bad_salt.new_server_salt
|
2017-10-20 17:48:54 +03:00
|
|
|
self.session.save()
|
2017-10-12 17:40:59 +03:00
|
|
|
|
2017-10-22 14:13:49 +03:00
|
|
|
# "the bad_server_salt response is received with the
|
|
|
|
# correct salt, and the message is to be re-sent with it"
|
|
|
|
self._resend_request(bad_salt.bad_msg_id)
|
2016-08-26 13:58:53 +03:00
|
|
|
return True
|
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
def _handle_bad_msg_notification(self, msg_id, sequence, bad_msg):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Handles a BadMessageError response.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the message.
|
|
|
|
:param sequence: the sequence of the message.
|
|
|
|
:param reader: the reader containing the BadMessageError.
|
|
|
|
:return: true, as it always succeeds.
|
|
|
|
"""
|
2017-10-12 17:40:59 +03:00
|
|
|
error = BadMessageError(bad_msg.error_code)
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.warning('Read bad msg notification %s: %s', bad_msg, error)
|
2017-10-12 17:40:59 +03:00
|
|
|
if bad_msg.error_code in (16, 17):
|
2017-05-26 17:39:59 +03:00
|
|
|
# sent msg_id too low or too high (respectively).
|
|
|
|
# Use the current msg_id to determine the right time offset.
|
|
|
|
self.session.update_time_offset(correct_msg_id=msg_id)
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.info('Attempting to use the correct time offset')
|
2017-10-22 14:13:49 +03:00
|
|
|
self._resend_request(bad_msg.bad_msg_id)
|
2017-06-11 18:53:53 +03:00
|
|
|
return True
|
2017-10-12 17:40:59 +03:00
|
|
|
elif bad_msg.error_code == 32:
|
2017-09-14 12:50:38 +03:00
|
|
|
# msg_seqno too low, so just pump it up by some "large" amount
|
|
|
|
# TODO A better fix would be to start with a new fresh session ID
|
2018-03-02 00:34:32 +03:00
|
|
|
self.session.sequence += 64
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.info('Attempting to set the right higher sequence')
|
2017-10-22 14:13:49 +03:00
|
|
|
self._resend_request(bad_msg.bad_msg_id)
|
2017-09-14 12:50:38 +03:00
|
|
|
return True
|
2017-10-12 17:40:59 +03:00
|
|
|
elif bad_msg.error_code == 33:
|
2017-09-14 12:50:38 +03:00
|
|
|
# msg_seqno too high never seems to happen but just in case
|
2018-03-02 00:34:32 +03:00
|
|
|
self.session.sequence -= 16
|
2017-12-20 14:47:10 +03:00
|
|
|
__log__.info('Attempting to set the right lower sequence')
|
2017-10-22 14:13:49 +03:00
|
|
|
self._resend_request(bad_msg.bad_msg_id)
|
2017-09-14 12:50:38 +03:00
|
|
|
return True
|
2017-05-26 17:39:59 +03:00
|
|
|
else:
|
|
|
|
raise error
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
def _handle_msg_detailed_info(self, msg_id, sequence, msg_new):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Handles a MsgDetailedInfo response.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the message.
|
|
|
|
:param sequence: the sequence of the message.
|
|
|
|
:param reader: the reader containing the MsgDetailedInfo.
|
|
|
|
:return: true, as it always succeeds.
|
|
|
|
"""
|
2017-10-12 22:09:09 +03:00
|
|
|
# TODO For now, simply ack msg_new.answer_msg_id
|
|
|
|
# Relevant tdesktop source code: https://goo.gl/VvpCC6
|
|
|
|
self._send_acknowledge(msg_new.answer_msg_id)
|
|
|
|
return True
|
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
def _handle_msg_new_detailed_info(self, msg_id, sequence, msg_new):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Handles a MsgNewDetailedInfo response.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the message.
|
|
|
|
:param sequence: the sequence of the message.
|
|
|
|
:param reader: the reader containing the MsgNewDetailedInfo.
|
|
|
|
:return: true, as it always succeeds.
|
|
|
|
"""
|
2017-10-12 22:09:09 +03:00
|
|
|
# TODO For now, simply ack msg_new.answer_msg_id
|
|
|
|
# Relevant tdesktop source code: https://goo.gl/G7DPsR
|
|
|
|
self._send_acknowledge(msg_new.answer_msg_id)
|
|
|
|
return True
|
|
|
|
|
2017-12-20 19:45:40 +03:00
|
|
|
def _handle_new_session_created(self, msg_id, sequence, new_session):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Handles a NewSessionCreated response.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the message.
|
|
|
|
:param sequence: the sequence of the message.
|
|
|
|
:param reader: the reader containing the NewSessionCreated.
|
|
|
|
:return: true, as it always succeeds.
|
|
|
|
"""
|
2017-12-04 22:34:35 +03:00
|
|
|
self.session.salt = new_session.server_salt
|
2017-10-12 22:09:09 +03:00
|
|
|
# TODO https://goo.gl/LMyN7A
|
|
|
|
return True
|
|
|
|
|
2017-06-11 15:58:16 +03:00
|
|
|
def _handle_rpc_result(self, msg_id, sequence, reader):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Handles a RPCResult response.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the message.
|
|
|
|
:param sequence: the sequence of the message.
|
|
|
|
:param reader: the reader containing the RPCResult.
|
|
|
|
:return: true if the request ID to which this result belongs is found,
|
|
|
|
false otherwise (meaning nothing was read).
|
|
|
|
"""
|
2017-01-13 23:22:53 +03:00
|
|
|
reader.read_int(signed=False) # code
|
2017-06-21 11:20:39 +03:00
|
|
|
request_id = reader.read_long()
|
2016-09-06 19:54:49 +03:00
|
|
|
inner_code = reader.read_int(signed=False)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2018-02-12 15:40:00 +03:00
|
|
|
__log__.debug('Received response for request with ID %d', request_id)
|
2017-09-03 11:50:55 +03:00
|
|
|
request = self._pop_request(request_id)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
if inner_code == 0x2144ca19: # RPC Error
|
2017-08-25 16:34:20 +03:00
|
|
|
if self.session.report_errors and request:
|
|
|
|
error = rpc_message_to_error(
|
|
|
|
reader.read_int(), reader.tgread_string(),
|
2017-09-29 14:11:33 +03:00
|
|
|
report_method=type(request).CONSTRUCTOR_ID
|
2017-08-25 16:34:20 +03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
error = rpc_message_to_error(
|
|
|
|
reader.read_int(), reader.tgread_string()
|
|
|
|
)
|
2017-04-09 14:14:04 +03:00
|
|
|
|
2017-06-09 11:46:39 +03:00
|
|
|
# Acknowledge that we received the error
|
2017-10-06 12:36:39 +03:00
|
|
|
self._send_acknowledge(request_id)
|
2017-06-09 11:46:39 +03:00
|
|
|
|
2017-09-02 21:41:00 +03:00
|
|
|
if request:
|
2017-09-03 12:19:28 +03:00
|
|
|
request.rpc_error = error
|
2017-09-02 21:41:00 +03:00
|
|
|
request.confirm_received.set()
|
|
|
|
# else TODO Where should this error be reported?
|
|
|
|
# Read may be async. Can an error not-belong to a request?
|
2017-10-12 18:58:37 +03:00
|
|
|
return True # All contents were read okay
|
|
|
|
|
|
|
|
elif request:
|
|
|
|
if inner_code == 0x3072cfa1: # GZip packed
|
|
|
|
unpacked_data = gzip.decompress(reader.tgread_bytes())
|
|
|
|
with BinaryReader(unpacked_data) as compressed_reader:
|
|
|
|
request.on_response(compressed_reader)
|
2017-06-11 18:53:53 +03:00
|
|
|
else:
|
2017-10-12 18:58:37 +03:00
|
|
|
reader.seek(-4)
|
|
|
|
request.on_response(reader)
|
|
|
|
|
|
|
|
self.session.process_entities(request.result)
|
|
|
|
request.confirm_received.set()
|
|
|
|
return True
|
|
|
|
|
|
|
|
# If it's really a result for RPC from previous connection
|
2018-02-12 15:40:00 +03:00
|
|
|
# session, it will be skipped by the handle_container().
|
|
|
|
# For some reason this also seems to happen when downloading
|
|
|
|
# photos, where the server responds with FileJpeg().
|
|
|
|
try:
|
|
|
|
obj = reader.tgread_object()
|
|
|
|
except Exception as e:
|
|
|
|
obj = '(failed to read: %s)' % e
|
|
|
|
|
|
|
|
__log__.warning(
|
|
|
|
'Lost request (ID %d) with code %s will be skipped, contents: %s',
|
|
|
|
request_id, hex(inner_code), obj
|
|
|
|
)
|
2017-10-12 18:58:37 +03:00
|
|
|
return False
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-09-07 19:49:08 +03:00
|
|
|
def _handle_gzip_packed(self, msg_id, sequence, reader, state):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Handles a GzipPacked response.
|
|
|
|
|
|
|
|
:param msg_id: the ID of the message.
|
|
|
|
:param sequence: the sequence of the message.
|
|
|
|
:param reader: the reader containing the GzipPacked.
|
|
|
|
:return: the result of processing the packed message.
|
|
|
|
"""
|
2017-09-27 14:46:53 +03:00
|
|
|
with BinaryReader(GzipPacked.read(reader)) as compressed_reader:
|
2017-10-20 23:44:00 +03:00
|
|
|
# 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}
|
2017-09-07 19:49:08 +03:00
|
|
|
return self._process_msg(msg_id, sequence, compressed_reader, state)
|
2016-08-28 14:43:00 +03:00
|
|
|
|
|
|
|
# endregion
|