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
|
|
|
import time
|
|
|
|
|
2017-09-17 19:38:03 +03:00
|
|
|
from ..errors import BrokenAuthKeyError
|
2017-09-28 12:43:06 +03:00
|
|
|
from ..extensions 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
|
|
|
"""
|
2017-08-28 22:23:31 +03:00
|
|
|
def __init__(self, connection):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""
|
|
|
|
Initializes the MTProto plain sender.
|
|
|
|
|
|
|
|
:param connection: the Connection to be used.
|
|
|
|
"""
|
2016-08-26 13:58:53 +03:00
|
|
|
self._sequence = 0
|
|
|
|
self._time_offset = 0
|
|
|
|
self._last_msg_id = 0
|
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)
|
|
|
|
msg_id = self._get_new_msg_id()
|
|
|
|
await self._connection.send(
|
|
|
|
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()
|
2017-09-17 19:38:03 +03:00
|
|
|
if body == b'l\xfe\xff\xff': # -404 little endian signed
|
|
|
|
# Broken authorization, must reset the auth key
|
|
|
|
raise BrokenAuthKeyError()
|
|
|
|
|
2016-09-08 17:11:37 +03:00
|
|
|
with BinaryReader(body) as reader:
|
2018-06-07 17:32:12 +03:00
|
|
|
assert reader.read_long() == 0 # auth_key_id
|
|
|
|
assert reader.read_long() > msg_id # msg_id
|
|
|
|
assert reader.read_int() # length
|
|
|
|
# No need to read "length" bytes first, just read the object
|
|
|
|
return reader.tgread_object()
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-05-29 18:06:48 +03:00
|
|
|
def _get_new_msg_id(self):
|
2017-11-30 15:20:51 +03:00
|
|
|
"""Generates a new message ID based on the current time since epoch."""
|
2017-06-26 12:00:43 +03:00
|
|
|
# See core.telegram.org/mtproto/description#message-identifier-msg-id
|
|
|
|
now = time.time()
|
|
|
|
nanoseconds = int((now - int(now)) * 1e+9)
|
|
|
|
# "message identifiers are divisible by 4"
|
|
|
|
new_msg_id = (int(now) << 32) | (nanoseconds << 2)
|
2016-08-26 13:58:53 +03:00
|
|
|
if self._last_msg_id >= new_msg_id:
|
|
|
|
new_msg_id = self._last_msg_id + 4
|
|
|
|
|
|
|
|
self._last_msg_id = new_msg_id
|
|
|
|
return new_msg_id
|