Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // SSL server port
            int port = 2222;

            if (args.Length > 0)
            {
                port = int.Parse(args[0]);
            }

            Console.WriteLine($"SSL server port: {port}");

            // Create a new service
            var service = new Service();

            // Start the service
            Console.Write("Service starting...");
            service.Start();
            Console.WriteLine("Done!");

            // Create and prepare a new SSL server context
            var context = new SslContext(SslMethod.TLSV12);

            context.SetPassword("qwerty");
            context.UseCertificateChainFile("server.pem");
            context.UsePrivateKeyFile("server.pem", SslFileFormat.PEM);
            context.UseTmpDHFile("dh4096.pem");

            // Create a new SSL chat server
            var server = new ChatServer(service, context, InternetProtocol.IPv4, port);

            // Start the server
            Console.Write("Server starting...");
            server.Start();
            Console.WriteLine("Done!");

            Console.WriteLine("Press Enter to stop the server or '!' to restart the server...");

            // Perform text input
            for (;;)
            {
                string line = Console.ReadLine();
                if (line == string.Empty)
                {
                    break;
                }

                // Restart the server
                if (line == "!")
                {
                    Console.Write("Server restarting...");
                    server.Restart();
                    Console.WriteLine("Done!");
                    continue;
                }

                // Multicast admin message to all sessions
                line = "(admin) " + line;
                server.Multicast(line);
            }

            // Stop the server
            Console.Write("Server stopping...");
            server.Stop();
            Console.WriteLine("Done!");

            // Stop the service
            Console.Write("Service stopping...");
            service.Stop();
            Console.WriteLine("Done!");
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // SSL server port
            int port = 2222;

            Console.WriteLine($"SSL server port: {port}");

            Console.WriteLine();

            /*  https://www.scottbrady91.com/C-Sharp/PEM-Loading-in-dotnet-core-and-dotnet
             *  https://stackoverflow.com/questions/50227580/create-x509certificate2-from-pem-file-in-net-core
             * // loaded PEM file with labels stripped. Full value omitted for brevity
             *  var certPem = "MIIB3zCCAYWgAwIBAgIUImttQCULqkHxYbDivb...FwT3WZO4S5JB5jvPg9hCnlXPjNwaC";
             *
             *  var cert = new X509Certificate2(Convert.FromBase64String(certPem));
             *
             *  // can be combined with the private key from the previous section
             *  var certWithKey = cert.CopyWithPrivateKey(key);
             */

            //var certLetsCrypt = X509Certificate2.CreateFromPemFile(@"/etc/letsencrypt/live/iothtnhust20201.xyz/fullchain.pem", @"/etc/letsencrypt/live/iothtnhust20201.xyz/privkey.pem");
            //var certLetsCrypt = X509Certificate2.
            ////Pass the file path and file name to the StreamReader constructor
            //StreamReader sr = new StreamReader(@"/etc/letsencrypt/live/iothtnhust20201.xyz/chain.pem");
            ////Read the first line of text
            //var lineRead = sr.ReadLine();
            ////Continue to read until you reach end of file
            //while (lineRead != null)
            //{
            //    //write the lie to console window
            //    Console.WriteLine(lineRead);
            //    //Read the next line
            //    lineRead = sr.ReadLine();
            //}
            ////close the file
            //sr.Close();

            // Create and prepare a new SSL server context
            //string certPath = @"/etc/letsencrypt/live/iothtnhust20201.xyz/my.pfx";
            //var context = new SslContext(SslProtocols.Tls12, new X509Certificate2(certPath, "Tru@1997"));

            string certPemPath = @"/etc/letsencrypt/live/iothtnhust20201.xyz/fullchain.pem";
            string keyPemPath  = @"/etc/letsencrypt/live/iothtnhust20201.xyz/privkey.pem";

            var eccPem  = File.ReadAllText(keyPemPath);
            var certPem = File.ReadAllText(certPemPath);

            var certLetsCrypt = X509Certificate2.CreateFromPem(certPem, eccPem);

            var context = new SslContext(SslProtocols.Tls12, certLetsCrypt);

            // Create a new SSL chat server
            var server = new ChatServer(context, IPAddress.Any, port);

            // Start the server
            Console.Write("Server starting...");
            server.Start();
            Console.WriteLine("Done!");

            Console.WriteLine("Press Enter to stop the server or '!' to restart the server...");

            // Perform text input
            for (; ;)
            {
                string line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }

                // Restart the server
                if (line == "!")
                {
                    Console.Write("Server restarting...");
                    server.Restart();
                    Console.WriteLine("Done!");
                    continue;
                }

                // Multicast admin message to all sessions
                line = "(admin) " + line;
                server.Multicast(line);
            }

            // Stop the server
            Console.Write("Server stopping...");
            server.Stop();
            Console.WriteLine("Done!");
        }