Documentation
¶
Index ¶
- func LoadLines(path string, handle func(line string) error) error
- func RemoveLineFromFile(filePath string, shouldRemove func(line string) bool) (bool, error)
- type DiffResult
- type Set
- func (s Set[T]) Add(v T)
- func (s Set[T]) Clear()
- func (s Set[T]) Clone() Set[T]
- func (s Set[T]) Difference(other Set[T]) Set[T]
- func (s Set[T]) Has(v T) bool
- func (s Set[T]) Intersection(other Set[T]) Set[T]
- func (s Set[T]) Len() int
- func (s Set[T]) Remove(v T)
- func (s Set[T]) ToSlice() []T
- func (s Set[T]) Union(other Set[T]) Set[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadLines ¶
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 RemoveLineFromFile ¶
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 ¶
Clone creates a shallow copy of the set. Useful when you need to preserve the original.
func (Set[T]) Difference ¶
Difference returns a new set containing elements in s but not in other.
func (Set[T]) Intersection ¶
Intersection returns a new set containing only elements present in both sets.
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.