wg-quicker can be enforced to use wireguard-go
Some checks reported errors
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build was killed

This commit is contained in:
Marvin Steadfast 2021-01-21 15:33:34 +01:00
parent 72253bd890
commit 06c32cb336
2 changed files with 21 additions and 10 deletions

View File

@ -39,7 +39,7 @@ var upCmd = &cobra.Command{
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
c, log := loadConfig(args[0]) 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") logrus.WithError(err).Errorln("cannot up interface")
} }
}, },
@ -63,7 +63,7 @@ var syncCmd = &cobra.Command{
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
c, log := loadConfig(args[0]) 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") logrus.WithError(err).Errorln("cannot sync interface")
} }
}, },

View File

@ -41,7 +41,7 @@ func wgGo(iface string) error {
} }
// Up sets and configures the wg interface. Mostly equivalent to `wg-quick up iface`. // 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) log := logger.WithField("iface", iface)
_, err := netlink.LinkByName(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") 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 return err
} }
@ -156,10 +156,10 @@ func execSh(command string, iface string, log logrus.FieldLogger, stdin ...strin
// * SyncWireguardDevice --> configures allowedIP & other wireguard specific settings. // * SyncWireguardDevice --> configures allowedIP & other wireguard specific settings.
// * SyncAddress --> synces linux addresses bounded to this interface. // * SyncAddress --> synces linux addresses bounded to this interface.
// * SyncRoutes --> synces all allowedIP routes to route 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) log := logger.WithField("iface", iface)
link, err := SyncLink(cfg, iface, log) link, err := SyncLink(cfg, iface, wgo, log)
if err != nil { if err != nil {
log.WithError(err).Errorln("cannot sync wireguard link") 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. // SyncLink synces link state with the config.
// It does not sync Wireguard settings, just makes sure the device is up and type wireguard. // 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) link, err := netlink.LinkByName(iface)
// nolint: nestif // nolint: nestif
if err != nil { if err != nil {
@ -242,15 +242,26 @@ func SyncLink(cfg *Config, iface string, log logrus.FieldLogger) (netlink.Link,
}, },
LinkType: "wireguard", LinkType: "wireguard",
} }
if err := netlink.LinkAdd(wgLink); err != nil {
log.WithError(err).Errorf("cannot create link: %s", err.Error()) if wgo {
log.Info("trying to use embedded wireguard-go...") log.Info("enforcing embedded wireguard-go")
if err := wgGo(iface); err != nil { if err := wgGo(iface); err != nil {
log.WithError(err).Errorf("cannot create link through wireguard-go: %s", err.Error()) log.WithError(err).Errorf("cannot create link through wireguard-go: %s", err.Error())
return nil, fmt.Errorf("cannot create link: %w", err) 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. // Needs some sleeping to wait for interface creating.