diff --git a/AGHTechDoc.md b/AGHTechDoc.md index c3b4b249..3045d6f7 100644 --- a/AGHTechDoc.md +++ b/AGHTechDoc.md @@ -202,9 +202,21 @@ If user clicks on "Fix" button, UI sends request to perform an automatic fix "dns":{"port":53,"ip":"127.0.0.1","autofix":true}, } -Deactivate (save backup as `resolved.conf.orig`) and stop DNSStubListener: +Deactivate DNSStubListener and update DNS server address. Create a new file: `/etc/systemd/resolved.conf.d/adguardhome.conf` (create a `/etc/systemd/resolved.conf.d` directory if necessary): + + [Resolve] + DNS=127.0.0.1 + DNSStubListener=no + +Specifying "127.0.0.1" as DNS server address is necessry because otherwise the nameserver will be "127.0.0.53" which doesn't work without DNSStubListener. + +Activate another resolv.conf file: + + mv /etc/resolv.conf /etc/resolv.conf.backup + ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf + +Stop DNSStubListener: - sed -r -i.orig 's/#?DNSStubListener=yes/DNSStubListener=no/g' /etc/systemd/resolved.conf systemctl reload-or-restart systemd-resolved Server replies: diff --git a/home/control_install.go b/home/control_install.go index fe811ba0..5311c091 100644 --- a/home/control_install.go +++ b/home/control_install.go @@ -4,9 +4,12 @@ import ( "context" "encoding/json" "fmt" + "io/ioutil" "net" "net/http" + "os" "os/exec" + "path/filepath" "runtime" "strconv" @@ -143,20 +146,34 @@ func checkDNSStubListener() bool { return true } +const resolvedConfPath = "/etc/systemd/resolved.conf.d/adguardhome.conf" +const resolvedConfData = `[Resolve] +DNS=127.0.0.1 +DNSStubListener=no +` +const resolvConfPath = "/etc/resolv.conf" + // Deactivate DNSStubListener func disableDNSStubListener() error { - cmd := exec.Command("sed", "-r", "-i.orig", "s/#?DNSStubListener=yes/DNSStubListener=no/g", "/etc/systemd/resolved.conf") - log.Tracef("executing %s %v", cmd.Path, cmd.Args) - _, err := cmd.Output() + dir := filepath.Dir(resolvedConfPath) + err := os.MkdirAll(dir, 0755) if err != nil { - return err - } - if cmd.ProcessState.ExitCode() != 0 { - return fmt.Errorf("process %s exited with an error: %d", - cmd.Path, cmd.ProcessState.ExitCode()) + return fmt.Errorf("os.MkdirAll: %s: %s", dir, err) } - cmd = exec.Command("systemctl", "reload-or-restart", "systemd-resolved") + err = ioutil.WriteFile(resolvedConfPath, []byte(resolvedConfData), 0644) + if err != nil { + return fmt.Errorf("ioutil.WriteFile: %s: %s", resolvedConfPath, err) + } + + _ = os.Rename(resolvConfPath, resolvConfPath+".backup") + err = os.Symlink("/run/systemd/resolve/resolv.conf", resolvConfPath) + if err != nil { + _ = os.Remove(resolvedConfPath) // remove the file we've just created + return fmt.Errorf("os.Symlink: %s: %s", resolvConfPath, err) + } + + cmd := exec.Command("systemctl", "reload-or-restart", "systemd-resolved") log.Tracef("executing %s %v", cmd.Path, cmd.Args) _, err = cmd.Output() if err != nil {