Пример #1
0
        static void Main(string[] args)
        {
            Console.Error.WriteLine("\u001b[31mHey!\u001b[0m");
            SetColour(2, 0);
            Console.Error.WriteLine("Hello World!");
            ResetColour();
            DTLSClient dtls = new DTLSClient("127.0.0.1", "10000", new byte[] { 0xBA, 0xA0 });

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                dtls.Unbuffer      = "winpty.exe";
                dtls.Unbuffer_Args = "-Xplain -Xallow-non-tty";
            }
            else
            {
                dtls.Unbuffer      = "stdbuf";
                dtls.Unbuffer_Args = "-i0 -o0";
            }
            dtls.Start();
            statpair IOStream = new statpair(new StreamReader(Console.OpenStandardInput()), new StreamWriter(Console.OpenStandardOutput()));

            new Thread(() => IOStream.CopyTo(dtls.GetStream(), 16)).Start();
            new Thread(() => dtls.GetStream().CopyTo(IOStream, 16)).Start();
            //new Thread(() => dtls.GetStream().Write(Encoding.Default.GetBytes("It Works!"+Environment.NewLine))).Start();
            pair.BindStreams(dtls.GetStream(), IOStream);
            pair.BindStreams(dtls.GetStream(), IOStream);
            Timer T = new Timer((S) => { float BR = (float)IOStream.BytesRead / (1024 * 1024 * 5); float BW = (float)IOStream.BytesWritten / (1024 * 1024 * 5); SetColour(2, 0); Console.Error.WriteLine($"R: {BR:000.00} MB/s.\tW: {BW:000.00} MB/s."); IOStream.ResetStats(); ResetColour(); }, new AutoResetEvent(false), 5000, 5000);

            Console.WriteLine("End of File");
            dtls.WaitForExit();
        }
Пример #2
0
        static void listen_connection(SslStream sslStream, TcpClient client)
        {
            myAes     = Aes.Create();
            myAes.Key = new byte[16] {
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16
            };
            myAes.IV = new byte[16] {
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };


            TCPCommunication.send_message_tcp(sslStream, "LISTEN_P");
            TCPCommunication.send_message_tcp(sslStream, HashString.GetHashString(pubKey.ToString()));

            string response = TCPCommunication.recieve_message_tcp(sslStream);

            if (String.Compare(response, "ACCEPT") == 0)
            {
                byte[] data = new Byte[256];
                data = Encoding.UTF8.GetBytes(pubKey.ToString());
                sslStream.Write(data);
                sslStream.Flush();


                data = new Byte[256];
                sslStream.Read(data, 0, data.Length);
                response = Encoding.UTF8.GetString(data);
                PublicKeyCoordinates request_key = JsonConvert.DeserializeObject <PublicKeyCoordinates>(response);

                sslStream.Close();
                client.Close();

                ECDiffieHellmanOpenSsl temp   = new ECDiffieHellmanOpenSsl();
                ECParameters           epTemp = temp.ExportParameters(false);

                epTemp.Q.X = request_key.X;
                epTemp.Q.Y = request_key.Y;

                ECDiffieHellmanPublicKey servePubKey = ECDiffieHellman.Create(epTemp).PublicKey;
                byte[] sharedKey = node.DeriveKeyMaterial(servePubKey);
                Console.WriteLine(BitConverter.ToString(sharedKey).Replace("-", ""));

                //myAes.Key = sharedKey;
                //myAes.Key = new byte[16] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };


                DTLSClient dtls_client = new DTLSClient(server_ip, server_port.ToString(), new byte[] { 0xBA, 0xA0 });

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    dtls_client.Unbuffer      = "winpty.exe";
                    dtls_client.Unbuffer_Args = "-Xplain -Xallow-non-tty";
                }
                else
                {
                    dtls_client.Unbuffer      = "stdbuf";
                    dtls_client.Unbuffer_Args = "-i0 -o0";
                }
                dtls_client.Start();

                /* statpair IOStream = new statpair(new StreamReader(Console.OpenStandardInput()), new StreamWriter(Console.OpenStandardOutput()));
                 * new Thread(() => dtls_client.GetStream().CopyTo(IOStream, 16)).Start();*/

                read_relay(dtls_client);

                /*while(true)
                 * {
                 *  string input = Console.ReadLine();
                 *  byte[] encryptedData = EncryptStringToBytes_Aes(input, myAes.Key, myAes.IV);
                 *  dtls_client.GetStream().Write(encryptedData);
                 *  //dtls_client.GetStream().Write(Encoding.Default.GetBytes(input+Environment.NewLine));
                 * }*/

                dtls_client.WaitForExit();
            }
            else if (String.Compare(response, "REJECT") == 0)
            {
                Console.WriteLine("Connection rejected");
                sslStream.Close();
                client.Close();
            }
        }
Пример #3
0
        static void req_connection(SslStream sslStream, TcpClient client, string dest_key)
        {
            myAes     = Aes.Create();
            myAes.Key = new byte[16] {
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16
            };
            myAes.IV = new byte[16] {
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };

            TCPCommunication.send_message_tcp(sslStream, "CONNECT_P");
            TCPCommunication.send_message_tcp(sslStream, HashString.GetHashString(pubKey.ToString()));

            string response = TCPCommunication.recieve_message_tcp(sslStream);

            if (String.Compare(response, "ACCEPT") == 0)
            {
                TCPCommunication.send_message_tcp(sslStream, dest_key);

                response = TCPCommunication.recieve_message_tcp(sslStream);
                Console.WriteLine(response);

                if (String.Compare(response, "ACCEPT") == 0)
                {
                    response = TCPCommunication.recieve_message_tcp(sslStream);
                    int dtls_port = Int32.Parse(response);


                    byte[] data = new Byte[256];
                    data = Encoding.UTF8.GetBytes(pubKey.ToString());

                    sslStream.Write(data);
                    sslStream.Flush();

                    data = new Byte[256];
                    sslStream.Read(data, 0, data.Length);
                    response = Encoding.UTF8.GetString(data);
                    PublicKeyCoordinates listen_key = JsonConvert.DeserializeObject <PublicKeyCoordinates>(response);



                    sslStream.Close();
                    client.Close();

                    ECDiffieHellmanOpenSsl temp   = new ECDiffieHellmanOpenSsl();
                    ECParameters           epTemp = temp.ExportParameters(false);

                    epTemp.Q.X = listen_key.X;
                    epTemp.Q.Y = listen_key.Y;

                    ECDiffieHellmanPublicKey servePubKey = ECDiffieHellman.Create(epTemp).PublicKey;
                    byte[] sharedKey = node.DeriveKeyMaterial(servePubKey);
                    Console.WriteLine(BitConverter.ToString(sharedKey).Replace("-", ""));
                    //myAes.Key = sharedKey;
                    //myAes.Key = new byte[16] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };

                    DTLSClient dtls_client = new DTLSClient(server_ip, dtls_port.ToString(), new byte[] { 0xBA, 0xA0 });

                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        dtls_client.Unbuffer      = "winpty.exe";
                        dtls_client.Unbuffer_Args = "-Xplain -Xallow-non-tty";
                    }
                    else
                    {
                        dtls_client.Unbuffer      = "stdbuf";
                        dtls_client.Unbuffer_Args = "-i0 -o0";
                    }
                    dtls_client.Start();

                    /*statpair IOStream = new statpair(new StreamReader(Console.OpenStandardInput()), new StreamWriter(Console.OpenStandardOutput()));
                     * new Thread(() => dtls_client.GetStream().CopyTo(IOStream, 16)).Start();*/

                    //new Thread(() => read_relay(dtls_client)).Start();

                    UdpClient receivingUdpClient = new UdpClient(32000);

                    //Creates an IPEndPoint to record the IP Address and port number of the sender.
                    // The IPEndPoint will allow you to read datagrams sent from any source.
                    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

                    /*Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                     *
                     * IPAddress broadcast = IPAddress.Parse("127.0.0.1");
                     *
                     * //byte[] sendbuf = Encoding.ASCII.GetBytes(args[0]);
                     * IPEndPoint ep = new IPEndPoint(broadcast, 11000);*/

                    dtls_client.GetStream().Write(Encoding.Default.GetBytes("SUCCESS\n"));
                    dtls_client.GetStream().Write(Encoding.Default.GetBytes("SUCCESS\n"));
                    //dtls_client.GetStream().Write(Encoding.Default.GetBytes("SUCCESS"));

                    while (true)
                    {
                        byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
                        //dtls_client.GetStream().Write(receiveBytes);
                        //dtls_client.GetStream().Flush();

                        string input = BitConverter.ToString(receiveBytes) + '\n';
                        //Console.WriteLine(input);

                        byte[] send = Encoding.Default.GetBytes(input);

                        //Console.WriteLine(receiveBytes);
                        dtls_client.GetStream().Write(send);
                        //Thread.Sleep(50);



                        //byte[] rec = Encoding.Default.GetBytes(cut_str);
                        //Console.WriteLine(bytes);

                        //s.SendTo(bytes, ep);

                        //dtls_client.GetStream().Write(Encoding.Default.GetBytes(input));

                        /*string input = Encoding.Default.GetString(receiveBytes);
                         *
                         * byte[] send = Encoding.Default.GetBytes(input);
                         *
                         * s.SendTo(send, ep);*/

                        /*byte[] out_byte = Encoding.Default.GetBytes(input);
                         *
                         * string out_str = Encoding.Default.GetString(out_byte);
                         *
                         * String[] arr=out_str.Split('-');
                         * byte[] bytes=new byte[arr.Length];
                         * for(int i=0; i<arr.Length; i++) bytes[i]=Convert.ToByte(arr[i],16);
                         *
                         * s.SendTo(bytes, ep);*/

                        /*String[] arr=input.Split('-');
                         * byte[] bytes=new byte[arr.Length];
                         * for(int i=0; i<arr.Length; i++) bytes[i]=Convert.ToByte(arr[i],16);*/

                        /*String[] arr_in=input.Split('-');
                         * byte[] array_in=new byte[arr.Length];
                         * for(int i=0; i<arr.Length; i++) array[i]=Convert.ToByte(arr[i],16);
                         *
                         * string out_str = BitConverter.ToString(out_bt);
                         *
                         * String[] arr=out_str.Split('-');
                         * byte[] bytes=new byte[arr.Length];
                         * for(int i=0; i<arr.Length; i++) bytes[i]=Convert.ToByte(arr[i],16);
                         *
                         * //byte[] bytes = BitConverter.GetBytes(input);
                         *
                         * s.SendTo(bytes, ep);*/


                        //string input = BitConverter.ToString(receiveBytes);

                        //byte[] encryptedData = EncryptStringToBytes_Aes(BitConverter.ToString(receiveBytes), myAes.Key, myAes.IV);

                        //dtls_client.GetStream().Write(encryptedData);

                        //dtls_client.GetStream().Write(receiveBytes);
                        //dtls_client.GetStream().Write(bytes);
                        //dtls_client.GetStream().Write();
                    }

                    dtls_client.WaitForExit();
                }
                else if (String.Compare(response, "REJECT") == 0)
                {
                    Console.WriteLine("Connection rejected");
                }
            }
            else if (String.Compare(response, "REJECT") == 0)
            {
                Console.WriteLine("Connection rejected");
                sslStream.Close();
                client.Close();
            }
        }