Ejemplo n.º 1
0
        public override void OnResponse(BinaryReader reader)
        {
            var code = reader.ReadUInt32();

            // if contactsNotModified then exit
            if (code == 0xb74ba9d2)
            {
                return;
            }

            reader.ReadInt32(); // vector code
            var contactLen = reader.ReadInt32();

            Contacts = new List <Contact>(contactLen);
            for (var importedIndex = 0; importedIndex < contactLen; importedIndex++)
            {
                var importedElement = TL.Parse <Contact>(reader);
                this.Contacts.Add(importedElement);
            }
            reader.ReadInt32(); // vector code
            var usersLen = reader.ReadInt32();

            Users = new List <User>(usersLen);
            for (var usersIndex = 0; usersIndex < usersLen; usersIndex++)
            {
                var usersElement = TL.Parse <User>(reader);
                this.Users.Add(usersElement);
            }
        }
Ejemplo n.º 2
0
        public override void OnResponse(BinaryReader reader)
        {
            var dataCode = reader.ReadUInt32(); //0xf6b673a4

            SessionExpires = reader.ReadInt32();
            user           = TL.Parse <User>(reader);
        }
Ejemplo n.º 3
0
        public override void OnResponse(BinaryReader reader)
        {
            var code = reader.ReadUInt32(); // upload.file#96a18d5

            type  = TL.Parse <storage_FileType>(reader);
            mtime = reader.ReadInt32();
            bytes = Serializers.Bytes.read(reader);
        }
Ejemplo n.º 4
0
 private bool HandleUpdate(ulong messageId, int sequence, BinaryReader messageReader)
 {
     try {
         UpdatesEvent(TL.Parse <Updates>(messageReader));
         return(true);
     } catch (Exception e) {
         logger.warning("update processing exception: {0}", e);
         return(false);
     }
 }
Ejemplo n.º 5
0
 private void Read(BinaryReader reader)
 {
     fromId  = reader.ReadInt32();
     toId    = reader.ReadInt32();
     date    = reader.ReadInt32();
     output  = reader.ReadBoolean();
     unread  = reader.ReadBoolean();
     message = TL.Parse <DecryptedMessage>(reader);
     file    = TL.Parse <EncryptedFile>(reader);
 }
Ejemplo n.º 6
0
        public void read(BinaryReader reader)
        {
            logger.info("read session...");
            id         = reader.ReadUInt64();
            sequence   = reader.ReadInt32();
            mainDcId   = reader.ReadInt32();
            timeOffset = reader.ReadInt32();
            cachedSalt = reader.ReadUInt64();
            int count = reader.ReadInt32();

            // contacts sync marker
            ContactsStateMarker = Serializers.String.read(reader);

            dcs = new Dictionary <int, TelegramDC>(count);
            for (int i = 0; i < count; i++)
            {
                int endpointId = reader.ReadInt32();
                dcs.Add(endpointId, new TelegramDC(reader));
            }

            int authorizationExists = reader.ReadInt32();

            if (authorizationExists != 0)
            {
                authorization = (Auth_authorizationConstructor)TL.Parse <auth_Authorization>(reader);
            }


            int usersCount = reader.ReadInt32();

            users = new Dictionary <int, UserModel>(usersCount + 10);
            for (int i = 0; i < usersCount; i++)
            {
                users.Add(reader.ReadInt32(), new UserModel(TL.Parse <User>(reader)));
            }

            int chatsCount = reader.ReadInt32();

            chats = new Dictionary <int, ChatModel>(chatsCount + 10);
            for (int i = 0; i < chatsCount; i++)
            {
                chats.Add(reader.ReadInt32(), new ChatModel(TL.Parse <Chat>(reader)));
            }

            logger.info("reading updates state....");
            updates = new UpdatesProcessor(this, reader);

            logger.info("reading dialogs...");
            dialogs = new Dialogs(this, reader);

            files          = new Files(this);
            encryptedChats = new EncryptedChats(this, reader);

            logger.info("session readed complete");
        }
Ejemplo n.º 7
0
        public override void OnResponse(BinaryReader reader)
        {
            var code = reader.ReadUInt32();

            if (code != 0xd07ae726 && code != 0xa9af2881)
            {
                throw new InvalidOperationException($"Expected Tl messages_StatedMessage type");
            }

            StatedMessage = TL.Parse <messages_StatedMessage>(reader);
        }
Ejemplo n.º 8
0
        public override void OnResponse(BinaryReader reader)
        {
            bool dialogsSlice = reader.ReadUInt32() == 0x71e094f3; // else dialogs#15ba6c40

            if (dialogsSlice)
            {
                count = reader.ReadInt32();               // count
            }
            // dialogs
            var result      = reader.ReadUInt32(); // vector#1cb5c415
            int dialogs_len = reader.ReadInt32();

            dialogs = new List <Dialog>(dialogs_len);
            for (int dialogs_index = 0; dialogs_index < dialogs_len; dialogs_index++)
            {
                Dialog dialog_element;
                dialog_element = TL.Parse <Dialog>(reader);
                dialogs.Add(dialog_element);
            }
            // messages
            result = reader.ReadUInt32(); // vector#1cb5c415
            int messages_len = reader.ReadInt32();

            messages = new List <Message>(messages_len);
            for (int message_index = 0; message_index < messages_len; message_index++)
            {
                Message messages_element;
                messages_element = TL.Parse <Message>(reader);
                messages.Add(messages_element);
            }
            // chats
            result = reader.ReadUInt32(); // vector#1cb5c415
            int chats_len = reader.ReadInt32();

            chats = new List <Chat>(chats_len);
            for (int chat_index = 0; chat_index < chats_len; chat_index++)
            {
                Chat chats_element;
                chats_element = TL.Parse <Chat>(reader);
                chats.Add(chats_element);
            }
            // users
            result = reader.ReadUInt32(); // vector#1cb5c415
            int users_len = reader.ReadInt32();

            users = new List <User>(users_len);
            for (int users_index = 0; users_index < users_len; users_index++)
            {
                User users_element;
                users_element = TL.Parse <User>(reader);
                users.Add(users_element);
            }
        }
Ejemplo n.º 9
0
        public override void OnResponse(BinaryReader reader)
        {
            var code      = reader.ReadUInt32(); // vector#1cb5c415
            int users_len = reader.ReadInt32();  // vector length

            if (users_len != 0)
            {
                users = new List <User>(users_len);
                for (int i = 0; i < users_len; i++)
                {
                    users.Add(TL.Parse <User>(reader));
                }
            }
        }
Ejemplo n.º 10
0
        public override void Read(BinaryReader reader)
        {
            chat = TL.Parse <EncryptedChat>(reader);
            ttl  = reader.ReadInt32();
            int keyExists = reader.ReadInt32();

            if (keyExists != 0)
            {
                key         = Serializers.Bytes.read(reader);
                fingerprint = reader.ReadInt64();
            }



            int aExists = reader.ReadInt32();

            if (aExists != 0)
            {
                a = Serializers.Bytes.read(reader);
            }

            int messagesCount = reader.ReadInt32();

            for (int i = 0; i < messagesCount; i++)
            {
                int type = reader.ReadInt32();
                switch (type)
                {
                case 1:
                    messages.Add(new MessageModelDelivered(reader));
                    break;

                case 2:
                    messages.Add(new MessageModelUndelivered(reader));
                    break;

                case 3:
                    messages.Add(new MessageModelEncryptedDelivered(reader));
                    break;
                }
            }
        }
Ejemplo n.º 11
0
        public static Session FromBytes(byte[] buffer, ISessionStore store, string sessionUserId)
        {
            using (var stream = new MemoryStream(buffer))
                using (var reader = new BinaryReader(stream))
                {
                    var id            = reader.ReadUInt64();
                    var sequence      = reader.ReadInt32();
                    var salt          = reader.ReadUInt64();
                    var lastMessageId = reader.ReadInt64();
                    var timeOffset    = reader.ReadInt32();
                    var serverAddress = Serializers.String.read(reader);
                    var port          = reader.ReadInt32();

                    var  isAuthExsist   = reader.ReadInt32() == 1;
                    int  sessionExpires = 0;
                    User user           = null;
                    if (isAuthExsist)
                    {
                        sessionExpires = reader.ReadInt32();
                        user           = TL.Parse <User>(reader);
                    }

                    var authData = Serializers.Bytes.read(reader);

                    return(new Session(store)
                    {
                        AuthKey = new AuthKey(authData),
                        Id = id,
                        Salt = salt,
                        Sequence = sequence,
                        LastMessageId = lastMessageId,
                        TimeOffset = timeOffset,
                        SessionExpires = sessionExpires,
                        User = user,
                        SessionUserId = sessionUserId,
                        ServerAddress = serverAddress,
                        Port = port
                    });
                }
        }
Ejemplo n.º 12
0
        public override void OnResponse(BinaryReader reader)
        {
            uint code = reader.ReadUInt32();

            System.Diagnostics.Debug.WriteLine("code: " + code.ToString("x"));
            ;

            if (code == 0x4e32b894) // config constructor
            {
                ConfigConstructor config = new ConfigConstructor();
                config.Read(reader);
                ConfigConstructor = config;
            }
            else if (code == 0xff036af1) // auth.authorization :/
            {
                Auth_authorizationConstructor auth = new Auth_authorizationConstructor();
                auth.Read(reader);
                UserConstructor = (UserConstructor)auth.user;
            }
            else // TODO something didn't go right...
            {
                if (code == 0xb446ae3) // messages slice still?
                {
                    var result = TL.Parse(reader, code);

                    ;
                }
                else
                {
                    try
                    {
                        while (true)
                        {
                            reader.ReadUInt32();          // flush reader, it's the best we can do!
                        }
                    }
                    catch (EndOfStreamException) { }
                }
            }
        }
Ejemplo n.º 13
0
        public void read(BinaryReader reader)
        {
            int endpointCount = reader.ReadInt32();

            endpoints = new List <TelegramEndpoint>(endpointCount);
            for (int i = 0; i < endpointCount; i++)
            {
                endpoints.Add(new TelegramEndpoint(reader));
            }

            int keyExists = reader.ReadInt32();

            if (keyExists == 0)
            {
                authKey = null;
            }
            else
            {
                authKey = new AuthKey(Serializers.Bytes.read(reader));
            }

            //int fileSessionExists = reader.ReadInt32();
            //if(fileSessionExists == 0) {
            //    fileSession = null;
            //} else {
            //    fileSession = new TelegramFileSession(reader);
            //}

            int fileAuthExists = reader.ReadInt32();

            if (fileAuthExists == 0)
            {
                fileAuthorization = null;
            }
            else
            {
                fileAuthorization = (Auth_authorizationConstructor)TL.Parse <auth_Authorization>(reader);
            }
        }
        public override void OnResponse(BinaryReader reader)
        {
            bool messagesSlice = reader.ReadUInt32() == 0xb446ae3; // else messages#8c718e87

            if (messagesSlice)
            {
                reader.ReadInt32();                // count
            }
            // messages
            var result       = reader.ReadUInt32(); // vector#1cb5c415
            int messages_len = reader.ReadInt32();

            messages = new List <Message>(messages_len);
            for (var i = 0; i < messages_len; i++)
            {
                var msgEl = TL.Parse <Message>(reader);

                messages.Add(msgEl);
            }

            // chats
            reader.ReadUInt32();
            int chats_len = reader.ReadInt32();

            chats = new List <Chat>(chats_len);
            for (int i = 0; i < chats_len; i++)
            {
                chats.Add(TL.Parse <Chat>(reader));
            }

            /*
             *          // users
             *          reader.ReadUInt32();
             *          int users_len = reader.ReadInt32();
             *          users = new List<User>(users_len);
             *          for (int i = 0; i < users_len; i++)
             *                  users.Add(TL.Parse<User>(reader));
             */
        }
Ejemplo n.º 15
0
        public override void OnResponse(BinaryReader reader)
        {
            var code         = reader.ReadUInt32();
            var result       = reader.ReadInt32();       // vector code
            int imported_len = reader.ReadInt32();

            this.imported = new List <ImportedContact>(imported_len);
            for (int imported_index = 0; imported_index < imported_len; imported_index++)
            {
                ImportedContact imported_element;
                imported_element = TL.Parse <ImportedContact>(reader);
                this.imported.Add(imported_element);
            }
            reader.ReadInt32();             // vector code
            int users_len = reader.ReadInt32();

            this.users = new List <User>(users_len);
            for (int users_index = 0; users_index < users_len; users_index++)
            {
                User users_element;
                users_element = TL.Parse <User>(reader);
                this.users.Add(users_element);
            }
        }
 public override void OnResponse(BinaryReader reader)
 {
     updatesDifference = TL.Parse <updates_Difference>(reader);
 }
Ejemplo n.º 17
0
        // in background thread
        public void ReceiveMessage(EncryptedMessage encryptedMessage)
        {
            try {
                if (encryptedMessage.Constructor == Constructor.encryptedMessage)
                {
                    logger.info("simple encrypted message");
                    EncryptedMessageConstructor encryptedMessageConstructor = (EncryptedMessageConstructor)encryptedMessage;
                    byte[] data = encryptedMessageConstructor.bytes;
                    byte[] msgKey;
                    long   keyFingerprint;

                    logger.info("encrypted message data: {0}", BitConverter.ToString(data).Replace("-", "").ToLower());

                    using (MemoryStream memory = new MemoryStream(data)) {
                        using (BinaryReader reader = new BinaryReader(memory)) {
                            keyFingerprint = reader.ReadInt64();
                            msgKey         = reader.ReadBytes(16);
                            data           = reader.ReadBytes(data.Length - 16 - 8);
                        }
                    }

                    if (fingerprint != keyFingerprint)
                    {
                        logger.error("invalid key fingerprint");
                        return;
                    }

                    logger.info("ciphertext data: {0}", BitConverter.ToString(data).Replace("-", "").ToLower());

                    AESKeyData aesKey = Helpers.CalcKey(key, msgKey, true);
                    data = AES.DecryptAES(aesKey, data);
                    byte[] data2 = AES.EncryptAES(aesKey, data);
                    byte[] data3 = AES.DecryptAES(aesKey, data2);

                    logger.info("aes equals: {0}", data.SequenceEqual(data3));

                    logger.info("plaintext data: {0}", BitConverter.ToString(data).Replace("-", "").ToLower());
                    logger.info("two-transformed plaintext: {0}", BitConverter.ToString(data3).Replace("-", "").ToLower());

                    byte[] calculatedMsgKey;

                    using (MemoryStream memory = new MemoryStream(data)) {
                        using (BinaryReader reader = new BinaryReader(memory)) {
                            int len = reader.ReadInt32();
                            logger.info("readed len = {0}, actual len = {1}", len, data.Length - 4);
                            if (len < 0 || len > data.Length - 4)
                            {
                                return;
                            }
                            calculatedMsgKey = Helpers.CalcMsgKey(data, 0, 4 + len);
                            data             = reader.ReadBytes(len);
                        }
                    }

                    if (!msgKey.SequenceEqual(calculatedMsgKey))
                    {
                        logger.info("incalid msg key: data {0}, sha1 {1}, received msg key {2}", BitConverter.ToString(data), BitConverter.ToString(Helpers.sha1(data)), BitConverter.ToString(msgKey));
                        return;
                    }

                    DecryptedMessage decryptedMessage;

                    using (MemoryStream memory = new MemoryStream(data)) {
                        using (BinaryReader reader = new BinaryReader(memory)) {
                            //DecryptedMessageLayerConstructor layer = (DecryptedMessageLayerConstructor) TL.Parse<DecryptedMessageLayer>(reader);
//                            if(layer.layer > 8) {
//                                logger.info("encrypted message layer {0} - need upgrade", layer.layer);
//                                // TODO: notify - need upgrade
//                                return;
//                            }

                            decryptedMessage = TL.Parse <DecryptedMessage>(reader);
                        }
                    }



                    logger.info("decrypted message: {0}", decryptedMessage);

                    if (decryptedMessage.Constructor == Constructor.decryptedMessageService)
                    {
                        DecryptedMessageAction action = ((DecryptedMessageServiceConstructor)decryptedMessage).action;
                        if (action.Constructor == Constructor.decryptedMessageActionSetMessageTTL)
                        {
                            DecryptedMessageActionSetMessageTTLConstructor actionttl = (DecryptedMessageActionSetMessageTTLConstructor)action;
                            UpdateTTL(actionttl.ttl_seconds);
                        }
                    }

                    MessageModel messageModel = new MessageModelEncryptedDelivered(OpponentId, TelegramSession.Instance.SelfId, encryptedMessageConstructor.date, false, true, decryptedMessage, encryptedMessageConstructor.file);

                    Deployment.Current.Dispatcher.BeginInvoke(() => {
                        messages.Add(messageModel);

                        if (this == TelegramSession.Instance.Dialogs.OpenedDialog)
                        {
                            OpenedRead();
                        }
                    });
                }
            } catch (Exception e) {
                logger.error("dialog model receive encrypted message exception: {0}", e);
            }
        }
 public override void OnResponse(BinaryReader reader)
 {
     message = TL.Parse <Messages_statedMessageConstructor>(reader);
 }
Ejemplo n.º 19
0
 public override void OnResponse(BinaryReader reader)
 {
     updates = TL.Parse <updates_State>(reader);
 }
Ejemplo n.º 20
0
 private void Read(BinaryReader reader)
 {
     message = TL.Parse <Message>(reader);
 }
Ejemplo n.º 21
0
 public override void OnResponse(BinaryReader reader)
 {
     contacts = TL.Parse <contacts_Contacts>(reader);
 }