initial logic added

This commit is contained in:
0xdcarns
2022-08-19 12:08:31 -04:00
parent a7ff340692
commit c05320a323
8 changed files with 81 additions and 46 deletions

View File

@@ -433,6 +433,7 @@ func SetNodeDefaults(node *models.Node) {
node.SetDefaultIsDocker() node.SetDefaultIsDocker()
node.SetDefaultIsK8S() node.SetDefaultIsK8S()
node.SetDefaultIsHub() node.SetDefaultIsHub()
node.SetDefaultConnected()
} }
// GetRecordKey - get record key // GetRecordKey - get record key

View File

@@ -58,6 +58,7 @@ func IfaceDelta(currentNode *models.Node, newNode *models.Node) bool {
newNode.MTU != currentNode.MTU || newNode.MTU != currentNode.MTU ||
newNode.PersistentKeepalive != currentNode.PersistentKeepalive || newNode.PersistentKeepalive != currentNode.PersistentKeepalive ||
newNode.DNSOn != currentNode.DNSOn || newNode.DNSOn != currentNode.DNSOn ||
newNode.Connected != currentNode.Connected ||
len(newNode.AllowedIPs) != len(currentNode.AllowedIPs) { len(newNode.AllowedIPs) != len(currentNode.AllowedIPs) {
return true return true
} }

View File

@@ -93,6 +93,7 @@ type Node struct {
TrafficKeys TrafficKeys `json:"traffickeys" bson:"traffickeys" yaml:"traffickeys"` TrafficKeys TrafficKeys `json:"traffickeys" bson:"traffickeys" yaml:"traffickeys"`
FirewallInUse string `json:"firewallinuse" bson:"firewallinuse" yaml:"firewallinuse"` FirewallInUse string `json:"firewallinuse" bson:"firewallinuse" yaml:"firewallinuse"`
InternetGateway string `json:"internetgateway" bson:"internetgateway" yaml:"internetgateway"` InternetGateway string `json:"internetgateway" bson:"internetgateway" yaml:"internetgateway"`
Connected string `json:"connected" bson:"connected" yaml:"connected" validate:"checkyesorno"`
} }
// NodesArray - used for node sorting // NodesArray - used for node sorting
@@ -121,6 +122,16 @@ func (node *Node) PrimaryAddress() string {
return node.Address6 return node.Address6
} }
// Node.SetDefaultConnected
func (node *Node) SetDefaultConnected() {
if node.Connected == "" {
node.Connected = "yes"
}
if node.IsServer == "yes" {
node.Connected = "yes"
}
}
// Node.SetDefaultMTU - sets default MTU of a node // Node.SetDefaultMTU - sets default MTU of a node
func (node *Node) SetDefaultMTU() { func (node *Node) SetDefaultMTU() {
if node.MTU == 0 { if node.MTU == 0 {
@@ -382,6 +393,7 @@ func (newNode *Node) Fill(currentNode *Node) { // TODO add new field for nftable
} }
if newNode.IsServer == "yes" { if newNode.IsServer == "yes" {
newNode.IsStatic = "yes" newNode.IsStatic = "yes"
newNode.Connected = "yes"
} }
if newNode.MTU == 0 { if newNode.MTU == 0 {
newNode.MTU = currentNode.MTU newNode.MTU = currentNode.MTU
@@ -413,6 +425,9 @@ func (newNode *Node) Fill(currentNode *Node) { // TODO add new field for nftable
if newNode.Server == "" { if newNode.Server == "" {
newNode.Server = currentNode.Server newNode.Server = currentNode.Server
} }
if newNode.Connected == "" {
newNode.Connected = currentNode.Connected
}
newNode.TrafficKeys = currentNode.TrafficKeys newNode.TrafficKeys = currentNode.TrafficKeys
} }

View File

@@ -22,6 +22,7 @@ func IfaceDelta(currentNode *models.Node, newNode *models.Node) bool {
newNode.IsPending != currentNode.IsPending || newNode.IsPending != currentNode.IsPending ||
newNode.PersistentKeepalive != currentNode.PersistentKeepalive || newNode.PersistentKeepalive != currentNode.PersistentKeepalive ||
newNode.DNSOn != currentNode.DNSOn || newNode.DNSOn != currentNode.DNSOn ||
newNode.Connected != currentNode.Connected ||
len(newNode.AllowedIPs) != len(currentNode.AllowedIPs) { len(newNode.AllowedIPs) != len(currentNode.AllowedIPs) {
return true return true
} }

View File

@@ -191,10 +191,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
} }
} }
logger.Log(1, "interface ready - netclient.. ENGAGE") logger.Log(1, "interface ready - netclient.. ENGAGE")
if syncconf { // should never be called really.
fmt.Println("why here")
err = SyncWGQuickConf(ifacename, confPath)
}
if !ncutils.HasWgQuick() && ncutils.IsLinux() { if !ncutils.HasWgQuick() && ncutils.IsLinux() {
err = SetPeers(ifacename, node, peers) err = SetPeers(ifacename, node, peers)
if err != nil { if err != nil {
@@ -284,16 +281,17 @@ func ApplyConf(node *models.Node, ifacename string, confPath string) error {
if ncutils.IsLinux() && !ncutils.HasWgQuick() { if ncutils.IsLinux() && !ncutils.HasWgQuick() {
os = "nowgquick" os = "nowgquick"
} }
var isConnected = node.Connected != "no"
var err error var err error
switch os { switch os {
case "windows": case "windows":
ApplyWindowsConf(confPath) ApplyWindowsConf(confPath, isConnected)
case "darwin": case "darwin":
ApplyMacOSConf(node, ifacename, confPath) ApplyMacOSConf(node, ifacename, confPath, isConnected)
case "nowgquick": case "nowgquick":
ApplyWithoutWGQuick(node, ifacename, confPath) ApplyWithoutWGQuick(node, ifacename, confPath, isConnected)
default: default:
ApplyWGQuickConf(confPath, ifacename) ApplyWGQuickConf(confPath, ifacename, isConnected)
} }
var nodeCfg config.ClientConfig var nodeCfg config.ClientConfig

View File

@@ -2,6 +2,7 @@ package wireguard
import ( import (
"errors" "errors"
"fmt"
"os" "os"
"os/exec" "os/exec"
"strconv" "strconv"
@@ -15,8 +16,10 @@ import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
const disconnect_error = "node disconnected"
// ApplyWithoutWGQuick - Function for running the equivalent of "wg-quick up" for linux if wg-quick is missing // ApplyWithoutWGQuick - Function for running the equivalent of "wg-quick up" for linux if wg-quick is missing
func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) error { func ApplyWithoutWGQuick(node *models.Node, ifacename, confPath string, isConnected bool) error {
ipExec, err := exec.LookPath("ip") ipExec, err := exec.LookPath("ip")
if err != nil { if err != nil {
@@ -72,7 +75,12 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) e
mask6 = netmask mask6 = netmask
address6 = node.Address6 address6 = node.Address6
} }
setKernelDevice(ifacename, address4, mask4, address6, mask6) err = setKernelDevice(ifacename, address4, mask4, address6, mask6, isConnected)
if err != nil {
if err.Error() == disconnect_error {
return nil
}
}
_, err = wgclient.Device(ifacename) _, err = wgclient.Device(ifacename)
if err != nil { if err != nil {
@@ -140,7 +148,7 @@ func RemoveWithoutWGQuick(ifacename string) error {
return err return err
} }
func setKernelDevice(ifacename, address4, mask4, address6, mask6 string) error { func setKernelDevice(ifacename, address4, mask4, address6, mask6 string, isConnected bool) error {
ipExec, err := exec.LookPath("ip") ipExec, err := exec.LookPath("ip")
if err != nil { if err != nil {
return err return err
@@ -148,6 +156,10 @@ func setKernelDevice(ifacename, address4, mask4, address6, mask6 string) error {
// == best effort == // == best effort ==
ncutils.RunCmd("ip link delete dev "+ifacename, false) ncutils.RunCmd("ip link delete dev "+ifacename, false)
if !isConnected {
return fmt.Errorf("node disconnected")
}
ncutils.RunCmd(ipExec+" link add dev "+ifacename+" type wireguard", true) ncutils.RunCmd(ipExec+" link add dev "+ifacename+" type wireguard", true)
if address4 != "" { if address4 != "" {
ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+address4+"/"+mask4, true) ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+address4+"/"+mask4, true)

View File

@@ -2,9 +2,7 @@ package wireguard
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"regexp"
"github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/models"
@@ -12,9 +10,9 @@ import (
) )
// ApplyWGQuickConf - applies wg-quick commands if os supports // ApplyWGQuickConf - applies wg-quick commands if os supports
func ApplyWGQuickConf(confPath string, ifacename string) error { func ApplyWGQuickConf(confPath, ifacename string, isConnected bool) error {
if ncutils.IsWindows() { if ncutils.IsWindows() {
return ApplyWindowsConf(confPath) return ApplyWindowsConf(confPath, isConnected)
} else { } else {
_, err := os.Stat(confPath) _, err := os.Stat(confPath)
if err != nil { if err != nil {
@@ -24,6 +22,9 @@ func ApplyWGQuickConf(confPath string, ifacename string) error {
if ncutils.IfaceExists(ifacename) { if ncutils.IfaceExists(ifacename) {
ncutils.RunCmd("wg-quick down "+confPath, true) ncutils.RunCmd("wg-quick down "+confPath, true)
} }
if !isConnected {
return nil
}
_, err = ncutils.RunCmd("wg-quick up "+confPath, true) _, err = ncutils.RunCmd("wg-quick up "+confPath, true)
return err return err
@@ -31,42 +32,45 @@ 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, confPath string, isConnected bool) error {
var err error var err error
_ = WgQuickDownMac(node, ifacename) _ = WgQuickDownMac(node, ifacename)
if !isConnected {
return nil
}
err = WgQuickUpMac(node, ifacename, confPath) err = WgQuickUpMac(node, ifacename, confPath)
return err return err
} }
// SyncWGQuickConf - formats config file and runs sync command // SyncWGQuickConf - formats config file and runs sync command - DEPRECATED
func SyncWGQuickConf(iface string, confPath string) error { // func SyncWGQuickConf(iface string, confPath string) error {
var tmpConf = confPath + ".sync.tmp" // var tmpConf = confPath + ".sync.tmp"
var confCmd = "wg-quick strip " // var confCmd = "wg-quick strip "
if ncutils.IsMac() { // if ncutils.IsMac() {
confCmd = "grep -v -e Address -e MTU -e PostUp -e PostDown " // confCmd = "grep -v -e Address -e MTU -e PostUp -e PostDown "
} // }
confRaw, err := ncutils.RunCmd(confCmd+confPath, false) // confRaw, err := ncutils.RunCmd(confCmd+confPath, false)
if err != nil { // if err != nil {
return err // return err
} // }
regex := regexp.MustCompile(".*Warning.*\n") // regex := regexp.MustCompile(".*Warning.*\n")
conf := regex.ReplaceAllString(confRaw, "") // conf := regex.ReplaceAllString(confRaw, "")
err = os.WriteFile(tmpConf, []byte(conf), 0600) // err = os.WriteFile(tmpConf, []byte(conf), 0600)
if err != nil { // if err != nil {
return err // return err
} // }
_, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true) // _, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true)
if err != nil { // if err != nil {
log.Println(err.Error()) // log.Println(err.Error())
logger.Log(0, "error syncing conf, resetting") // logger.Log(0, "error syncing conf, resetting")
err = ApplyWGQuickConf(confPath, iface) // err = ApplyWGQuickConf(confPath, iface)
} // }
errN := os.Remove(tmpConf) // errN := os.Remove(tmpConf)
if errN != nil { // if errN != nil {
logger.Log(0, errN.Error()) // logger.Log(0, errN.Error())
} // }
return err // return err
} // }
// RemoveWGQuickConf - calls wg-quick down // RemoveWGQuickConf - calls wg-quick down
func RemoveWGQuickConf(confPath string, printlog bool) error { func RemoveWGQuickConf(confPath string, printlog bool) error {

View File

@@ -8,7 +8,10 @@ import (
) )
// ApplyWindowsConf - applies the WireGuard configuration file on Windows // ApplyWindowsConf - applies the WireGuard configuration file on Windows
func ApplyWindowsConf(confPath string) error { func ApplyWindowsConf(confPath string, isConnected bool) error {
if !isConnected {
return nil
}
var commandLine = fmt.Sprintf(`wireguard.exe /installtunnelservice "%s"`, confPath) var commandLine = fmt.Sprintf(`wireguard.exe /installtunnelservice "%s"`, confPath)
if _, err := ncutils.RunCmdFormatted(commandLine, false); err != nil { if _, err := ncutils.RunCmdFormatted(commandLine, false); err != nil {
return err return err