mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-06 17:29:15 +08:00
fixing logging, disable non-linux relays, reset dns on error
This commit is contained in:
@@ -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
|
||||||
|
@@ -29,13 +29,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")
|
||||||
|
@@ -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 {
|
||||||
|
Reference in New Issue
Block a user