From 06c32cb336c19793037cfe6bc3ac698ca17689fa Mon Sep 17 00:00:00 2001 From: Marvin Steadfast Date: Thu, 21 Jan 2021 15:33:34 +0100 Subject: [PATCH] wg-quicker can be enforced to use wireguard-go --- cmd/root.go | 4 ++-- wgquick/wg.go | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index c4834df..366f1fe 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -39,7 +39,7 @@ var upCmd = &cobra.Command{ Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { c, log := loadConfig(args[0]) - if err := wgquick.Up(c, iface, log); err != nil { + if err := wgquick.Up(c, iface, wgo, log); err != nil { logrus.WithError(err).Errorln("cannot up interface") } }, @@ -63,7 +63,7 @@ var syncCmd = &cobra.Command{ Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { c, log := loadConfig(args[0]) - if err := wgquick.Sync(c, iface, log); err != nil { + if err := wgquick.Sync(c, iface, wgo, log); err != nil { logrus.WithError(err).Errorln("cannot sync interface") } }, diff --git a/wgquick/wg.go b/wgquick/wg.go index 33bf528..8c8bbf4 100644 --- a/wgquick/wg.go +++ b/wgquick/wg.go @@ -41,7 +41,7 @@ func wgGo(iface string) error { } // Up sets and configures the wg interface. Mostly equivalent to `wg-quick up iface`. -func Up(cfg *Config, iface string, logger logrus.FieldLogger) error { +func Up(cfg *Config, iface string, wgo bool, logger logrus.FieldLogger) error { log := logger.WithField("iface", iface) _, err := netlink.LinkByName(iface) @@ -67,7 +67,7 @@ func Up(cfg *Config, iface string, logger logrus.FieldLogger) error { log.Infoln("applied pre-up command") } - if err := Sync(cfg, iface, logger); err != nil { + if err := Sync(cfg, iface, wgo, logger); err != nil { return err } @@ -156,10 +156,10 @@ func execSh(command string, iface string, log logrus.FieldLogger, stdin ...strin // * SyncWireguardDevice --> configures allowedIP & other wireguard specific settings. // * SyncAddress --> synces linux addresses bounded to this interface. // * SyncRoutes --> synces all allowedIP routes to route to this interface. -func Sync(cfg *Config, iface string, logger logrus.FieldLogger) error { +func Sync(cfg *Config, iface string, wgo bool, logger logrus.FieldLogger) error { log := logger.WithField("iface", iface) - link, err := SyncLink(cfg, iface, log) + link, err := SyncLink(cfg, iface, wgo, log) if err != nil { log.WithError(err).Errorln("cannot sync wireguard link") @@ -223,7 +223,7 @@ func SyncWireguardDevice(cfg *Config, link netlink.Link, log logrus.FieldLogger) // SyncLink synces link state with the config. // It does not sync Wireguard settings, just makes sure the device is up and type wireguard. -func SyncLink(cfg *Config, iface string, log logrus.FieldLogger) (netlink.Link, error) { +func SyncLink(cfg *Config, iface string, wgo bool, log logrus.FieldLogger) (netlink.Link, error) { link, err := netlink.LinkByName(iface) // nolint: nestif if err != nil { @@ -242,15 +242,26 @@ func SyncLink(cfg *Config, iface string, log logrus.FieldLogger) (netlink.Link, }, LinkType: "wireguard", } - if err := netlink.LinkAdd(wgLink); err != nil { - log.WithError(err).Errorf("cannot create link: %s", err.Error()) - log.Info("trying to use embedded wireguard-go...") + + if wgo { + log.Info("enforcing embedded wireguard-go") if err := wgGo(iface); err != nil { log.WithError(err).Errorf("cannot create link through wireguard-go: %s", err.Error()) return nil, fmt.Errorf("cannot create link: %w", err) } + } else if !wgo { + if err := netlink.LinkAdd(wgLink); err != nil { + log.WithError(err).Errorf("cannot create link: %s", err.Error()) + log.Info("trying to use embedded wireguard-go") + + if err := wgGo(iface); err != nil { + log.WithError(err).Errorf("cannot create link through wireguard-go: %s", err.Error()) + + return nil, fmt.Errorf("cannot create link: %w", err) + } + } } // Needs some sleeping to wait for interface creating.