mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-16 13:17:19 +02:00
cmd/cli: use resolvconffile lib for parsing
This commit is contained in:
committed by
Cuong Manh Le
parent
aaf31b6471
commit
95699fa4a1
+3
-21
@@ -3,36 +3,18 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
|
|
||||||
|
"github.com/Control-D-Inc/ctrld/internal/resolvconffile"
|
||||||
)
|
)
|
||||||
|
|
||||||
// parseResolvConfNameservers reads the resolv.conf file and returns the nameservers found.
|
// parseResolvConfNameservers reads the resolv.conf file and returns the nameservers found.
|
||||||
// Returns nil if no nameservers are found.
|
// Returns nil if no nameservers are found.
|
||||||
func (p *prog) parseResolvConfNameservers(path string) ([]string, error) {
|
func (p *prog) parseResolvConfNameservers(path string) ([]string, error) {
|
||||||
content, err := os.ReadFile(path)
|
return resolvconffile.NameserversFromFile(path)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse the file for "nameserver" lines
|
|
||||||
var currentNS []string
|
|
||||||
lines := strings.Split(string(content), "\n")
|
|
||||||
for _, line := range lines {
|
|
||||||
trimmed := strings.TrimSpace(line)
|
|
||||||
if strings.HasPrefix(trimmed, "nameserver") {
|
|
||||||
parts := strings.Fields(trimmed)
|
|
||||||
if len(parts) >= 2 {
|
|
||||||
currentNS = append(currentNS, parts[1])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return currentNS, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// watchResolvConf watches any changes to /etc/resolv.conf file,
|
// watchResolvConf watches any changes to /etc/resolv.conf file,
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
//go:build unix
|
||||||
|
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/Control-D-Inc/ctrld/internal/dns/resolvconffile"
|
||||||
|
)
|
||||||
|
|
||||||
|
func oldParseResolvConfNameservers(path string) ([]string, error) {
|
||||||
|
content, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the file for "nameserver" lines
|
||||||
|
var currentNS []string
|
||||||
|
lines := strings.Split(string(content), "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
trimmed := strings.TrimSpace(line)
|
||||||
|
if strings.HasPrefix(trimmed, "nameserver") {
|
||||||
|
parts := strings.Fields(trimmed)
|
||||||
|
if len(parts) >= 2 {
|
||||||
|
currentNS = append(currentNS, parts[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentNS, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_prog_parseResolvConfNameservers(t *testing.T) {
|
||||||
|
oldNss, _ := oldParseResolvConfNameservers(resolvconffile.Path)
|
||||||
|
p := &prog{}
|
||||||
|
nss, _ := p.parseResolvConfNameservers(resolvconffile.Path)
|
||||||
|
slices.Sort(oldNss)
|
||||||
|
slices.Sort(nss)
|
||||||
|
if !slices.Equal(oldNss, nss) {
|
||||||
|
t.Errorf("result mismatched, old: %v, new: %v", oldNss, nss)
|
||||||
|
}
|
||||||
|
t.Logf("result: %v", nss)
|
||||||
|
}
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
//go:build !js && !windows
|
|
||||||
|
|
||||||
package resolvconffile
|
package resolvconffile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -24,15 +22,20 @@ func NameServersWithPort() []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NameServers() []string {
|
func NameServers() []string {
|
||||||
c, err := resolvconffile.ParseFile(resolvconfPath)
|
nss, _ := NameserversFromFile(resolvconfPath)
|
||||||
|
return nss
|
||||||
|
}
|
||||||
|
|
||||||
|
func NameserversFromFile(path string) ([]string, error) {
|
||||||
|
c, err := resolvconffile.ParseFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil, err
|
||||||
}
|
}
|
||||||
ns := make([]string, 0, len(c.Nameservers))
|
ns := make([]string, 0, len(c.Nameservers))
|
||||||
for _, nameserver := range c.Nameservers {
|
for _, nameserver := range c.Nameservers {
|
||||||
ns = append(ns, nameserver.String())
|
ns = append(ns, nameserver.String())
|
||||||
}
|
}
|
||||||
return ns
|
return ns, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchDomains returns the current search domains config in /etc/resolv.conf file.
|
// SearchDomains returns the current search domains config in /etc/resolv.conf file.
|
||||||
|
|||||||
Reference in New Issue
Block a user