Clash.Meta/dns/system_posix.go

28 lines
552 B
Go
Raw Normal View History

2023-06-01 12:36:53 +08:00
//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
}