mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-11 19:08:20 +00:00
Initial Work on Backend Interface Management
This commit is contained in:
parent
d54ceb8a0f
commit
a90e7648db
10 changed files with 196 additions and 12 deletions
20
internal/api/network/interfaces.go
Normal file
20
internal/api/network/interfaces.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
type GetInterfacesParameters struct {
|
||||
}
|
||||
|
||||
type GetInterfacesResult struct {
|
||||
Interfaces []definitions.Interface
|
||||
}
|
||||
|
||||
func (f *Network) GetInterfaces(ctx context.Context, params GetInterfacesParameters) (GetInterfacesResult, error) {
|
||||
return GetInterfacesResult{
|
||||
Interfaces: f.Conf.Network.Interfaces,
|
||||
}, nil
|
||||
}
|
9
internal/api/network/network.go
Normal file
9
internal/api/network/network.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
type Network struct {
|
||||
Conf *definitions.Config
|
||||
}
|
|
@ -12,7 +12,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"`
|
||||
Network *IPNet `json:"network,omitempty" validate:"excluded_unless=Type 2"`
|
||||
NetworkAddress *IPNet `json:"network,omitempty" validate:"excluded_unless=Type 2"`
|
||||
Children *[]string `json:"children,omitempty"`
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ type AddressType int
|
|||
const (
|
||||
Host AddressType = iota
|
||||
Range
|
||||
Network
|
||||
NetworkAddress
|
||||
AddressGroup
|
||||
)
|
||||
|
||||
|
@ -33,7 +33,7 @@ func (t *AddressType) FromString(input string) AddressType {
|
|||
return map[string]AddressType{
|
||||
"host": Host,
|
||||
"range": Range,
|
||||
"network": Network,
|
||||
"network": NetworkAddress,
|
||||
"group": AddressGroup,
|
||||
}[input]
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ 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"`
|
||||
}
|
||||
|
||||
func ValidateConfig(conf *Config) error {
|
||||
|
|
83
internal/definitions/interface.go
Normal file
83
internal/definitions/interface.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package definitions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
type Interface struct {
|
||||
Type InterfaceType `json:"type" validate:"min=0,max=3"`
|
||||
AddressingMode InterfaceAddressingMode `json:"addressing_mode" validate:"min=0,max=2"`
|
||||
Address netip.Addr `json:"address" validate:"min=0,max=2"`
|
||||
Comment string `json:"comment,omitempty"`
|
||||
}
|
||||
|
||||
type InterfaceType int
|
||||
|
||||
const (
|
||||
Hardware InterfaceType = iota
|
||||
Vlan
|
||||
Bond
|
||||
Bridge
|
||||
)
|
||||
|
||||
func (t InterfaceType) String() string {
|
||||
return [...]string{"hardware", "vlan", "bond", "bridge"}[t]
|
||||
}
|
||||
|
||||
func (t *InterfaceType) FromString(input string) InterfaceType {
|
||||
return map[string]InterfaceType{
|
||||
"hardware": Hardware,
|
||||
"vlan": Vlan,
|
||||
"bond": Bond,
|
||||
"bridge": Bridge,
|
||||
}[input]
|
||||
}
|
||||
|
||||
func (t InterfaceType) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(t.String())
|
||||
}
|
||||
|
||||
func (t *InterfaceType) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
err := json.Unmarshal(b, &s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = t.FromString(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
type InterfaceAddressingMode int
|
||||
|
||||
const (
|
||||
None InterfaceAddressingMode = iota
|
||||
Static
|
||||
Dhcp
|
||||
)
|
||||
|
||||
func (t InterfaceAddressingMode) String() string {
|
||||
return [...]string{"none", "static", "dhcp"}[t]
|
||||
}
|
||||
|
||||
func (t *InterfaceAddressingMode) FromString(input string) InterfaceAddressingMode {
|
||||
return map[string]InterfaceAddressingMode{
|
||||
"none": None,
|
||||
"static": Static,
|
||||
"dhcp": Dhcp,
|
||||
}[input]
|
||||
}
|
||||
|
||||
func (t InterfaceAddressingMode) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(t.String())
|
||||
}
|
||||
|
||||
func (t *InterfaceAddressingMode) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
err := json.Unmarshal(b, &s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = t.FromString(s)
|
||||
return nil
|
||||
}
|
5
internal/definitions/network.go
Normal file
5
internal/definitions/network.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package definitions
|
||||
|
||||
type Network struct {
|
||||
Interfaces []Interface `json:"interfaces" validate:"required,dive"`
|
||||
}
|
47
internal/interfaces/interfaces.go
Normal file
47
internal/interfaces/interfaces.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package interfaces
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
func GenerateInterfacesFile(conf definitions.Config) (string, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
err := templates.ExecuteTemplate(buf, "interfaces.tmpl", conf)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("executing template: %w", err)
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func ApplyInterfacesFile(content string) (string, error) {
|
||||
f, err := os.Create("interfaces.conf")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("creating File: %w", err)
|
||||
}
|
||||
|
||||
_, err = f.WriteString(content + "\n")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("writing File: %w", err)
|
||||
}
|
||||
|
||||
err = f.Sync()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("syncing File: %w", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command("ifreload", "-a")
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("reloading Interfaces: %w", err)
|
||||
}
|
||||
return out.String(), nil
|
||||
}
|
19
internal/interfaces/template.go
Normal file
19
internal/interfaces/template.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package interfaces
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
//go:embed template
|
||||
var templateFS embed.FS
|
||||
var templates *template.Template
|
||||
|
||||
func init() {
|
||||
|
||||
var err error
|
||||
templates, err = template.New("").ParseFS(templateFS, "template/*.tmpl")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
0
internal/interfaces/template/interfaces.tmpl
Normal file
0
internal/interfaces/template/interfaces.tmpl
Normal file
|
@ -77,8 +77,8 @@ func GenerateAddressMatcher(allAddresses map[string]definitions.Address, match d
|
|||
sourceAddresses = append(sourceAddresses, address.Host.String())
|
||||
case definitions.Range:
|
||||
sourceAddresses = append(sourceAddresses, address.Range.String())
|
||||
case definitions.Network:
|
||||
sourceAddresses = append(sourceAddresses, address.Network.String())
|
||||
case definitions.NetworkAddress:
|
||||
sourceAddresses = append(sourceAddresses, address.NetworkAddress.String())
|
||||
default:
|
||||
panic("invalid address type")
|
||||
}
|
||||
|
@ -90,8 +90,8 @@ func GenerateAddressMatcher(allAddresses map[string]definitions.Address, match d
|
|||
destinationAddresses = append(destinationAddresses, address.Host.String())
|
||||
case definitions.Range:
|
||||
destinationAddresses = append(destinationAddresses, address.Range.String())
|
||||
case definitions.Network:
|
||||
destinationAddresses = append(destinationAddresses, address.Network.String())
|
||||
case definitions.NetworkAddress:
|
||||
destinationAddresses = append(destinationAddresses, address.NetworkAddress.String())
|
||||
default:
|
||||
panic("invalid address type")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue