This commit is contained in:
Christoph
2026-12-03 14:56:54 +01:00
parent 10507d54c3
commit d5a74a10ac
18 changed files with 344 additions and 436 deletions
+45 -19
View File
@@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
@@ -29,12 +28,30 @@ import (
// WgCtrlRepo is used to control local WireGuard devices via the wgctrl-go library.
type WgCtrlRepo interface {
io.Closer
Devices() ([]*wgtypes.Device, error)
Device(name string) (*wgtypes.Device, error)
Devices() ([]lowlevel.WireGuardDevice, error)
Device(name string) (lowlevel.WireGuardDevice, error)
ConfigureDevice(name string, cfg wgtypes.Config) error
}
type wireGuardClient struct{ *wgctrl.Client }
func (c wireGuardClient) Device(name string) (lowlevel.WireGuardDevice, error) {
device, err := c.Client.Device(name)
return lowlevel.WireGuardDevice{Device: device}, err
}
func (c wireGuardClient) Devices() ([]lowlevel.WireGuardDevice, error) {
devices, err := c.Client.Devices()
if err != nil {
return nil, err
}
result := make([]lowlevel.WireGuardDevice, len(devices))
for i, device := range devices {
result[i].Device = device
}
return result, nil
}
// A NetlinkClient is a type which can control a netlink device.
type NetlinkClient interface {
LinkAdd(link netlink.Link) error
@@ -65,6 +82,8 @@ type LocalController struct {
wg WgCtrlRepo
nl NetlinkClient
importSource string
shellCmd string
resolvConfIfacePrefix string
}
@@ -77,19 +96,20 @@ func NewLocalController(cfg *config.Config) (*LocalController, error) {
return nil, fmt.Errorf("failed to create wgctrl client: %w", err)
}
nl := &lowlevel.NetlinkManager{}
return newLocalController(cfg, wireGuardClient{wg}, &lowlevel.NetlinkManager{}, domain.ControllerTypeLocal), nil
}
repo := &LocalController{
func newLocalController(cfg *config.Config, wg WgCtrlRepo, nl NetlinkClient, source string) *LocalController {
return &LocalController{
cfg: cfg,
wg: wg,
nl: nl,
importSource: source,
shellCmd: "bash", // we only support bash at the moment
resolvConfIfacePrefix: cfg.Backend.LocalResolvconfPrefix, // WireGuard interfaces have a tun. prefix in resolvconf
}
return repo, nil
}
func (c LocalController) GetId() domain.InterfaceBackend {
@@ -123,7 +143,7 @@ func (c LocalController) GetInterface(_ context.Context, id domain.InterfaceIden
return c.getInterface(id)
}
func (c LocalController) convertWireGuardInterface(device *wgtypes.Device) (domain.PhysicalInterface, error) {
func (c LocalController) convertWireGuardInterface(device lowlevel.WireGuardDevice) (domain.PhysicalInterface, error) {
// read data from wgctrl interface
iface := domain.PhysicalInterface{
@@ -135,7 +155,7 @@ func (c LocalController) convertWireGuardInterface(device *wgtypes.Device) (doma
Mtu: 0,
FirewallMark: uint32(device.FirewallMark),
DeviceUp: false,
ImportSource: domain.ControllerTypeLocal,
ImportSource: c.importSource,
DeviceType: device.Type.String(),
BytesUpload: 0,
BytesDownload: 0,
@@ -162,6 +182,9 @@ func (c LocalController) convertWireGuardInterface(device *wgtypes.Device) (doma
iface.BytesDownload = stats.RxBytes
}
if c.importSource == domain.ControllerTypeAmneziaWG {
iface.SetExtras(domain.AmneziaWGInterfaceExtras{Options: device.AmneziaWG})
}
return iface, nil
}
@@ -198,7 +221,7 @@ func (c LocalController) convertWireGuardPeer(peer *wgtypes.Peer) (domain.Physic
ProtocolVersion: peer.ProtocolVersion,
BytesUpload: uint64(peer.ReceiveBytes),
BytesDownload: uint64(peer.TransmitBytes),
ImportSource: domain.ControllerTypeLocal,
ImportSource: c.importSource,
}
// Set local extras - local peers are never disabled in the kernel
@@ -338,10 +361,10 @@ func (c LocalController) updateLowLevelInterface(pi *domain.PhysicalInterface) e
return nil
}
func (c LocalController) updateWireGuardInterface(pi *domain.PhysicalInterface) error {
func kernelInterfaceConfig(pi *domain.PhysicalInterface) (wgtypes.Config, error) {
pKey, err := wgtypes.NewKey(pi.KeyPair.GetPrivateKeyBytes())
if err != nil {
return err
return wgtypes.Config{}, err
}
var fwMark *int
@@ -349,17 +372,19 @@ func (c LocalController) updateWireGuardInterface(pi *domain.PhysicalInterface)
intFwMark := int(pi.FirewallMark)
fwMark = &intFwMark
}
err = c.wg.ConfigureDevice(string(pi.Identifier), wgtypes.Config{
return wgtypes.Config{
PrivateKey: &pKey,
ListenPort: &pi.ListenPort,
FirewallMark: fwMark,
ReplacePeers: false,
})
}, nil
}
func (c LocalController) updateWireGuardInterface(pi *domain.PhysicalInterface) error {
cfg, err := kernelInterfaceConfig(pi)
if err != nil {
return err
}
return nil
return c.wg.ConfigureDevice(string(pi.Identifier), cfg)
}
func (c LocalController) DeleteInterface(_ context.Context, id domain.InterfaceIdentifier) error {
@@ -862,7 +887,8 @@ func (c LocalController) RemoveRoutes(_ context.Context, info domain.RoutingTabl
slog.Debug("removing linux routes", "interface", interfaceId, "table", info.Table, "fwMark", info.FwMark,
"cidrs", info.AllowedIps)
wgDev, err := c.wg.Device(string(interfaceId))
device, err := c.wg.Device(string(interfaceId))
wgDev := device.Device
if err != nil {
slog.Debug("wg device already removed, route cleanup might be incomplete", "interface", interfaceId)
wgDev = nil