adding checks for DNS to ensure connectivity

This commit is contained in:
afeiszli
2022-02-02 15:33:51 -05:00
committed by 0xdcarns
parent 239b9d36fc
commit cbf709166c
2 changed files with 53 additions and 3 deletions

View File

@@ -1,8 +1,11 @@
package local
import (
"fmt"
"net"
"os"
"strings"
"time"
//"github.com/davecgh/go-spew/spew"
"log"
@@ -11,6 +14,8 @@ import (
"github.com/gravitl/netmaker/netclient/ncutils"
)
const DNS_UNREACHABLE_ERROR = "nameserver unreachable"
// SetDNS - sets the DNS of a local machine
func SetDNS(nameserver string) error {
bytes, err := os.ReadFile("/etc/resolv.conf")
@@ -35,9 +40,21 @@ func SetDNS(nameserver string) error {
// UpdateDNS - updates local DNS of client
func UpdateDNS(ifacename string, network string, nameserver string) error {
if ifacename == "" {
return fmt.Errorf("cannot set dns: interface name is blank")
}
if network == "" {
return fmt.Errorf("cannot set dns: network name is blank")
}
if nameserver == "" {
return fmt.Errorf("cannot set dns: nameserver is blank")
}
if ncutils.IsWindows() {
return nil
}
if !IsDNSReachable(nameserver) {
return fmt.Errorf(DNS_UNREACHABLE_ERROR + " : " + nameserver + ":53")
}
_, err := exec.LookPath("resolvectl")
if err != nil {
log.Println(err)
@@ -60,3 +77,21 @@ func UpdateDNS(ifacename string, network string, nameserver string) error {
}
return err
}
func IsDNSReachable(nameserver string) bool {
port := "53"
protocols := [2]string{"tcp", "udp"}
for _, proto := range protocols {
timeout := time.Second
conn, err := net.DialTimeout(proto, net.JoinHostPort(nameserver, port), timeout)
if err != nil {
return false
}
if conn != nil {
defer conn.Close()
} else {
return false
}
}
return true
}