mirror of
https://github.com/h44z/wg-portal.git
synced 2026-12-05 11:46:09 +01:00
161 lines
5.9 KiB
Go
161 lines
5.9 KiB
Go
package wgcontroller
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"maps"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/vishvananda/netlink"
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"github.com/h44z/wg-portal/internal/config"
|
|
"github.com/h44z/wg-portal/internal/domain"
|
|
"github.com/h44z/wg-portal/internal/lowlevel"
|
|
)
|
|
|
|
type awgControllerNetlink struct {
|
|
NetlinkClient
|
|
link netlink.Link
|
|
deleted bool
|
|
upErr error
|
|
}
|
|
|
|
func (n *awgControllerNetlink) LinkAdd(link netlink.Link) error {
|
|
link.Attrs().Index = 1
|
|
n.link = link
|
|
return nil
|
|
}
|
|
func (n *awgControllerNetlink) LinkByName(string) (netlink.Link, error) {
|
|
if n.link == nil {
|
|
return nil, netlink.LinkNotFoundError{}
|
|
}
|
|
return n.link, nil
|
|
}
|
|
func (n *awgControllerNetlink) LinkDel(netlink.Link) error {
|
|
n.deleted = true
|
|
n.link = nil
|
|
return nil
|
|
}
|
|
func (n *awgControllerNetlink) AddrList(netlink.Link) ([]netlink.Addr, error) { return nil, nil }
|
|
func (n *awgControllerNetlink) LinkSetUp(netlink.Link) error { return n.upErr }
|
|
func (n *awgControllerNetlink) LinkSetDown(netlink.Link) error { return nil }
|
|
|
|
type awgControllerClient struct {
|
|
nl *awgControllerNetlink
|
|
options domain.AmneziaWGOptions
|
|
validationErr error
|
|
reads int
|
|
deleted bool
|
|
peer *wgtypes.Peer
|
|
}
|
|
|
|
func (w *awgControllerClient) Devices() ([]lowlevel.WireGuardDevice, error) {
|
|
device, err := w.Device("awg0")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []lowlevel.WireGuardDevice{device}, nil
|
|
}
|
|
|
|
func (w *awgControllerClient) Device(name string) (lowlevel.WireGuardDevice, error) {
|
|
w.reads++
|
|
if w.nl.link == nil {
|
|
return lowlevel.WireGuardDevice{}, os.ErrNotExist
|
|
}
|
|
device := &wgtypes.Device{Name: name, Type: wgtypes.LinuxKernel}
|
|
if w.peer != nil {
|
|
device.Peers = []wgtypes.Peer{*w.peer}
|
|
}
|
|
return lowlevel.WireGuardDevice{Device: device, AmneziaWG: maps.Clone(w.options)}, nil
|
|
}
|
|
func (w *awgControllerClient) ValidateOptions(domain.AmneziaWGOptions) error { return w.validationErr }
|
|
func (w *awgControllerClient) ConfigureAmneziaWGDevice(_ string, _ wgtypes.Config, options domain.AmneziaWGOptions) error {
|
|
maps.Copy(w.options, options)
|
|
return nil
|
|
}
|
|
func (w *awgControllerClient) ConfigureDevice(_ string, cfg wgtypes.Config) error {
|
|
for _, peer := range cfg.Peers {
|
|
if peer.Remove {
|
|
w.deleted = true
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestAmneziaWGControllerCreatesCorrectLinkAndPreservesOptions(t *testing.T) {
|
|
nl := &awgControllerNetlink{}
|
|
wg := &awgControllerClient{nl: nl, options: domain.AmneziaWGOptions{"H1": "100-200", "Jc": "4"}}
|
|
controller := newAmneziaWGController(&config.Config{}, wg, nl)
|
|
key, err := wgtypes.GeneratePrivateKey()
|
|
require.NoError(t, err)
|
|
update := &domain.Interface{Identifier: "awg0", PrivateKey: key.String(), AmneziaWG: domain.AmneziaWGOptions{"Jc": "6"}}
|
|
save := func() error {
|
|
return controller.SaveInterface(t.Context(), update.Identifier, func(pi *domain.PhysicalInterface) (*domain.PhysicalInterface, error) {
|
|
domain.MergeToPhysicalInterface(pi, update)
|
|
update.AmneziaWG = pi.GetExtras().(domain.AmneziaWGInterfaceExtras).Options
|
|
return pi, nil
|
|
})
|
|
}
|
|
wg.validationErr = errors.New("unsupported option")
|
|
require.ErrorIs(t, save(), wg.validationErr)
|
|
require.Nil(t, nl.link)
|
|
wg.validationErr = nil
|
|
nl.upErr = errors.New("link setup failed")
|
|
require.ErrorIs(t, save(), nl.upErr)
|
|
require.Nil(t, nl.link)
|
|
nl.upErr = nil
|
|
require.NoError(t, save())
|
|
require.Equal(t, "amneziawg", nl.link.Type())
|
|
require.Equal(t, domain.InterfaceBackend(config.AmneziaWGBackendName), controller.GetId())
|
|
require.Equal(t, domain.AmneziaWGOptions{"H1": "100-200", "Jc": "6"}, update.AmneziaWG)
|
|
wg.reads = 0
|
|
physical, err := controller.GetInterface(context.Background(), "awg0")
|
|
require.NoError(t, err)
|
|
require.Equal(t, domain.ControllerTypeAmneziaWG, physical.ImportSource)
|
|
require.Equal(t, domain.AmneziaWGInterfaceExtras{Options: wg.options}, physical.GetExtras())
|
|
require.Equal(t, wg.options, domain.ConvertPhysicalInterface(physical).AmneziaWG)
|
|
interfaces, err := controller.GetInterfaces(context.Background())
|
|
require.NoError(t, err)
|
|
require.Len(t, interfaces, 1)
|
|
require.Equal(t, *physical, interfaces[0])
|
|
require.Equal(t, 2, wg.reads, "one device dump for GetInterface and one for GetInterfaces")
|
|
}
|
|
|
|
func TestAmneziaWGDisabledPeersUseLocalDeletion(t *testing.T) {
|
|
key, err := wgtypes.GeneratePrivateKey()
|
|
require.NoError(t, err)
|
|
public := key.PublicKey()
|
|
nl := &awgControllerNetlink{link: &netlink.GenericLink{Name: "awg0", LinkType: "amneziawg"}}
|
|
wg := &awgControllerClient{nl: nl, peer: &wgtypes.Peer{PublicKey: public}}
|
|
controller := newAmneziaWGController(&config.Config{}, wg, nl)
|
|
id := domain.PeerIdentifier(public.String())
|
|
peers, err := controller.GetPeers(context.Background(), "awg0")
|
|
require.NoError(t, err)
|
|
require.Len(t, peers, 1)
|
|
require.Equal(t, domain.ControllerTypeAmneziaWG, peers[0].ImportSource)
|
|
require.False(t, domain.ConvertPhysicalPeer(&peers[0]).IsDisabled())
|
|
disabled := time.Now()
|
|
require.NoError(t, controller.SavePeer(context.Background(), "awg0", id, func(peer *domain.PhysicalPeer) (*domain.PhysicalPeer, error) {
|
|
require.Equal(t, domain.ControllerTypeAmneziaWG, peer.ImportSource)
|
|
domain.MergeToPhysicalPeer(peer, &domain.Peer{Identifier: id, Disabled: &disabled})
|
|
return peer, nil
|
|
}))
|
|
require.True(t, wg.deleted)
|
|
}
|
|
|
|
func TestAmneziaWGControllerDeletesOnlyAmneziaWGLinks(t *testing.T) {
|
|
nl := &awgControllerNetlink{}
|
|
controller := newAmneziaWGController(&config.Config{}, &awgControllerClient{nl: nl}, nl)
|
|
require.NoError(t, controller.DeleteInterface(context.Background(), "missing"))
|
|
nl.link = &netlink.GenericLink{Name: "wg0", LinkType: "wireguard"}
|
|
require.ErrorContains(t, controller.DeleteInterface(context.Background(), "wg0"), "not an AmneziaWG kernel device")
|
|
require.False(t, nl.deleted)
|
|
nl.link = &netlink.GenericLink{Name: "awg0", LinkType: "amneziawg"}
|
|
require.NoError(t, controller.DeleteInterface(context.Background(), "awg0"))
|
|
require.True(t, nl.deleted)
|
|
}
|