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); }
private static void RunAsServer(int?port, Queue <string> arguments, IKeyCapture capture, bool interactive) { var keyPath = arguments.EndOfSubArguments() ? Constants.DefaultKeyFilePath : arguments.Dequeue(); if (!KeyFileManager.TryResolveKeyPath(keyPath, out keyFilePath, false, true)) { return; } var shouldCreateKeyFile = !File.Exists(keyFilePath) || new FileInfo(keyFilePath).Length == 0; if (shouldCreateKeyFile) { File.WriteAllBytes(keyFilePath, new byte[KeyFileManager.KeyFileBytes]); } Console.Out.WriteInfoLine($"Key file path resolved to '{keyFilePath}'"); if (shouldCreateKeyFile && !KeyFileManager.Create(keyFilePath, false, true, capture ?? Constants.ConsoleKeyCapture)) { Console.Error.WriteErrorLine("Cannot start server without a key file"); return; } if (_exclusiveLock) { try { keyFileStream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read, FileShare.None); } catch (IOException) { Console.Error.WriteErrorLine("Could not obtain exclusive lock on key file"); return; } } else { try { keyFileStream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); } catch (IOException) { Console.Error.WriteErrorLine("Could not open key file"); return; } } var eggPath = Environment.GetEnvironmentVariable(Constants.EnvVars.EggFilePath); if (string.IsNullOrWhiteSpace(eggPath)) { eggPath = Constants.DefaultEggPath; } Console.Out.WriteInfoLine($"Egg file path resolved to '{eggPath}'"); if (!File.Exists(eggPath) && !EggFileManager.Create(eggPath)) { Console.Error.WriteWarningLine("Server started without an egg"); } capture?.Reset(); if (!interactive) { LaunchBrowserUrl($"https://localhost:{port.GetValueOrDefault(Constants.DefaultPort)}"); } PrintMasthead(); var builder = CreateHostBuilder(port, eggPath, capture, arguments.ToArray()); var host = builder.Build(); host.Run(); }