cmd/ctrld: make NetworkManger ignore auto dns

So the DNS that set by ctrld won't be override on startup.
This commit is contained in:
Cuong Manh Le
2023-02-01 21:26:21 +07:00
committed by Cuong Manh Le
parent 44bd580e48
commit 8c47ffb5ec
4 changed files with 54 additions and 0 deletions

View File

@@ -223,6 +223,7 @@ func initCLI() {
{s.Start, true},
}
if doTasks(tasks) {
disableAutoDNS(iface)
prog.setDNS()
mainLog.Info().Msg("Service started")
}
@@ -254,6 +255,7 @@ func initCLI() {
}
initLogging()
if doTasks([]task{{s.Stop, true}}) {
enableAutoDNS(iface)
prog.resetDNS()
mainLog.Info().Msg("Service stopped")
}
@@ -323,6 +325,7 @@ func initCLI() {
}
initLogging()
if doTasks(tasks) {
enableAutoDNS(iface)
prog.resetDNS()
mainLog.Info().Msg("Service uninstalled")
return

View File

@@ -9,6 +9,7 @@ import (
"net/netip"
"os/exec"
"reflect"
"runtime"
"strings"
"syscall"
"time"
@@ -174,6 +175,25 @@ func getDNSByNmcli(iface string) []string {
return dns
}
func getConnByNmcli(iface string) string {
if iface == "auto" {
iface = defaultIfaceName()
}
b, err := exec.Command("nmcli", "dev", "show", iface).Output()
if err != nil {
return ""
}
s := bufio.NewScanner(bytes.NewReader(b))
for s.Scan() {
line := s.Text()
if _, connName, found := strings.Cut(line, "GENERAL.CONNECTION:"); found {
return strings.TrimSpace(connName)
}
}
return ""
}
func ignoringEINTR(fn func() error) error {
for {
err := fn()
@@ -182,3 +202,22 @@ func ignoringEINTR(fn func() error) error {
}
}
}
func disableAutoDNS(iface string) {
networkManagerIgnoreAutoDNS(iface, "yes")
}
func enableAutoDNS(iface string) {
networkManagerIgnoreAutoDNS(iface, "no")
}
func networkManagerIgnoreAutoDNS(iface, answer string) {
if runtime.GOOS != "linux" {
return
}
if connName := getConnByNmcli(iface); connName != "" {
mainLog.Debug().Msg("enable auto DNS from network manager")
_ = exec.Command("nmcli", "con", "mod", connName, "ipv4.ignore-auto-dns", answer).Run()
_ = exec.Command("nmcli", "con", "mod", connName, "ipv6.ignore-auto-dns", answer).Run()
}
}

View File

@@ -60,3 +60,9 @@ func resetDNS(iface *net.Interface) error {
func currentDNS(_ *net.Interface) []string {
return resolvconffile.NameServers("")
}
func disableAutoDNS(iface string) {
}
func enableAutoDNS(iface string) {
}

View File

@@ -104,3 +104,9 @@ func currentDNS(iface *net.Interface) []string {
}
return ns
}
func disableAutoDNS(iface string) {
}
func enableAutoDNS(iface string) {
}