gofw

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gofw

A small Go firewall library that exposes a simple API and manages Linux nftables internally.

WARNING: This library manages nftables rules. Use carefully on remote servers to avoid locking yourself out.

Installation

go get github.com/drunkleen/gofw

Quick Start

package main

import (
    "log"
    "net/netip"

    "github.com/drunkleen/gofw"
)

func main() {
    fw, err := gofw.New()
    if err != nil {
        log.Fatal(err)
    }

    // Allow SSH
    if err := fw.AllowPort(22, gofw.TCP, "SSH"); err != nil {
        log.Fatal(err)
    }

    // Deny Telnet
    if err := fw.DenyPort(23, gofw.TCP, "Block Telnet"); err != nil {
        log.Fatal(err)
    }

    // Allow an IP
    ip := netip.MustParseAddr("192.168.1.10")
    if err := fw.AllowIP(ip, gofw.From, "Trusted source"); err != nil {
        log.Fatal(err)
    }

    // Block a subnet
    cidr := netip.MustParsePrefix("10.0.0.0/8")
    if err := fw.BlockIPRange(cidr, gofw.To, "Blocked subnet"); err != nil {
        log.Fatal(err)
    }
}

Mock Backend

For testing and development without root privileges:

fw, _ := gofw.NewWithConfig(gofw.Config{Backend: gofw.BackendMock})

API Overview

Port Rules
  • AllowPort(port, protocol, comment) - Allow traffic to a port
  • DenyPort(port, protocol, comment) - Deny traffic to a port
  • DeletePort(port, protocol) - Remove a port rule
IP Rules
  • AllowIP(ip, direction, comment) - Allow traffic from/to an IP
  • DenyIP(ip, direction, comment) - Deny traffic from/to an IP
  • DeleteIP(ip, direction) - Remove an IP rule
IP+Port Rules
  • AllowIPPort(ip, port, protocol, direction, comment) - Allow IP+port combination
  • DenyIPPort(ip, port, protocol, direction, comment) - Deny IP+port combination
  • DeleteIPPort(ip, port, protocol, direction) - Remove an IP+port rule
IP Range Rules
  • AllowIPRange(cidr, direction, comment) - Allow traffic from/to a CIDR range
  • BlockIPRange(cidr, direction, comment) - Block traffic from/to a CIDR range
  • DeleteIPRange(cidr, direction) - Remove a CIDR range rule
Status Queries
  • GetPortStatus(port, protocol) -> Status
  • GetIPStatus(ip, direction) -> Status
  • GetIPPortStatus(ip, port, protocol, direction) -> Status
  • GetIPRangeStatus(cidr, direction) -> Status

Returns StatusAllowed, StatusDenied, or StatusNone.

Listing Rules
  • ListAllRules() -> []Rule
  • ListRules(filter) -> []Rule
  • ListAllowedPorts() -> []uint16
  • ListDeniedPorts() -> []uint16
  • ListAllowedIPs(direction) -> []netip.Addr
  • ListDeniedIPs(direction) -> []netip.Addr
  • ListAllowedIPRanges(direction) -> []netip.Prefix
  • ListDeniedIPRanges(direction) -> []netip.Prefix
Management
  • Flush() - Removes all rules managed by gofw

nftables Backend Notes

  • Creates table inet gofw and chain input by default.
  • Only manages resources it creates.
  • Does not flush unrelated tables or chains.
  • Uses github.com/google/nftables for all communication.
  • Comments are stored as rule UserData.
  • Rules are matched deterministically by their nftables expressions.
  • Integration tests require root and are behind the integration build tag.

Testing

# Unit tests (no root required)
go test ./...

# With race detector
go test -race ./...

# Integration tests (requires root)
sudo go test -tags=integration ./...

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPort      = errors.New("invalid port")
	ErrInvalidProtocol  = errors.New("invalid protocol")
	ErrInvalidDirection = errors.New("invalid direction")
	ErrInvalidIP        = errors.New("invalid ip")
	ErrInvalidPrefix    = errors.New("invalid prefix")
	ErrRuleNotFound     = errors.New("rule not found")
)

Functions

func Ptr

func Ptr[T any](v T) *T

func RuleID

func RuleID(kind RuleKind, action Action, proto Protocol, direction Direction, port uint16, ip netip.Addr, prefix netip.Prefix) string

func ValidateDirection

func ValidateDirection(direction Direction) error

func ValidateIP

func ValidateIP(ip netip.Addr) error

func ValidatePort

func ValidatePort(port uint16) error

func ValidatePrefix

func ValidatePrefix(prefix netip.Prefix) error

func ValidateProtocol

func ValidateProtocol(proto Protocol) error

Types

type Action

type Action string
const (
	Allow Action = "allow"
	Deny  Action = "deny"
)

type Backend

type Backend string
const (
	BackendNFT  Backend = "nftables"
	BackendMock Backend = "mock"
)

type Config

type Config struct {
	Backend Backend
	Table   string
	Chain   string
}

func DefaultConfig

func DefaultConfig() Config

type Direction

type Direction string
const (
	From Direction = "from"
	To   Direction = "to"
)

type Firewall

type Firewall interface {
	AllowPort(port uint16, proto Protocol, comment string) error
	DenyPort(port uint16, proto Protocol, comment string) error
	DeletePort(port uint16, proto Protocol) error

	AllowIP(ip netip.Addr, direction Direction, comment string) error
	DenyIP(ip netip.Addr, direction Direction, comment string) error
	DeleteIP(ip netip.Addr, direction Direction) error

	AllowIPPort(ip netip.Addr, port uint16, proto Protocol, direction Direction, comment string) error
	DenyIPPort(ip netip.Addr, port uint16, proto Protocol, direction Direction, comment string) error
	DeleteIPPort(ip netip.Addr, port uint16, proto Protocol, direction Direction) error

	AllowIPRange(cidr netip.Prefix, direction Direction, comment string) error
	BlockIPRange(cidr netip.Prefix, direction Direction, comment string) error
	DeleteIPRange(cidr netip.Prefix, direction Direction) error

	GetPortStatus(port uint16, proto Protocol) (Status, error)
	GetIPStatus(ip netip.Addr, direction Direction) (Status, error)
	GetIPPortStatus(ip netip.Addr, port uint16, proto Protocol, direction Direction) (Status, error)
	GetIPRangeStatus(cidr netip.Prefix, direction Direction) (Status, error)

	ListRules(filter RuleFilter) ([]Rule, error)
	ListAllRules() ([]Rule, error)

	ListAllowedPorts() ([]uint16, error)
	ListDeniedPorts() ([]uint16, error)

	ListAllowedIPs(direction Direction) ([]netip.Addr, error)
	ListDeniedIPs(direction Direction) ([]netip.Addr, error)

	ListAllowedIPRanges(direction Direction) ([]netip.Prefix, error)
	ListDeniedIPRanges(direction Direction) ([]netip.Prefix, error)

	Flush() error
}

func New

func New() (Firewall, error)

func NewWithConfig

func NewWithConfig(config Config) (Firewall, error)

type Protocol

type Protocol string
const (
	TCP Protocol = "tcp"
	UDP Protocol = "udp"
)

type Rule

type Rule struct {
	ID        string
	Kind      RuleKind
	Action    Action
	Protocol  Protocol
	Direction Direction
	Port      uint16
	IP        netip.Addr
	Prefix    netip.Prefix
	Comment   string
}

type RuleFilter

type RuleFilter struct {
	Action    *Action
	Kind      *RuleKind
	Protocol  *Protocol
	Direction *Direction
}

type RuleKind

type RuleKind string
const (
	RuleKindPort    RuleKind = "port"
	RuleKindIP      RuleKind = "ip"
	RuleKindIPPort  RuleKind = "ip_port"
	RuleKindIPRange RuleKind = "ip_range"
)

type Status

type Status string
const (
	StatusAllowed Status = "allowed"
	StatusDenied  Status = "denied"
	StatusNone    Status = "none"
)

Directories

Path Synopsis
examples
basic command
mock command
internal
nft

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址