예제 #1
0
        public async Task ValidateCryptoHandshake()
        {
            var     server = new WsServer();
            var     serverKey = CryptoExtensions.GenerateKey();
            var     clientKey = CryptoExtensions.GenerateKey();
            ECPoint serverPub, clientPub = new ECPoint();
            await server.StartAsync(new IPEndPoint(IPAddress.Loopback, 0), async stream =>
            {
                clientPub = await stream.EncryptAsync(serverKey);
            });

            await Task.Delay(500);

            // start client
            var client = new WsClient();
            var conn   = await client.ConnectAsync(new Uri($"ws://" + server.ListeningAddresses[0].Substring(7)));

            serverPub = await conn.EncryptAsync(clientKey);

            await Task.Delay(500);

            Assert.Equal(serverKey.Q.X, serverPub.X);
            Assert.Equal(clientKey.Q.X, clientPub.X);
            server.Dispose();
            client.Dispose();
        }
예제 #2
0
        private static async Task MainAsync(Settings settings)
        {
            DbClient.Init(settings.DbUri);
            Channels.WriteTimeout = 200;
            var server = new WsServer(settings.Port, WsHandler.ProcessWsConnectionAsync);
            await server.StartAsync().ConfigureAwait(false);

            Thread.Sleep(Timeout.Infinite);
        }
예제 #3
0
        public async Task Test()
        {
            // create the server
            var server    = new WsServer();
            var serverKey = CryptoExtensions.GenerateKey();
            var clientKey = CryptoExtensions.GenerateKey();

            Console.WriteLine($"Server key is: [{serverKey.Q.GetFingerprintString()}]");
            Console.WriteLine($"Client key is: [{clientKey.Q.GetFingerprintString()}]");
            await server.StartAsync(new IPEndPoint(IPAddress.Any, 8080), async stream =>
            {
                // encrypt
                // called when a client connects
                var br = new BinaryWriter(stream);
                var ct = Encoding.ASCII.GetBytes($"Hello from Server! {DateTime.Now}");
                br.Write(ct.Length);
                br.Write(ct);
                var res = await stream.EncryptAsync(serverKey);
                Console.WriteLine($"Client connected with fingerprint of [{res.GetFingerprintString()}]");
                while (stream.Connected)
                {
                    var bytes = Encoding.ASCII.GetBytes($"Hello from Server! {DateTime.Now}");
                    br.Write(bytes.Length);
                    br.Write(bytes);
                    await Task.Delay(1000);
                }
                await stream.CloseAsync();
            });

            // start client
            var client     = new WsClient();
            var connection = await client.ConnectAsync(new Uri("ws://localhost:8080"));

            // measure client data

            // read data
            var binReader = new BinaryReader(connection);
            int e         = binReader.ReadInt32();
            var g         = binReader.ReadBytes(e);

            Console.WriteLine(Encoding.ASCII.GetString(g));

            var serverRes = await connection.EncryptAsync(clientKey);

            Console.WriteLine($"Connected to server with fingerprint of [{serverRes.GetFingerprintString()}]");

            for (int i = 0; i < 100; i++)
            {
                int b         = binReader.ReadInt32();
                var bytesRead = binReader.ReadBytes(b);
                Console.WriteLine(Encoding.ASCII.GetString(bytesRead));
            }
            // cleanup
            client.Dispose();
            server.Dispose();
        }
예제 #4
0
        public async Task Test()
        {
            // create the server
            var server = new WsServer();
            await server.StartAsync(new IPEndPoint(IPAddress.Loopback, 8080), async stream =>
            {
                //await stream.EncryptAsync();
                Console.WriteLine($"Tunnel Opened: remote => localhost:12345");
                var sock = new Socket(SocketType.Stream, ProtocolType.Tcp);
                await sock.ConnectAsync("localhost", 12345);
                var ns = new NetworkStream(sock);
                var t1 = ns.CopyToAsync(stream, 62000);
                var t2 = stream.CopyToAsync(ns, 62000);
                await Task.WhenAny(t1, t2);
                sock.Dispose();
                await stream.DisposeAsync();
                Console.WriteLine($"Tunnel Closed: remote => localhost:25565");
            });

            Socket lSock = new Socket(SocketType.Stream, ProtocolType.Tcp);

            lSock.Bind(new IPEndPoint(IPAddress.Loopback, 1234));
            lSock.Listen(10);
            var cts = new CancellationTokenSource();

            Console.WriteLine($"Started... Listening to localhost:1234. Press ctrl + c to exit");
            Console.CancelKeyPress += (q, e) =>
            {
                cts.Cancel();
            };
            while (!cts.Token.IsCancellationRequested)
            {
                var ksock = await lSock.AcceptAsync();

                Task.Run(async() =>
                {
                    // start client
                    var client     = new WsClient();
                    var connection = await client.ConnectAsync(new Uri("ws://localhost:8080"));
                    //await connection.EncryptAsync();
                    var ns = new NetworkStream(ksock);
                    var t1 = ns.CopyToAsync(connection, 62000);
                    var t2 = connection.CopyToAsync(ns, 62000);
                    await Task.WhenAny(t1, t2);
                    ksock.Dispose();
                    await connection.DisposeAsync();
                    client.Dispose();
                });
            }
            // cleanup
            server.Dispose();
        }
예제 #5
0
        public IntegrityTestFixture()
        {
            // create the server
            var server = new WsServer();

            server.StartAsync(new IPEndPoint(IPAddress.Loopback, 0), async stream =>
            {
                Server = stream;
            }).GetAwaiter().GetResult();

            // start client
            var client = new WsClient();

            Client = client.ConnectAsync(new Uri("ws://" + server.ListeningAddresses[0].Substring(7))).GetAwaiter().GetResult();
            Thread.Sleep(1000);
            Reader = new AsyncBinaryReader(Client);
            Writer = new AsyncBinaryWriter(Server);
        }
예제 #6
0
        public async Task ValidateReadWriteStatistics()
        {
            InterceptData clientData = null, serverData = null;
            var           server = new WsServer();
            await server.StartAsync(new IPEndPoint(IPAddress.Loopback, 0), async stream =>
            {
                await stream.WrapSocketAsync(x =>
                                             Task.FromResult <WStreamSocket>(new Interceptor(x, out serverData))
                                             );
                var abr = new AsyncBinaryReader(stream);
                for (int i = 0; i < 100000; i++)
                {
                    Assert.Equal(i, await abr.ReadInt32Async());
                }
                await stream.CloseAsync();
            });

            var client     = new WsClient();
            var connection = await client.ConnectAsync(new Uri("ws://" + server.ListeningAddresses[0].Substring(7)));

            await connection.WrapSocketAsync(x =>
                                             Task.FromResult <WStreamSocket>(new Interceptor(x, out clientData))
                                             );

            var abw = new AsyncBinaryWriter(connection);

            for (int i = 0; i < 100000; i++)
            {
                await abw.WriteAsync(i);
            }

            await abw.FlushAsync();

            await Task.Delay(1000);

            Assert.Equal(4 * 100000, clientData.BytesWritten);
            Assert.Equal(4 * 100000, serverData.BytesRead);

            client.Dispose();
            server.Dispose();
        }
예제 #7
0
        public async Task Test()
        {
            InterceptData clientData = null, serverData = null;
            // create the server
            var server = new WsServer();
            await server.StartAsync(new IPEndPoint(IPAddress.Any, 8080), async stream =>
            {
                // measure server data
                await stream.WrapSocketAsync(x =>
                                             // create a new interceptor
                                             Task.FromResult <WStreamSocket>(new Interceptor(x, out serverData))
                                             );
                // called when a client connects
                await stream.WriteAsync(BitConverter.GetBytes(DateTime.Now.ToBinary()));
                await stream.CloseAsync();
            });

            // start client
            var client = new WsClient();
            var connection = await client.ConnectAsync(new Uri("ws://localhost:8080"));

            // measure client data
            await connection.WrapSocketAsync(x =>
                                             // create a new interceptor
                                             Task.FromResult <WStreamSocket>(new Interceptor(x, out clientData))
                                             );

            // read data
            var binReader = new BinaryReader(connection);

            Console.WriteLine($"Current time is {DateTime.FromBinary(binReader.ReadInt64())}");

            Console.WriteLine($"Intercepted Data from client - Written {clientData.BytesWritten} / Read {clientData.BytesRead}");
            Console.WriteLine($"Intercepted Data from server - Written {serverData.BytesWritten} / Read {serverData.BytesRead}");

            // cleanup
            client.Dispose();
            server.Dispose();
        }
예제 #8
0
        public async Task Test()
        {
            // create the server
            var server = new WsServer();
            await server.StartAsync(new IPEndPoint(IPAddress.Any, 8080), async stream =>
            {
                // called when a client connects
                var br = new BinaryWriter(stream);
                while (stream.Connected)
                {
                    var bytes = Encoding.ASCII.GetBytes($"Hello from Server! {DateTime.Now}");
                    br.Write(bytes.Length);
                    br.Write(bytes);
                    await Task.Delay(1000);
                }
                await stream.CloseAsync();
            });

            // start client
            var client     = new WsClient();
            var connection = await client.ConnectAsync(new Uri("ws://localhost:8080"));

            // read data
            var binReader = new BinaryReader(connection);

            for (int i = 0; i < 100; i++)
            {
                int b         = binReader.ReadInt32();
                var bytesRead = binReader.ReadBytes(b);
                Console.WriteLine(Encoding.ASCII.GetString(bytesRead));
            }

            // cleanup
            client.Dispose();
            server.Dispose();
        }
예제 #9
0
        public async Task Test()
        {
            // create the server
            var server = new WsServer();
            await server.StartAsync(new IPEndPoint(IPAddress.Any, 8080), async stream =>
            {
                // called when a client connects
                await stream.WriteAsync(BitConverter.GetBytes(DateTime.Now.ToBinary()));
                await stream.CloseAsync();
            });

            // start client
            var client     = new WsClient();
            var connection = await client.ConnectAsync(new Uri("ws://localhost:8080"));

            // read data
            var binReader = new BinaryReader(connection);

            Console.WriteLine($"Current time is {DateTime.FromBinary(binReader.ReadInt64())}");

            // cleanup
            client.Dispose();
            server.Dispose();
        }
예제 #10
0
 static async Task Main(string[] args)
 {
     WsServer server = new WsServer("127.0.0.1", 3000);
     await server.StartAsync();
 }