예제 #1
0
        public async Task ClientConnectSample(
            IPAddress serverIpAddress,
            Guid serverId,
            X509Certificate serverCertificate)
        {
            var client = new TcpClient(AddressFamily.InterNetwork);
            await client.ConnectAsync(serverIpAddress, 1234);

            var result = await Lapi.ConnectToServer(
                client.GetStream(),
                applicationSpecificPresharedKey,
                clientId,
                serverId,
                clientCertificate,
                serverCertificate);

            if (result.Successful)
            {
                var stream = result.Result;
                await stream.WriteSafelyAsync(CancellationToken.None, "Hi there, server!");

                var response = await stream.ReceiveStringSafelyAsync(CancellationToken.None);

                if (response.Successful)
                {
                    Console.WriteLine($"Server responded with: '{response.Result}'");
                }
            }
        }
예제 #2
0
파일: Program.cs 프로젝트: JMarianczuk/LAPI
        private static async Task FirstTest()
        {
            var server       = new TcpServer(IPAddress.Loopback, Port);
            var presharedKey = Enumerable.Range(1, 20).Select(x => (byte)x).ToArray();
            var serverCert   = X509CertificateService.GetOwnCertificate(TestGuid, Password);
            var clientCert   = X509CertificateService.GetOwnCertificate(OtherTestGuid, Password);
            var otpBuffer    = Enumerable.Range(1, 32).Select(x => (byte)x).ToArray();
            var otp          = new AesCryptographicService(SymmetricKey.FromBuffer(otpBuffer));

            var reducedServerCert = new X509Certificate2(serverCert.Export(X509ContentType.Cert));
            var reducedClientCert = new X509Certificate2(clientCert.Export(X509ContentType.Cert));

            var serverControl = Lapi.RunServer(
                server,
                presharedKey,
                TestGuid,
                serverCert,
                () => otp,
                ClientConnected,
                (_, __) => { },
                x =>
            {
                if (x.Equals(OtherTestGuid))
                {
                    return(clientCert);
                }

                return(null);
            });

            serverControl.OnError += error => Console.WriteLine($"{error.ErrorType}: {error.Message}");

            var client = new TcpClient();
            await client.ConnectAsync(IPAddress.Loopback, Port);

            var stream = await Lapi.ConnectToServer(
                client.GetStream(),
                presharedKey,
                OtherTestGuid,
                TestGuid,
                clientCert,
                serverCert);

            Console.WriteLine($"Successful connected: {stream.Successful}");
            //Console.WriteLine("Attempting to read from server");

            //byte[] buf = new byte[4];
            //stream.Result.ReadAsync(buf, 0, 4).Wait();
            //Console.WriteLine($"Received: {string.Join(", ", buf)}");


            //var registerResult = await Lapi.RegisterWithServer(IPAddress.Loopback, presharedKey, OtherTestGuid, TestGuid, clientCert, serverCert, otp, Port);
            //Console.WriteLine("Registering was " + (registerResult.Successful ? "" : "not ") + "successful");
            //registerResult.Result.Close();

            //var guidsResult = await Lapi.DiscoverServersOnLocalNetwork(presharedKey);
            //Console.WriteLine($"Discovery was " + (guidsResult.Successful ? "" : "not ") + "successful");
            //if (guidsResult.Successful)
            //{
            //    Console.WriteLine($"Found servers on network: {string.Join(", ", guidsResult.Result)}");
            //}

            Console.ReadLine();
            serverControl.StopServer();
        }