mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
cmd/cli: restart systemd-resolved after setting DNS
So the current selected DNS server will be reset, and the new one will be used by systemd-resolved after first query made.
This commit is contained in:
+37
-3
@@ -9,7 +9,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@@ -85,8 +84,13 @@ func setDNS(iface *net.Interface, nameservers []string) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if useSystemdResolved {
|
||||||
|
if out, err := exec.Command("systemctl", "restart", "systemd-resolved").CombinedOutput(); err != nil {
|
||||||
|
mainLog.Load().Warn().Err(err).Msgf("could not restart systemd-resolved: %s", string(out))
|
||||||
|
}
|
||||||
|
}
|
||||||
currentNS := currentDNS(iface)
|
currentNS := currentDNS(iface)
|
||||||
if reflect.DeepEqual(currentNS, nameservers) {
|
if isSubSet(nameservers, currentNS) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,7 +108,7 @@ func setDNS(iface *net.Interface, nameservers []string) error {
|
|||||||
return fmt.Errorf("%s: %w", string(out), err)
|
return fmt.Errorf("%s: %w", string(out), err)
|
||||||
}
|
}
|
||||||
currentNS := currentDNS(iface)
|
currentNS := currentDNS(iface)
|
||||||
if reflect.DeepEqual(currentNS, nameservers) {
|
if isSubSet(nameservers, currentNS) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
@@ -265,3 +269,33 @@ func ignoringEINTR(fn func() error) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isSubSet reports whether s2 contains all elements of s1.
|
||||||
|
func isSubSet(s1, s2 []string) bool {
|
||||||
|
ok := true
|
||||||
|
for _, ns := range s1 {
|
||||||
|
// TODO(cuonglm): use slices.Contains once upgrading to go1.21
|
||||||
|
if sliceContains(s2, ns) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ok = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// sliceContains reports whether v is present in s.
|
||||||
|
func sliceContains[S ~[]E, E comparable](s S, v E) bool {
|
||||||
|
return sliceIndex(s, v) >= 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// sliceIndex returns the index of the first occurrence of v in s,
|
||||||
|
// or -1 if not present.
|
||||||
|
func sliceIndex[S ~[]E, E comparable](s S, v E) int {
|
||||||
|
for i := range s {
|
||||||
|
if v == s[i] {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user