mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-09 02:30:49 +08:00
routes for different OSs should be handled, cleaned up apply conf
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
|||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TODO handle ipv6 in future
|
||||||
|
|
||||||
// SetPeerRoutes - sets/removes ip routes for each peer on a network
|
// SetPeerRoutes - sets/removes ip routes for each peer on a network
|
||||||
func SetPeerRoutes(iface, currentNodeAddr string, oldPeers map[string][]net.IPNet, newPeers []wgtypes.PeerConfig) {
|
func SetPeerRoutes(iface, currentNodeAddr string, oldPeers map[string][]net.IPNet, newPeers []wgtypes.PeerConfig) {
|
||||||
// traverse through all recieved peers
|
// traverse through all recieved peers
|
||||||
@@ -55,3 +57,8 @@ func SetCurrentPeerRoutes(iface, currentAddr string, peers []wgtypes.Peer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetCIDRRoute - sets the CIDR route, used on join and restarts
|
||||||
|
func SetCIDRRoute(iface, currentAddr string, cidr *net.IPNet) {
|
||||||
|
setCidr(iface, currentAddr, cidr)
|
||||||
|
}
|
||||||
|
@@ -2,6 +2,7 @@ package local
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||||
)
|
)
|
||||||
@@ -30,3 +31,7 @@ func deleteRoute(iface string, addr *net.IPNet, address string) error {
|
|||||||
_, err = ncutils.RunCmd("route -q -n delete "+addr.String(), true)
|
_, err = ncutils.RunCmd("route -q -n delete "+addr.String(), true)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setCidr(iface, address string, addr *net.IPNet) {
|
||||||
|
ncutils.RunCmd("route -q -n add -net "+addr.String()+" "+address, true)
|
||||||
|
}
|
||||||
|
@@ -17,3 +17,7 @@ func deleteRoute(iface string, addr *net.IPNet, address string) error {
|
|||||||
_, err = ncutils.RunCmd("route delete -net "+addr.String()+" -interface "+iface, true)
|
_, err = ncutils.RunCmd("route delete -net "+addr.String()+" -interface "+iface, true)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setCidr(iface, address string, addr *net.IPNet) {
|
||||||
|
ncutils.RunCmd("route add -net "+addr.String()+" -interface "+iface, true)
|
||||||
|
}
|
||||||
|
@@ -20,3 +20,7 @@ func deleteRoute(iface string, addr *net.IPNet, address string) error {
|
|||||||
_, err = ncutils.RunCmd(fmt.Sprintf("ip route del %s dev %s", addr.String(), iface), true)
|
_, err = ncutils.RunCmd(fmt.Sprintf("ip route del %s dev %s", addr.String(), iface), true)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setCidr(iface, address string, addr *net.IPNet) {
|
||||||
|
ncutils.RunCmd("ip -4 route add "+addr.String()+" dev "+iface, false)
|
||||||
|
}
|
||||||
|
@@ -20,3 +20,9 @@ func deleteRoute(iface string, addr *net.IPNet, address string) error {
|
|||||||
_, err = ncutils.RunCmd("route delete "+addr.IP.String()+" mask "+addr.Mask.String()+" "+address, true)
|
_, err = ncutils.RunCmd("route delete "+addr.IP.String()+" mask "+addr.Mask.String()+" "+address, true)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setCidr(iface, address string, addr *net.IPNet) {
|
||||||
|
ncutils.RunCmd("route -p add "+addr.IP.String()+" mask "+addr.Mask.String()+" "+address, true)
|
||||||
|
time.Sleep(time.Second >> 2)
|
||||||
|
ncutils.RunCmd("route change "+addr.IP.String()+" mask "+addr.Mask.String()+" "+address, true)
|
||||||
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
package wireguard
|
package wireguard
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -157,60 +157,52 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
|||||||
|
|
||||||
// spin up userspace / windows interface + apply the conf file
|
// spin up userspace / windows interface + apply the conf file
|
||||||
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
|
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
|
||||||
var deviceiface string
|
var deviceiface = ifacename
|
||||||
if ncutils.IsMac() {
|
if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
|
||||||
deviceiface, err = local.GetMacIface(node.Address)
|
deviceiface, err = local.GetMacIface(node.Address)
|
||||||
if err != nil || deviceiface == "" {
|
if err != nil || deviceiface == "" {
|
||||||
deviceiface = ifacename
|
deviceiface = ifacename
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if syncconf {
|
// ensure you clear any existing interface first
|
||||||
err = SyncWGQuickConf(ifacename, confPath)
|
|
||||||
} else {
|
|
||||||
if !ncutils.IsMac() {
|
|
||||||
d, _ := wgclient.Device(deviceiface)
|
d, _ := wgclient.Device(deviceiface)
|
||||||
for d != nil && d.Name == deviceiface {
|
for d != nil && d.Name == deviceiface {
|
||||||
RemoveConf(ifacename, false) // remove interface first
|
RemoveConf(ifacename, false) // remove interface first
|
||||||
time.Sleep(time.Second >> 2)
|
time.Sleep(time.Second >> 2)
|
||||||
d, _ = wgclient.Device(deviceiface)
|
d, _ = wgclient.Device(deviceiface)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if !ncutils.IsWindows() {
|
|
||||||
err = ApplyConf(*node, ifacename, confPath)
|
|
||||||
if err != nil {
|
|
||||||
ncutils.PrintLog("failed to create wireguard interface", 1)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
var output string
|
|
||||||
starttime := time.Now()
|
|
||||||
RemoveConf(ifacename, false)
|
|
||||||
time.Sleep(time.Second >> 2)
|
|
||||||
ncutils.PrintLog("waiting for interface...", 1)
|
|
||||||
for !strings.Contains(output, ifacename) && !(time.Now().After(starttime.Add(time.Duration(10) * time.Second))) {
|
|
||||||
output, _ = ncutils.RunCmd("wg", false)
|
|
||||||
err = ApplyConf(*node, ifacename, confPath)
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
}
|
|
||||||
if !strings.Contains(output, ifacename) {
|
|
||||||
return errors.New("could not create wg interface for " + ifacename)
|
|
||||||
}
|
|
||||||
ip, mask, err := ncutils.GetNetworkIPMask(nodecfg.NetworkSettings.AddressRange)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
ncutils.RunCmd("route add "+ip+" mask "+mask+" "+node.Address, true)
|
|
||||||
time.Sleep(time.Second >> 2)
|
|
||||||
ncutils.RunCmd("route change "+ip+" mask "+mask+" "+node.Address, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//extra network route setting
|
ApplyConf(node, deviceiface, confPath) // Apply initially
|
||||||
if ncutils.IsFreeBSD() {
|
|
||||||
_, _ = ncutils.RunCmd("route add -net "+nodecfg.NetworkSettings.AddressRange+" -interface "+ifacename, true)
|
ncutils.PrintLog("waiting for interface...", 1) // ensure interface is created
|
||||||
} else if ncutils.IsLinux() {
|
output, _ := ncutils.RunCmd("wg", false)
|
||||||
_, _ = ncutils.RunCmd("ip -4 route add "+nodecfg.NetworkSettings.AddressRange+" dev "+ifacename, false)
|
starttime := time.Now()
|
||||||
|
ifaceReady := false
|
||||||
|
for !strings.Contains(output, ifacename) && !(time.Now().After(starttime.Add(time.Second << 4))) {
|
||||||
|
output, _ = ncutils.RunCmd("wg", false)
|
||||||
|
err = ApplyConf(node, ifacename, confPath)
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
ifaceReady = !strings.Contains(output, ifacename)
|
||||||
|
}
|
||||||
|
newDevice, devErr := wgclient.Device(deviceiface)
|
||||||
|
if !ifaceReady || devErr != nil {
|
||||||
|
return fmt.Errorf("could not reliably create interface, please check wg installation and retry")
|
||||||
|
}
|
||||||
|
ncutils.PrintLog("interface ready - netclient engage", 1)
|
||||||
|
|
||||||
|
if syncconf { // should never be called really.
|
||||||
|
err = SyncWGQuickConf(ifacename, confPath)
|
||||||
|
}
|
||||||
|
currentPeers := newDevice.Peers
|
||||||
|
if len(currentPeers) == 0 { // if no peers currently, apply cidr
|
||||||
|
_, cidr, cidrErr := net.ParseCIDR(modcfg.NetworkSettings.AddressRange)
|
||||||
|
if cidrErr == nil {
|
||||||
|
local.SetCIDRRoute(ifacename, node.Address, cidr)
|
||||||
|
} else {
|
||||||
|
ncutils.PrintLog("could not set cidr route properly: "+cidrErr.Error(), 1)
|
||||||
|
}
|
||||||
|
} else { // if peers, apply each
|
||||||
|
local.SetCurrentPeerRoutes(ifacename, node.Address, currentPeers[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
@@ -272,7 +264,7 @@ func RemoveConf(iface string, printlog bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ApplyConf - applys a conf on disk to WireGuard interface
|
// ApplyConf - applys a conf on disk to WireGuard interface
|
||||||
func ApplyConf(node models.Node, ifacename string, confPath string) error {
|
func ApplyConf(node *models.Node, ifacename string, confPath string) error {
|
||||||
os := runtime.GOOS
|
os := runtime.GOOS
|
||||||
var err error
|
var err error
|
||||||
switch os {
|
switch os {
|
||||||
|
@@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// WgQuickDownMac - bring down mac interface, remove routes, and run post-down commands
|
// WgQuickDownMac - bring down mac interface, remove routes, and run post-down commands
|
||||||
func WgQuickDownMac(node models.Node, iface string) error {
|
func WgQuickDownMac(node *models.Node, iface string) error {
|
||||||
if err := RemoveConfMac(iface); err != nil {
|
if err := RemoveConfMac(iface); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ func RemoveConfMac(iface string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WgQuickUpMac - bring up mac interface and set routes
|
// WgQuickUpMac - bring up mac interface and set routes
|
||||||
func WgQuickUpMac(node models.Node, iface string, confPath string) error {
|
func WgQuickUpMac(node *models.Node, iface string, confPath string) error {
|
||||||
var err error
|
var err error
|
||||||
var realIface string
|
var realIface string
|
||||||
realIface, err = getRealIface(iface)
|
realIface, err = getRealIface(iface)
|
||||||
|
@@ -73,7 +73,7 @@ func ApplyWGQuickConf(confPath string, ifacename string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ApplyMacOSConf - applies system commands similar to wg-quick using golang for MacOS
|
// ApplyMacOSConf - applies system commands similar to wg-quick using golang for MacOS
|
||||||
func ApplyMacOSConf(node models.Node, ifacename string, confPath string) error {
|
func ApplyMacOSConf(node *models.Node, ifacename string, confPath string) error {
|
||||||
var err error
|
var err error
|
||||||
_ = WgQuickDownMac(node, ifacename)
|
_ = WgQuickDownMac(node, ifacename)
|
||||||
err = WgQuickUpMac(node, ifacename, confPath)
|
err = WgQuickUpMac(node, ifacename, confPath)
|
||||||
|
Reference in New Issue
Block a user