Skip to content

Commit 4c9af92

Browse files
Merge pull request #31 from stackroost/dev
feat(cli): add enable-firewall command to configure UFW with default …
2 parents 5b79f26 + ba6efe6 commit 4c9af92

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

cmd/enable_firewall.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
"stackroost/internal"
9+
"stackroost/internal/logger"
10+
)
11+
12+
var firewallPorts []int
13+
14+
var enableFirewallCmd = &cobra.Command{
15+
Use: "enable-firewall",
16+
Short: "Enable UFW and allow common and custom ports",
17+
Run: func(cmd *cobra.Command, args []string) {
18+
logger.Info("Enabling UFW (Uncomplicated Firewall)")
19+
20+
// Install ufw if not installed
21+
if err := internal.RunCommand("sudo", "apt-get", "install", "-y", "ufw"); err != nil {
22+
logger.Error(fmt.Sprintf("Failed to install UFW: %v", err))
23+
os.Exit(1)
24+
}
25+
26+
// Allow essential ports
27+
defaultPorts := []int{22, 80, 443}
28+
for _, port := range defaultPorts {
29+
logger.Info(fmt.Sprintf("Allowing port: %d", port))
30+
internal.RunCommand("sudo", "ufw", "allow", fmt.Sprintf("%d", port))
31+
}
32+
33+
// Allow custom ports
34+
for _, port := range firewallPorts {
35+
logger.Info(fmt.Sprintf("Allowing custom port: %d", port))
36+
internal.RunCommand("sudo", "ufw", "allow", fmt.Sprintf("%d", port))
37+
}
38+
39+
// Enable ufw
40+
logger.Info("Enabling UFW")
41+
internal.RunCommand("sudo", "ufw", "--force", "enable")
42+
43+
// Show status
44+
logger.Info("Firewall status:")
45+
internal.RunCommand("sudo", "ufw", "status", "verbose")
46+
47+
logger.Success("Firewall configured and enabled successfully")
48+
},
49+
}
50+
51+
func init() {
52+
rootCmd.AddCommand(enableFirewallCmd)
53+
enableFirewallCmd.Flags().IntSliceVarP(&firewallPorts, "port", "p", []int{}, "Additional custom ports to allow (comma separated)")
54+
}

0 commit comments

Comments
 (0)