mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 18:38:22 +00:00
Seperate Definitions by Sub System
This commit is contained in:
parent
14c8da64cc
commit
72bf96295d
31 changed files with 120 additions and 108 deletions
|
@ -4,11 +4,11 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/firewall"
|
||||
)
|
||||
|
||||
type GetDestinationNATRulesResult struct {
|
||||
DestinationNATRules []definitions.DestinationNATRule `json:"destination_nat_rules"`
|
||||
DestinationNATRules []firewall.DestinationNATRule `json:"destination_nat_rules"`
|
||||
}
|
||||
|
||||
func (f *Firewall) GetDestinationNATRules(ctx context.Context, params struct{}) (GetDestinationNATRulesResult, error) {
|
||||
|
@ -18,7 +18,7 @@ func (f *Firewall) GetDestinationNATRules(ctx context.Context, params struct{})
|
|||
}
|
||||
|
||||
type CreateDestinationNATRuleParameters struct {
|
||||
DestinationNATRule definitions.DestinationNATRule `json:"destination_nat_rule"`
|
||||
DestinationNATRule firewall.DestinationNATRule `json:"destination_nat_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) CreateDestinationNATRule(ctx context.Context, params CreateDestinationNATRuleParameters) (struct{}, error) {
|
||||
|
@ -30,8 +30,8 @@ func (f *Firewall) CreateDestinationNATRule(ctx context.Context, params CreateDe
|
|||
}
|
||||
|
||||
type UpdateDestinationNATRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
DestinationNATRule definitions.DestinationNATRule `json:"destination_nat_rule"`
|
||||
Index uint64 `json:"index"`
|
||||
DestinationNATRule firewall.DestinationNATRule `json:"destination_nat_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) UpdateDestinationNATRule(ctx context.Context, params UpdateDestinationNATRuleParameters) (struct{}, error) {
|
||||
|
@ -61,7 +61,7 @@ func (f *Firewall) MoveDestinationNATRule(ctx context.Context, params MoveDestin
|
|||
|
||||
rule := conf.Firewall.DestinationNATRules[params.Index]
|
||||
sliceWithoutRule := append(conf.Firewall.DestinationNATRules[:params.Index], conf.Firewall.DestinationNATRules[params.Index+1:]...)
|
||||
newSlice := make([]definitions.DestinationNATRule, params.ToIndex+1)
|
||||
newSlice := make([]firewall.DestinationNATRule, params.ToIndex+1)
|
||||
copy(newSlice, sliceWithoutRule[:params.ToIndex])
|
||||
newSlice[params.ToIndex] = rule
|
||||
conf.Firewall.DestinationNATRules = append(newSlice, sliceWithoutRule[params.ToIndex:]...)
|
||||
|
|
|
@ -4,11 +4,11 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/firewall"
|
||||
)
|
||||
|
||||
type GetForwardRulesResult struct {
|
||||
ForwardRules []definitions.ForwardRule `json:"forward_rules"`
|
||||
ForwardRules []firewall.ForwardRule `json:"forward_rules"`
|
||||
}
|
||||
|
||||
func (f *Firewall) GetForwardRules(ctx context.Context, params struct{}) (GetForwardRulesResult, error) {
|
||||
|
@ -18,7 +18,7 @@ func (f *Firewall) GetForwardRules(ctx context.Context, params struct{}) (GetFor
|
|||
}
|
||||
|
||||
type CreateForwardRuleParameters struct {
|
||||
ForwardRule definitions.ForwardRule `json:"forward_rule"`
|
||||
ForwardRule firewall.ForwardRule `json:"forward_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) CreateForwardRule(ctx context.Context, params CreateForwardRuleParameters) (struct{}, error) {
|
||||
|
@ -30,8 +30,8 @@ func (f *Firewall) CreateForwardRule(ctx context.Context, params CreateForwardRu
|
|||
}
|
||||
|
||||
type UpdateForwardRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
ForwardRule definitions.ForwardRule `json:"forward_rule"`
|
||||
Index uint64 `json:"index"`
|
||||
ForwardRule firewall.ForwardRule `json:"forward_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) UpdateForwardRule(ctx context.Context, params UpdateForwardRuleParameters) (struct{}, error) {
|
||||
|
@ -61,7 +61,7 @@ func (f *Firewall) MoveForwardRule(ctx context.Context, params MoveForwardRulePa
|
|||
|
||||
rule := conf.Firewall.ForwardRules[params.Index]
|
||||
sliceWithoutRule := append(conf.Firewall.ForwardRules[:params.Index], conf.Firewall.ForwardRules[params.Index+1:]...)
|
||||
newSlice := make([]definitions.ForwardRule, params.ToIndex+1)
|
||||
newSlice := make([]firewall.ForwardRule, params.ToIndex+1)
|
||||
copy(newSlice, sliceWithoutRule[:params.ToIndex])
|
||||
newSlice[params.ToIndex] = rule
|
||||
conf.Firewall.ForwardRules = append(newSlice, sliceWithoutRule[params.ToIndex:]...)
|
||||
|
|
|
@ -4,11 +4,11 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/firewall"
|
||||
)
|
||||
|
||||
type GetSourceNATRulesResult struct {
|
||||
SourceNATRules []definitions.SourceNATRule `json:"source_nat_rules"`
|
||||
SourceNATRules []firewall.SourceNATRule `json:"source_nat_rules"`
|
||||
}
|
||||
|
||||
func (f *Firewall) GetSourceNATRules(ctx context.Context, params struct{}) (GetSourceNATRulesResult, error) {
|
||||
|
@ -18,7 +18,7 @@ func (f *Firewall) GetSourceNATRules(ctx context.Context, params struct{}) (GetS
|
|||
}
|
||||
|
||||
type CreateSourceNATRuleParameters struct {
|
||||
SourceNATRule definitions.SourceNATRule `json:"source_nat_rule"`
|
||||
SourceNATRule firewall.SourceNATRule `json:"source_nat_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) CreateSourceNATRule(ctx context.Context, params CreateSourceNATRuleParameters) (struct{}, error) {
|
||||
|
@ -30,8 +30,8 @@ func (f *Firewall) CreateSourceNATRule(ctx context.Context, params CreateSourceN
|
|||
}
|
||||
|
||||
type UpdateSourceNATRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
SourceNATRule definitions.SourceNATRule `json:"source_nat_rule"`
|
||||
Index uint64 `json:"index"`
|
||||
SourceNATRule firewall.SourceNATRule `json:"source_nat_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) UpdateSourceNATRule(ctx context.Context, params UpdateSourceNATRuleParameters) (struct{}, error) {
|
||||
|
@ -61,7 +61,7 @@ func (f *Firewall) MoveSourceNATRule(ctx context.Context, params MoveSourceNATRu
|
|||
|
||||
rule := conf.Firewall.SourceNATRules[params.Index]
|
||||
sliceWithoutRule := append(conf.Firewall.SourceNATRules[:params.Index], conf.Firewall.SourceNATRules[params.Index+1:]...)
|
||||
newSlice := make([]definitions.SourceNATRule, params.ToIndex+1)
|
||||
newSlice := make([]firewall.SourceNATRule, params.ToIndex+1)
|
||||
copy(newSlice, sliceWithoutRule[:params.ToIndex])
|
||||
newSlice[params.ToIndex] = rule
|
||||
conf.Firewall.SourceNATRules = append(newSlice, sliceWithoutRule[params.ToIndex:]...)
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/network"
|
||||
"nfsense.net/nfsense/internal/networkd/dbus"
|
||||
)
|
||||
|
||||
|
@ -28,7 +28,7 @@ type GetInterfaceParameters struct {
|
|||
|
||||
type GetInterfaceResult struct {
|
||||
Name string `json:"name"`
|
||||
definitions.Interface
|
||||
network.Interface
|
||||
}
|
||||
|
||||
func (f *Network) GetInterface(ctx context.Context, params GetInterfaceParameters) (GetInterfaceResult, error) {
|
||||
|
@ -44,7 +44,7 @@ func (f *Network) GetInterface(ctx context.Context, params GetInterfaceParameter
|
|||
}
|
||||
|
||||
type GetInterfacesResult struct {
|
||||
Interfaces map[string]definitions.Interface
|
||||
Interfaces map[string]network.Interface
|
||||
}
|
||||
|
||||
func (f *Network) GetInterfaces(ctx context.Context, params struct{}) (GetInterfacesResult, error) {
|
||||
|
@ -55,7 +55,7 @@ func (f *Network) GetInterfaces(ctx context.Context, params struct{}) (GetInterf
|
|||
|
||||
type CreateInterfaceParameters struct {
|
||||
Name string `json:"name"`
|
||||
definitions.Interface
|
||||
network.Interface
|
||||
}
|
||||
|
||||
func (f *Network) CreateInterface(ctx context.Context, params CreateInterfaceParameters) (struct{}, error) {
|
||||
|
@ -73,7 +73,7 @@ func (f *Network) CreateInterface(ctx context.Context, params CreateInterfacePar
|
|||
|
||||
type UpdateInterfaceParameters struct {
|
||||
Name string
|
||||
definitions.Interface
|
||||
network.Interface
|
||||
}
|
||||
|
||||
func (f *Network) UpdateInterface(ctx context.Context, params UpdateInterfaceParameters) (struct{}, error) {
|
||||
|
|
|
@ -4,11 +4,11 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/network"
|
||||
)
|
||||
|
||||
type GetStaticRoutesResult struct {
|
||||
StaticRoutes []definitions.StaticRoute
|
||||
StaticRoutes []network.StaticRoute
|
||||
}
|
||||
|
||||
func (f *Network) GetStaticRoutes(ctx context.Context, params struct{}) (GetStaticRoutesResult, error) {
|
||||
|
@ -17,7 +17,7 @@ func (f *Network) GetStaticRoutes(ctx context.Context, params struct{}) (GetStat
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (f *Network) CreateStaticRoute(ctx context.Context, params definitions.StaticRoute) (struct{}, error) {
|
||||
func (f *Network) CreateStaticRoute(ctx context.Context, params network.StaticRoute) (struct{}, error) {
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
|
@ -27,7 +27,7 @@ func (f *Network) CreateStaticRoute(ctx context.Context, params definitions.Stat
|
|||
|
||||
type UpdateStaticRouteParameters struct {
|
||||
Index uint
|
||||
definitions.StaticRoute
|
||||
network.StaticRoute
|
||||
}
|
||||
|
||||
func (f *Network) UpdateStaticRoute(ctx context.Context, params UpdateStaticRouteParameters) (struct{}, error) {
|
||||
|
|
|
@ -4,11 +4,11 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/object"
|
||||
)
|
||||
|
||||
type GetAddressesResult struct {
|
||||
Addresses map[string]definitions.Address
|
||||
Addresses map[string]object.Address
|
||||
}
|
||||
|
||||
func (f *Object) GetAddresses(ctx context.Context, params struct{}) (GetAddressesResult, error) {
|
||||
|
@ -19,7 +19,7 @@ func (f *Object) GetAddresses(ctx context.Context, params struct{}) (GetAddresse
|
|||
|
||||
type CreateAddressParameters struct {
|
||||
Name string
|
||||
Address definitions.Address
|
||||
Address object.Address
|
||||
}
|
||||
|
||||
func (f *Object) CreateAddress(ctx context.Context, params CreateAddressParameters) (struct{}, error) {
|
||||
|
@ -37,7 +37,7 @@ func (f *Object) CreateAddress(ctx context.Context, params CreateAddressParamete
|
|||
|
||||
type UpdateAddressParameters struct {
|
||||
Name string
|
||||
Address definitions.Address
|
||||
Address object.Address
|
||||
}
|
||||
|
||||
func (f *Object) UpdateAddress(ctx context.Context, params UpdateAddressParameters) (struct{}, error) {
|
||||
|
|
|
@ -4,11 +4,11 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/object"
|
||||
)
|
||||
|
||||
type GetServicesResult struct {
|
||||
Services map[string]definitions.Service
|
||||
Services map[string]object.Service
|
||||
}
|
||||
|
||||
func (f *Object) GetServices(ctx context.Context, params struct{}) (GetServicesResult, error) {
|
||||
|
@ -19,7 +19,7 @@ func (f *Object) GetServices(ctx context.Context, params struct{}) (GetServicesR
|
|||
|
||||
type CreateServiceParameters struct {
|
||||
Name string
|
||||
Service definitions.Service
|
||||
Service object.Service
|
||||
}
|
||||
|
||||
func (f *Object) CreateService(ctx context.Context, params CreateServiceParameters) (struct{}, error) {
|
||||
|
@ -37,7 +37,7 @@ func (f *Object) CreateService(ctx context.Context, params CreateServiceParamete
|
|||
|
||||
type UpdateServiceParameters struct {
|
||||
Name string
|
||||
Service definitions.Service
|
||||
Service object.Service
|
||||
}
|
||||
|
||||
func (f *Object) UpdateService(ctx context.Context, params UpdateServiceParameters) (struct{}, error) {
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"os"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
// ApplyPendingChanges Takes all pending Changes and Tries to Apply them using the Registered Apply Functions.
|
||||
|
@ -47,6 +47,6 @@ func revertToCurrent(m *ConfigManager) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (m *ConfigManager) RegisterApplyFunction(fn func(currentConfig definitions.Config, pendingConfig definitions.Config) error) {
|
||||
func (m *ConfigManager) RegisterApplyFunction(fn func(currentConfig config.Config, pendingConfig config.Config) error) {
|
||||
m.applyFunctions = append(m.applyFunctions, fn)
|
||||
}
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
package config
|
||||
|
||||
import "nfsense.net/nfsense/internal/definitions"
|
||||
import (
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
func (m *ConfigManager) GetCurrentConfig() definitions.Config {
|
||||
func (m *ConfigManager) GetCurrentConfig() config.Config {
|
||||
return *m.currentConfig.Clone()
|
||||
}
|
||||
|
||||
func (m *ConfigManager) GetPendingConfig() definitions.Config {
|
||||
func (m *ConfigManager) GetPendingConfig() config.Config {
|
||||
return *m.pendingConfig.Clone()
|
||||
}
|
||||
|
|
|
@ -5,11 +5,11 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
func (m *ConfigManager) LoadCurrentConfigFromDisk() error {
|
||||
var config definitions.Config
|
||||
var conf config.Config
|
||||
configFile, err := os.Open(m.currentConfigFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening Config File %w", err)
|
||||
|
@ -18,22 +18,22 @@ func (m *ConfigManager) LoadCurrentConfigFromDisk() error {
|
|||
|
||||
jsonParser := json.NewDecoder(configFile)
|
||||
jsonParser.DisallowUnknownFields()
|
||||
err = jsonParser.Decode(&config)
|
||||
err = jsonParser.Decode(&conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decoding Config File %w", err)
|
||||
}
|
||||
|
||||
err = definitions.ValidateConfig(&config)
|
||||
err = config.ValidateConfig(&conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("validating Config: %w", err)
|
||||
}
|
||||
|
||||
m.currentConfig = &config
|
||||
m.currentConfig = &conf
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ConfigManager) LoadPendingConfigFromDisk() error {
|
||||
var config definitions.Config
|
||||
var conf config.Config
|
||||
configFile, err := os.Open(m.pendingConfigFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening Config File %w", err)
|
||||
|
@ -42,16 +42,16 @@ func (m *ConfigManager) LoadPendingConfigFromDisk() error {
|
|||
|
||||
jsonParser := json.NewDecoder(configFile)
|
||||
jsonParser.DisallowUnknownFields()
|
||||
err = jsonParser.Decode(&config)
|
||||
err = jsonParser.Decode(&conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decoding Config File %w", err)
|
||||
}
|
||||
|
||||
err = definitions.ValidateConfig(&config)
|
||||
err = config.ValidateConfig(&conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("validating Config: %w", err)
|
||||
}
|
||||
|
||||
m.pendingConfig = &config
|
||||
m.pendingConfig = &conf
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -3,27 +3,27 @@ package config
|
|||
import (
|
||||
"sync"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
type ConfigManager struct {
|
||||
currentConfigFilePath string
|
||||
pendingConfigFilePath string
|
||||
|
||||
currentConfig *definitions.Config
|
||||
pendingConfig *definitions.Config
|
||||
currentConfig *config.Config
|
||||
pendingConfig *config.Config
|
||||
|
||||
transactionMutex sync.Mutex
|
||||
|
||||
applyFunctions []func(currentConfig definitions.Config, pendingConfig definitions.Config) error
|
||||
applyFunctions []func(currentConfig config.Config, pendingConfig config.Config) error
|
||||
}
|
||||
|
||||
func CreateConfigManager() *ConfigManager {
|
||||
manager := ConfigManager{
|
||||
currentConfigFilePath: "config.json",
|
||||
pendingConfigFilePath: "pending.json",
|
||||
currentConfig: &definitions.Config{},
|
||||
pendingConfig: &definitions.Config{},
|
||||
currentConfig: &config.Config{},
|
||||
pendingConfig: &config.Config{},
|
||||
}
|
||||
return &manager
|
||||
}
|
||||
|
|
|
@ -5,10 +5,10 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
func (m *ConfigManager) saveConfig(path string, conf *definitions.Config) error {
|
||||
func (m *ConfigManager) saveConfig(path string, conf *config.Config) error {
|
||||
data, err := json.MarshalIndent(conf, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Marshal Config: %w", err)
|
||||
|
|
|
@ -4,17 +4,17 @@ import (
|
|||
"fmt"
|
||||
"sync"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
type ConfigTransaction struct {
|
||||
finished bool
|
||||
mutex sync.Mutex
|
||||
configManager *ConfigManager
|
||||
changes *definitions.Config
|
||||
changes *config.Config
|
||||
}
|
||||
|
||||
func (m *ConfigManager) StartTransaction() (*ConfigTransaction, *definitions.Config) {
|
||||
func (m *ConfigManager) StartTransaction() (*ConfigTransaction, *config.Config) {
|
||||
m.transactionMutex.Lock()
|
||||
confCopy := m.pendingConfig.Clone()
|
||||
return &ConfigTransaction{
|
||||
|
@ -34,7 +34,7 @@ func (t *ConfigTransaction) Commit() error {
|
|||
t.finished = true
|
||||
defer t.configManager.transactionMutex.Unlock()
|
||||
|
||||
err := definitions.ValidateConfig(t.changes)
|
||||
err := config.ValidateConfig(t.changes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("validating Config before Apply: %w", err)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
@ -6,13 +6,16 @@ import (
|
|||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/internal/definitions/firewall"
|
||||
"nfsense.net/nfsense/internal/definitions/network"
|
||||
"nfsense.net/nfsense/internal/definitions/object"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ConfigVersion uint64 `json:"config_version" validate:"required,eq=1"`
|
||||
Firewall Firewall `json:"firewall" validate:"required,dive"`
|
||||
Object Object `json:"object" validate:"required,dive"`
|
||||
Network Network `json:"network" validate:"required,dive"`
|
||||
ConfigVersion uint64 `json:"config_version" validate:"required,eq=1"`
|
||||
Firewall firewall.Firewall `json:"firewall" validate:"required,dive"`
|
||||
Object object.Object `json:"object" validate:"required,dive"`
|
||||
Network network.Network `json:"network" validate:"required,dive"`
|
||||
}
|
||||
|
||||
// Clone TODO find a better way to deep copy
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package firewall
|
||||
|
||||
type DestinationNATRule struct {
|
||||
Rule
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package firewall
|
||||
|
||||
type Firewall struct {
|
||||
ForwardRules []ForwardRule `json:"forward_rules" validate:"required,dive"`
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package firewall
|
||||
|
||||
type Match struct {
|
||||
TCPDestinationPort uint64 `json:"tcp_destination_port,omitempty"`
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package firewall
|
||||
|
||||
import "encoding/json"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package firewall
|
||||
|
||||
import "encoding/json"
|
||||
|
|
@ -1,14 +1,16 @@
|
|||
package definitions
|
||||
package network
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions/common"
|
||||
)
|
||||
|
||||
type Interface struct {
|
||||
Alias string `json:"alias,omitempty" validate:"min=0,max=3"`
|
||||
Type InterfaceType `json:"type" validate:"min=0,max=3"`
|
||||
AddressingMode InterfaceAddressingMode `json:"addressing_mode" validate:"min=0,max=2"`
|
||||
Address *IPCIDR `json:"address,omitempty" validate:"excluded_unless=AddressingMode 1"`
|
||||
Address *common.IPCIDR `json:"address,omitempty" validate:"excluded_unless=AddressingMode 1"`
|
||||
HardwareDevice *string `json:"hardware_device,omitempty"`
|
||||
// TODO fix Validator for int pointers with min=0,max=4094
|
||||
VlanID *uint `json:"vlan_id,omitempty"`
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package network
|
||||
|
||||
type Network struct {
|
||||
Interfaces map[string]Interface `json:"interfaces" validate:"required,dive"`
|
15
internal/definitions/network/static_route.go
Normal file
15
internal/definitions/network/static_route.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions/common"
|
||||
)
|
||||
|
||||
type StaticRoute struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Interface string `json:"interface,omitempty"`
|
||||
Gateway netip.Addr `json:"gateway,omitempty"`
|
||||
Destination common.IPNet `json:"destination,omitempty"`
|
||||
Metric uint `json:"metric,omitempty"`
|
||||
}
|
|
@ -1,10 +1,11 @@
|
|||
package definitions
|
||||
package object
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/netip"
|
||||
|
||||
"go4.org/netipx"
|
||||
"nfsense.net/nfsense/internal/definitions/common"
|
||||
)
|
||||
|
||||
type Address struct {
|
||||
|
@ -12,7 +13,7 @@ type Address struct {
|
|||
Comment string `json:"comment,omitempty"`
|
||||
Host *netip.Addr `json:"host,omitempty" validate:"excluded_unless=Type 0"`
|
||||
Range *netipx.IPRange `json:"range,omitempty" validate:"excluded_unless=Type 1"`
|
||||
NetworkAddress *IPNet `json:"network,omitempty" validate:"excluded_unless=Type 2"`
|
||||
NetworkAddress *common.IPNet `json:"network,omitempty" validate:"excluded_unless=Type 2"`
|
||||
Children *[]string `json:"children,omitempty"`
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package object
|
||||
|
||||
type Object struct {
|
||||
Addresses map[string]Address `json:"addresses" validate:"required,dive"`
|
|
@ -1,4 +1,4 @@
|
|||
package definitions
|
||||
package object
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -1,13 +0,0 @@
|
|||
package definitions
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
type StaticRoute struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Interface string `json:"interface,omitempty"`
|
||||
Gateway netip.Addr `json:"gateway,omitempty"`
|
||||
Destination IPNet `json:"destination,omitempty"`
|
||||
Metric uint `json:"metric,omitempty"`
|
||||
}
|
|
@ -8,12 +8,12 @@ import (
|
|||
"path/filepath"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
)
|
||||
|
||||
const basepath = "/etc/systemd/network"
|
||||
|
||||
func ApplyNetworkdConfiguration(currentConfig definitions.Config, pendingConfig definitions.Config) error {
|
||||
func ApplyNetworkdConfiguration(currentConfig config.Config, pendingConfig config.Config) error {
|
||||
files, err := GenerateNetworkdConfiguration(pendingConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Generating Networkd Configuration: %w", err)
|
||||
|
@ -41,6 +41,7 @@ func ApplyNetworkdConfiguration(currentConfig definitions.Config, pendingConfig
|
|||
}
|
||||
}
|
||||
|
||||
// TODO Use dbus instead
|
||||
cmd := exec.Command("systemctl", "restart", "systemd-networkd")
|
||||
|
||||
var out bytes.Buffer
|
||||
|
|
|
@ -5,7 +5,8 @@ import (
|
|||
"fmt"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions/config"
|
||||
"nfsense.net/nfsense/internal/definitions/network"
|
||||
)
|
||||
|
||||
type NetworkdConfigFile struct {
|
||||
|
@ -15,9 +16,9 @@ type NetworkdConfigFile struct {
|
|||
|
||||
type InterfaceWithName struct {
|
||||
Name string
|
||||
definitions.Interface
|
||||
network.Interface
|
||||
Vlans []string
|
||||
StaticRoutes []definitions.StaticRoute
|
||||
StaticRoutes []network.StaticRoute
|
||||
}
|
||||
|
||||
type BondMembership struct {
|
||||
|
@ -30,12 +31,12 @@ type BridgeMembership struct {
|
|||
BridgeName string
|
||||
}
|
||||
|
||||
func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFile, error) {
|
||||
func GenerateNetworkdConfiguration(conf config.Config) ([]NetworkdConfigFile, error) {
|
||||
files := []NetworkdConfigFile{}
|
||||
|
||||
// Step 1 Generate vlan netdev files
|
||||
for name, inter := range conf.Network.Interfaces {
|
||||
if inter.Type == definitions.Vlan {
|
||||
if inter.Type == network.Vlan {
|
||||
buf := new(bytes.Buffer)
|
||||
err := templates.ExecuteTemplate(buf, "create-vlan.netdev.tmpl", InterfaceWithName{
|
||||
Name: name,
|
||||
|
@ -53,7 +54,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
|
|||
|
||||
// Step 2 Generate bond netdev files
|
||||
for name, inter := range conf.Network.Interfaces {
|
||||
if inter.Type == definitions.Bond {
|
||||
if inter.Type == network.Bond {
|
||||
buf := new(bytes.Buffer)
|
||||
err := templates.ExecuteTemplate(buf, "create-bond.netdev.tmpl", InterfaceWithName{
|
||||
Name: name,
|
||||
|
@ -71,7 +72,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
|
|||
|
||||
// Step 3 Generate bridge netdev files
|
||||
for name, inter := range conf.Network.Interfaces {
|
||||
if inter.Type == definitions.Bridge {
|
||||
if inter.Type == network.Bridge {
|
||||
buf := new(bytes.Buffer)
|
||||
err := templates.ExecuteTemplate(buf, "create-bridge.netdev.tmpl", InterfaceWithName{
|
||||
Name: name,
|
||||
|
@ -89,7 +90,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
|
|||
|
||||
// Step 4 Generate Bond Members
|
||||
for name, inter := range conf.Network.Interfaces {
|
||||
if inter.Type == definitions.Bond && inter.BondMembers != nil {
|
||||
if inter.Type == network.Bond && inter.BondMembers != nil {
|
||||
for _, member := range *inter.BondMembers {
|
||||
buf := new(bytes.Buffer)
|
||||
err := templates.ExecuteTemplate(buf, "bond-membership.network.tmpl", BondMembership{
|
||||
|
@ -109,7 +110,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
|
|||
|
||||
// Step 5 Generate Bridge Members
|
||||
for name, inter := range conf.Network.Interfaces {
|
||||
if inter.Type == definitions.Bridge && inter.BridgeMembers != nil {
|
||||
if inter.Type == network.Bridge && inter.BridgeMembers != nil {
|
||||
for _, member := range *inter.BridgeMembers {
|
||||
buf := new(bytes.Buffer)
|
||||
err := templates.ExecuteTemplate(buf, "bridge-membership.network.tmpl", BridgeMembership{
|
||||
|
@ -131,10 +132,10 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
|
|||
for name, inter := range conf.Network.Interfaces {
|
||||
// Vlans
|
||||
vlans := []string{}
|
||||
if inter.Type != definitions.Vlan {
|
||||
if inter.Type != network.Vlan {
|
||||
vlans := []string{}
|
||||
for vlanName, vlanInter := range conf.Network.Interfaces {
|
||||
if vlanInter.Type == definitions.Vlan {
|
||||
if vlanInter.Type == network.Vlan {
|
||||
if *vlanInter.VlanParent == name {
|
||||
vlans = append(vlans, vlanName)
|
||||
}
|
||||
|
@ -144,7 +145,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
|
|||
}
|
||||
|
||||
// Static Routes
|
||||
staticRoutes := []definitions.StaticRoute{}
|
||||
staticRoutes := []network.StaticRoute{}
|
||||
for _, route := range conf.Network.StaticRoutes {
|
||||
if route.Interface == name {
|
||||
staticRoutes = append(staticRoutes, route)
|
||||
|
|
Loading…
Add table
Reference in a new issue