diff --git a/client/internal/dns/file_linux.go b/client/internal/dns/file_linux.go index 1d3a1e383..0172e3413 100644 --- a/client/internal/dns/file_linux.go +++ b/client/internal/dns/file_linux.go @@ -15,7 +15,8 @@ const ( fileGeneratedResolvConfSearchBeginContent = "search " fileGeneratedResolvConfContentFormat = fileGeneratedResolvConfContentHeader + "\n# If needed you can restore the original file by copying back %s\n\nnameserver %s\n" + - fileGeneratedResolvConfSearchBeginContent + "%s\n" + fileGeneratedResolvConfSearchBeginContent + "%s\n\n" + + "%s\n" ) const ( @@ -91,7 +92,12 @@ func (f *fileConfigurator) applyDNSConfig(config hostDNSConfig) error { searchDomains += " " + dConf.domain appendedDomains++ } - content := fmt.Sprintf(fileGeneratedResolvConfContentFormat, fileDefaultResolvConfBackupLocation, config.serverIP, searchDomains) + + originalContent, err := os.ReadFile(fileDefaultResolvConfBackupLocation) + if err != nil { + log.Errorf("Could not read existing resolv.conf") + } + content := fmt.Sprintf(fileGeneratedResolvConfContentFormat, fileDefaultResolvConfBackupLocation, config.serverIP, searchDomains, string(originalContent)) err = writeDNSConfig(content, defaultResolvConfPath, f.originalPerms) if err != nil { err = f.restore() diff --git a/client/internal/dns/host_darwin.go b/client/internal/dns/host_darwin.go index 0960b7961..f02c32c22 100644 --- a/client/internal/dns/host_darwin.go +++ b/client/internal/dns/host_darwin.go @@ -182,12 +182,11 @@ func (s *systemConfigurator) addDNSState(state, domains, dnsServer string, port } func (s *systemConfigurator) addDNSSetupForAll(dnsServer string, port int) error { - primaryServiceKey := s.getPrimaryService() + primaryServiceKey, existingNameserver := s.getPrimaryService() if primaryServiceKey == "" { return fmt.Errorf("couldn't find the primary service key") } - - err := s.addDNSSetup(getKeyWithInput(primaryServiceSetupKeyFormat, primaryServiceKey), dnsServer, port) + err := s.addDNSSetup(getKeyWithInput(primaryServiceSetupKeyFormat, primaryServiceKey), dnsServer, port, existingNameserver) if err != nil { return err } @@ -196,27 +195,32 @@ func (s *systemConfigurator) addDNSSetupForAll(dnsServer string, port int) error return nil } -func (s *systemConfigurator) getPrimaryService() string { +func (s *systemConfigurator) getPrimaryService() (string, string) { line := buildCommandLine("show", globalIPv4State, "") stdinCommands := wrapCommand(line) b, err := runSystemConfigCommand(stdinCommands) if err != nil { log.Error("got error while sending the command: ", err) - return "" + return "", "" } scanner := bufio.NewScanner(bytes.NewReader(b)) + primaryService := "" + router := "" for scanner.Scan() { text := scanner.Text() if strings.Contains(text, "PrimaryService") { - return strings.TrimSpace(strings.Split(text, ":")[1]) + primaryService = strings.TrimSpace(strings.Split(text, ":")[1]) + } + if strings.Contains(text, "Router") { + router = strings.TrimSpace(strings.Split(text, ":")[1]) } } - return "" + return primaryService, router } -func (s *systemConfigurator) addDNSSetup(setupKey, dnsServer string, port int) error { +func (s *systemConfigurator) addDNSSetup(setupKey, dnsServer string, port int, existingDNSServer string) error { lines := buildAddCommandLine(keySupplementalMatchDomainsNoSearch, digitSymbol+strconv.Itoa(0)) - lines += buildAddCommandLine(keyServerAddresses, arraySymbol+dnsServer) + lines += buildAddCommandLine(keyServerAddresses, arraySymbol+dnsServer+" "+existingDNSServer) lines += buildAddCommandLine(keyServerPort, digitSymbol+strconv.Itoa(port)) addDomainCommand := buildCreateStateWithOperation(setupKey, lines) stdinCommands := wrapCommand(addDomainCommand) diff --git a/client/internal/dns/resolvconf_linux.go b/client/internal/dns/resolvconf_linux.go index d50ca4550..b358d3bd5 100644 --- a/client/internal/dns/resolvconf_linux.go +++ b/client/internal/dns/resolvconf_linux.go @@ -4,6 +4,7 @@ package dns import ( "fmt" + "os" "os/exec" "strings" @@ -59,7 +60,11 @@ func (r *resolvconf) applyDNSConfig(config hostDNSConfig) error { appendedDomains++ } - content := fmt.Sprintf(fileGeneratedResolvConfContentFormat, fileDefaultResolvConfBackupLocation, config.serverIP, searchDomains) + originalContent, err := os.ReadFile(fileDefaultResolvConfBackupLocation) + if err != nil { + log.Errorf("Could not read existing resolv.conf") + } + content := fmt.Sprintf(fileGeneratedResolvConfContentFormat, fileDefaultResolvConfBackupLocation, config.serverIP, searchDomains, string(originalContent)) err = r.applyConfig(content) if err != nil { diff --git a/client/internal/dns/systemd_linux.go b/client/internal/dns/systemd_linux.go index 0358b0251..4d146130f 100644 --- a/client/internal/dns/systemd_linux.go +++ b/client/internal/dns/systemd_linux.go @@ -13,8 +13,6 @@ import ( "github.com/miekg/dns" log "github.com/sirupsen/logrus" "golang.org/x/sys/unix" - - nbdns "github.com/netbirdio/netbird/dns" ) const ( @@ -123,10 +121,6 @@ func (s *systemdDbusConfigurator) applyDNSConfig(config hostDNSConfig) error { if err != nil { return fmt.Errorf("setting link as default dns router, failed with error: %s", err) } - domainsInput = append(domainsInput, systemdDbusLinkDomainsInput{ - Domain: nbdns.RootZone, - MatchOnly: true, - }) s.routingAll = true } else if s.routingAll { log.Infof("removing %s:%d as main DNS forwarder for this peer", config.serverIP, config.serverPort)