fix for issue #621

This commit is contained in:
Matthew R Kasun
2022-01-21 13:15:54 -05:00
parent 1a00f8377e
commit 95dea6a4ba
7 changed files with 186 additions and 184 deletions

View File

@@ -12,6 +12,7 @@ import (
"github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/ncutils" "github.com/gravitl/netmaker/netclient/ncutils"
"github.com/gravitl/netmaker/netclient/wireguard"
"golang.zx2c4.com/wireguard/wgctrl" "golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
@@ -84,17 +85,22 @@ func initWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
} }
if !ncutils.IsKernel() { if !ncutils.IsKernel() {
var newConf string //var newConf string
newConf, _ = ncutils.CreateWireGuardConf(node, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), peers) //newConf, _ = ncutils.CreateWireGuardConf(node, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), peers)
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf" //confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
logger.Log(1, "writing wg conf file to:", confPath) //logger.Log(1, "writing wg conf file to:", confPath)
err = os.WriteFile(confPath, []byte(newConf), 0644) //err = os.WriteFile(confPath, []byte(newConf), 0644)
if err != nil { //if err != nil {
logger.Log(1, "error writing wg conf file to", confPath, ":", err.Error()) //logger.Log(1, "error writing wg conf file to", confPath, ":", err.Error())
//return err
//}
if err := wireguard.WriteWgConfig(node, key.String(), peers); err != nil {
logger.Log(1, "error writing wg conf file: ", err.Error())
return err return err
} }
// spin up userspace + apply the conf file // spin up userspace + apply the conf file
var deviceiface = ifacename var deviceiface = ifacename
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
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

View File

@@ -164,7 +164,7 @@ func GetMacAddr() ([]string, error) {
func parsePeers(keepalive int32, peers []wgtypes.PeerConfig) (string, error) { func parsePeers(keepalive int32, peers []wgtypes.PeerConfig) (string, error) {
peersString := "" peersString := ""
if keepalive <= 0 { if keepalive <= 0 {
keepalive = 20 keepalive = 0
} }
for _, peer := range peers { for _, peer := range peers {

View File

@@ -1,14 +1,9 @@
package ncutils package ncutils
import ( import (
"fmt"
"log" "log"
"os/exec" "os/exec"
"strconv"
"strings" "strings"
"github.com/gravitl/netmaker/models"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
// RunCmd - runs a local command // RunCmd - runs a local command
@@ -35,31 +30,31 @@ func GetEmbedded() error {
} }
// CreateWireGuardConf - creates a WireGuard conf string // CreateWireGuardConf - creates a WireGuard conf string
func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, peers []wgtypes.PeerConfig) (string, error) { //func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, peers []wgtypes.PeerConfig) (string, error) {
peersString, err := parsePeers(node.PersistentKeepalive, peers) // peersString, err := parsePeers(node.PersistentKeepalive, peers)
var listenPortString string // var listenPortString string
if node.MTU <= 0 { // if node.MTU <= 0 {
node.MTU = 1280 // node.MTU = 1280
} // }
if listenPort != "" { // if listenPort != "" {
listenPortString += "ListenPort = " + listenPort // listenPortString += "ListenPort = " + listenPort
} // }
if err != nil { // if err != nil {
return "", err // return "", err
} // }
config := fmt.Sprintf(`[Interface] // config := fmt.Sprintf(`[Interface]
Address = %s //Address = %s
PrivateKey = %s //PrivateKey = %s
MTU = %s //MTU = %s
%s //%s
//
%s //%s
//
`, //`,
node.Address+"/32", // node.Address+"/32",
privatekey, // privatekey,
strconv.Itoa(int(node.MTU)), // strconv.Itoa(int(node.MTU)),
listenPortString, // listenPortString,
peersString) // peersString)
return config, nil // return config, nil
} //}

View File

@@ -2,16 +2,11 @@ package ncutils
import ( import (
"context" "context"
"fmt"
"log" "log"
"os/exec" "os/exec"
"strconv"
"strings" "strings"
"syscall" "syscall"
"time" "time"
"github.com/gravitl/netmaker/models"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
// RunCmdFormatted - run a command formatted for freebsd // RunCmdFormatted - run a command formatted for freebsd
@@ -44,31 +39,31 @@ func RunCmd(command string, printerr bool) (string, error) {
} }
// CreateWireGuardConf - creates a WireGuard conf string // CreateWireGuardConf - creates a WireGuard conf string
func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, peers []wgtypes.PeerConfig) (string, error) { //func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, peers []wgtypes.PeerConfig) (string, error) {
peersString, err := parsePeers(node.PersistentKeepalive, peers) // peersString, err := parsePeers(node.PersistentKeepalive, peers)
var listenPortString string // var listenPortString string
if node.MTU <= 0 { // if node.MTU <= 0 {
node.MTU = 1280 // node.MTU = 1280
} // }
if listenPort != "" { // if listenPort != "" {
listenPortString += "ListenPort = " + listenPort // listenPortString += "ListenPort = " + listenPort
} // }
if err != nil { // if err != nil {
return "", err // return "", err
} // }
config := fmt.Sprintf(`[Interface] // config := fmt.Sprintf(`[Interface]
Address = %s //Address = %s
PrivateKey = %s //PrivateKey = %s
MTU = %s //MTU = %s
%s //%s
//
%s //%s
//
`, //`,
node.Address+"/32", // node.Address+"/32",
privatekey, // privatekey,
strconv.Itoa(int(node.MTU)), // strconv.Itoa(int(node.MTU)),
listenPortString, // listenPortString,
peersString) // peersString)
return config, nil // return config, nil
} //}

View File

@@ -3,11 +3,7 @@ package ncutils
import ( import (
"fmt" "fmt"
"os/exec" "os/exec"
"strconv"
"strings" "strings"
"github.com/gravitl/netmaker/models"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
// RunCmd - runs a local command // RunCmd - runs a local command
@@ -34,43 +30,43 @@ func GetEmbedded() error {
} }
// CreateWireGuardConf - creates a user space WireGuard conf // CreateWireGuardConf - creates a user space WireGuard conf
func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, peers []wgtypes.PeerConfig) (string, error) { //func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, peers []wgtypes.PeerConfig) (string, error) {
peersString, err := parsePeers(node.PersistentKeepalive, peers) // peersString, err := parsePeers(node.PersistentKeepalive, peers)
var listenPortString, postDownString, postUpString string // var listenPortString, postDownString, postUpString string
if node.MTU <= 0 { // if node.MTU <= 0 {
node.MTU = 1280 // node.MTU = 1280
} // }
if node.PostDown != "" { // if node.PostDown != "" {
postDownString = fmt.Sprintf("PostDown = %s", node.PostDown) // postDownString = fmt.Sprintf("PostDown = %s", node.PostDown)
} // }
if node.PostUp != "" { // if node.PostUp != "" {
postUpString = fmt.Sprintf("PostUp = %s", node.PostUp) // postUpString = fmt.Sprintf("PostUp = %s", node.PostUp)
} // }
//
if listenPort != "" { // if listenPort != "" {
listenPortString = fmt.Sprintf("ListenPort = %s", listenPort) // listenPortString = fmt.Sprintf("ListenPort = %s", listenPort)
} // }
//
if err != nil { // if err != nil {
return "", err // return "", err
} // }
config := fmt.Sprintf(`[Interface] // config := fmt.Sprintf(`[Interface]
Address = %s //Address = %s
PrivateKey = %s //PrivateKey = %s
MTU = %s //MTU = %s
%s //%s
%s //%s
%s //%s
//
%s //%s
//
`, //`,
node.Address+"/32", // node.Address+"/32",
privatekey, // privatekey,
strconv.Itoa(int(node.MTU)), // strconv.Itoa(int(node.MTU)),
postDownString, // postDownString,
postUpString, // postUpString,
listenPortString, // listenPortString,
peersString) // peersString)
return config, nil // return config, nil
} //}

View File

@@ -6,12 +6,8 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"strconv"
"strings" "strings"
"syscall" "syscall"
"github.com/gravitl/netmaker/models"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
//go:embed windowsdaemon/winsw.exe //go:embed windowsdaemon/winsw.exe
@@ -49,35 +45,35 @@ func RunCmdFormatted(command string, printerr bool) (string, error) {
} }
// CreateWireGuardConf - creates a WireGuard conf string // CreateWireGuardConf - creates a WireGuard conf string
func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, peers []wgtypes.PeerConfig) (string, error) { //func CreateWireGuardConf(node *models.Node, privatekey string, listenPort string, peers []wgtypes.PeerConfig) (string, error) {
peersString, err := parsePeers(node.PersistentKeepalive, peers) // peersString, err := parsePeers(node.PersistentKeepalive, peers)
var listenPortString string // var listenPortString string
if node.MTU <= 0 { // if node.MTU <= 0 {
node.MTU = 1280 // node.MTU = 1280
} // }
if listenPort != "" { // if listenPort != "" {
listenPortString += "ListenPort = " + listenPort // listenPortString += "ListenPort = " + listenPort
} // }
if err != nil { // if err != nil {
return "", err // return "", err
} // }
config := fmt.Sprintf(`[Interface] // config := fmt.Sprintf(`[Interface]
Address = %s //Address = %s
PrivateKey = %s //PrivateKey = %s
MTU = %s //MTU = %s
%s //%s
//
%s //%s
//
`, //`,
node.Address+"/32", // node.Address+"/32",
privatekey, // privatekey,
strconv.Itoa(int(node.MTU)), // strconv.Itoa(int(node.MTU)),
listenPortString, // listenPortString,
peersString) // peersString)
return config, nil // return config, nil
} //}
//
// GetEmbedded - Gets the Windows daemon creator // GetEmbedded - Gets the Windows daemon creator
func GetEmbedded() error { func GetEmbedded() error {
data, err := winswContent.ReadFile("windowsdaemon/winsw.exe") data, err := winswContent.ReadFile("windowsdaemon/winsw.exe")

View File

@@ -3,7 +3,6 @@ package wireguard
import ( import (
"errors" "errors"
"log" "log"
"os"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@@ -144,28 +143,36 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
if node.Address == "" { if node.Address == "" {
log.Fatal("no address to configure") log.Fatal("no address to configure")
} }
var newConf string
if node.UDPHolePunch != "yes" { if node.UDPHolePunch != "yes" {
newConf, _ = ncutils.CreateWireGuardConf(node, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), peers) node.ListenPort = 0
} else {
newConf, _ = ncutils.CreateWireGuardConf(node, key.String(), "", peers)
} }
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf" if err := WriteWgConfig(&modcfg.Node, key.String(), peers); err != nil {
ncutils.PrintLog("writing wg conf file to: "+confPath, 1) ncutils.PrintLog("error writing wg conf file: "+err.Error(), 1)
err = os.WriteFile(confPath, []byte(newConf), 0644)
if err != nil {
ncutils.PrintLog("error writing wg conf file to "+confPath+": "+err.Error(), 1)
return err return err
} }
if ncutils.IsWindows() {
wgConfPath := ncutils.GetWGPathSpecific() + ifacename + ".conf" //var newConf string
err = os.WriteFile(wgConfPath, []byte(newConf), 0644) //if node.UDPHolePunch != "yes" {
if err != nil { // newConf, _ = ncutils.CreateWireGuardConf(node, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), peers)
ncutils.PrintLog("error writing wg conf file to "+wgConfPath+": "+err.Error(), 1) //} else {
return err // newConf, _ = ncutils.CreateWireGuardConf(node, key.String(), "", peers)
} //}
confPath = wgConfPath //confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
} //ncutils.PrintLog("writing wg conf file to: "+confPath, 1)
//err = os.WriteFile(confPath, []byte(newConf), 0644)
//if err != nil {
// ncutils.PrintLog("error writing wg conf file to "+confPath+": "+err.Error(), 1)
// return err
//}
//if ncutils.IsWindows() {
confPath := ncutils.GetWGPathSpecific() + ifacename + ".conf"
// err = os.WriteFile(wgConfPath, []byte(newConf), 0644)
// if err != nil {
// ncutils.PrintLog("error writing wg conf file to "+wgConfPath+": "+err.Error(), 1)
// return err
// }
// confPath = wgConfPath
//}
// spin up userspace / windows interface + apply the conf file // spin up userspace / windows interface + apply the conf file
var deviceiface string var deviceiface string
if ncutils.IsMac() { if ncutils.IsMac() {
@@ -297,28 +304,32 @@ func ApplyConf(node models.Node, ifacename string, confPath string) error {
} }
// WriteWgConfig - creates a wireguard config file // WriteWgConfig - creates a wireguard config file
func WriteWgConfig(cfg config.ClientConfig, privateKey string, peers []wgtypes.Peer) error { //func WriteWgConfig(cfg *config.ClientConfig, privateKey string, peers []wgtypes.PeerConfig) error {
func WriteWgConfig(node *models.Node, privateKey string, peers []wgtypes.PeerConfig) error {
options := ini.LoadOptions{ options := ini.LoadOptions{
AllowNonUniqueSections: true, AllowNonUniqueSections: true,
AllowShadows: true, AllowShadows: true,
} }
wireguard := ini.Empty(options) wireguard := ini.Empty(options)
wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey) wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
wireguard.Section(section_interface).Key("ListenPort").SetValue(strconv.Itoa(int(cfg.Node.ListenPort))) if node.ListenPort > 0 {
if cfg.Node.Address != "" { wireguard.Section(section_interface).Key("ListenPort").SetValue(strconv.Itoa(int(node.ListenPort)))
wireguard.Section(section_interface).Key("Address").SetValue(cfg.Node.Address)
} }
if cfg.Node.Address6 != "" { if node.Address != "" {
wireguard.Section(section_interface).Key("Address").SetValue(cfg.Node.Address6) wireguard.Section(section_interface).Key("Address").SetValue(node.Address)
} }
if cfg.Node.DNSOn == "yes" { if node.Address6 != "" {
wireguard.Section(section_interface).Key("DNS").SetValue(cfg.Server.CoreDNSAddr) wireguard.Section(section_interface).Key("Address").SetValue(node.Address6)
} }
if cfg.Node.PostUp != "" { // need to figure out DNS
wireguard.Section(section_interface).Key("PostUp").SetValue(cfg.Node.PostUp) //if node.DNSOn == "yes" {
// wireguard.Section(section_interface).Key("DNS").SetValue(cfg.Server.CoreDNSAddr)
//}
if node.PostUp != "" {
wireguard.Section(section_interface).Key("PostUp").SetValue(node.PostUp)
} }
if cfg.Node.PostDown != "" { if node.PostDown != "" {
wireguard.Section(section_interface).Key("PostDown").SetValue(cfg.Node.PostDown) wireguard.Section(section_interface).Key("PostDown").SetValue(node.PostDown)
} }
for i, peer := range peers { for i, peer := range peers {
wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String()) wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String())
@@ -339,8 +350,11 @@ func WriteWgConfig(cfg config.ClientConfig, privateKey string, peers []wgtypes.P
if peer.Endpoint != nil { if peer.Endpoint != nil {
wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String()) wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String())
} }
if peer.PersistentKeepaliveInterval != nil && peer.PersistentKeepaliveInterval.Seconds() > 0 {
wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepalive").SetValue(strconv.FormatInt((int64)(peer.PersistentKeepaliveInterval.Seconds()), 10))
} }
if err := wireguard.SaveTo(ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf"); err != nil { }
if err := wireguard.SaveTo(ncutils.GetNetclientPathSpecific() + node.Interface + ".conf"); err != nil {
return err return err
} }
return nil return nil
@@ -379,7 +393,7 @@ func UpdateWgPeers(wgInterface string, peers []wgtypes.PeerConfig) error {
if peer.Endpoint != nil { if peer.Endpoint != nil {
wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String()) wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String())
} }
if peer.PersistentKeepaliveInterval != nil && peer.PersistentKeepaliveInterval.Seconds() != 0 { if peer.PersistentKeepaliveInterval != nil && peer.PersistentKeepaliveInterval.Seconds() > 0 {
wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepalive").SetValue(strconv.FormatInt((int64)(peer.PersistentKeepaliveInterval.Seconds()), 10)) wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepalive").SetValue(strconv.FormatInt((int64)(peer.PersistentKeepaliveInterval.Seconds()), 10))
} }
} }