mihomo/dns/system_posix.go

44 lines
788 B
Go
Raw Permalink Normal View History

2023-06-01 12:36:53 +08:00
//go:build !windows
package dns
import (
2023-06-28 09:17:40 +08:00
"bufio"
2023-06-01 12:36:53 +08:00
"fmt"
2023-06-28 09:17:40 +08:00
"net/netip"
2023-06-01 12:36:53 +08:00
"os"
2023-06-28 09:17:40 +08:00
"strings"
2023-06-01 12:36:53 +08:00
)
2023-06-28 09:17:40 +08:00
const resolvConf = "/etc/resolv.conf"
2023-06-01 12:36:53 +08:00
func dnsReadConfig() (servers []string, err error) {
2023-06-28 09:17:40 +08:00
file, err := os.Open(resolvConf)
2023-06-01 12:36:53 +08:00
if err != nil {
2023-06-28 09:17:40 +08:00
err = fmt.Errorf("failed to read %s: %w", resolvConf, err)
2023-06-01 12:36:53 +08:00
return
}
2023-06-28 09:17:40 +08:00
defer func() { _ = file.Close() }()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if len(line) > 0 && (line[0] == ';' || line[0] == '#') {
// comment.
continue
}
f := strings.Fields(line)
if len(f) < 1 {
continue
}
switch f[0] {
case "nameserver": // add one name server
if len(f) > 1 {
if addr, err := netip.ParseAddr(f[1]); err == nil {
servers = append(servers, addr.String())
}
}
}
2023-06-01 12:36:53 +08:00
}
return
}