2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
This module contains the class used to communicate with Telegram's servers
|
|
|
|
in plain text, when no authorization key has been created yet.
|
|
|
|
"""
|
2017-09-28 12:43:06 +03:00
|
|
|
import struct
|
2016-11-30 00:29:42 +03:00
|
|
|
|
2018-06-09 12:36:59 +03:00
|
|
|
from .mtprotostate import MTProtoState
|
2018-10-12 20:47:40 +03:00
|
|
|
from ..errors import InvalidBufferError
|
2021-09-12 17:58:06 +03:00
|
|
|
from .._misc.binaryreader import BinaryReader
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
|
2018-06-07 17:32:12 +03:00
|
|
|
class MTProtoPlainSender:
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
MTProto Mobile Protocol plain sender
|
|
|
|
(https://core.telegram.org/mtproto/description#unencrypted-messages)
|
2017-09-04 18:10:04 +03:00
|
|
|
"""
|
2019-01-12 14:15:29 +03:00
|
|
|
def __init__(self, connection, *, loggers):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Initializes the MTProto plain sender.
|
|
|
|
|
|
|
|
:param connection: the Connection to be used.
|
|
|
|
"""
|
2019-01-12 14:15:29 +03:00
|
|
|
self._state = MTProtoState(auth_key=None, loggers=loggers)
|
2017-08-28 22:23:31 +03:00
|
|
|
self._connection = connection
|
2017-05-20 12:33:37 +03:00
|
|
|
|
2018-06-07 17:32:12 +03:00
|
|
|
async def send(self, request):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
2018-06-07 17:32:12 +03:00
|
|
|
Sends and receives the result for the given request.
|
2017-09-04 18:10:04 +03:00
|
|
|
"""
|
2018-06-07 17:32:12 +03:00
|
|
|
body = bytes(request)
|
2018-06-09 12:36:59 +03:00
|
|
|
msg_id = self._state._get_new_msg_id()
|
2018-06-07 17:32:12 +03:00
|
|
|
await self._connection.send(
|
2018-10-01 10:58:53 +03:00
|
|
|
struct.pack('<qqi', 0, msg_id, len(body)) + body
|
2017-09-28 12:43:06 +03:00
|
|
|
)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2018-06-07 17:32:12 +03:00
|
|
|
body = await self._connection.recv()
|
2018-10-12 20:47:40 +03:00
|
|
|
if len(body) < 8:
|
|
|
|
raise InvalidBufferError(body)
|
2017-09-17 19:38:03 +03:00
|
|
|
|
2016-09-08 17:11:37 +03:00
|
|
|
with BinaryReader(body) as reader:
|
2018-08-14 20:14:13 +03:00
|
|
|
auth_key_id = reader.read_long()
|
|
|
|
assert auth_key_id == 0, 'Bad auth_key_id'
|
2018-06-25 13:54:33 +03:00
|
|
|
|
2018-08-14 20:14:13 +03:00
|
|
|
msg_id = reader.read_long()
|
|
|
|
assert msg_id != 0, 'Bad msg_id'
|
2018-06-28 10:48:03 +03:00
|
|
|
# ^ We should make sure that the read ``msg_id`` is greater
|
|
|
|
# than our own ``msg_id``. However, under some circumstances
|
|
|
|
# (bad system clock/working behind proxies) this seems to not
|
|
|
|
# be the case, which would cause endless assertion errors.
|
|
|
|
|
2018-08-14 20:14:13 +03:00
|
|
|
length = reader.read_int()
|
|
|
|
assert length > 0, 'Bad length'
|
2018-06-25 13:54:33 +03:00
|
|
|
# We could read length bytes and use those in a new reader to read
|
|
|
|
# the next TLObject without including the padding, but since the
|
|
|
|
# reader isn't used for anything else after this, it's unnecessary.
|
2018-06-07 17:32:12 +03:00
|
|
|
return reader.tgread_object()
|