mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-03-13 10:26:06 +00:00
By adding a logger field to "prog" struct, and use this field inside its method instead of always accessing global mainLog variable. This at least ensure more consistent usage of the logger during ctrld prog runtime, and also help refactoring the code more easily in the future (like replacing the logger library).
50 lines
1.0 KiB
Go
50 lines
1.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"net"
|
|
"net/netip"
|
|
"os"
|
|
"slices"
|
|
|
|
"github.com/Control-D-Inc/ctrld/internal/dns/resolvconffile"
|
|
)
|
|
|
|
const resolvConfPath = "/etc/resolv.conf"
|
|
|
|
// setResolvConf sets the content of resolv.conf file using the given nameservers list.
|
|
func (p *prog) setResolvConf(iface *net.Interface, ns []netip.Addr) error {
|
|
servers := make([]string, len(ns))
|
|
for i := range ns {
|
|
servers[i] = ns[i].String()
|
|
}
|
|
if err := setDNS(iface, servers); err != nil {
|
|
return err
|
|
}
|
|
slices.Sort(servers)
|
|
curNs := currentDNS(iface)
|
|
slices.Sort(curNs)
|
|
if !slices.Equal(curNs, servers) {
|
|
c, err := resolvconffile.ParseFile(resolvConfPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.Nameservers = ns
|
|
f, err := os.Create(resolvConfPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
if err := c.Write(f); err != nil {
|
|
return err
|
|
}
|
|
return f.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// shouldWatchResolvconf reports whether ctrld should watch changes to resolv.conf file with given OS configurator.
|
|
func shouldWatchResolvconf() bool {
|
|
return true
|
|
}
|