netutil

package
v1.230.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package netutil provides network utility functions for NFTBan Centralizes IP address handling, whitelist checking, and CIDR operations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnforcementClassReject added in v1.220.2

func EnforcementClassReject(ipStr string) (bool, string)

EnforcementClassReject reports whether ip should be rejected from a hostile enforcement action by ADDRESS CLASS. Absolute classes (loopback/unspecified/multicast) are always rejected — non-overridable. Non-public classes (RFC1918, ULA, link-local, CGNAT, documentation, reserved/benchmark) are rejected by DEFAULT, pending an explicit private-network enforcement feature that does not exist today (and must never be inferred from a raw ban). Public routable addresses return (false, ""). A non-bare-IP token (CIDR/range/blank) returns (false, "") so legitimate public feed CIDRs pass.

func GetClientIP

func GetClientIP(r *http.Request) string

GetClientIP extracts the real client IP address from an HTTP request. SECURITY: Only trusts X-Forwarded-For and X-Real-IP headers when the direct connection comes from a trusted proxy (loopback or private network). This prevents IP spoofing via forged headers from external clients.

func GetIPFamily

func GetIPFamily(ipStr string) string

GetIPFamily returns "ipv4" or "ipv6" based on the IP address

func IPContainedInCIDR

func IPContainedInCIDR(ipStr, cidr string) bool

IPContainedInCIDR checks if an IP is contained within a CIDR range

func IsAbsolutelyNonBannable added in v1.220.2

func IsAbsolutelyNonBannable(ipStr string) bool

IsAbsolutelyNonBannable reports whether ip must NEVER enter a hostile enforcement/drop set under any circumstance (non-overridable): loopback, unspecified, or multicast. These address classes have no hostile-source meaning. A non-bare-IP token returns false (callers validate CIDR/range separately).

func IsCIDR

func IsCIDR(s string) bool

IsCIDR checks if a string is a valid CIDR notation

func IsIPWhitelisted

func IsIPWhitelisted(clientIP string, whitelistFile string) (bool, error)

IsIPWhitelisted checks if an IP is in a whitelist file Returns (true, nil) if IP is whitelisted Returns (false, nil) if IP is not whitelisted or file doesn't exist Returns (false, error) on file read errors

func IsIPv4

func IsIPv4(ipStr string) bool

IsIPv4 checks if an IP string represents an IPv4 address

func IsIPv6

func IsIPv6(ipStr string) bool

IsIPv6 checks if an IP string represents an IPv6 address

func IsPrivateIP

func IsPrivateIP(ipStr string) bool

IsPrivateIP checks if an IP is a private/local address

func IsPublicIP

func IsPublicIP(ipStr string) bool

IsPublicIP checks if an IP is a public/routable address

func IsValidIP

func IsValidIP(ipStr string) bool

IsValidIP checks if a string is a valid IP address (v4 or v6)

func NormalizeIP

func NormalizeIP(ipStr string) string

NormalizeIP returns a normalized string representation of an IP Useful for consistent map keys and comparisons

func ParseCIDR

func ParseCIDR(cidr string) (net.IP, *net.IPNet, error)

ParseCIDR parses a CIDR string and returns the IP and network

func ParseIP

func ParseIP(ipStr string) net.IP

ParseIP parses an IP address string and returns the net.IP Returns nil if the string is not a valid IP

func ValidateAndNormalizeIP

func ValidateAndNormalizeIP(ipStr string) (string, bool, error)

ValidateAndNormalizeIP validates and normalizes an IP or CIDR Returns: (normalized string, isIPv4, error) Used by blacklist/whitelist loaders for consistent IP handling

Types

type CoverageResult added in v1.202.0

type CoverageResult struct {
	MissingFromKernel []string // baseline tokens whose coverage is NOT in the kernel union
	ExtraInKernel     []string // kernel tokens whose coverage is NOT in baseline∪sessions union
	BadBaseline       []string // unparseable baseline tokens (surfaced, not silently dropped)
	BadKernel         []string // unparseable kernel tokens
}

CoverageResult holds the real (range-aware) drift between baseline and kernel.

func CoverageDiff added in v1.202.0

func CoverageDiff(baseline, kernel, sessions []string) CoverageResult

CoverageDiff compares the durable baseline against the live kernel set using range coverage. A kernel element is "extra" only if it is covered by neither the durable baseline NOR the (allowed) session/runtime entries. CIDR<->interval representation differences are NOT reported (coverage-equivalent == match).

type IPRange added in v1.202.0

type IPRange struct {
	Start *big.Int
	End   *big.Int
	V6    bool
}

IPRange is an inclusive [Start,End] address range within one family (V6 flag).

func ParseRangeToken added in v1.202.0

func ParseRangeToken(tok string) (*IPRange, error)

ParseRangeToken parses a single IP, a CIDR, or an nft interval ("a-b") into an inclusive IPRange. Trailing nft decorations (comment/timeout) must be stripped by the caller. Returns an error on anything unparseable.

Jump to

Keyboard shortcuts

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