diff --git a/logic/util.go b/logic/util.go index d89b0daa..989bff4e 100644 --- a/logic/util.go +++ b/logic/util.go @@ -249,6 +249,14 @@ func GetPeersList(networkName string, excludeRelayed bool, relayedNodeAddr strin network, err := GetNetwork(networkName) if err == nil { peerNode.AllowedIPs = append(peerNode.AllowedIPs, network.AddressRange) + var _, egressNetworkNodes, err = getNetworkEgressAndNodes(networkName) + if err == nil { + for _, egress := range egressNetworkNodes { + if egress.Address != relayedNodeAddr { + peerNode.AllowedIPs = append(peerNode.AllowedIPs, egress.EgressGatewayRanges...) + } + } + } } else { peerNode.AllowedIPs = append(peerNode.AllowedIPs, peerNode.RelayAddrs...) } diff --git a/logic/wireguard.go b/logic/wireguard.go index 2090ec18..3eff6b80 100644 --- a/logic/wireguard.go +++ b/logic/wireguard.go @@ -87,7 +87,7 @@ func initWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig if !ncutils.IsKernel() { var newConf string - newConf, _ = ncutils.CreateWireGuardConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, servercfg.GetCoreDNSAddr(), node.PersistentKeepalive, peers) + newConf, _ = ncutils.CreateWireGuardConf(node, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), servercfg.GetCoreDNSAddr(), peers) confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf" logger.Log(1, "writing wg conf file to:", confPath) err = ioutil.WriteFile(confPath, []byte(newConf), 0644) diff --git a/netclient/command/commands.go b/netclient/command/commands.go index 940eb67b..49e075a2 100644 --- a/netclient/command/commands.go +++ b/netclient/command/commands.go @@ -205,7 +205,8 @@ func List(cfg config.ClientConfig) error { // Uninstall - runs uninstall command from cli func Uninstall() error { - ncutils.PrintLog("uninstalling netclient", 0) + ncutils.PrintLog("uninstalling netclient...", 0) err := functions.Uninstall() + ncutils.PrintLog("uninstalled netclient", 0) return err } diff --git a/netclient/ncutils/netclientutils_darwin.go b/netclient/ncutils/netclientutils_darwin.go index 79fc85d7..a073f989 100644 --- a/netclient/ncutils/netclientutils_darwin.go +++ b/netclient/ncutils/netclientutils_darwin.go @@ -7,6 +7,7 @@ import ( "strconv" "strings" + "github.com/gravitl/netmaker/models" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) @@ -33,12 +34,12 @@ func GetEmbedded() error { return nil } -// CreateUserSpaceConf - creates a user space WireGuard conf -func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) { - peersString, err := parsePeers(perskeepalive, peers) +// CreateWireGuardConf - creates a WireGuard conf string +func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, dns string, peers []wgtypes.PeerConfig) (string, error) { + peersString, err := parsePeers(node.PersistentKeepalive, peers) var listenPortString string - if mtu <= 0 { - mtu = 1280 + if node.MTU <= 0 { + node.MTU = 1280 } if listenPort != "" { listenPortString += "ListenPort = " + listenPort @@ -55,9 +56,9 @@ MTU = %s %s `, - address+"/32", + node.Address+"/32", privatekey, - strconv.Itoa(int(mtu)), + strconv.Itoa(int(node.MTU)), listenPortString, peersString) return config, nil diff --git a/netclient/ncutils/netclientutils_freebsd.go b/netclient/ncutils/netclientutils_freebsd.go index 19d3aa19..bbacdc6c 100644 --- a/netclient/ncutils/netclientutils_freebsd.go +++ b/netclient/ncutils/netclientutils_freebsd.go @@ -3,13 +3,15 @@ package ncutils import ( "context" "fmt" - "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "log" "os/exec" "strconv" "strings" "syscall" "time" + + "github.com/gravitl/netmaker/models" + "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) // RunCmdFormatted - run a command formatted for freebsd @@ -41,12 +43,12 @@ func RunCmd(command string, printerr bool) (string, error) { return string(out), err } -// CreateUserSpaceConf - creates a user space WireGuard conf -func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) { - peersString, err := parsePeers(perskeepalive, peers) +// CreateWireGuardConf - creates a WireGuard conf string +func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, dns string, peers []wgtypes.PeerConfig) (string, error) { + peersString, err := parsePeers(node.PersistentKeepalive, peers) var listenPortString string - if mtu <= 0 { - mtu = 1280 + if node.MTU <= 0 { + node.MTU = 1280 } if listenPort != "" { listenPortString += "ListenPort = " + listenPort @@ -63,9 +65,9 @@ MTU = %s %s `, - address+"/32", + node.Address+"/32", privatekey, - strconv.Itoa(int(mtu)), + strconv.Itoa(int(node.MTU)), listenPortString, peersString) return config, nil diff --git a/netclient/ncutils/netclientutils_linux.go b/netclient/ncutils/netclientutils_linux.go index ae40c292..bc4c399e 100644 --- a/netclient/ncutils/netclientutils_linux.go +++ b/netclient/ncutils/netclientutils_linux.go @@ -6,6 +6,7 @@ import ( "strconv" "strings" + "github.com/gravitl/netmaker/models" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) @@ -33,15 +34,23 @@ func GetEmbedded() error { } // CreateWireGuardConf - creates a user space WireGuard conf -func CreateWireGuardConf(address string, privatekey string, listenPort string, mtu int32, dns string, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) { - peersString, err := parsePeers(perskeepalive, peers) - var listenPortString string - if mtu <= 0 { - mtu = 1280 +func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, dns string, peers []wgtypes.PeerConfig) (string, error) { + peersString, err := parsePeers(node.PersistentKeepalive, peers) + var listenPortString, postDownString, postUpString string + if node.MTU <= 0 { + node.MTU = 1280 } + if node.PostDown != "" { + postDownString = fmt.Sprintf("PostDown = %s", node.PostDown) + } + if node.PostUp != "" { + postUpString = fmt.Sprintf("PostUp = %s", node.PostUp) + } + if listenPort != "" { - listenPortString += "ListenPort = " + listenPort + listenPortString = fmt.Sprintf("ListenPort = %s", listenPort) } + if err != nil { return "", err } @@ -51,14 +60,18 @@ DNS = %s PrivateKey = %s MTU = %s %s +%s +%s %s `, - address+"/32", + node.Address+"/32", dns, privatekey, - strconv.Itoa(int(mtu)), + strconv.Itoa(int(node.MTU)), + postDownString, + postUpString, listenPortString, peersString) return config, nil diff --git a/netclient/ncutils/netclientutils_windows.go b/netclient/ncutils/netclientutils_windows.go index 45974297..5acf73a7 100644 --- a/netclient/ncutils/netclientutils_windows.go +++ b/netclient/ncutils/netclientutils_windows.go @@ -10,6 +10,7 @@ import ( "strings" "syscall" + "github.com/gravitl/netmaker/models" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) @@ -47,12 +48,12 @@ func RunCmdFormatted(command string, printerr bool) (string, error) { return string(out), err } -// CreateUserSpaceConf - creates a user space WireGuard conf -func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) { - peersString, err := parsePeers(perskeepalive, peers) +// CreateWireGuardConf - creates a WireGuard conf string +func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, dns string, peers []wgtypes.PeerConfig) (string, error) { + peersString, err := parsePeers(node.PersistentKeepalive, peers) var listenPortString string - if mtu <= 0 { - mtu = 1280 + if node.MTU <= 0 { + node.MTU = 1280 } if listenPort != "" { listenPortString += "ListenPort = " + listenPort @@ -69,9 +70,9 @@ MTU = %s %s `, - address+"/32", + node.Address+"/32", privatekey, - strconv.Itoa(int(mtu)), + strconv.Itoa(int(node.MTU)), listenPortString, peersString) return config, nil diff --git a/netclient/wireguard/common.go b/netclient/wireguard/common.go index 53613790..f65016dd 100644 --- a/netclient/wireguard/common.go +++ b/netclient/wireguard/common.go @@ -149,9 +149,9 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig } var newConf string if node.UDPHolePunch != "yes" { - newConf, _ = ncutils.CreateWireGuardConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, nameserver, node.PersistentKeepalive, peers) + newConf, _ = ncutils.CreateWireGuardConf(node, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), nameserver, peers) } else { - newConf, _ = ncutils.CreateWireGuardConf(node.Address, key.String(), "", node.MTU, nameserver, node.PersistentKeepalive, peers) + newConf, _ = ncutils.CreateWireGuardConf(node, key.String(), "", nameserver, peers) } confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf" ncutils.PrintLog("writing wg conf file to: "+confPath, 1) @@ -182,7 +182,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig } else { d, _ := wgclient.Device(deviceiface) for d != nil && d.Name == deviceiface { - _ = RemoveConf(ifacename, false) // remove interface first + RemoveConf(ifacename, false) // remove interface first time.Sleep(time.Second >> 2) d, _ = wgclient.Device(deviceiface) } diff --git a/netclient/wireguard/unix.go b/netclient/wireguard/unix.go index 8b557d0a..6e9e52af 100644 --- a/netclient/wireguard/unix.go +++ b/netclient/wireguard/unix.go @@ -1,6 +1,7 @@ package wireguard import ( + "fmt" "io/ioutil" "log" "os" @@ -86,7 +87,7 @@ func SyncWGQuickConf(iface string, confPath string) error { // RemoveWGQuickConf - calls wg-quick down func RemoveWGQuickConf(confPath string, printlog bool) error { - _, err := ncutils.RunCmd("wg-quick down "+confPath, printlog) + _, err := ncutils.RunCmd(fmt.Sprintf("wg-quick down %s", confPath), printlog) return err }