util

package
v1.8.7 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

SPDX-License-Identifier: MPL-2.0 Copyright (c) 2025 Antonios Voulvoulis <contact@nftban.com>

Package util provides common utility functions for NFTBan.

meta:name="util" meta:type="package" meta:description="Common utility functions (JSON, parsing, etc.)" meta:inventory.files="" meta:inventory.binaries="" meta:inventory.env_vars="" meta:inventory.config_files="" meta:inventory.systemd_units="" meta:inventory.network="" meta:inventory.privileges=""

SPDX-License-Identifier: MPL-2.0 Copyright (c) 2025 Antonios Voulvoulis <contact@nftban.com>

meta:name="util_parse" meta:type="package" meta:description="Common parsing utilities (bool, float, int)" meta:inventory.files="" meta:inventory.binaries="" meta:inventory.env_vars="" meta:inventory.config_files="" meta:inventory.systemd_units="" meta:inventory.network="" meta:inventory.privileges=""

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractJSON added in v1.8.2

func ExtractJSON(output string) string

ExtractJSON extracts JSON object from mixed output string. Useful for parsing CLI output that may contain non-JSON text.

func ExtractJSONArray added in v1.8.2

func ExtractJSONArray(output string) string

ExtractJSONArray extracts JSON array from mixed output string.

func FileExists added in v1.8.2

func FileExists(path string) bool

FileExists checks if a file or directory exists at the given path. Returns true if the path exists, false otherwise.

func FormatBytes added in v1.8.2

func FormatBytes(bytes int64) string

FormatBytes formats a byte count into a human-readable string. Uses binary units (1024-based): B, KB, MB, GB, TB, PB, EB.

func FormatBytesUint added in v1.8.2

func FormatBytesUint(bytes uint64) string

FormatBytesUint formats an unsigned byte count into a human-readable string.

func FormatPercent added in v1.8.2

func FormatPercent(value float64) string

FormatPercent formats a percentage value with one decimal place.

func IsDir added in v1.8.2

func IsDir(path string) bool

IsDir checks if the given path is a directory. Returns true if it's a directory, false if it doesn't exist or is a file.

func IsFile added in v1.8.2

func IsFile(path string) bool

IsFile checks if the given path is a regular file. Returns true if it's a file, false if it doesn't exist or is a directory.

func IsJSON added in v1.8.2

func IsJSON(s string) bool

IsJSON checks if a string looks like valid JSON (starts with { or [)

func LoadLines

func LoadLines(path string, handle func(line string) error) error

LoadLines reads a file line-by-line and calls the handler for each line. Designed for efficient processing of large files (threat feeds, logs, configs).

Key optimizations: - Preallocated 64KB buffer prevents allocations for typical lines - 1MB max line length handles pathological cases safely - Single pass, streaming design for memory efficiency - Early error returns prevent partial processing

The handler function receives each line as a string and can: - Return nil to continue processing - Return error to stop and propagate error

Use cases in nftban v1.0: - Whitelist/blacklist file loading - Threat feed parsing - Suricata eve.json tailing - Mail/panel log parsing - Any line-oriented file processing

Example usage:

err := util.LoadLines("/etc/nftban/whitelist.conf", func(line string) error {
    line = strings.TrimSpace(line)
    if line == "" || strings.HasPrefix(line, "#") {
        return nil // skip empty lines and comments
    }
    // process line...
    return nil
})

func ParseBool added in v1.8.2

func ParseBool(s string, defaultVal bool) bool

ParseBool parses boolean string with multiple formats. Recognizes: true/false, yes/no, 1/0, on/off

func ParseFloat added in v1.8.2

func ParseFloat(s string, defaultVal float64) float64

ParseFloat parses float string with default fallback

func ParseInt added in v1.8.2

func ParseInt(s string, defaultVal int) int

ParseInt parses int string with default fallback

func ParseInt64 added in v1.8.2

func ParseInt64(s string, defaultVal int64) int64

ParseInt64 parses int64 string with default fallback

func ParseUint added in v1.8.2

func ParseUint(s string, defaultVal uint64) uint64

ParseUint parses unsigned int string with default fallback

func RemoveLineFromFile

func RemoveLineFromFile(filePath string, shouldRemove func(line string) bool) (bool, error)

RemoveLineFromFile removes lines from a file that match the given predicate. The predicate function receives each line and returns true if the line should be removed. Returns whether any lines were removed.

This is used by both whitelist and blacklist packages to remove IPs from config files. The atomic write pattern (write to tmp, then rename) ensures file integrity.

Example usage:

removed, err := util.RemoveLineFromFile("/etc/nftban/blacklist.d/99-manual.conf",
    func(line string) bool {
        ip := strings.Fields(strings.TrimSpace(line))[0]
        return ip == "192.168.1.100"
    })

Types

type DiffResult

type DiffResult[T comparable] struct {
	ToAdd     []T
	ToRemove  []T
	Unchanged int
}

DiffResult is a generic diff result between two slices of comparable values. This replaces the old string-specific diff and can be reused for IPs, ports, feed IDs, alert types, or any comparable type.

func ComputeDiff

func ComputeDiff[T comparable](desired, current []T) DiffResult[T]

ComputeDiff returns which items should be added/removed to transform `current` into `desired`.

This is the foundation for all sync operations in nftban v1.0: - IP set synchronization (whitelist/blacklist) - Threat feed updates - Suricata alert type management - Port set updates - Any set-based diff operation

Performance characteristics: - O(n + m) time complexity where n=len(desired), m=len(current) - Preallocated maps with capacity hints for efficiency - Zero allocations for unchanged items

Example usage:

desired := []string{"1.2.3.4", "5.6.7.8"}
current := []string{"5.6.7.8", "9.9.9.9"}
diff := util.ComputeDiff(desired, current)
// Result: ToAdd=["1.2.3.4"], ToRemove=["9.9.9.9"], Unchanged=1

type Set

type Set[T comparable] map[T]struct{}

Set is a generic set implementation using Go's map type. Uses struct{} as value type for zero memory overhead.

This replaces map[string]bool and similar patterns throughout the codebase with a consistent, efficient, and type-safe API.

Benefits over map[T]bool: - Zero memory overhead for values (struct{} vs bool) - Consistent API across all set operations - Type-safe - Self-documenting code

func NewSet

func NewSet[T comparable](values ...T) Set[T]

NewSet creates a new set and optionally adds initial values. Preallocates map with correct capacity if initial values provided.

Example:

// Empty set
ips := util.NewSet[string]()

// Set with initial values
ports := util.NewSet(80, 443, 8080)

func (Set[T]) Add

func (s Set[T]) Add(v T)

Add adds a value to the set. Safe to call multiple times with same value (idempotent).

func (Set[T]) Clear

func (s Set[T]) Clear()

Clear removes all elements from the set. More efficient than creating a new set if you plan to reuse.

func (Set[T]) Clone

func (s Set[T]) Clone() Set[T]

Clone creates a shallow copy of the set. Useful when you need to preserve the original.

func (Set[T]) Difference

func (s Set[T]) Difference(other Set[T]) Set[T]

Difference returns a new set containing elements in s but not in other.

func (Set[T]) Has

func (s Set[T]) Has(v T) bool

Has checks if a value exists in the set. O(1) average time complexity.

func (Set[T]) Intersection

func (s Set[T]) Intersection(other Set[T]) Set[T]

Intersection returns a new set containing only elements present in both sets.

func (Set[T]) Len

func (s Set[T]) Len() int

Len returns the number of elements in the set.

func (Set[T]) Remove

func (s Set[T]) Remove(v T)

Remove removes a value from the set. Safe to call even if value doesn't exist.

func (Set[T]) ToSlice

func (s Set[T]) ToSlice() []T

ToSlice converts the set to a slice. Order is not guaranteed (map iteration is random). Preallocates slice with exact capacity to avoid resizing.

func (Set[T]) Union

func (s Set[T]) Union(other Set[T]) Set[T]

Union returns a new set containing all elements from both sets.

Jump to

Keyboard shortcuts

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