Creating a Go Command-Line Firewall Manager

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setting Up Go
  4. Creating the Firewall Manager
  5. Adding Firewall Rules
  6. Listing Existing Rules
  7. Deleting Firewall Rules
  8. Conclusion

Introduction

In this tutorial, we will create a Go command-line firewall manager that allows users to add, list, and delete firewall rules. We will leverage Go’s networking and file I/O capabilities to accomplish this task. By the end of this tutorial, you will have a basic understanding of Go scripting and be able to build a simple firewall manager.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of the Go programming language and be familiar with concepts such as functions, variables, and command-line interfaces. Additionally, you will need to have Go installed on your system.

Setting Up Go

Before we begin, make sure you have Go installed on your system. You can download the latest version of Go from the official website (https://golang.org/dl/). Follow the installation instructions specific to your operating system.

To verify your Go installation, open a terminal and run the following command:

go version

If you see the Go version printed, it means Go is successfully installed on your system.

Creating the Firewall Manager

Let’s start by creating a new Go script file for our firewall manager. Open your favorite text editor and create a file named firewall.go.

package main

import "fmt"

func main() {
    fmt.Println("Welcome to the Firewall Manager!")
}

Save the file and open a terminal in the directory where you saved the firewall.go file. Run the following command to build and execute the Go script:

go run firewall.go

You should see the output Welcome to the Firewall Manager!. This confirms that our Go script is working correctly.

Adding Firewall Rules

Now let’s implement the functionality to add firewall rules. In this tutorial, we will use the iptables command-line tool to manage the firewall rules. Ensure that the iptables command-line tool is installed on your system.

Inside the main function, add the following code:

package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
)

func main() {
	fmt.Println("Welcome to the Firewall Manager!")

	args := os.Args[1:]
	if len(args) < 3 {
		log.Fatal("Insufficient arguments. Usage: firewall add <protocol> <port>")
	}

	command := exec.Command("iptables", "-A", "INPUT", "-p", args[1], "--dport", args[2], "-j", "ACCEPT")
	err := command.Run()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Firewall rule added successfully!")
}

Save the file and run the following command to add a firewall rule:

go run firewall.go add <protocol> <port>

Replace <protocol> with the desired protocol (e.g., tcp or udp) and <port> with the desired port number. You should see the message Firewall rule added successfully! if the rule was added successfully.

Listing Existing Rules

Let’s add functionality to list the existing firewall rules. We will use the iptables-save command to list all the rules.

Inside the main function, add the following code:

package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
)

func main() {
	fmt.Println("Welcome to the Firewall Manager!")

	args := os.Args[1:]
	if len(args) == 1 && args[0] == "list" {
		command := exec.Command("iptables-save")
		output, err := command.Output()
		if err != nil {
			log.Fatal(err)
		}

		fmt.Println(string(output))
		return
	}

	// Rest of the code...

	fmt.Println("Firewall rule added successfully!")
}

Save the file and run the following command to list the firewall rules:

go run firewall.go list

You should see a list of existing firewall rules printed in the terminal.

Deleting Firewall Rules

Finally, let’s implement the functionality to delete firewall rules.

Inside the main function, add the following code:

package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"strings"
)

func main() {
	fmt.Println("Welcome to the Firewall Manager!")

	args := os.Args[1:]
	if len(args) == 2 && args[0] == "delete" {
		command := exec.Command("iptables-save")
		output, err := command.Output()
		if err != nil {
			log.Fatal(err)
		}

		rules := strings.Split(string(output), "\n")
		ruleNumber := args[1]
		for _, rule := range rules {
			if strings.Contains(rule, fmt.Sprintf("-%s", ruleNumber)) {
				ruleParts := strings.Fields(rule)
				command = exec.Command("iptables", "-D")
				command.Args = append(command.Args, ruleParts[1:len(ruleParts)-1]...)
				err = command.Run()
				if err != nil {
					log.Fatal(err)
				}
				fmt.Println("Firewall rule deleted successfully!")
				return
			}
		}

		fmt.Println("Firewall rule not found!")
		return
	}

	// Rest of the code...

	fmt.Println("Firewall rule added successfully!")
}

Save the file and run the following command to delete a firewall rule:

go run firewall.go delete <rule_number>

Replace <rule_number> with the number of the rule you want to delete. You should see the message Firewall rule deleted successfully! if the rule was deleted successfully.

Conclusion

Congratulations! You have successfully created a Go command-line firewall manager. In this tutorial, we learned how to use Go to interact with the iptables command-line tool and perform operations such as adding, listing, and deleting firewall rules. This basic firewall manager can serve as a starting point for building more complex network security tools using Go.