chore: cleanup system dns code

This commit is contained in:
wwqgtxx
2023-06-01 12:36:53 +08:00
parent 1120c8185d
commit 7fa3d3aa0b
3 changed files with 43 additions and 57 deletions

27
dns/system_posix.go Normal file
View File

@ -0,0 +1,27 @@
//go:build !windows
package dns
import (
"fmt"
"os"
"regexp"
)
var (
// nameserver xxx.xxx.xxx.xxx
nameserverPattern = regexp.MustCompile(`nameserver\s+(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})`)
)
func dnsReadConfig() (servers []string, err error) {
content, err := os.ReadFile("/etc/resolv.conf")
if err != nil {
err = fmt.Errorf("failed to read /etc/resolv.conf: %w", err)
return
}
for _, line := range nameserverPattern.FindAllStringSubmatch(string(content), -1) {
addr := line[1]
servers = append(servers, addr)
}
return
}