52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
// SPDX-License-Identifier: BSD-3-Clause
|
|
//go:build !windows
|
|
// +build !windows
|
|
|
|
package conf
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
// Get the DNS configuration, either from /etc/resolv.conf or somewhere else
|
|
func GetDNSConfig() (*dns.ClientConfig, error) {
|
|
if runtime.GOOS == "plan9" {
|
|
dat, err := os.ReadFile("/net/ndb")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return getPlan9Config(string(dat))
|
|
} else {
|
|
return dns.ClientConfigFromFile("/etc/resolv.conf")
|
|
}
|
|
}
|
|
|
|
// Plan 9 stores its network data in /net/ndb, which seems to be formatted a specific way
|
|
func getPlan9Config(str string) (*dns.ClientConfig, error) {
|
|
str = strings.ReplaceAll(str, "\n", "")
|
|
spl := strings.FieldsFunc(str, split)
|
|
servers := []string{}
|
|
for _, option := range spl {
|
|
if strings.HasPrefix(option, "dns=") {
|
|
servers = append(servers, strings.TrimPrefix(option, "dns="))
|
|
}
|
|
}
|
|
|
|
// TODO: read more about how customizable Plan 9 is
|
|
return &dns.ClientConfig{
|
|
Servers: servers,
|
|
Search: []string{},
|
|
Port: "53",
|
|
Ndots: 1,
|
|
Timeout: 5,
|
|
Attempts: 1,
|
|
}, nil
|
|
}
|
|
|
|
func split(r rune) bool {
|
|
return r == ' ' || r == '\t'
|
|
}
|