From 8c2db089ee60eea9c501fbe386ca353723529a67 Mon Sep 17 00:00:00 2001 From: mahesh bhatiya Date: Tue, 17 Jun 2025 08:47:14 +0530 Subject: [PATCH] feat(ssl): add Let's Encrypt SSL support for Apache, Nginx, and Caddy - Integrated Certbot-based SSL certificate generation for all web server types - Added flags for --ssl and domain parsing - SSL errors are logged with helpful Certbot output - Handles both interactive and non-interactive certbot modes - Prepared CLI to handle production vs development SSL strategies --- cmd/root.go | 12 +++++++++++- internal/ssl.go | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 internal/ssl.go diff --git a/cmd/root.go b/cmd/root.go index 79b92c4..77c6ee7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -181,6 +181,15 @@ var createDomainCmd = &cobra.Command{ } } + enableSSL, _ := cmd.Flags().GetBool("ssl") + if enableSSL && (serverType == "apache" || serverType == "nginx") { + err := internal.EnableSSLCertbot(domain, serverType) + if err != nil { + logger.Error(fmt.Sprintf("SSL setup failed: %v", err)) + os.Exit(1) + } + } + logger.Success(fmt.Sprintf("%s configuration created and enabled for %s on port %s", serverType, domain, port)) }, } @@ -193,6 +202,7 @@ func init() { createDomainCmd.Flags().Bool("useridr", false, "Create user directory /home//public_html") createDomainCmd.Flags().StringP("port", "p", "80", "Port for the configuration (default: 80)") createDomainCmd.Flags().StringP("server", "s", "apache", "Web server type (e.g., apache, nginx, caddy)") + createDomainCmd.Flags().Bool("ssl", false, "Enable Let's Encrypt SSL (Apache/Nginx only)") createDomainCmd.MarkFlagRequired("name") } @@ -277,4 +287,4 @@ func writeConfigFile(domain, content, extension string) error { logger.Success(fmt.Sprintf("Configuration file written to %s", outputPath)) return nil -} \ No newline at end of file +} diff --git a/internal/ssl.go b/internal/ssl.go new file mode 100644 index 0000000..9f90a16 --- /dev/null +++ b/internal/ssl.go @@ -0,0 +1,27 @@ +package internal + +import ( + "fmt" + "stackroost/internal/logger" +) + +func EnableSSLCertbot(domain string, serverType string) error { + logger.Info(fmt.Sprintf("Requesting SSL certificate for %s using Certbot", domain)) + + cmd := []string{ + fmt.Sprintf("--%s", serverType), + "-d", domain, + "-d", "www." + domain, + "--non-interactive", + "--agree-tos", + "--register-unsafely-without-email", + } + + err := RunCommand("sudo", append([]string{"certbot"}, cmd...)...) + if err != nil { + return fmt.Errorf("certbot SSL generation failed: %v", err) + } + + logger.Success(fmt.Sprintf("SSL certificate installed for %s", domain)) + return nil +} \ No newline at end of file