mirror of
https://github.com/h44z/wg-portal.git
synced 2026-12-05 11:46:09 +01:00
112 lines
3.1 KiB
Go
112 lines
3.1 KiB
Go
package wgcontroller
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"os"
|
|
|
|
"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 amneziaWGRepo interface {
|
|
WgCtrlRepo
|
|
ValidateOptions(domain.AmneziaWGOptions) error
|
|
ConfigureAmneziaWGDevice(string, wgtypes.Config, domain.AmneziaWGOptions) error
|
|
}
|
|
|
|
// AmneziaWGController shares peer management, hooks, DNS and routing with the local controller.
|
|
type AmneziaWGController struct {
|
|
*LocalController
|
|
amneziaWGRepo
|
|
}
|
|
|
|
func NewAmneziaWGController(cfg *config.Config) (*AmneziaWGController, error) {
|
|
awg, err := lowlevel.NewAmneziaWGClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return newAmneziaWGController(cfg, awg, &lowlevel.NetlinkManager{}), nil
|
|
}
|
|
|
|
func newAmneziaWGController(cfg *config.Config, awg amneziaWGRepo, nl NetlinkClient) *AmneziaWGController {
|
|
return &AmneziaWGController{
|
|
LocalController: newLocalController(cfg, awg, nl, domain.ControllerTypeAmneziaWG),
|
|
amneziaWGRepo: awg,
|
|
}
|
|
}
|
|
|
|
func (c AmneziaWGController) GetId() domain.InterfaceBackend {
|
|
return config.AmneziaWGBackendName
|
|
}
|
|
|
|
func (c AmneziaWGController) SaveInterface(ctx context.Context, id domain.InterfaceIdentifier,
|
|
updateFunc func(*domain.PhysicalInterface) (*domain.PhysicalInterface, error),
|
|
) (err error) {
|
|
iface, err := c.GetInterface(ctx, id)
|
|
create := errors.Is(err, os.ErrNotExist)
|
|
if create {
|
|
iface = &domain.PhysicalInterface{Identifier: id, ImportSource: domain.ControllerTypeAmneziaWG,
|
|
DeviceType: wgtypes.LinuxKernel.String(), PrivateKey: (wgtypes.Key{}).String()}
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
if updateFunc != nil {
|
|
iface, err = updateFunc(iface)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
extras, _ := iface.GetExtras().(domain.AmneziaWGInterfaceExtras)
|
|
if err := c.ValidateOptions(extras.Options); err != nil {
|
|
return err
|
|
}
|
|
cfg, err := kernelInterfaceConfig(iface)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if create {
|
|
link := &netlink.GenericLink{Name: string(id), LinkType: "amneziawg"}
|
|
if err := c.nl.LinkAdd(link); err != nil {
|
|
return fmt.Errorf("AmneziaWG link add failed: %w", err)
|
|
}
|
|
defer func() {
|
|
if err != nil && link.Index != 0 {
|
|
err = errors.Join(err, c.nl.LinkDel(link))
|
|
}
|
|
}()
|
|
}
|
|
if err := c.ConfigureAmneziaWGDevice(string(id), cfg, extras.Options); err != nil {
|
|
return err
|
|
}
|
|
if create && extras.Options != nil {
|
|
// New links can get kernel defaults that clients also need in their exports.
|
|
device, err := c.Device(string(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
maps.Copy(extras.Options, device.AmneziaWG)
|
|
}
|
|
return c.updateLowLevelInterface(iface)
|
|
}
|
|
|
|
func (c AmneziaWGController) DeleteInterface(_ context.Context, id domain.InterfaceIdentifier) error {
|
|
link, err := c.nl.LinkByName(string(id))
|
|
if err != nil {
|
|
if _, ok := errors.AsType[netlink.LinkNotFoundError](err); ok {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
if link.Type() != "amneziawg" {
|
|
return fmt.Errorf("interface %s is not an AmneziaWG kernel device", id)
|
|
}
|
|
return c.nl.LinkDel(link)
|
|
}
|