firewall

package
v2.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackendType

type BackendType string

BackendType represents the detected firewall backend type

const (
	BackendNone     BackendType = "none"
	BackendIptables BackendType = "iptables"
	BackendNftables BackendType = "nftables"
)

type BackupManager

type BackupManager struct {
	// contains filtered or unexported fields
}

BackupManager manages firewall state backups for atomic operations with rollback

func NewBackupManager

func NewBackupManager(backend FirewallBackend) *BackupManager

NewBackupManager creates a new backup manager

func (*BackupManager) BackupAge

func (bm *BackupManager) BackupAge() time.Duration

BackupAge returns the age of the current backup

func (*BackupManager) ClearBackup

func (bm *BackupManager) ClearBackup()

ClearBackup clears the stored backup

func (*BackupManager) CreateBackup

func (bm *BackupManager) CreateBackup(ctx context.Context) error

CreateBackup creates a backup of current firewall state

func (*BackupManager) GetLastBackup

func (bm *BackupManager) GetLastBackup() (backup string, backupTime time.Time, exists bool)

GetLastBackup returns the last backup and its timestamp

func (*BackupManager) HasBackup

func (bm *BackupManager) HasBackup() bool

HasBackup checks if a backup exists

func (*BackupManager) Rollback

func (bm *BackupManager) Rollback(ctx context.Context) error

Rollback restores the last backup

func (*BackupManager) SetBackend

func (bm *BackupManager) SetBackend(backend FirewallBackend)

SetBackend updates the backup manager's backend

func (*BackupManager) WithBackup

func (bm *BackupManager) WithBackup(ctx context.Context, operation func() error) error

WithBackup executes an operation with automatic backup and rollback on failure

type BatchResult

type BatchResult struct {
	Success      bool     `json:"success"`
	AppliedRules int      `json:"applied_rules"`
	FailedRules  []string `json:"failed_rules"`
	RolledBack   bool     `json:"rolled_back"`
	Message      string   `json:"message,omitempty"`
}

BatchResult represents the result of a batch operation

type DetectionResult

type DetectionResult struct {
	Backend           BackendType
	HighLevel         HighLevelFirewall
	NftablesAvailable bool
	IptablesAvailable bool
	Disabled          bool
	DetectedAt        time.Time
}

DetectionResult holds the cached detection results

type FirewallBackend

type FirewallBackend interface {
	// Name returns the backend name (e.g., "iptables", "nftables")
	Name() string

	// Detect checks if this firewall backend is available and active
	Detect(ctx context.Context) bool

	// AddRule adds a single firewall rule
	AddRule(ctx context.Context, rule *common.FirewallRule) error

	// DeleteRule removes a single firewall rule by ID
	DeleteRule(ctx context.Context, ruleID, chainName string) error

	// FlushChain removes all rules from a chain
	FlushChain(ctx context.Context, chainName string) error

	// DeleteChain deletes a chain entirely
	DeleteChain(ctx context.Context, chainName string) error

	// ListRules returns all rules in a chain
	ListRules(ctx context.Context, chainName string) ([]common.FirewallRule, error)

	// BatchApply applies multiple rules atomically
	// Returns: applied count, failed rule descriptions, error
	BatchApply(ctx context.Context, chainName string, rules []common.FirewallRule) (applied int, failed []string, err error)

	// ReorderChains reorders jump rules in INPUT chain
	ReorderChains(ctx context.Context, chainNames []string) (map[string]interface{}, error)

	// ReorderRules reorders rules within a chain
	ReorderRules(ctx context.Context, chainName string, rules []common.FirewallRule) error

	// Backup creates a backup of current firewall state
	Backup(ctx context.Context) (string, error)

	// Restore restores firewall state from backup
	Restore(ctx context.Context, backup string) error
}

FirewallBackend defines the interface for firewall backend implementations

type FirewallData

type FirewallData struct {
	Operation   string                   `json:"operation"`
	ChainName   string                   `json:"chain_name,omitempty"`
	Rules       []map[string]interface{} `json:"rules,omitempty"`
	RuleID      string                   `json:"rule_id,omitempty"`
	OldRuleID   string                   `json:"old_rule_id,omitempty"`
	ChainNames  []string                 `json:"chain_names,omitempty"`
	Method      string                   `json:"method,omitempty"`
	Chain       string                   `json:"chain,omitempty"`
	Protocol    string                   `json:"protocol,omitempty"`
	Source      string                   `json:"source,omitempty"`
	Destination string                   `json:"destination,omitempty"`
	Target      string                   `json:"target,omitempty"`
	Description string                   `json:"description,omitempty"`
}

FirewallData contains data for firewall operations

type FirewallDetector

type FirewallDetector struct {
	// contains filtered or unexported fields
}

FirewallDetector detects available firewall backends and high-level tools

func NewFirewallDetector

func NewFirewallDetector(executor common.CommandExecutor) *FirewallDetector

NewFirewallDetector creates a new firewall detector

func (*FirewallDetector) CreateBackend

func (d *FirewallDetector) CreateBackend(ctx context.Context) FirewallBackend

CreateBackend creates the appropriate backend based on detection result

func (*FirewallDetector) Detect

Detect performs full firewall detection Returns cached result if already detected, otherwise performs detection

func (*FirewallDetector) GetBackendType

func (d *FirewallDetector) GetBackendType() BackendType

GetBackendType returns the detected backend type

func (*FirewallDetector) GetResult

func (d *FirewallDetector) GetResult() *DetectionResult

GetResult returns the cached detection result without re-detecting

func (*FirewallDetector) IsDisabled

func (d *FirewallDetector) IsDisabled() bool

IsDisabled returns true if firewall management is disabled

func (*FirewallDetector) Reset

func (d *FirewallDetector) Reset()

Reset clears the cached detection result, forcing re-detection on next call

type FirewallHandler

type FirewallHandler struct {
	*common.BaseHandler
	// contains filtered or unexported fields
}

FirewallHandler handles firewall management commands

func NewFirewallHandler

func NewFirewallHandler(cmdExecutor common.CommandExecutor) *FirewallHandler

NewFirewallHandler creates a new firewall handler

func (*FirewallHandler) CollectAllRules

func (h *FirewallHandler) CollectAllRules(ctx context.Context) (map[string][]common.FirewallRule, error)

CollectAllRules collects all firewall rules from the system Returns rules in the format compatible with utils.FirewallSyncPayload

func (*FirewallHandler) Execute

func (h *FirewallHandler) Execute(ctx context.Context, cmd string, args *common.CommandArgs) (int, string, error)

Execute runs the firewall management command

func (*FirewallHandler) GetBackend

func (h *FirewallHandler) GetBackend() FirewallBackend

GetBackend returns the current backend for external use

func (*FirewallHandler) GetBackupManager

func (h *FirewallHandler) GetBackupManager() *BackupManager

GetBackupManager returns the backup manager for external use

func (*FirewallHandler) GetDetector

func (h *FirewallHandler) GetDetector() *FirewallDetector

GetDetector returns the firewall detector for external use

func (*FirewallHandler) GetHighLevelFirewall

func (h *FirewallHandler) GetHighLevelFirewall(ctx context.Context) (bool, string)

GetHighLevelFirewall returns the detected high-level firewall tool name if any

func (*FirewallHandler) IsFirewallAvailable

func (h *FirewallHandler) IsFirewallAvailable(ctx context.Context) bool

IsFirewallAvailable checks if firewall functionality is available

func (*FirewallHandler) Validate

func (h *FirewallHandler) Validate(cmd string, args *common.CommandArgs) error

Validate checks if the arguments are valid for the command

type HighLevelFirewall

type HighLevelFirewall string

HighLevelFirewall represents high-level firewall management tools

const (
	HighLevelNone      HighLevelFirewall = ""
	HighLevelUFW       HighLevelFirewall = "ufw"
	HighLevelFirewalld HighLevelFirewall = "firewalld"
)

type IptablesBackend

type IptablesBackend struct {
	// contains filtered or unexported fields
}

IptablesBackend implements FirewallBackend interface for iptables

func NewIptablesBackend

func NewIptablesBackend(executor common.CommandExecutor) *IptablesBackend

NewIptablesBackend creates a new iptables backend

func (*IptablesBackend) AddRule

func (s *IptablesBackend) AddRule(ctx context.Context, rule *common.FirewallRule) error

AddRule adds a single firewall rule

func (*IptablesBackend) Backup

func (s *IptablesBackend) Backup(ctx context.Context) (string, error)

Backup creates a backup of current firewall state

func (*IptablesBackend) BatchApply

func (s *IptablesBackend) BatchApply(ctx context.Context, chainName string, rules []common.FirewallRule) (applied int, failed []string, err error)

BatchApply applies multiple rules atomically

func (*IptablesBackend) DeleteChain

func (s *IptablesBackend) DeleteChain(ctx context.Context, chainName string) error

DeleteChain deletes a chain entirely

func (*IptablesBackend) DeleteRule

func (s *IptablesBackend) DeleteRule(ctx context.Context, ruleID, chainName string) error

DeleteRule removes a single firewall rule by ID

func (*IptablesBackend) Detect

func (s *IptablesBackend) Detect(ctx context.Context) bool

Detect checks if iptables is available

func (*IptablesBackend) FlushChain

func (s *IptablesBackend) FlushChain(ctx context.Context, chainName string) error

FlushChain removes all rules from a chain

func (*IptablesBackend) ListRules

func (s *IptablesBackend) ListRules(ctx context.Context, chainName string) ([]common.FirewallRule, error)

ListRules returns all rules in a chain

func (*IptablesBackend) Name

func (s *IptablesBackend) Name() string

Name returns the backend name

func (*IptablesBackend) ReorderChains

func (s *IptablesBackend) ReorderChains(ctx context.Context, chainNames []string) (map[string]interface{}, error)

ReorderChains reorders jump rules in INPUT chain

func (*IptablesBackend) ReorderRules

func (s *IptablesBackend) ReorderRules(ctx context.Context, chainName string, rules []common.FirewallRule) error

ReorderRules reorders rules within a chain

func (*IptablesBackend) Restore

func (s *IptablesBackend) Restore(ctx context.Context, backup string) error

Restore restores firewall state from backup

type NftablesBackend

type NftablesBackend struct {
	// contains filtered or unexported fields
}

NftablesBackend implements FirewallBackend interface for nftables

func NewNftablesBackend

func NewNftablesBackend(executor common.CommandExecutor) *NftablesBackend

NewNftablesBackend creates a new nftables backend

func (*NftablesBackend) AddRule

func (s *NftablesBackend) AddRule(ctx context.Context, rule *common.FirewallRule) error

AddRule adds a single firewall rule

func (*NftablesBackend) Backup

func (s *NftablesBackend) Backup(ctx context.Context) (string, error)

Backup creates a backup of current firewall state

func (*NftablesBackend) BatchApply

func (s *NftablesBackend) BatchApply(ctx context.Context, chainName string, rules []common.FirewallRule) (applied int, failed []string, err error)

BatchApply applies multiple rules atomically

func (*NftablesBackend) DeleteChain

func (s *NftablesBackend) DeleteChain(ctx context.Context, chainName string) error

DeleteChain deletes a chain entirely

func (*NftablesBackend) DeleteRule

func (s *NftablesBackend) DeleteRule(ctx context.Context, ruleID, chainName string) error

DeleteRule removes a single firewall rule by ID

func (*NftablesBackend) Detect

func (s *NftablesBackend) Detect(ctx context.Context) bool

Detect checks if nftables is available

func (*NftablesBackend) FlushChain

func (s *NftablesBackend) FlushChain(ctx context.Context, chainName string) error

FlushChain removes all rules from a chain

func (*NftablesBackend) ListRules

func (s *NftablesBackend) ListRules(ctx context.Context, chainName string) ([]common.FirewallRule, error)

ListRules returns all rules in a chain

func (*NftablesBackend) Name

func (s *NftablesBackend) Name() string

Name returns the backend name

func (*NftablesBackend) ReorderChains

func (s *NftablesBackend) ReorderChains(ctx context.Context, chainNames []string) (map[string]interface{}, error)

ReorderChains reorders jump rules in INPUT chain

func (*NftablesBackend) ReorderRules

func (s *NftablesBackend) ReorderRules(ctx context.Context, chainName string, rules []common.FirewallRule) error

ReorderRules reorders rules within a chain

func (*NftablesBackend) Restore

func (s *NftablesBackend) Restore(ctx context.Context, backup string) error

Restore restores firewall state from backup

type ReorderResult

type ReorderResult struct {
	Success        bool   `json:"success"`
	ReorderedCount int    `json:"reordered_count,omitempty"`
	DeletedRules   int    `json:"deleted_rules,omitempty"`
	Message        string `json:"message,omitempty"`
}

ReorderResult represents the result of a reorder operation

type Validator

type Validator struct{}

Validator validates firewall rules before execution

func NewValidator

func NewValidator() *Validator

NewValidator creates a new validator

func (*Validator) ValidateBatchRules

func (v *Validator) ValidateBatchRules(rules []common.FirewallRule) error

ValidateBatchRules validates a batch of rules

func (*Validator) ValidateCIDR

func (v *Validator) ValidateCIDR(cidr string) error

ValidateCIDR validates an IP address or CIDR notation

func (*Validator) ValidateChainName

func (v *Validator) ValidateChainName(chainName string) error

ValidateChainName validates a chain name

func (*Validator) ValidateICMPType

func (v *Validator) ValidateICMPType(icmpType string) error

ValidateICMPType validates an ICMP type

func (*Validator) ValidatePort

func (v *Validator) ValidatePort(port int) error

ValidatePort validates a port number

func (*Validator) ValidatePortRange

func (v *Validator) ValidatePortRange(start, end int) error

ValidatePortRange validates a port range

func (*Validator) ValidateProtocol

func (v *Validator) ValidateProtocol(protocol string) error

ValidateProtocol validates the protocol field

func (*Validator) ValidateRule

func (v *Validator) ValidateRule(rule *common.FirewallRule) error

ValidateRule validates a single firewall rule

func (*Validator) ValidateTarget

func (v *Validator) ValidateTarget(target string) error

ValidateTarget validates a firewall target/action

Jump to

Keyboard shortcuts

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