2023-12-08 10:48:21 +01:00
|
|
|
package manager
|
2023-03-16 10:00:08 +01:00
|
|
|
|
|
|
|
import (
|
2023-12-08 10:48:21 +01:00
|
|
|
"fmt"
|
2023-03-16 10:00:08 +01:00
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
const (
|
|
|
|
NatFormat = "netbird-nat-%s"
|
|
|
|
ForwardingFormat = "netbird-fwd-%s"
|
|
|
|
InNatFormat = "netbird-nat-in-%s"
|
|
|
|
InForwardingFormat = "netbird-fwd-in-%s"
|
|
|
|
)
|
|
|
|
|
2023-03-16 10:00:08 +01:00
|
|
|
// Rule abstraction should be implemented by each firewall manager
|
|
|
|
//
|
|
|
|
// Each firewall type for different OS can use different type
|
|
|
|
// of the properties to hold data of the created rule
|
|
|
|
type Rule interface {
|
|
|
|
// GetRuleID returns the rule id
|
|
|
|
GetRuleID() string
|
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
// RuleDirection is the traffic direction which a rule is applied
|
|
|
|
type RuleDirection int
|
2023-03-16 10:00:08 +01:00
|
|
|
|
|
|
|
const (
|
2023-05-29 16:00:18 +02:00
|
|
|
// RuleDirectionIN applies to filters that handlers incoming traffic
|
|
|
|
RuleDirectionIN RuleDirection = iota
|
|
|
|
// RuleDirectionOUT applies to filters that handlers outgoing traffic
|
|
|
|
RuleDirectionOUT
|
2023-03-16 10:00:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Action is the action to be taken on a rule
|
|
|
|
type Action int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ActionAccept is the action to accept a packet
|
2023-12-08 10:48:21 +01:00
|
|
|
ActionAccept Action = iota
|
2023-03-16 10:00:08 +01:00
|
|
|
// ActionDrop is the action to drop a packet
|
|
|
|
ActionDrop
|
|
|
|
)
|
|
|
|
|
|
|
|
// Manager is the high level abstraction of a firewall manager
|
|
|
|
//
|
|
|
|
// It declares methods which handle actions required by the
|
|
|
|
// Netbird client for ACL and routing functionality
|
|
|
|
type Manager interface {
|
2023-09-05 21:07:32 +02:00
|
|
|
// AllowNetbird allows netbird interface traffic
|
|
|
|
AllowNetbird() error
|
|
|
|
|
2023-03-16 10:00:08 +01:00
|
|
|
// AddFiltering rule to the firewall
|
2023-05-29 16:00:18 +02:00
|
|
|
//
|
|
|
|
// If comment argument is empty firewall manager should set
|
|
|
|
// rule ID as comment for the rule
|
2023-03-16 10:00:08 +01:00
|
|
|
AddFiltering(
|
|
|
|
ip net.IP,
|
2023-05-29 16:00:18 +02:00
|
|
|
proto Protocol,
|
|
|
|
sPort *Port,
|
|
|
|
dPort *Port,
|
|
|
|
direction RuleDirection,
|
2023-03-16 10:00:08 +01:00
|
|
|
action Action,
|
2023-07-18 11:12:50 +02:00
|
|
|
ipsetName string,
|
2023-03-16 10:00:08 +01:00
|
|
|
comment string,
|
2023-12-08 10:48:21 +01:00
|
|
|
) ([]Rule, error)
|
2023-03-16 10:00:08 +01:00
|
|
|
|
|
|
|
// DeleteRule from the firewall by rule definition
|
|
|
|
DeleteRule(rule Rule) error
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
// IsServerRouteSupported returns true if the firewall supports server side routing operations
|
|
|
|
IsServerRouteSupported() bool
|
|
|
|
|
|
|
|
// InsertRoutingRules inserts a routing firewall rule
|
|
|
|
InsertRoutingRules(pair RouterPair) error
|
|
|
|
|
|
|
|
// RemoveRoutingRules removes a routing firewall rule
|
|
|
|
RemoveRoutingRules(pair RouterPair) error
|
|
|
|
|
2023-03-16 10:00:08 +01:00
|
|
|
// Reset firewall to the default state
|
|
|
|
Reset() error
|
|
|
|
|
2023-07-18 11:12:50 +02:00
|
|
|
// Flush the changes to firewall controller
|
|
|
|
Flush() error
|
2023-12-08 10:48:21 +01:00
|
|
|
}
|
2023-07-18 11:12:50 +02:00
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
func GenKey(format string, input string) string {
|
|
|
|
return fmt.Sprintf(format, input)
|
2023-03-16 10:00:08 +01:00
|
|
|
}
|