protected override void Encode(IChannelHandlerContext context, IObject message, IByteBuffer output)
        {
            if (message == null)
            {
                return;
            }

            var messageId = ClientSettings.ClientSession.GenerateMessageId();

            var isRequest = message is IRequest;

            var sequenceNumber = ClientSettings.ClientSession.GenerateSequenceNumber(isRequest);

            SessionWriter.Save(ClientSettings.ClientSession);

            var messageBuffer = Serializer.Serialize(message);

            try
            {
                MtProtoHelper.ToServerEncrypt(messageBuffer, ClientSettings.ClientSession, messageId, sequenceNumber, output);
            }
            finally
            {
                messageBuffer.Release();
            }

            Log.Debug($"#{ClientSettings.ClientSession.SessionId}: Send secure message {message} with messageId = {messageId} and sequenceNumber = {sequenceNumber}");

            if (isRequest)
            {
                RequestService.AttachRequestToMessageId(message.Is <IRequest>(), messageId);
            }
        }
Example #2
0
        protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output)
        {
            if (input is EmptyByteBuffer)
            {
                return;
            }

            if (ClientSettings.ClientSession.AuthKey == null)
            {
                throw new UserNotAuthorizeException();
            }

            var decodeBuffer = MtProtoHelper.FromServerDecrypt(input, ClientSettings.ClientSession, out var authKeyId, out var serverSalt, out var sessionId, out var messageId, out var seqNumber);

            try
            {
                var message = Serializer.Deserialize(decodeBuffer);

                Log.Debug($"#{ClientSettings.ClientSession.SessionId}: Receive the secure message {message}");

                output.Add(message);
            }
            catch
            {
                decodeBuffer.ResetReaderIndex();
                Log.Error($"#{ClientSettings.ClientSession.SessionId}: Can't deserialize message: \n{ByteBufferUtil.PrettyHexDump(decodeBuffer)}");
                throw;
            }
        }
Example #3
0
        public void ToServerEncryption()
        {
            var user = new TUser
            {
                AccessHash = 11111,
                Id         = 1
            };

            var input = Serializer.Serialize(user);

            var output = PooledByteBufferAllocator.Default.Buffer();

            MtProtoHelper.ToServerEncrypt(input, _session, 0, SeqNumber, output);

            var dencryptedData = MtProtoHelper.FromClientDecrypt(output, _session, out var authKeyId,
                                                                 out var serverSalt, out var sessionId, out var messageId, out var seqNumber);

            input.ResetReaderIndex();
            Assert.Equal(input.ToArray(input.ReadableBytes), dencryptedData.ToArray(dencryptedData.ReadableBytes));
            Assert.Equal(_session.AuthKey.Id, authKeyId);
            Assert.Equal(_session.ServerSalt, serverSalt);
            Assert.Equal(_session.SessionId, sessionId);
            Assert.Equal(_seqNumber - 1, seqNumber);
        }