hostfw

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package hostfw reads the host firewall (ufw, firewalld, nftables, iptables) and opens exactly ports 80 and 443 in it, nothing else. It never enables a disabled firewall, never edits or deletes a rule it did not add, and every change it makes has a printed undo.

Index

Constants

This section is empty.

Variables

View Source
var ErrRefused = errors.New("hostfw: refused")

ErrRefused is wrapped by Apply when the change is not safe to make here.

View Source
var WebPorts = []int{80, 443}

WebPorts are the only ports this package ever opens.

Functions

This section is empty.

Types

type Backend

type Backend string

Backend names a firewall tool.

const (
	BackendNone      Backend = "none"
	BackendUFW       Backend = "ufw"
	BackendFirewalld Backend = "firewalld"
	BackendNftables  Backend = "nftables"
	BackendIptables  Backend = "iptables"
)

Backend values, in detection order.

type Command

type Command struct {
	Name string   `json:"name"`
	Args []string `json:"args"`
}

Command is one program invocation, shown to the operator before it runs.

func (Command) String

func (c Command) String() string

String is the command as typed in a shell.

type Deps

type Deps struct {
	LookPath func(string) (string, error)
	Run      Runner
}

Deps are the host seams; tests replace both.

func SystemDeps

func SystemDeps() Deps

SystemDeps runs the real tools.

type Plan

type Plan struct {
	Backend Backend   `json:"backend"`
	Ports   []int     `json:"ports"`
	Add     []Command `json:"add"`
	Undo    []Command `json:"undo"`
	// Persistent is false when the rule is lost on reboot unless the operator saves the ruleset.
	Persistent bool   `json:"persistent"`
	Note       string `json:"note,omitempty"`
	// Refusal is set when the change must not be made from here; Add is then empty.
	Refusal string `json:"refusal,omitempty"`
}

Plan is exactly what opening the web ports would run and how to reverse it.

func PlanOpen

func PlanOpen(st Status) Plan

PlanOpen builds the plan for st. It fails closed: a firewall that is off, unreadable, ambiguous or holding an explicit deny yields a Refusal or an empty plan, never a guess.

type Result

type Result struct {
	Plan   Plan   `json:"plan"`
	Status Status `json:"status"`
	// Undo are the commands that reverse exactly what was added.
	Undo []Command `json:"undo"`
}

Result is what Apply did.

func Apply

func Apply(ctx context.Context, d Deps) (Result, error)

Apply re-reads the firewall, then runs the plan. On any failure it undoes what it already added and returns the error, so a half-applied run does not stay behind. It never enables a disabled firewall.

type Runner

type Runner func(ctx context.Context, name string, args ...string) ([]byte, error)

Runner runs one command and returns its combined output.

type Status

type Status struct {
	Backend Backend `json:"backend"`
	// Installed is true when a firewall tool exists on the host.
	Installed bool `json:"installed"`
	// Active is true when the firewall filters incoming traffic.
	Active bool `json:"active"`
	// Unknown is true when a tool exists but could not be read, usually for lack of privileges.
	Unknown bool `json:"unknown"`
	// Open reports, per web port, whether incoming TCP is allowed.
	Open   map[int]bool `json:"open"`
	Detail string       `json:"detail"`
	// contains filtered or unexported fields
}

Status is what the host firewall does with ports 80 and 443 right now.

func Detect

func Detect(ctx context.Context, d Deps) Status

Detect reads every installed firewall tool and reports the one that is enforcing, else the first that is installed, else BackendNone.

func (Status) Blocking

func (s Status) Blocking() bool

Blocking is true when an active firewall would drop traffic to a web port.

func (Status) MissingPorts

func (s Status) MissingPorts() []int

MissingPorts lists the web ports an active firewall does not allow.

Jump to

Keyboard shortcuts

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