First attempt at TelegramClient. Added fixes and doc

This commit is contained in:
Lonami
2016-09-04 11:07:18 +02:00
parent c863537b7b
commit 39a23559f0
19 changed files with 256 additions and 65 deletions
+12 -9
View File
@@ -4,18 +4,21 @@
# https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/Auth/Step2_DHExchange.cs
# https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/Auth/Step3_CompleteDHExchange.cs
from network.mtproto_plain_sender import MtProtoPlainSender
from utils.binary_writer import BinaryWriter
from utils.binary_reader import BinaryReader
from utils.factorizator import Factorizator
from utils.auth_key import AuthKey
import utils.helpers as utils
import time
from utils.rsa import RSA
from utils.aes import AES
import utils.helpers as utils
from crypto.aes import AES
from crypto.auth_key import AuthKey
from crypto.factorizator import Factorizator
from crypto.rsa import RSA
from network.mtproto_plain_sender import MtProtoPlainSender
from utils.binary_reader import BinaryReader
from utils.binary_writer import BinaryWriter
def do_authentication(transport):
"""Executes the authentication process with the Telegram servers.
If no error is rose, returns both the authorization key and the time offset"""
sender = MtProtoPlainSender(transport)
# Step 1 sending: PQ Request
@@ -201,4 +204,4 @@ def do_authentication(transport):
def get_fingerprint_text(fingerprint):
"""Gets a fingerprint text in 01-23-45-67-89-AB-CD-EF format (no hyphens)"""
return ''.join(hex(b)[2:].rjust(2, '0').upper() for b in fingerprint)
return ''.join(hex(b)[2:].rjust(2, '0').upper() for b in fingerprint)
+7 -9
View File
@@ -2,9 +2,9 @@
# https://github.com/sochix/TLSharp/blob/master/TLSharp.Core/Network/MtProtoSender.cs
import re
import zlib
import pyaes
from time import sleep
from crypto.aes import AES
from utils.binary_writer import BinaryWriter
from utils.binary_reader import BinaryReader
from tl.types.msgs_ack import MsgsAck
@@ -76,13 +76,12 @@ class MtProtoSender:
msg_key = helpers.calc_msg_key(writer.get_bytes())
key, iv = helpers.calc_key(self.session.auth_key.data, msg_key, True)
aes = pyaes.AESModeOfOperationCFB(key, iv, 16)
cipher_text = aes.encrypt(writer.get_bytes())
key, iv = helpers.calc_key(self.session.auth_key.key, msg_key, True)
cipher_text = AES.encrypt_ige(writer.get_bytes(), key, iv)
# And then finally send the packet
with BinaryWriter() as writer:
writer.write_long(self.session.auth_key.id, signed=False)
writer.write_long(self.session.auth_key.key_id, signed=False)
writer.write(msg_key)
writer.write(cipher_text)
@@ -96,15 +95,14 @@ class MtProtoSender:
with BinaryReader(body) as reader:
if len(body) < 8:
raise BufferError("Can't decode packet")
raise BufferError("Can't decode packet ({})".format(body))
# TODO Check for both auth key ID and msg_key correctness
remote_auth_key_id = reader.read_long()
msg_key = reader.read(16)
key, iv = helpers.calc_key(self.session.auth_key.data, msg_key, False)
aes = pyaes.AESModeOfOperationCFB(key, iv, 16)
plain_text = aes.decrypt(reader.read(len(body) - reader.tell_position()))
plain_text = AES.decrypt_ige(reader.read(len(body) - reader.tell_position()), key, iv)
with BinaryReader(plain_text) as plain_text_reader:
remote_salt = plain_text_reader.read_long()
@@ -278,7 +276,7 @@ class MtProtoSender:
elif error_msg.startswith('PHONE_MIGRATE_'):
dc_index = int(re.search(r'\d+', error_msg).group(0))
raise ConnectionError('Your phone number registered to {} dc. Please update settings. '
raise ConnectionError('Your phone number is registered to {} DC. Please update settings. '
'See https://github.com/sochix/TLSharp#i-get-an-error-migrate_x '
'for details.'.format(dc_index))
else: