public override async void Command(MenuItem menuItem, string UserInput) { try { string[] commands = UserInput.Split(" "); if (commands.Length != 1 || !commands[0].Equals(this.Name, StringComparison.OrdinalIgnoreCase)) { menuItem.PrintInvalidOptionError(UserInput); return; } HttpListener HttpListener = ((HTTPListenerMenuItem)menuItem).HttpListener; if ((HttpListener.UseSSL ?? default) && (string.IsNullOrEmpty(HttpListener.SslCertHash) || string.IsNullOrEmpty(HttpListener.SslCertificate))) { EliteConsole.PrintWarning("No SSLCertificate specified. Would you like to generate and use a self-signed certificate? [y/N] "); string input = EliteConsole.Read(); if (input.StartsWith("y", StringComparison.OrdinalIgnoreCase)) { X509Certificate2 certificate = Utilities.CreateSelfSignedCertificate(HttpListener.BindAddress); string autopath = "httplistener-" + HttpListener.Id + "-certificate.pfx"; File.WriteAllBytes( Path.Combine(Common.EliteDataFolder, autopath), certificate.Export(X509ContentType.Pfx, HttpListener.SslCertificatePassword) ); EliteConsole.PrintFormattedHighlightLine("Certificate written to: " + autopath); EliteConsole.PrintFormattedWarningLine("(Be sure to disable certificate validation on Launchers/Grunts using this self-signed certificate)"); menuItem.AdditionalOptions.FirstOrDefault(O => O.Name == "Set").Command(menuItem, "Set SSLCertPath " + autopath); menuItem.Refresh(); HttpListener = ((HTTPListenerMenuItem)menuItem).HttpListener; } else { EliteConsole.PrintFormattedErrorLine("Must specify an SSLCertfiicate to Start an HTTP Listener with SSL."); return; } } HttpListener.Status = ListenerStatus.Active; await this.CovenantClient.ApiListenersHttpPutAsync(HttpListener); ((HTTPListenerMenuItem)menuItem).RefreshHTTPTemplate(); } catch (HttpOperationException e) { EliteConsole.PrintFormattedWarningLine("CovenantException: " + e.Response.Content); } }
public override void Command(MenuItem menuItem, string UserInput) { HTTPListenerMenuItem httpListenerMenuItem = (HTTPListenerMenuItem)menuItem; // TODO: error if http lsitener already on this port if ((httpListenerMenuItem.httpListener.UseSSL ?? default) && (httpListenerMenuItem.httpListener.SslCertHash == "" || httpListenerMenuItem.httpListener.SslCertificate == "")) { EliteConsole.PrintWarning("No SSLCertificate specified. Would you like to generate and use a self-signed certificate? [y/N] "); string input = EliteConsole.Read(); if (input.ToLower().StartsWith("y")) { X509Certificate2 certificate = Utilities.CreateSelfSignedCertificate(httpListenerMenuItem.httpListener.BindAddress); string autopath = "httplistener-" + httpListenerMenuItem.httpListener.Id + "-certificate.pfx"; File.WriteAllBytes(Path.Combine(Common.EliteDataFolder, autopath), certificate.Export(X509ContentType.Pfx, httpListenerMenuItem.httpListener.SslCertificatePassword)); EliteConsole.PrintFormattedInfoLine("Certificate written to: " + autopath); httpListenerMenuItem.AdditionalOptions.FirstOrDefault(O => O.Name == "Set").Command(httpListenerMenuItem, "Set SSLCertPath " + autopath); } else { EliteConsole.PrintFormattedErrorLine("Must specify an SSLCertfiicate to Start an HTTP Listener with SSL."); return; } } httpListenerMenuItem.Refresh(); httpListenerMenuItem.httpListener.Status = ListenerStatus.Active; httpListenerMenuItem.httpListener = this.CovenantClient.ApiListenersHttpPut(httpListenerMenuItem.httpListener); EventModel eventModel = new EventModel { Message = "Started HTTP Listener: " + httpListenerMenuItem.httpListener.Name + " at: " + httpListenerMenuItem.httpListener.Url, Level = EventLevel.Highlight, Context = "*" }; eventModel = this.CovenantClient.ApiEventsPost(eventModel); this.EventPrinter.PrintEvent(eventModel); httpListenerMenuItem.RefreshHTTPTemplate(); httpListenerMenuItem.Refresh(); }