2023-06-12 14:43:55 +02:00
|
|
|
//go:build !android
|
|
|
|
|
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"
|
|
|
|
"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
|
2023-07-14 21:56:22 +02:00
|
|
|
func newResolvConfConfigurator(wgInterface WGIface) (hostManager, error) {
|
2023-11-03 13:05:39 +01:00
|
|
|
originalSearchDomains, nameServers, others, err := originalDNSConfigs("/etc/resolv.conf")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-11-29 14:51:18 +01:00
|
|
|
return &resolvconf{
|
2023-11-03 13:05:39 +01:00
|
|
|
ifaceName: wgInterface.Name(),
|
|
|
|
originalSearchDomains: originalSearchDomains,
|
|
|
|
originalNameServers: nameServers,
|
|
|
|
othersConfigs: 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
|
|
|
|
}
|
|
|
|
|
2022-11-29 14:51:18 +01:00
|
|
|
func (r *resolvconf) applyDNSConfig(config hostDNSConfig) error {
|
|
|
|
var err error
|
|
|
|
if !config.routeAll {
|
|
|
|
err = r.restoreHostDNS()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2023-11-03 13:05:39 +01:00
|
|
|
buf := prepareResolvConfContent(
|
|
|
|
searchDomainList,
|
|
|
|
append([]string{config.serverIP}, r.originalNameServers...),
|
|
|
|
r.othersConfigs)
|
2022-11-29 14:51:18 +01:00
|
|
|
|
2023-11-03 13:05:39 +01:00
|
|
|
err = r.applyConfig(buf)
|
2022-11-29 14:51:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
cmd := exec.Command(resolvconfCommand, "-f", "-d", r.ifaceName)
|
|
|
|
_, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("got an error while removing resolvconf configuration for %s interface, error: %s", r.ifaceName, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-03 13:05:39 +01:00
|
|
|
func (r *resolvconf) applyConfig(content bytes.Buffer) error {
|
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 {
|
2023-11-01 17:11:16 +01:00
|
|
|
return fmt.Errorf("got an error while applying resolvconf configuration for %s interface, error: %s", r.ifaceName, err)
|
2022-11-29 14:51:18 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|