mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-09-29 13:51:51 +02:00
cmd/cli: only emit error for running interfaces
While at it, also ensure setDNS/resetDNS return a wrapped error on Darwin/Windows, so the caller can decide whether to print the error to users.
This commit is contained in:
1 parent
5145729ab1
commit
fdb82f6ec3
4 files changed
+23
-14
No files matched your search
@@ -2223,3 +2223,8 @@ func absHomeDir(filename string) string {
|
|||||||
}
|
}
|
||||||
return filepath.Join(dir, filename)
|
return filepath.Join(dir, filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ifaceUp reports whether the net interface is up.
|
||||||
|
func ifaceUp(iface *net.Interface) bool {
|
||||||
|
return iface != nil && iface.Flags&net.FlagUp != 0
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
@@ -36,10 +37,8 @@ func setDNS(iface *net.Interface, nameservers []string) error {
|
|||||||
cmd := "networksetup"
|
cmd := "networksetup"
|
||||||
args := []string{"-setdnsservers", iface.Name}
|
args := []string{"-setdnsservers", iface.Name}
|
||||||
args = append(args, nameservers...)
|
args = append(args, nameservers...)
|
||||||
|
if out, err := exec.Command(cmd, args...).CombinedOutput(); err != nil {
|
||||||
if err := exec.Command(cmd, args...).Run(); err != nil {
|
return fmt.Errorf("%v: %w", string(out), err)
|
||||||
mainLog.Load().Error().Err(err).Msgf("setDNS failed, ips = %q", nameservers)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -53,10 +52,8 @@ func resetDNS(iface *net.Interface) error {
|
|||||||
}
|
}
|
||||||
cmd := "networksetup"
|
cmd := "networksetup"
|
||||||
args := []string{"-setdnsservers", iface.Name, "empty"}
|
args := []string{"-setdnsservers", iface.Name, "empty"}
|
||||||
|
if out, err := exec.Command(cmd, args...).CombinedOutput(); err != nil {
|
||||||
if err := exec.Command(cmd, args...).Run(); err != nil {
|
return fmt.Errorf("%v: %w", string(out), err)
|
||||||
mainLog.Load().Error().Err(err).Msgf("resetDNS failed")
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -71,7 +68,9 @@ func currentStaticDNS(iface *net.Interface) []string {
|
|||||||
args := []string{"-getdnsservers", iface.Name}
|
args := []string{"-getdnsservers", iface.Name}
|
||||||
out, err := exec.Command(cmd, args...).Output()
|
out, err := exec.Command(cmd, args...).Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLog.Load().Error().Err(err).Msg("could not get current static DNS")
|
if ifaceUp(iface) {
|
||||||
|
mainLog.Load().Error().Err(err).Msg("could not get current static DNS")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
scanner := bufio.NewScanner(bytes.NewReader(out))
|
scanner := bufio.NewScanner(bytes.NewReader(out))
|
||||||
|
|||||||
@@ -89,8 +89,7 @@ func resetDNS(iface *net.Interface) error {
|
|||||||
// Restoring ipv4 DHCP.
|
// Restoring ipv4 DHCP.
|
||||||
output, err := netsh("interface", "ipv4", "set", "dnsserver", strconv.Itoa(iface.Index), "dhcp")
|
output, err := netsh("interface", "ipv4", "set", "dnsserver", strconv.Itoa(iface.Index), "dhcp")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLog.Load().Error().Err(err).Msgf("failed to reset ipv4 DNS: %s", string(output))
|
return fmt.Errorf("%s: %w", string(output), err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
// If there's static DNS saved, restoring it.
|
// If there's static DNS saved, restoring it.
|
||||||
if nss := savedStaticNameservers(iface); len(nss) > 0 {
|
if nss := savedStaticNameservers(iface); len(nss) > 0 {
|
||||||
@@ -178,12 +177,16 @@ func currentDNS(iface *net.Interface) []string {
|
|||||||
func currentStaticDNS(iface *net.Interface) []string {
|
func currentStaticDNS(iface *net.Interface) []string {
|
||||||
luid, err := winipcfg.LUIDFromIndex(uint32(iface.Index))
|
luid, err := winipcfg.LUIDFromIndex(uint32(iface.Index))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLog.Load().Error().Err(err).Msg("could not get interface LUID")
|
if ifaceUp(iface) {
|
||||||
|
mainLog.Load().Error().Err(err).Msg("could not get interface LUID")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
guid, err := luid.GUID()
|
guid, err := luid.GUID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLog.Load().Error().Err(err).Msg("could not get interface GUID")
|
if ifaceUp(iface) {
|
||||||
|
mainLog.Load().Error().Err(err).Msg("could not get interface GUID")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var ns []string
|
var ns []string
|
||||||
|
|||||||
+3
-1
@@ -704,7 +704,9 @@ func withEachPhysicalInterfaces(excludeIfaceName, context string, f func(i *net.
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := f(netIface); err != nil {
|
if err := f(netIface); err != nil {
|
||||||
mainLog.Load().Warn().Err(err).Msgf("failed to %s for interface: %q", context, i.Name)
|
if ifaceUp(netIface) {
|
||||||
|
mainLog.Load().Warn().Err(err).Msgf("failed to %s for interface: %q", context, i.Name)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
mainLog.Load().Debug().Msgf("%s for interface %q successfully", context, i.Name)
|
mainLog.Load().Debug().Msgf("%s for interface %q successfully", context, i.Name)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user