@@ -25,6 +25,11 @@ const (
2525 resolvconfUplinkPath = "/run/systemd/resolve/resolv.conf"
2626)
2727
28+ const (
29+ configStartMarker = "# NETMAKER DNS CONFIG START"
30+ configEndMarker = "# NETMAKER DNS CONFIG END"
31+ )
32+
2833func isStubSupported () bool {
2934 return config .Netclient ().DNSManagerType == DNS_MANAGER_STUB
3035}
@@ -63,6 +68,7 @@ func SetupDNSConfig() (err error) {
6368 } else if isUplinkSupported () {
6469 err = setupResolveUplink ()
6570 } else if isResolveconfSupported () {
71+ err = setupResolveconf ()
6672 } else {
6773 err = setupResolveconf ()
6874 }
@@ -82,6 +88,7 @@ func RestoreDNSConfig() (err error) {
8288 } else if isUplinkSupported () {
8389 err = restoreResolveUplink ()
8490 } else if isResolveconfSupported () {
91+ err = restoreResolveconf ()
8592 } else {
8693 err = restoreResolveconf ()
8794 }
@@ -257,15 +264,36 @@ func buildAddConfigContent() ([]string, error) {
257264 }
258265 lines := strings .Split (string (rawBytes ), "\n " )
259266 lNo := 0
267+ var foundMarkers bool
260268 for i , line := range lines {
269+ // If we found the start marker, we need to replace the content.
270+ if strings .HasPrefix (line , configStartMarker ) {
271+ lNo = i
272+ foundMarkers = true
273+ break
274+ }
275+
261276 if strings .HasPrefix (line , "nameserver" ) {
262277 lNo = i
263278 break
264279 }
265280 }
266281
267- lines = slices .Insert (lines , lNo , ns )
268- lines = slices .Insert (lines , lNo , domains )
282+ if foundMarkers && len (lines ) > lNo + 2 {
283+ lines [lNo + 1 ] = domains
284+ lines [lNo + 2 ] = ns
285+ } else {
286+ // Yes, we add the end marker first, then the config, and then the start marker.
287+ // We always insert at lNo index, so at the end the config will be:
288+ // lNo: configStartMarker
289+ // lNo+1: search <domain>
290+ // lNo+2: nameserver <nameserver>
291+ // lNo+3: configEndMarker
292+ lines = slices .Insert (lines , lNo , configEndMarker )
293+ lines = slices .Insert (lines , lNo , ns )
294+ lines = slices .Insert (lines , lNo , domains )
295+ lines = slices .Insert (lines , lNo , configStartMarker )
296+ }
269297
270298 return lines , nil
271299}
@@ -435,7 +463,14 @@ func buildDeleteConfigContent() ([]string, error) {
435463
436464 var lNo int
437465 var found bool
466+ var foundMarkers bool
438467 for i , line := range lines {
468+ if strings .Contains (line , configStartMarker ) {
469+ lNo = i
470+ found = true
471+ foundMarkers = true
472+ break
473+ }
439474 if strings .Contains (line , domains ) {
440475 lNo = i
441476 found = true
@@ -444,8 +479,12 @@ func buildDeleteConfigContent() ([]string, error) {
444479 }
445480
446481 if found {
447- lines = slices .Delete (lines , lNo , lNo + 1 )
448- lines = slices .Delete (lines , lNo , lNo + 1 )
482+ if foundMarkers && len (lines ) > lNo + 3 {
483+ lines = slices .Delete (lines , lNo , lNo + 4 )
484+ } else {
485+ lines = slices .Delete (lines , lNo , lNo + 1 )
486+ lines = slices .Delete (lines , lNo , lNo + 1 )
487+ }
449488 }
450489
451490 return lines , nil
0 commit comments