2024-06-13 13:24:24 +02:00
|
|
|
//go:build (linux && !android) || freebsd
|
2023-06-12 14:43:55 +02:00
|
|
|
|
2022-11-29 14:51:18 +01:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2023-11-03 13:05:39 +01:00
|
|
|
"bytes"
|
2022-11-29 14:51:18 +01:00
|
|
|
"fmt"
|
2024-01-30 09:58:56 +01:00
|
|
|
"net/netip"
|
2022-11-29 14:51:18 +01:00
|
|
|
"os/exec"
|
2023-02-13 15:25:11 +01:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-11-29 14:51:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const resolvconfCommand = "resolvconf"
|
|
|
|
|
|
|
|
type resolvconf struct {
|
|
|
|
ifaceName string
|
2023-11-03 13:05:39 +01:00
|
|
|
|
|
|
|
originalSearchDomains []string
|
|
|
|
originalNameServers []string
|
|
|
|
othersConfigs []string
|
2022-11-29 14:51:18 +01:00
|
|
|
}
|
|
|
|
|
2023-11-03 13:05:39 +01:00
|
|
|
// supported "openresolv" only
|
2024-01-30 09:58:56 +01:00
|
|
|
func newResolvConfConfigurator(wgInterface string) (hostManager, error) {
|
2024-01-24 16:47:26 +01:00
|
|
|
resolvConfEntries, err := parseDefaultResolvConf()
|
2023-11-03 13:05:39 +01:00
|
|
|
if err != nil {
|
2024-01-30 09:58:56 +01:00
|
|
|
log.Errorf("could not read original search domains from %s: %s", defaultResolvConfPath, err)
|
2023-11-03 13:05:39 +01:00
|
|
|
}
|
|
|
|
|
2022-11-29 14:51:18 +01:00
|
|
|
return &resolvconf{
|
2024-01-30 09:58:56 +01:00
|
|
|
ifaceName: wgInterface,
|
2024-01-24 16:47:26 +01:00
|
|
|
originalSearchDomains: resolvConfEntries.searchDomains,
|
|
|
|
originalNameServers: resolvConfEntries.nameServers,
|
|
|
|
othersConfigs: resolvConfEntries.others,
|
2022-11-29 14:51:18 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-17 00:03:26 +02:00
|
|
|
func (r *resolvconf) supportCustomPort() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-12-18 11:46:58 +01:00
|
|
|
func (r *resolvconf) applyDNSConfig(config HostDNSConfig) error {
|
2022-11-29 14:51:18 +01:00
|
|
|
var err error
|
2023-12-18 11:46:58 +01:00
|
|
|
if !config.RouteAll {
|
2022-11-29 14:51:18 +01:00
|
|
|
err = r.restoreHostDNS()
|
|
|
|
if err != nil {
|
2024-01-30 09:58:56 +01:00
|
|
|
log.Errorf("restore host dns: %s", err)
|
2022-11-29 14:51:18 +01:00
|
|
|
}
|
|
|
|
return fmt.Errorf("unable to configure DNS for this peer using resolvconf manager without a nameserver group with all domains configured")
|
|
|
|
}
|
|
|
|
|
2023-11-03 13:05:39 +01:00
|
|
|
searchDomainList := searchDomains(config)
|
|
|
|
searchDomainList = mergeSearchDomains(searchDomainList, r.originalSearchDomains)
|
2022-11-29 14:51:18 +01:00
|
|
|
|
2024-03-01 15:17:35 +01:00
|
|
|
options := prepareOptionsWithTimeout(r.othersConfigs, int(dnsFailoverTimeout.Seconds()), dnsFailoverAttempts)
|
|
|
|
|
2023-11-03 13:05:39 +01:00
|
|
|
buf := prepareResolvConfContent(
|
|
|
|
searchDomainList,
|
2023-12-18 11:46:58 +01:00
|
|
|
append([]string{config.ServerIP}, r.originalNameServers...),
|
2024-03-01 15:17:35 +01:00
|
|
|
options)
|
2022-11-29 14:51:18 +01:00
|
|
|
|
2024-01-30 09:58:56 +01:00
|
|
|
// create a backup for unclean shutdown detection before the resolv.conf is changed
|
|
|
|
if err := createUncleanShutdownIndicator(defaultResolvConfPath, resolvConfManager, config.ServerIP); err != nil {
|
|
|
|
log.Errorf("failed to create unclean shutdown resolv.conf backup: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-11-03 13:05:39 +01:00
|
|
|
err = r.applyConfig(buf)
|
2022-11-29 14:51:18 +01:00
|
|
|
if err != nil {
|
2024-01-30 09:58:56 +01:00
|
|
|
return fmt.Errorf("apply config: %w", err)
|
2022-11-29 14:51:18 +01:00
|
|
|
}
|
|
|
|
|
2023-11-03 13:05:39 +01:00
|
|
|
log.Infof("added %d search domains. Search list: %s", len(searchDomainList), searchDomainList)
|
2022-11-29 14:51:18 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolvconf) restoreHostDNS() error {
|
2024-01-30 09:58:56 +01:00
|
|
|
// openresolv only, debian resolvconf doesn't support "-f"
|
2022-11-29 14:51:18 +01:00
|
|
|
cmd := exec.Command(resolvconfCommand, "-f", "-d", r.ifaceName)
|
|
|
|
_, err := cmd.Output()
|
|
|
|
if err != nil {
|
2024-01-30 09:58:56 +01:00
|
|
|
return fmt.Errorf("removing resolvconf configuration for %s interface, error: %w", r.ifaceName, err)
|
2022-11-29 14:51:18 +01:00
|
|
|
}
|
2024-01-30 09:58:56 +01:00
|
|
|
|
|
|
|
if err := removeUncleanShutdownIndicator(); err != nil {
|
|
|
|
log.Errorf("failed to remove unclean shutdown resolv.conf backup: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-11-29 14:51:18 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-03 13:05:39 +01:00
|
|
|
func (r *resolvconf) applyConfig(content bytes.Buffer) error {
|
2024-01-30 09:58:56 +01:00
|
|
|
// openresolv only, debian resolvconf doesn't support "-x"
|
2022-11-29 14:51:18 +01:00
|
|
|
cmd := exec.Command(resolvconfCommand, "-x", "-a", r.ifaceName)
|
2023-11-03 13:05:39 +01:00
|
|
|
cmd.Stdin = &content
|
2022-11-29 14:51:18 +01:00
|
|
|
_, err := cmd.Output()
|
|
|
|
if err != nil {
|
2024-01-30 09:58:56 +01:00
|
|
|
return fmt.Errorf("applying resolvconf configuration for %s interface, error: %w", r.ifaceName, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolvconf) restoreUncleanShutdownDNS(*netip.Addr) error {
|
|
|
|
if err := r.restoreHostDNS(); err != nil {
|
|
|
|
return fmt.Errorf("restoring dns for interface %s: %w", r.ifaceName, err)
|
2022-11-29 14:51:18 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|