feat: Handle nameserver parsing on Windows

* system default nameserver in windows

* change
This commit is contained in:
huangnauh 2021-01-20 18:02:15 +08:00 committed by GitHub
parent 250591098b
commit 3019f1cee6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 166 additions and 18 deletions

30
pkg/config/config_unix.go Normal file
View file

@ -0,0 +1,30 @@
// +build !windows
package config
import (
"net"
"github.com/miekg/dns"
)
// DefaultResolvConfPath specifies path to default resolv config file on UNIX.
const DefaultResolvConfPath = "/etc/resolv.conf"
// GetDefaultServers get system default nameserver
func GetDefaultServers() ([]string, int, []string, error) {
// if no nameserver is provided, take it from `resolv.conf`
cfg, err := dns.ClientConfigFromFile(DefaultResolvConfPath)
if err != nil {
return nil, 0, nil, err
}
servers := make([]string, 0)
for _, server := range cfg.Servers {
ip := net.ParseIP(server)
if isUnicastLinkLocal(ip) {
continue
}
servers = append(servers, server)
}
return servers, cfg.Ndots, cfg.Search, nil
}