2023-06-12 14:43:55 +02:00
|
|
|
//go:build !android
|
|
|
|
|
2022-11-23 13:39:42 +01:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2023-07-14 21:56:22 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-11-23 13:39:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultResolvConfPath = "/etc/resolv.conf"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
netbirdManager osManagerType = iota
|
|
|
|
fileManager
|
|
|
|
networkManager
|
|
|
|
systemdManager
|
|
|
|
resolvConfManager
|
|
|
|
)
|
|
|
|
|
|
|
|
type osManagerType int
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
func (t osManagerType) String() string {
|
|
|
|
switch t {
|
|
|
|
case netbirdManager:
|
|
|
|
return "netbird"
|
|
|
|
case fileManager:
|
|
|
|
return "file"
|
|
|
|
case networkManager:
|
|
|
|
return "networkManager"
|
|
|
|
case systemdManager:
|
|
|
|
return "systemd"
|
|
|
|
case resolvConfManager:
|
|
|
|
return "resolvconf"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
func newHostManager(wgInterface WGIface) (hostManager, error) {
|
2022-11-23 13:39:42 +01:00
|
|
|
osManager, err := getOSDNSManagerType()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
log.Debugf("discovered mode is: %s", osManager)
|
2022-11-23 13:39:42 +01:00
|
|
|
switch osManager {
|
|
|
|
case networkManager:
|
|
|
|
return newNetworkManagerDbusConfigurator(wgInterface)
|
|
|
|
case systemdManager:
|
|
|
|
return newSystemdDbusConfigurator(wgInterface)
|
2022-11-29 14:51:18 +01:00
|
|
|
case resolvConfManager:
|
|
|
|
return newResolvConfConfigurator(wgInterface)
|
2022-11-23 13:39:42 +01:00
|
|
|
default:
|
|
|
|
return newFileConfigurator()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOSDNSManagerType() (osManagerType, error) {
|
|
|
|
|
|
|
|
file, err := os.Open(defaultResolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("unable to open %s for checking owner, got error: %s", defaultResolvConfPath, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
text := scanner.Text()
|
|
|
|
if len(text) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if text[0] != '#' {
|
|
|
|
return fileManager, nil
|
|
|
|
}
|
|
|
|
if strings.Contains(text, fileGeneratedResolvConfContentHeader) {
|
|
|
|
return netbirdManager, nil
|
|
|
|
}
|
|
|
|
if strings.Contains(text, "NetworkManager") && isDbusListenerRunning(networkManagerDest, networkManagerDbusObjectNode) && isNetworkManagerSupported() {
|
|
|
|
return networkManager, nil
|
|
|
|
}
|
|
|
|
if strings.Contains(text, "systemd-resolved") && isDbusListenerRunning(systemdResolvedDest, systemdDbusObjectNode) {
|
|
|
|
return systemdManager, nil
|
|
|
|
}
|
|
|
|
if strings.Contains(text, "resolvconf") {
|
2022-11-29 13:37:50 +01:00
|
|
|
if isDbusListenerRunning(systemdResolvedDest, systemdDbusObjectNode) {
|
|
|
|
var value string
|
|
|
|
err = getSystemdDbusProperty(systemdDbusResolvConfModeProperty, &value)
|
|
|
|
if err == nil {
|
|
|
|
if value == systemdDbusResolvConfModeForeign {
|
|
|
|
return systemdManager, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Errorf("got an error while checking systemd resolv conf mode, error: %s", err)
|
|
|
|
}
|
2022-11-23 13:39:42 +01:00
|
|
|
return resolvConfManager, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fileManager, nil
|
|
|
|
}
|