mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-21 12:59:08 +02:00
* [client] Split resolv.conf parsing from file reading Pure refactor, no behavior change. parseResolvConf parses content that was already read, so a caller can compare and parse the same snapshot of the file instead of reading it twice. * [client] Return the written content from the resolv.conf update function Pure refactor, no behavior change. The repair watcher needs to know what NetBird wrote so it can tell its own write apart from a third party's. Callers ignore the content for now. * [client] Keep the resolv.conf watch armed while repairing In file mode the repair watcher removed its inotify watch, rewrote resolv.conf and re-added the watch, without looking at the file again. A second third-party write landing in that window raised no event, so the file stayed on the upstream resolver until something else touched it. systemd-resolved with the stub listener off, the Amazon Linux 2023 default, writes the file twice in a row on restart and does not rewrite it on an unchanged DHCP renewal, so the host stayed off NetBird DNS while the client reported connected. The watch now stays armed during the repair. NetBird's own write is recognized by comparing the file with the content it wrote, which also covers a write whose search list was truncated to the resolv.conf limits and would otherwise fail the parameter check forever. The watcher checks the file once when it is armed, since a change made before that produced no event. * [client] Consume inotify errors in the resolv.conf watcher fsnotify blocks its reader until an error is received. The watcher never read the Errors channel, so a single error, such as an event queue overflow, stopped all further events and silently disabled the repair. The watcher now consumes errors and checks the file, since an overflow means events were dropped. * [client] Parse resolv.conf one line at a time Pure refactor, no behavior change. The per-line handling moves into parseResolvConfLine, which keeps parseResolvConf under the cognitive complexity limit. * [client] Drop the comment restating parseResolvConfLine
126 lines
2.8 KiB
Go
126 lines
2.8 KiB
Go
//go:build (linux && !android) || freebsd
|
|
|
|
package dns
|
|
|
|
import (
|
|
"fmt"
|
|
"net/netip"
|
|
"os"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const (
|
|
defaultResolvConfPath = "/etc/resolv.conf"
|
|
nsswitchConfPath = "/etc/nsswitch.conf"
|
|
)
|
|
|
|
type resolvConf struct {
|
|
nameServers []netip.Addr
|
|
searchDomains []string
|
|
others []string
|
|
}
|
|
|
|
func (r *resolvConf) String() string {
|
|
return fmt.Sprintf("search domains: %v, name servers: %v, others: %s", r.searchDomains, r.nameServers, r.others)
|
|
}
|
|
|
|
func parseDefaultResolvConf() (*resolvConf, error) {
|
|
return parseResolvConfFile(defaultResolvConfPath)
|
|
}
|
|
|
|
func parseBackupResolvConf() (*resolvConf, error) {
|
|
return parseResolvConfFile(fileDefaultResolvConfBackupLocation)
|
|
}
|
|
|
|
func parseResolvConfFile(resolvConfFile string) (*resolvConf, error) {
|
|
rconf := newResolvConf()
|
|
|
|
file, err := os.Open(resolvConfFile)
|
|
if err != nil {
|
|
return rconf, fmt.Errorf("failed to open %s file: %w", resolvConfFile, err)
|
|
}
|
|
defer func() {
|
|
if err := file.Close(); err != nil {
|
|
log.Errorf("failed closing %s: %s", resolvConfFile, err)
|
|
}
|
|
}()
|
|
|
|
cur, err := os.ReadFile(resolvConfFile)
|
|
if err != nil {
|
|
return rconf, fmt.Errorf("failed to read %s file: %w", resolvConfFile, err)
|
|
}
|
|
|
|
return parseResolvConf(cur)
|
|
}
|
|
|
|
// parseResolvConf parses resolv.conf content. The returned config is never nil, so callers can
|
|
// fall back to its empty lists on error.
|
|
func parseResolvConf(cur []byte) (*resolvConf, error) {
|
|
rconf := newResolvConf()
|
|
|
|
if len(cur) == 0 {
|
|
return rconf, fmt.Errorf("file is empty")
|
|
}
|
|
|
|
for _, line := range strings.Split(string(cur), "\n") {
|
|
parseResolvConfLine(rconf, strings.TrimSpace(line))
|
|
}
|
|
return rconf, nil
|
|
}
|
|
|
|
func parseResolvConfLine(rconf *resolvConf, line string) {
|
|
if strings.HasPrefix(line, "#") {
|
|
return
|
|
}
|
|
|
|
if strings.HasPrefix(line, "domain") {
|
|
return
|
|
}
|
|
|
|
if strings.HasPrefix(line, "options") && strings.Contains(line, "rotate") {
|
|
line = strings.ReplaceAll(line, "rotate", "")
|
|
splitLines := strings.Fields(line)
|
|
if len(splitLines) == 1 {
|
|
return
|
|
}
|
|
line = strings.Join(splitLines, " ")
|
|
}
|
|
|
|
if strings.HasPrefix(line, "search") {
|
|
splitLines := strings.Fields(line)
|
|
if len(splitLines) < 2 {
|
|
return
|
|
}
|
|
|
|
rconf.searchDomains = splitLines[1:]
|
|
return
|
|
}
|
|
|
|
if strings.HasPrefix(line, "nameserver") {
|
|
splitLines := strings.Fields(line)
|
|
if len(splitLines) != 2 {
|
|
return
|
|
}
|
|
if addr, err := netip.ParseAddr(splitLines[1]); err == nil {
|
|
rconf.nameServers = append(rconf.nameServers, addr.Unmap())
|
|
} else {
|
|
log.Warnf("invalid nameserver address in resolv.conf: %s, skipping", splitLines[1])
|
|
}
|
|
return
|
|
}
|
|
|
|
if line != "" {
|
|
rconf.others = append(rconf.others, line)
|
|
}
|
|
}
|
|
|
|
func newResolvConf() *resolvConf {
|
|
return &resolvConf{
|
|
searchDomains: make([]string, 0),
|
|
nameServers: make([]netip.Addr, 0),
|
|
others: make([]string, 0),
|
|
}
|
|
}
|