doggo/pkg/config/config_unix.go

31 lines
715 B
Go
Raw Permalink Normal View History

// +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
}