mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-06 03:46:02 +08:00
Merge pull request #698 from gravitl/bugfix_v0.10.0_alex_2
Bugfix v0.10.0 alex 2
This commit is contained in:
commit
b0c05d23aa
3 changed files with 24 additions and 14 deletions
|
@ -3,6 +3,7 @@ package logic
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gravitl/netmaker/database"
|
"github.com/gravitl/netmaker/database"
|
||||||
|
@ -18,7 +19,9 @@ func CreateRelay(relay models.RelayRequest) ([]models.Node, models.Node, error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return returnnodes, models.Node{}, err
|
return returnnodes, models.Node{}, err
|
||||||
}
|
}
|
||||||
|
if node.OS != "linux" {
|
||||||
|
return returnnodes, models.Node{}, fmt.Errorf("only linux machines can be relay nodes")
|
||||||
|
}
|
||||||
err = ValidateRelay(relay)
|
err = ValidateRelay(relay)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return returnnodes, models.Node{}, err
|
return returnnodes, models.Node{}, err
|
||||||
|
|
|
@ -20,6 +20,9 @@ const DNS_UNREACHABLE_ERROR = "nameserver unreachable"
|
||||||
// SetDNSWithRetry - Attempt setting dns, if it fails return true (to reset dns)
|
// SetDNSWithRetry - Attempt setting dns, if it fails return true (to reset dns)
|
||||||
func SetDNSWithRetry(node models.Node, address string) bool {
|
func SetDNSWithRetry(node models.Node, address string) bool {
|
||||||
var reachable bool
|
var reachable bool
|
||||||
|
if !hasPrereqs() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
for counter := 0; !reachable && counter < 5; counter++ {
|
for counter := 0; !reachable && counter < 5; counter++ {
|
||||||
reachable = IsDNSReachable(address)
|
reachable = IsDNSReachable(address)
|
||||||
time.Sleep(time.Second << 1)
|
time.Sleep(time.Second << 1)
|
||||||
|
@ -29,13 +32,17 @@ func SetDNSWithRetry(node models.Node, address string) bool {
|
||||||
return true
|
return true
|
||||||
} else if err := UpdateDNS(node.Interface, node.Network, address); err != nil {
|
} else if err := UpdateDNS(node.Interface, node.Network, address); err != nil {
|
||||||
ncutils.Log("error applying dns" + err.Error())
|
ncutils.Log("error applying dns" + err.Error())
|
||||||
return false
|
|
||||||
} else if IsDNSWorking(node.Network, address) {
|
} else if IsDNSWorking(node.Network, address) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
resetDNS()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resetDNS() {
|
||||||
|
ncutils.RunCmd("systemctl restart systemd-resolved", true)
|
||||||
|
}
|
||||||
|
|
||||||
// SetDNS - sets the DNS of a local machine
|
// SetDNS - sets the DNS of a local machine
|
||||||
func SetDNS(nameserver string) error {
|
func SetDNS(nameserver string) error {
|
||||||
bytes, err := os.ReadFile("/etc/resolv.conf")
|
bytes, err := os.ReadFile("/etc/resolv.conf")
|
||||||
|
@ -58,6 +65,14 @@ func SetDNS(nameserver string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasPrereqs() bool {
|
||||||
|
if !ncutils.IsLinux() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, err := exec.LookPath("resolvectl")
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateDNS - updates local DNS of client
|
// UpdateDNS - updates local DNS of client
|
||||||
func UpdateDNS(ifacename string, network string, nameserver string) error {
|
func UpdateDNS(ifacename string, network string, nameserver string) error {
|
||||||
if !ncutils.IsLinux() {
|
if !ncutils.IsLinux() {
|
||||||
|
|
|
@ -63,7 +63,7 @@ func SetPeers(iface, currentNodeAddr string, keepalive int32, peers []wgtypes.Pe
|
||||||
if !found {
|
if !found {
|
||||||
_, err := ncutils.RunCmd("wg set "+iface+" peer "+currentPeer.PublicKey.String()+" remove", true)
|
_, err := ncutils.RunCmd("wg set "+iface+" peer "+currentPeer.PublicKey.String()+" remove", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error removing peer", currentPeer.Endpoint.String())
|
ncutils.PrintLog("error removing peer: "+currentPeer.Endpoint.String(), 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,7 +116,7 @@ func SetPeers(iface, currentNodeAddr string, keepalive int32, peers []wgtypes.Pe
|
||||||
" allowed-ips "+allowedips, true)
|
" allowed-ips "+allowedips, true)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error setting peer", peer.PublicKey.String())
|
ncutils.PrintLog("error setting peer: "+peer.PublicKey.String(), 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,17 +153,16 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to open client: %v", err)
|
log.Fatalf("failed to open client: %v", err)
|
||||||
}
|
}
|
||||||
log.Println("-2")
|
|
||||||
var ifacename string
|
var ifacename string
|
||||||
if nodecfg.Interface != "" {
|
if nodecfg.Interface != "" {
|
||||||
ifacename = nodecfg.Interface
|
ifacename = nodecfg.Interface
|
||||||
} else if node.Interface != "" {
|
} else if node.Interface != "" {
|
||||||
ifacename = node.Interface
|
ifacename = node.Interface
|
||||||
} else {
|
} else {
|
||||||
log.Fatal("no interface to configure")
|
return fmt.Errorf("no interface to configure")
|
||||||
}
|
}
|
||||||
if node.Address == "" {
|
if node.Address == "" {
|
||||||
log.Fatal("no address to configure")
|
return fmt.Errorf("no address to configure")
|
||||||
}
|
}
|
||||||
if node.UDPHolePunch == "yes" {
|
if node.UDPHolePunch == "yes" {
|
||||||
node.ListenPort = 0
|
node.ListenPort = 0
|
||||||
|
@ -172,7 +171,6 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
||||||
ncutils.PrintLog("error writing wg conf file: "+err.Error(), 1)
|
ncutils.PrintLog("error writing wg conf file: "+err.Error(), 1)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Println("-1")
|
|
||||||
// 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 = ifacename
|
var deviceiface = ifacename
|
||||||
|
@ -182,12 +180,9 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
||||||
deviceiface = ifacename
|
deviceiface = ifacename
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Println("0")
|
|
||||||
// ensure you clear any existing interface first
|
// ensure you clear any existing interface first
|
||||||
d, _ := wgclient.Device(deviceiface)
|
d, _ := wgclient.Device(deviceiface)
|
||||||
for d != nil && d.Name == deviceiface {
|
for d != nil && d.Name == deviceiface {
|
||||||
log.Println("d==", d.Name)
|
|
||||||
log.Println("deviceiface==", deviceiface)
|
|
||||||
err = RemoveConf(deviceiface, false) // remove interface first
|
err = RemoveConf(deviceiface, false) // remove interface first
|
||||||
if strings.Contains(err.Error(), "does not exist") {
|
if strings.Contains(err.Error(), "does not exist") {
|
||||||
err = nil
|
err = nil
|
||||||
|
@ -196,14 +191,12 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
||||||
time.Sleep(time.Second >> 2)
|
time.Sleep(time.Second >> 2)
|
||||||
d, _ = wgclient.Device(deviceiface)
|
d, _ = wgclient.Device(deviceiface)
|
||||||
}
|
}
|
||||||
log.Println("1")
|
|
||||||
ApplyConf(node, deviceiface, confPath) // Apply initially
|
ApplyConf(node, deviceiface, confPath) // Apply initially
|
||||||
ncutils.PrintLog("waiting for interface...", 1) // ensure interface is created
|
ncutils.PrintLog("waiting for interface...", 1) // ensure interface is created
|
||||||
output, _ := ncutils.RunCmd("wg", false)
|
output, _ := ncutils.RunCmd("wg", false)
|
||||||
starttime := time.Now()
|
starttime := time.Now()
|
||||||
ifaceReady := strings.Contains(output, deviceiface)
|
ifaceReady := strings.Contains(output, deviceiface)
|
||||||
for !ifaceReady && !(time.Now().After(starttime.Add(time.Second << 4))) {
|
for !ifaceReady && !(time.Now().After(starttime.Add(time.Second << 4))) {
|
||||||
log.Println("2")
|
|
||||||
if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
|
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 == "" {
|
||||||
|
@ -215,7 +208,6 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
ifaceReady = strings.Contains(output, deviceiface)
|
ifaceReady = strings.Contains(output, deviceiface)
|
||||||
}
|
}
|
||||||
log.Println("3")
|
|
||||||
//wgclient does not work well on freebsd
|
//wgclient does not work well on freebsd
|
||||||
if node.OS == "freebsd" {
|
if node.OS == "freebsd" {
|
||||||
if !ifaceReady {
|
if !ifaceReady {
|
||||||
|
|
Loading…
Add table
Reference in a new issue