mirror of
https://github.com/h44z/wg-portal.git
synced 2026-12-05 19:56:06 +01:00
feat: add support for AmneziaWG (#649)
This commit is contained in:
@@ -2033,6 +2033,12 @@
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"domain.AmneziaWGOptions": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"model.AuditEntry": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -2150,6 +2156,9 @@
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"AmneziaWG": {
|
||||
"$ref": "#/definitions/domain.AmneziaWGOptions"
|
||||
},
|
||||
"Backend": {
|
||||
"description": "the backend used for this interface e.g., local, mikrotik, ...",
|
||||
"type": "string",
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
basePath: /api/v0
|
||||
definitions:
|
||||
domain.AmneziaWGOptions:
|
||||
additionalProperties:
|
||||
type: string
|
||||
type: object
|
||||
model.AuditEntry:
|
||||
properties:
|
||||
ContextUser:
|
||||
@@ -76,6 +80,8 @@ definitions:
|
||||
items:
|
||||
type: string
|
||||
type: array
|
||||
AmneziaWG:
|
||||
$ref: '#/definitions/domain.AmneziaWGOptions'
|
||||
Backend:
|
||||
description: the backend used for this interface e.g., local, mikrotik, ...
|
||||
example: local
|
||||
|
||||
@@ -1601,6 +1601,13 @@
|
||||
"10.11.12.1/24"
|
||||
]
|
||||
},
|
||||
"AmneziaWG": {
|
||||
"description": "AmneziaWG contains per-interface obfuscation options for the amneziawg backend.\nOmitted options preserve the current kernel settings. Values use awg configuration syntax.",
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"Backend": {
|
||||
"description": "Backend is the configured backend ID used to manage this interface. Omit it on create to use the local backend, or on update to preserve the current backend.",
|
||||
"type": "string",
|
||||
|
||||
@@ -52,6 +52,13 @@ definitions:
|
||||
items:
|
||||
type: string
|
||||
type: array
|
||||
AmneziaWG:
|
||||
additionalProperties:
|
||||
type: string
|
||||
description: |-
|
||||
AmneziaWG contains per-interface obfuscation options for the amneziawg backend.
|
||||
Omitted options preserve the current kernel settings. Values use awg configuration syntax.
|
||||
type: object
|
||||
Backend:
|
||||
description: Backend is the configured backend ID used to manage this interface.
|
||||
Omit it on create to use the local backend, or on update to preserve the
|
||||
|
||||
@@ -49,6 +49,8 @@ type Interface struct {
|
||||
PeerDefPreDown string `json:"PeerDefPreDown"` // default action that is executed before the device is down
|
||||
PeerDefPostDown string `json:"PeerDefPostDown"` // default action that is executed after the device is down
|
||||
|
||||
AmneziaWG domain.AmneziaWGOptions `json:"AmneziaWG,omitempty"`
|
||||
|
||||
// Calculated values
|
||||
|
||||
EnabledPeers int `json:"EnabledPeers"`
|
||||
@@ -94,6 +96,8 @@ func NewInterface(src *domain.Interface, peers []domain.Peer) *Interface {
|
||||
PeerDefPreDown: src.PeerDefPreDown,
|
||||
PeerDefPostDown: src.PeerDefPostDown,
|
||||
|
||||
AmneziaWG: src.AmneziaWG,
|
||||
|
||||
EnabledPeers: 0,
|
||||
TotalPeers: 0,
|
||||
Filename: src.GetConfigFileName(),
|
||||
@@ -174,6 +178,7 @@ func NewDomainInterface(src *Interface) *domain.Interface {
|
||||
PeerDefPostUp: src.PeerDefPostUp,
|
||||
PeerDefPreDown: src.PeerDefPreDown,
|
||||
PeerDefPostDown: src.PeerDefPostDown,
|
||||
AmneziaWG: src.AmneziaWG,
|
||||
}
|
||||
|
||||
if src.Disabled {
|
||||
|
||||
@@ -118,6 +118,9 @@ func (s InterfaceService) Update(ctx context.Context, id domain.InterfaceIdentif
|
||||
iface.IsDynamic = existingInterface.IsDynamic
|
||||
iface.DriverType = existingInterface.DriverType
|
||||
iface.LdapAllowedUsers = existingInterface.LdapAllowedUsers
|
||||
if iface.AmneziaWG == nil {
|
||||
iface.AmneziaWG = existingInterface.AmneziaWG
|
||||
}
|
||||
|
||||
updatedInterface, updatedPeers, err := s.interfaces.UpdateInterface(ctx, iface)
|
||||
if err != nil {
|
||||
@@ -134,6 +137,7 @@ func (s InterfaceService) validateBackend(backend domain.InterfaceBackend) error
|
||||
|
||||
// Empty is the legacy representation of the local backend in domain objects.
|
||||
if id == "" || id == config.LocalBackendName ||
|
||||
(id == config.AmneziaWGBackendName && s.cfg.Backend.AmneziaWG.Enabled) ||
|
||||
slices.ContainsFunc(s.cfg.Backend.Mikrotik, func(b config.BackendMikrotik) bool { return b.Id == id }) ||
|
||||
slices.ContainsFunc(s.cfg.Backend.Pfsense, func(b config.BackendPfsense) bool { return b.Id == id }) ||
|
||||
slices.ContainsFunc(s.cfg.Backend.Opnsense, func(b config.BackendOpnsense) bool { return b.Id == id }) {
|
||||
|
||||
@@ -83,6 +83,10 @@ type Interface struct {
|
||||
// PeerDefPostDown specifies the default action that is executed after the device is down for a new peer.
|
||||
PeerDefPostDown string `json:"PeerDefPostDown"`
|
||||
|
||||
// AmneziaWG contains per-interface obfuscation options for the amneziawg backend.
|
||||
// Omitted options preserve the current kernel settings. Values use awg configuration syntax.
|
||||
AmneziaWG domain.AmneziaWGOptions `json:"AmneziaWG,omitempty" swaggertype:"object,string"`
|
||||
|
||||
// Calculated values
|
||||
|
||||
// EnabledPeers is the number of enabled peers for this interface. Only enabled peers are able to connect.
|
||||
@@ -135,6 +139,8 @@ func NewInterface(src *domain.Interface, peers []domain.Peer) *Interface {
|
||||
PeerDefPreDown: src.PeerDefPreDown,
|
||||
PeerDefPostDown: src.PeerDefPostDown,
|
||||
|
||||
AmneziaWG: src.AmneziaWG,
|
||||
|
||||
EnabledPeers: 0,
|
||||
TotalPeers: 0,
|
||||
Filename: src.GetConfigFileName(),
|
||||
@@ -204,6 +210,7 @@ func NewDomainInterface(src *Interface) *domain.Interface {
|
||||
PeerDefPostUp: src.PeerDefPostUp,
|
||||
PeerDefPreDown: src.PeerDefPreDown,
|
||||
PeerDefPostDown: src.PeerDefPostDown,
|
||||
AmneziaWG: src.AmneziaWG,
|
||||
}
|
||||
|
||||
if src.Disabled {
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package configfile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/h44z/wg-portal/internal/config"
|
||||
"github.com/h44z/wg-portal/internal/domain"
|
||||
)
|
||||
|
||||
func TestAmneziaWGConfigTemplates(t *testing.T) {
|
||||
tpl, err := newTemplateHandler()
|
||||
require.NoError(t, err)
|
||||
options := domain.AmneziaWGOptions{"H1": "100-200", "Jc": "4", "I1": "<b 0x1234>"}
|
||||
iface := &domain.Interface{Identifier: "awg0", Backend: config.AmneziaWGBackendName, AmneziaWG: options, PrivateKey: "private"}
|
||||
reader, err := tpl.GetInterfaceConfig(iface, nil)
|
||||
require.NoError(t, err)
|
||||
contents, err := io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(contents), "PrivateKey = private\nH1 = 100-200\nI1 = <b 0x1234>\nJc = 4\n")
|
||||
require.Contains(t, string(contents), "\nAddress = ")
|
||||
peer := &domain.Peer{AmneziaWG: options}
|
||||
peer.Interface.PrivateKey = "private"
|
||||
for _, style := range []string{domain.ConfigStyleWgQuick, domain.ConfigStyleRaw} {
|
||||
reader, err := tpl.GetPeerConfig(peer, style)
|
||||
require.NoError(t, err)
|
||||
contents, err := io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(contents), "PrivateKey = private\nH1 = 100-200\nI1 = <b 0x1234>\nJc = 4\n")
|
||||
require.True(t, strings.Index(string(contents), "H1 =") < strings.Index(string(contents), "[Peer]"))
|
||||
}
|
||||
peer.AmneziaWG = nil
|
||||
reader, err = tpl.GetPeerConfig(peer, domain.ConfigStyleWgQuick)
|
||||
require.NoError(t, err)
|
||||
contents, err = io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.NotContains(t, string(contents), "H1 =")
|
||||
require.Contains(t, string(contents), "PrivateKey = private\nAddress =")
|
||||
}
|
||||
|
||||
type amneziaConfigRepo struct {
|
||||
iface domain.Interface
|
||||
peer domain.Peer
|
||||
}
|
||||
|
||||
func (r *amneziaConfigRepo) GetInterface(context.Context, domain.InterfaceIdentifier) (*domain.Interface, error) {
|
||||
return &r.iface, nil
|
||||
}
|
||||
func (r *amneziaConfigRepo) GetPeer(context.Context, domain.PeerIdentifier) (*domain.Peer, error) {
|
||||
peer := r.peer
|
||||
return &peer, nil
|
||||
}
|
||||
func (r *amneziaConfigRepo) GetInterfaceAndPeers(context.Context, domain.InterfaceIdentifier) (*domain.Interface, []domain.Peer, error) {
|
||||
return &r.iface, []domain.Peer{r.peer}, nil
|
||||
}
|
||||
|
||||
func TestAmneziaWGDownloadsAndQRUseParentOptions(t *testing.T) {
|
||||
tpl, err := newTemplateHandler()
|
||||
require.NoError(t, err)
|
||||
repo := &amneziaConfigRepo{
|
||||
iface: domain.Interface{Identifier: "awg0", Backend: config.AmneziaWGBackendName, AmneziaWG: domain.AmneziaWGOptions{"H1": "100-200", "Jc": "4"}},
|
||||
peer: domain.Peer{Identifier: "peer", InterfaceIdentifier: "awg0"},
|
||||
}
|
||||
manager := Manager{wg: repo, tplHandler: tpl}
|
||||
ctx := domain.SetUserInfo(context.Background(), domain.SystemAdminContextUserInfo())
|
||||
reader, err := manager.GetPeerConfig(ctx, "peer", domain.ConfigStyleWgQuick)
|
||||
require.NoError(t, err)
|
||||
contents, err := io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(contents), "H1 = 100-200")
|
||||
reader, err = manager.GetPeerConfigQrCode(ctx, "peer", domain.ConfigStyleWgQuick)
|
||||
require.NoError(t, err)
|
||||
contents, err = io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, contents)
|
||||
// Changing parent options affects the next download without editing peers.
|
||||
repo.iface.AmneziaWG["H1"] = "300-400"
|
||||
reader, err = manager.GetPeerConfig(ctx, "peer", domain.ConfigStyleWgQuick)
|
||||
require.NoError(t, err)
|
||||
contents, err = io.ReadAll(reader)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(contents), "H1 = 300-400")
|
||||
}
|
||||
@@ -195,6 +195,9 @@ func (m Manager) GetPeerConfig(ctx context.Context, id domain.PeerIdentifier, st
|
||||
if err := domain.ValidateUserAccessRights(ctx, peer.UserIdentifier); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.loadAmneziaWGOptions(ctx, peer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m.tplHandler.GetPeerConfig(peer, style)
|
||||
}
|
||||
@@ -209,6 +212,9 @@ func (m Manager) GetPeerConfigQrCode(ctx context.Context, id domain.PeerIdentifi
|
||||
if err := domain.ValidateUserAccessRights(ctx, peer.UserIdentifier); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.loadAmneziaWGOptions(ctx, peer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfgData, err := m.tplHandler.GetPeerConfig(peer, style)
|
||||
if err != nil {
|
||||
@@ -250,6 +256,17 @@ func (m Manager) GetPeerConfigQrCode(ctx context.Context, id domain.PeerIdentifi
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (m Manager) loadAmneziaWGOptions(ctx context.Context, peer *domain.Peer) error {
|
||||
iface, err := m.wg.GetInterface(ctx, peer.InterfaceIdentifier)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load parent interface: %w", err)
|
||||
}
|
||||
if iface.Backend == config.AmneziaWGBackendName {
|
||||
peer.Interface.AmneziaWG = iface.AmneziaWG
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PersistInterfaceConfig writes the configuration file for the given interface to the file system.
|
||||
func (m Manager) PersistInterfaceConfig(ctx context.Context, id domain.InterfaceIdentifier) error {
|
||||
iface, peers, err := m.wg.GetInterfaceAndPeers(ctx, id)
|
||||
|
||||
@@ -38,6 +38,9 @@ func newTemplateHandler() (*TemplateHandler, error) {
|
||||
|
||||
// GetInterfaceConfig returns the rendered configuration file for a WireGuard interface.
|
||||
func (c TemplateHandler) GetInterfaceConfig(cfg *domain.Interface, peers []domain.Peer) (io.Reader, error) {
|
||||
if err := cfg.AmneziaWG.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tplBuff bytes.Buffer
|
||||
|
||||
err := c.templates.ExecuteTemplate(&tplBuff, "wg_interface.tpl", map[string]any{
|
||||
@@ -56,6 +59,9 @@ func (c TemplateHandler) GetInterfaceConfig(cfg *domain.Interface, peers []domai
|
||||
|
||||
// GetPeerConfig returns the rendered configuration file for a WireGuard peer.
|
||||
func (c TemplateHandler) GetPeerConfig(peer *domain.Peer, style string) (io.Reader, error) {
|
||||
if err := peer.Interface.AmneziaWG.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tplBuff bytes.Buffer
|
||||
|
||||
err := c.templates.ExecuteTemplate(&tplBuff, "wg_peer.tpl", map[string]any{
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
|
||||
# Core settings
|
||||
PrivateKey = {{ .Interface.KeyPair.PrivateKey }}
|
||||
{{- if .Interface.AmneziaWG }}
|
||||
{{ .Interface.AmneziaWG.ConfigLines -}}
|
||||
{{- end }}
|
||||
Address = {{ CidrsToString .Interface.Addresses }}
|
||||
|
||||
# Misc. settings (optional)
|
||||
|
||||
@@ -23,6 +23,9 @@
|
||||
|
||||
# Core settings
|
||||
PrivateKey = {{ .Peer.Interface.KeyPair.PrivateKey }}
|
||||
{{- if .Peer.Interface.AmneziaWG }}
|
||||
{{ .Peer.Interface.AmneziaWG.ConfigLines -}}
|
||||
{{- end }}
|
||||
{{- if eq .Style "wgquick"}}
|
||||
Address = {{ CidrsToString .Peer.Interface.Addresses }}
|
||||
{{- end}}
|
||||
|
||||
@@ -40,6 +40,10 @@ func (c *ControllerManager) init() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.registerAmneziaWGController(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.registerMikrotikControllers(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -136,6 +140,26 @@ func (c *ControllerManager) registerOpnsenseControllers() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ControllerManager) registerAmneziaWGController() error {
|
||||
if !c.cfg.Backend.AmneziaWG.Enabled {
|
||||
return nil
|
||||
}
|
||||
controller, err := wgcontroller.NewAmneziaWGController(c.cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create AmneziaWG controller: %w", err)
|
||||
}
|
||||
c.controllers[config.AmneziaWGBackendName] = backendInstance{
|
||||
Config: config.BackendBase{
|
||||
Id: config.AmneziaWGBackendName,
|
||||
DisplayName: "AmneziaWG",
|
||||
Type: "amneziawg",
|
||||
IgnoredInterfaces: c.cfg.Backend.AmneziaWG.IgnoredInterfaces,
|
||||
},
|
||||
Implementation: controller,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ControllerManager) logRegisteredControllers() {
|
||||
for backend, controller := range c.controllers {
|
||||
slog.Debug("backend controller registered",
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
|
||||
"github.com/h44z/wg-portal/internal/app"
|
||||
"github.com/h44z/wg-portal/internal/app/audit"
|
||||
"github.com/h44z/wg-portal/internal/config"
|
||||
"github.com/h44z/wg-portal/internal/domain"
|
||||
)
|
||||
|
||||
@@ -284,7 +283,7 @@ func (m Manager) RestoreInterfaceState(
|
||||
// restore peers
|
||||
for _, peer := range peers {
|
||||
switch {
|
||||
case iface.IsDisabled() && iface.Backend == config.LocalBackendName: // if interface is disabled, delete all peers
|
||||
case iface.IsDisabled() && iface.Backend.IsLocal(): // if interface is disabled, delete all peers
|
||||
if err := controller.DeletePeer(ctx, iface.Identifier,
|
||||
peer.Identifier); err != nil {
|
||||
return fmt.Errorf("failed to remove peer %s for disabled interface %s: %w",
|
||||
@@ -380,6 +379,7 @@ func (m Manager) PrepareInterface(ctx context.Context) (*domain.Interface, error
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
Identifier: id,
|
||||
Backend: domain.InterfaceBackend(m.cfg.Backend.Default),
|
||||
KeyPair: kp,
|
||||
ListenPort: port,
|
||||
Addresses: ips,
|
||||
@@ -563,6 +563,9 @@ func (m Manager) saveInterface(ctx context.Context, iface *domain.Interface) (
|
||||
err := m.wg.GetController(*iface).SaveInterface(ctx, iface.Identifier,
|
||||
func(pi *domain.PhysicalInterface) (*domain.PhysicalInterface, error) {
|
||||
domain.MergeToPhysicalInterface(pi, iface)
|
||||
if extras, ok := pi.GetExtras().(domain.AmneziaWGInterfaceExtras); ok {
|
||||
iface.AmneziaWG = extras.Options
|
||||
}
|
||||
return pi, nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -633,7 +636,7 @@ func (m Manager) saveInterface(ctx context.Context, iface *domain.Interface) (
|
||||
}
|
||||
|
||||
// If the interface has just been enabled, restore its peers on the physical controller
|
||||
if !oldEnabled && newEnabled && iface.Backend == config.LocalBackendName {
|
||||
if !oldEnabled && newEnabled && iface.Backend.IsLocal() {
|
||||
peers, err := m.db.GetInterfacePeers(ctx, iface.Identifier)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load peers for interface %s: %w", iface.Identifier, err)
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package wireguard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/h44z/wg-portal/internal/config"
|
||||
"github.com/h44z/wg-portal/internal/domain"
|
||||
)
|
||||
|
||||
type amneziaWGExtrasController struct {
|
||||
mockController
|
||||
physical domain.PhysicalInterface
|
||||
}
|
||||
|
||||
func (c *amneziaWGExtrasController) SaveInterface(_ context.Context, _ domain.InterfaceIdentifier,
|
||||
update func(*domain.PhysicalInterface) (*domain.PhysicalInterface, error),
|
||||
) error {
|
||||
physical, err := update(&c.physical)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.physical = *physical
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestSaveInterfacePersistsAmneziaWGExtras(t *testing.T) {
|
||||
for name, options := range map[string]domain.AmneziaWGOptions{
|
||||
"omitted options": nil,
|
||||
"partial update": {"Jc": "6"},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
controller := &amneziaWGExtrasController{physical: domain.PhysicalInterface{
|
||||
Identifier: "awg0", ImportSource: domain.ControllerTypeAmneziaWG,
|
||||
}}
|
||||
controller.physical.SetExtras(domain.AmneziaWGInterfaceExtras{
|
||||
Options: domain.AmneziaWGOptions{"H1": "100-200", "Jc": "4"},
|
||||
})
|
||||
db := &mockDB{iface: &domain.Interface{
|
||||
Identifier: "awg0", Backend: config.AmneziaWGBackendName, Type: domain.InterfaceTypeServer,
|
||||
}}
|
||||
manager := Manager{
|
||||
cfg: &config.Config{}, db: db, bus: &mockBus{},
|
||||
wg: &ControllerManager{controllers: map[domain.InterfaceBackend]backendInstance{
|
||||
config.AmneziaWGBackendName: {Implementation: controller},
|
||||
}},
|
||||
}
|
||||
update := *db.iface
|
||||
update.AmneziaWG = options
|
||||
ctx := domain.SetUserInfo(context.Background(), domain.SystemAdminContextUserInfo())
|
||||
saved, err := manager.saveInterface(ctx, &update)
|
||||
require.NoError(t, err)
|
||||
expected := domain.AmneziaWGOptions{"H1": "100-200", "Jc": "4"}
|
||||
if options != nil {
|
||||
expected["Jc"] = "6"
|
||||
}
|
||||
require.Equal(t, expected, saved.AmneziaWG)
|
||||
require.Equal(t, expected, db.iface.AmneziaWG)
|
||||
require.Equal(t, domain.AmneziaWGInterfaceExtras{Options: expected}, controller.physical.GetExtras())
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user