feat(dns): use system nameservers from /etc/resolv.conf as fallback
When no forwarder or dns metadata is configured, the DNS handler now reads system nameservers from /etc/resolv.conf (Unix) before falling back to the hardcoded 127.0.0.1:53 default. This makes the DNS proxy work out-of-the-box without requiring explicit DNS server configuration. Fixes go-gost/gost#89
This commit is contained in:
@@ -108,6 +108,23 @@ func (h *dnsHandler) Init(md md.Metadata) (err error) {
|
||||
h.exchangers[node.Name] = ex
|
||||
}
|
||||
|
||||
if len(h.exchangers) == 0 {
|
||||
// Try system nameservers first (from /etc/resolv.conf on Unix).
|
||||
for i, addr := range systemNameservers() {
|
||||
ex, err := exchanger.NewExchanger(
|
||||
addr,
|
||||
exchanger.RouterOption(h.options.Router),
|
||||
exchanger.TimeoutOption(h.md.timeout),
|
||||
exchanger.LoggerOption(log),
|
||||
)
|
||||
if err != nil {
|
||||
log.Warnf("system nameserver %s: %v", addr, err)
|
||||
continue
|
||||
}
|
||||
h.exchangers[fmt.Sprintf("system-%d", i)] = ex
|
||||
}
|
||||
}
|
||||
|
||||
if len(h.exchangers) == 0 {
|
||||
ex, err := exchanger.NewExchanger(
|
||||
defaultNameserver,
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
//go:build !windows
|
||||
|
||||
package dns
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// systemNameservers reads the system DNS configuration from /etc/resolv.conf
|
||||
// and returns the nameserver addresses as URL-like strings suitable for
|
||||
// exchanger.NewExchanger (e.g., "udp://1.1.1.1:53").
|
||||
// Returns nil if the file cannot be read or contains no nameserver entries.
|
||||
func systemNameservers() []string {
|
||||
f, err := os.Open("/etc/resolv.conf")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var servers []string
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || line[0] == '#' || line[0] == ';' {
|
||||
continue
|
||||
}
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) < 2 || fields[0] != "nameserver" {
|
||||
continue
|
||||
}
|
||||
addr := fields[1]
|
||||
// Verify it's a valid IP address.
|
||||
if ip := net.ParseIP(addr); ip == nil {
|
||||
continue
|
||||
}
|
||||
// If no port, default to 53.
|
||||
if _, _, err := net.SplitHostPort(addr); err != nil {
|
||||
addr = net.JoinHostPort(addr, "53")
|
||||
}
|
||||
servers = append(servers, fmt.Sprintf("udp://%s", addr))
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil
|
||||
}
|
||||
return servers
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package dns
|
||||
|
||||
// systemNameservers returns nil on Windows because reading the system DNS
|
||||
// configuration requires the Windows IP Helper API. Callers should fall
|
||||
// back to the hardcoded default when this returns nil.
|
||||
func systemNameservers() []string {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user