# -*- coding: utf-8 -*- """ Created on Tue Sep 2 19:26:15 2014 @author: Anton Grigoryev @author: Sammy Pfeiffer """ from binascii import crc32 as originalcrc32 def crc32(data): return originalcrc32(data) & 0xffffffff from datetime import datetime from time import time import io import os.path import json import socket import struct def vis(bs): """ Function to visualize byte streams. Split into bytes, print to console. :param bs: BYTE STRING """ bs = bytearray(bs) symbols_in_one_line = 8 n = len(bs) // symbols_in_one_line for i in range(n): print(str(i*symbols_in_one_line)+" | "+" ".join(["%02X" % b for b in bs[i*symbols_in_one_line:(i+1)*symbols_in_one_line]])) # for every 8 symbols line if not len(bs) % symbols_in_one_line == 0: print(str((i+1)*symbols_in_one_line)+" | "+" ".join(["%02X" % b for b in bs[(i+1)*symbols_in_one_line:]])+"\n") # for last line class TlConstructor: def __init__(self, json_dict): self.id = int(json_dict['id']) self.type = json_dict['type'] self.predicate = json_dict['predicate'] self.params = [] # case of vector for param in json_dict['params']: if param['type'] == "Vector": param['type'] = "Vector t" param['subtype'] = "long" else: param['subtype'] = None self.params.append(param) class TlMethod: def __init__(self, json_dict): self.id = int(json_dict['id']) self.type = json_dict['type'] self.method = json_dict['method'] self.params = json_dict['params'] class TL: def __init__(self, filename): with open(filename, 'r') as f: TL_dict = json.load(f) # Read constructors self.constructors = TL_dict['constructors'] self.constructor_id = {} self.constructor_type = {} for elem in self.constructors: z = TlConstructor(elem) self.constructor_id[z.id] = z self.constructor_type[z.predicate] = z self.methods = TL_dict['methods'] self.method_id = {} self.method_name = {} for elem in self.methods: z = TlMethod(elem) self.method_id[z.id] = z self.method_name[z.method] = z ## Loading TL_schema (should be placed in the same directory as mtproto.py tl = TL(os.path.join(os.path.dirname(__file__), "TL_schema.JSON")) def serialize_obj(bytes_io, type_, **kwargs): try: tl_constructor = tl.constructor_type[type_] except KeyError: raise Exception("Could not extract type: %s" % type_) bytes_io.write(struct.pack('>') vis(message) # Sending message visualisation to console data = self.header_unencrypted(message) + message step1 = struct.pack(' 0: # if we have smth. in the socket packet_length = struct.unpack("