From 6f261803a7c0b454915e2ba25965f4db447fa13b Mon Sep 17 00:00:00 2001 From: d3m0k1d Date: Mon, 9 Feb 2026 21:51:31 +0300 Subject: [PATCH] feat: add to cli commands for open/close ports on firewall --- cmd/banforge/command/fw.go | 63 +++++++++++++++++++++++++++++++++++++- cmd/banforge/main.go | 1 + 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/cmd/banforge/command/fw.go b/cmd/banforge/command/fw.go index c18e8f5..ae4591e 100644 --- a/cmd/banforge/command/fw.go +++ b/cmd/banforge/command/fw.go @@ -12,7 +12,9 @@ import ( ) var ( - ttl_fw string + ttl_fw string + port int + protocol string ) var UnbanCmd = &cobra.Command{ Use: "unban", @@ -114,6 +116,65 @@ var BanCmd = &cobra.Command{ }, } +var PortCmd = &cobra.Command{ + Use: "port", + Short: "Ports commands", +} + +var PortOpenCmd = &cobra.Command{ + Use: "open", + Short: "Open ports on firewall", + Run: func(cmd *cobra.Command, args []string) { + if protocol == "" { + fmt.Println("Protocol can't be empty") + os.Exit(1) + } + cfg, err := config.LoadConfig() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fw := cfg.Firewall.Name + b := blocker.GetBlocker(fw, cfg.Firewall.Config) + err = b.PortOpen(port, protocol) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println("Port opened successfully!") + }, +} + +var PortCloseCmd = &cobra.Command{ + Use: "close", + Short: "Close ports on firewall", + Run: func(cmd *cobra.Command, args []string) { + if protocol == "" { + fmt.Println("Protocol can't be empty") + os.Exit(1) + } + cfg, err := config.LoadConfig() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fw := cfg.Firewall.Name + b := blocker.GetBlocker(fw, cfg.Firewall.Config) + err = b.PortClose(port, protocol) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Println("Port closed successfully!") + }, +} + func FwRegister() { BanCmd.Flags().StringVarP(&ttl_fw, "ttl", "t", "", "ban time") + PortCmd.AddCommand(PortOpenCmd) + PortCmd.AddCommand(PortCloseCmd) + PortOpenCmd.Flags().IntVarP(&port, "port", "p", 0, "port number") + PortOpenCmd.Flags().StringVarP(&protocol, "protocol", "c", "", "protocol") + PortCloseCmd.Flags().IntVarP(&port, "port", "p", 0, "port number") + PortCloseCmd.Flags().StringVarP(&protocol, "protocol", "c", "", "protocol") } diff --git a/cmd/banforge/main.go b/cmd/banforge/main.go index 2df9e49..7dd35ce 100644 --- a/cmd/banforge/main.go +++ b/cmd/banforge/main.go @@ -28,6 +28,7 @@ func Execute() { rootCmd.AddCommand(command.UnbanCmd) rootCmd.AddCommand(command.BanListCmd) rootCmd.AddCommand(command.VersionCmd) + rootCmd.AddCommand(command.PortCmd) command.RuleRegister() command.FwRegister() if err := rootCmd.Execute(); err != nil {