Exemple #1
0
        private static bool ProcessCommandLineArguments(Queue <string> arguments)
        {
            while (arguments.Count > 0)
            {
                var arg = arguments.Dequeue();
                switch (arg.ToLower())
                {
                    #region Unattended Commands

                case "--nolock":
                    _exclusiveLock = false;
                    break;

                case "--port":
                case "-p":
                {
                    var portString = arguments.Dequeue();
                    int.TryParse(portString, out _port);
                    break;
                }

                    #endregion

                    #region CLI Commands

                case "--cert":
                case "--certs":
                case "-c":
                {
                    var freshOrClear = arguments.EndOfSubArguments() ? "false" : arguments.Dequeue();
                    if (freshOrClear?.Equals("clear", StringComparison.OrdinalIgnoreCase) ?? false)
                    {
                        CertificateBuilder.ClearAll(Console.Out);
                    }
                    else
                    {
                        CertificateBuilder.GetOrCreateSelfSignedCert(Console.Out,
                                                                     freshOrClear?.Equals("fresh", StringComparison.OrdinalIgnoreCase) ?? false);
                    }
                    return(false);
                }

                case "--egg":
                case "-e":
                {
                    var eggPath = arguments.EndOfSubArguments() ? Constants.DefaultEggPath : arguments.Dequeue();
                    EggFileManager.Create(eggPath);
                    return(false);
                }

                case "--keygen":
                case "-k":
                {
                    var keyPath = arguments.EndOfSubArguments()
                            ? Constants.DefaultKeyFilePath
                            : arguments.Dequeue();
                    KeyFileManager.Create(keyPath, true, false, Constants.ConsoleKeyCapture);
                    return(false);
                }

                case "--server":
                case "-s":
                {
                    RunAsServer(null, arguments, null, true);
                    return(false);
                }

                    #endregion
                }
            }

            return(true);
        }
Exemple #2
0
        internal static IHostBuilder CreateHostBuilder(int?port, string eggPath, IKeyCapture capture, params string[] args)
        {
            var builder = Host.CreateDefaultBuilder(args);

            builder.ConfigureWebHostDefaults(webBuilder =>
            {
                Activity.DefaultIdFormat = ActivityIdFormat.W3C;

                webBuilder.ConfigureAppConfiguration((context, configBuilder) =>
                {
                    configBuilder.AddEnvironmentVariables();
                });

                webBuilder.ConfigureKestrel((context, options) =>
                {
                    var x509 = CertificateBuilder.GetOrCreateSelfSignedCert(Console.Out);

                    options.AddServerHeader = false;
                    options.ListenLocalhost(port.GetValueOrDefault(Constants.DefaultPort), x =>
                    {
                        x.Protocols = HttpProtocols.Http1AndHttp2;
                        x.UseHttps(a =>
                        {
                            a.SslProtocols = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                                ? SslProtocols.Tls12
                                : SslProtocols.Tls13;
                            a.ServerCertificate = x509;
                        });
                    });
                });

                webBuilder.ConfigureLogging((context, loggingBuilder) =>
                {
                    loggingBuilder.ClearProviders();

                    loggingBuilder.AddConfiguration(context.Configuration.GetSection("Logging"));
                    loggingBuilder.AddDebug();
                    loggingBuilder.AddEventSourceLogger();
                    loggingBuilder.AddLogging(() =>
                    {
                        var serviceProvider = loggingBuilder.Services.BuildServiceProvider();
                        return(Path.Combine(Constants.DefaultRootPath, $"{serviceProvider.GetRequiredService<IOptions<WebServerOptions>>().Value.PublicKeyString}_logs.egg"));
                    });

                    if (context.HostingEnvironment.IsDevelopment())
                    {
                        loggingBuilder.AddColorConsole(); // unnecessary overhead
                    }
                });

                webBuilder.ConfigureServices((context, services) =>
                {
                    services.AddWebServer(eggPath, port.GetValueOrDefault(Constants.DefaultPort), capture, context.HostingEnvironment, context.Configuration);
                });

                webBuilder.UseStartup <Startup>();

                var contentRoot = Directory.GetCurrentDirectory();
                var webRoot     = Path.Combine(contentRoot, "wwwroot");
                if (!File.Exists(Path.Combine(webRoot, "css", "signin.css")))
                {
                    webRoot = Path.GetFullPath(Path.Combine(contentRoot, "..", "egregore.Client", "wwwroot"));
                }
                webBuilder.UseContentRoot(contentRoot);
                webBuilder.UseWebRoot(webRoot);
            });

            return(builder);
        }