mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
cmd/cli: ignore un-usable interfaces on darwin
So multi interfaces config won't emit un-necessary errors if the network cable adapters are not being used on MacOS.
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/Control-D-Inc/ctrld/internal/resolvconffile"
|
"github.com/Control-D-Inc/ctrld/internal/resolvconffile"
|
||||||
)
|
)
|
||||||
@@ -30,6 +31,18 @@ func deAllocateIP(ip string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setDnsIgnoreUnusableInterface likes setDNS, but return a nil error if the interface is not usable.
|
||||||
|
func setDnsIgnoreUnusableInterface(iface *net.Interface, nameservers []string) error {
|
||||||
|
if err := setDNS(iface, nameservers); err != nil {
|
||||||
|
// TODO: investiate whether we can detect this without relying on error message.
|
||||||
|
if strings.Contains(err.Error(), " is not a recognized network service") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// set the dns server for the provided network interface
|
// set the dns server for the provided network interface
|
||||||
// networksetup -setdnsservers Wi-Fi 8.8.8.8 1.1.1.1
|
// networksetup -setdnsservers Wi-Fi 8.8.8.8 1.1.1.1
|
||||||
// TODO(cuonglm): use system API
|
// TODO(cuonglm): use system API
|
||||||
|
|||||||
@@ -29,6 +29,11 @@ func deAllocateIP(ip string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setDnsIgnoreUnusableInterface likes setDNS, but return a nil error if the interface is not usable.
|
||||||
|
func setDnsIgnoreUnusableInterface(iface *net.Interface, nameservers []string) error {
|
||||||
|
return setDNS(iface, nameservers)
|
||||||
|
}
|
||||||
|
|
||||||
// set the dns server for the provided network interface
|
// set the dns server for the provided network interface
|
||||||
func setDNS(iface *net.Interface, nameservers []string) error {
|
func setDNS(iface *net.Interface, nameservers []string) error {
|
||||||
r, err := dns.NewOSConfigurator(logf, iface.Name)
|
r, err := dns.NewOSConfigurator(logf, iface.Name)
|
||||||
|
|||||||
+5
-1
@@ -45,7 +45,11 @@ func deAllocateIP(ip string) error {
|
|||||||
|
|
||||||
const maxSetDNSAttempts = 5
|
const maxSetDNSAttempts = 5
|
||||||
|
|
||||||
// set the dns server for the provided network interface
|
// setDnsIgnoreUnusableInterface likes setDNS, but return a nil error if the interface is not usable.
|
||||||
|
func setDnsIgnoreUnusableInterface(iface *net.Interface, nameservers []string) error {
|
||||||
|
return setDNS(iface, nameservers)
|
||||||
|
}
|
||||||
|
|
||||||
func setDNS(iface *net.Interface, nameservers []string) error {
|
func setDNS(iface *net.Interface, nameservers []string) error {
|
||||||
r, err := dns.NewOSConfigurator(logf, iface.Name)
|
r, err := dns.NewOSConfigurator(logf, iface.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -26,6 +26,12 @@ var (
|
|||||||
resetDNSOnce sync.Once
|
resetDNSOnce sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// setDnsIgnoreUnusableInterface likes setDNS, but return a nil error if the interface is not usable.
|
||||||
|
func setDnsIgnoreUnusableInterface(iface *net.Interface, nameservers []string) error {
|
||||||
|
return setDNS(iface, nameservers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// setDNS sets the dns server for the provided network interface
|
||||||
func setDNS(iface *net.Interface, nameservers []string) error {
|
func setDNS(iface *net.Interface, nameservers []string) error {
|
||||||
if len(nameservers) == 0 {
|
if len(nameservers) == 0 {
|
||||||
return errors.New("empty DNS nameservers")
|
return errors.New("empty DNS nameservers")
|
||||||
|
|||||||
+1
-1
@@ -477,7 +477,7 @@ func (p *prog) setDNS() {
|
|||||||
}
|
}
|
||||||
if allIfaces {
|
if allIfaces {
|
||||||
withEachPhysicalInterfaces(netIface.Name, "set DNS", func(i *net.Interface) error {
|
withEachPhysicalInterfaces(netIface.Name, "set DNS", func(i *net.Interface) error {
|
||||||
return setDNS(i, nameservers)
|
return setDnsIgnoreUnusableInterface(i, nameservers)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user