firewall

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 2 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.

Documentation

Overview

Package firewall reads Windows Firewall profile types and inbound rules for a program, adds an inbound allow rule, and self-elevates that add through ShellExecuteExW runas. CurrentProfileTypes, Rules, Add and Elevate return ErrUnsupported on operating systems other than Windows.

Index

Constants

View Source
const (
	// ProfileDomain is NET_FW_PROFILE2_DOMAIN.
	ProfileDomain int32 = 1
	// ProfilePrivate is NET_FW_PROFILE2_PRIVATE.
	ProfilePrivate int32 = 2
	// ProfilePublic is NET_FW_PROFILE2_PUBLIC.
	ProfilePublic int32 = 4
	// ProfilesAll is the rule value that matches every profile (all bits set).
	ProfilesAll int32 = 0x7FFFFFFF
	// ProfilesLocal is Domain and Private, the profiles an allow rule covers
	// when Public is blocked explicitly so Windows never prompts.
	ProfilesLocal = ProfileDomain | ProfilePrivate
	// ProfilesEvery is Domain, Private and Public.
	ProfilesEvery = ProfilesLocal | ProfilePublic
)

Windows Firewall profile bits (NET_FW_PROFILE2_*).

Variables

View Source
var ErrUnsupported = errors.New("firewall: unsupported")

ErrUnsupported is returned by CurrentProfileTypes, Rules, Add and Elevate when the operating system has no Windows Firewall COM API.

Functions

func Add

func Add(exe, name, publicName string, anyAddr bool) error

Add removes inbound block rules for exe and writes an inbound allow rule. It returns ErrUnsupported on this operating system.

func Blocked

func Blocked(rules []Rule, exe string, anyAddr bool) bool

Blocked reports whether rules are not yet the set that lets exe accept inbound connections without Windows asking: an allow rule on every profile it should be reachable on, no block rule on those, and, when anyAddr is false, a block rule on Public so every profile has a matching rule.

func CurrentProfileTypes

func CurrentProfileTypes() (int32, error)

CurrentProfileTypes returns the connected networks' Windows Firewall profile bits (NET_FW_PROFILE2_*). It returns ErrUnsupported on this operating system.

func Elevate

func Elevate(exe, args string) error

Elevate starts exe with args via ShellExecuteExW runas and waits for its exit code. It returns ErrUnsupported on this operating system.

func OnlyPublic

func OnlyPublic(current int32) bool

OnlyPublic reports that the connected networks' profile bits (CurrentProfileTypes) name Public and neither Domain nor Private.

Types

type Rule

type Rule struct {
	// App is the rule's ApplicationName.
	App string
	// Inbound reports whether Direction is inbound.
	Inbound bool
	// Allow reports whether Action is allow.
	Allow bool
	// Enabled reports whether the rule is on.
	Enabled bool
	// Profiles is the rule's NET_FW_PROFILE2_* bit mask.
	Profiles int32
}

Rule is the part of a Windows Firewall rule that decides whether a program is reachable inbound.

func Rules

func Rules() ([]Rule, error)

Rules returns the inbound-relevant fields of every Windows Firewall rule. It returns ErrUnsupported on this operating system.

Jump to

Keyboard shortcuts

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