private void Send() { while (true) { string input = TXData.Take(); if (input == "") { continue; } var Timestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); var MD5Sum = BitConverter.ToString(sha256Ctx.ComputeHash(System.Text.Encoding.Default.GetBytes(input + rand.Next(0, 65536).ToString()))).Replace("-", ""); Stack <string> PiecedMsg = new Stack <string>(); while (input.Length > 0) { if (input.Length <= 5) { PiecedMsg.Push(input); input = ""; } else { var cutlen = rand.Next(0, input.Length); PiecedMsg.Push(input.Substring(0, cutlen)); input = input.Substring(cutlen); } } var PacketTotal = PiecedMsg.Count; while (PiecedMsg.Count > 0) { IPAddress SrcIP, DstIP; switch (rand.Next(0, 2)) { case 0: if (IPv6SrcList.Count != 0 && IPv6DstList.Count != 0) { DstIP = IPv6DstList[rand.Next(0, IPv6DstList.Count)]; SrcIP = IPv6SrcList[rand.Next(0, IPv6SrcList.Count)]; } else { DstIP = IPv4DstList[rand.Next(0, IPv4DstList.Count)]; SrcIP = IPv4SrcList[rand.Next(0, IPv4SrcList.Count)]; } break; default: if (IPv4SrcList.Count != 0 && IPv4DstList.Count != 0) { DstIP = IPv4DstList[rand.Next(0, IPv4DstList.Count)]; SrcIP = IPv4SrcList[rand.Next(0, IPv4SrcList.Count)]; } else { DstIP = IPv6DstList[rand.Next(0, IPv6DstList.Count)]; SrcIP = IPv6SrcList[rand.Next(0, IPv6SrcList.Count)]; } break; } var TXProtocolPacket = new ProtocolPacket { PacketTimestamp = Timestamp, PacketTotal = PacketTotal, PacketMD5Sum = MD5Sum, PacketCount = PiecedMsg.Count, SrcIP = SrcIP.ToString(), DstIP = DstIP.ToString() }; TXProtocolPacket.PiecedMsg = PiecedMsg.Pop(); var udpPacket = new PacketDotNet.UdpPacket((ushort)rand.Next(1, 65536), (ushort)rand.Next(1, 65536)); long nonceTime = (DateTimeOffset.Now.ToUnixTimeSeconds() / 300) * 300; byte[] nonce = sha256Ctx.ComputeHash(System.Text.Encoding.Default.GetBytes(nonceTime.ToString())).Take(8).ToArray(); var NSecKey = Key.Import(AeadAlgorithm.ChaCha20Poly1305, key, KeyBlobFormat.RawSymmetricKey); var NSecNonce = new Nonce(nonce, 4); udpPacket.PayloadData = AeadAlgorithm.ChaCha20Poly1305.Encrypt(NSecKey, NSecNonce, null, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(TXProtocolPacket))); if (DstIP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { var ipv4Packet = new PacketDotNet.IPv4Packet(SrcIP, DstIP); ipv4Packet.PayloadPacket = udpPacket; udpPacket.UpdateCalculatedValues(); udpPacket.UpdateUDPChecksum(); ipv4Packet.UpdateCalculatedValues(); txBuffer.Add(new DivertPacket { Addr = new WINDIVERT_ADDRESS { Direction = 0 }, Data = ipv4Packet.Bytes }); } if (DstIP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) { var ipv6Packet = new PacketDotNet.IPv6Packet(SrcIP, DstIP); ipv6Packet.NextHeader = PacketDotNet.IPProtocolType.UDP; ipv6Packet.PayloadPacket = udpPacket; udpPacket.UpdateCalculatedValues(); udpPacket.UpdateUDPChecksum(); ipv6Packet.UpdateCalculatedValues(); txBuffer.Add(new DivertPacket { Addr = new WINDIVERT_ADDRESS { Direction = 0 }, Data = ipv6Packet.Bytes }); } } } }
public override int GetHashCode() { return(SrcIP.GetHashCode() + DestIP.GetHashCode()); }