2016-09-04 22:07:09 +03:00
|
|
|
import random
|
2016-11-30 00:29:42 +03:00
|
|
|
import time
|
|
|
|
|
2017-06-09 17:13:39 +03:00
|
|
|
from ..extensions import BinaryReader, BinaryWriter
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
class MtProtoPlainSender:
|
2016-08-28 14:43:00 +03:00
|
|
|
"""MTProto Mobile Protocol plain sender (https://core.telegram.org/mtproto/description#unencrypted-messages)"""
|
2016-11-30 00:29:42 +03:00
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
def __init__(self, transport):
|
|
|
|
self._sequence = 0
|
|
|
|
self._time_offset = 0
|
|
|
|
self._last_msg_id = 0
|
|
|
|
self._transport = transport
|
2017-05-20 12:33:37 +03:00
|
|
|
|
|
|
|
def connect(self):
|
2017-04-29 21:57:08 +03:00
|
|
|
self._transport.connect()
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-05-20 12:33:37 +03:00
|
|
|
def disconnect(self):
|
|
|
|
self._transport.close()
|
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
def send(self, data):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Sends a plain packet (auth_key_id = 0) containing the given message body (data)"""
|
2016-08-26 13:58:53 +03:00
|
|
|
with BinaryWriter() as writer:
|
|
|
|
writer.write_long(0)
|
2017-05-29 18:06:48 +03:00
|
|
|
writer.write_long(self._get_new_msg_id())
|
2016-08-26 13:58:53 +03:00
|
|
|
writer.write_int(len(data))
|
|
|
|
writer.write(data)
|
|
|
|
|
|
|
|
packet = writer.get_bytes()
|
|
|
|
self._transport.send(packet)
|
|
|
|
|
|
|
|
def receive(self):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Receives a plain packet, returning the body of the response"""
|
2016-09-08 17:11:37 +03:00
|
|
|
seq, body = self._transport.receive()
|
|
|
|
with BinaryReader(body) as reader:
|
2017-01-13 23:22:53 +03:00
|
|
|
reader.read_long() # auth_key_id
|
|
|
|
reader.read_long() # msg_id
|
2016-08-26 13:58:53 +03:00
|
|
|
message_length = reader.read_int()
|
|
|
|
|
|
|
|
response = reader.read(message_length)
|
|
|
|
return response
|
|
|
|
|
2017-05-29 18:06:48 +03:00
|
|
|
def _get_new_msg_id(self):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Generates a new message ID based on the current time (in ms) since epoch"""
|
2016-09-04 22:07:09 +03:00
|
|
|
# See https://core.telegram.org/mtproto/description#message-identifier-msg-id
|
|
|
|
ms_time = int(time.time() * 1000)
|
2016-11-30 00:29:42 +03:00
|
|
|
new_msg_id = (((ms_time // 1000) << 32)
|
2017-05-21 14:59:16 +03:00
|
|
|
| # "must approximately equal unix time*2^32"
|
2016-11-30 00:29:42 +03:00
|
|
|
((ms_time % 1000) << 22)
|
|
|
|
| # "approximate moment in time the message was created"
|
|
|
|
random.randint(0, 524288)
|
|
|
|
<< 2) # "message identifiers are divisible by 4"
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-08-28 14:43:00 +03:00
|
|
|
# Ensure that we always return a message ID which is higher than the previous one
|
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
|