mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
cmd/cli: improve error message returned by FlushDNSCache
By recording both the error and output of external commands. While at it: - Removing un-necessary usages of sudo, since ctrld already running with root privilege. - Removing un-used function triggerCaptiveCheck.
This commit is contained in:
committed by
Cuong Manh Le
parent
eb27d1482b
commit
57ef717080
@@ -7,7 +7,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
@@ -1295,46 +1294,22 @@ func (p *prog) reinitializeOSResolver(networkChange bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func triggerCaptiveCheck() {
|
||||
// Wait for a short period to ensure DNS reinitialization is complete.
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// if not Mac OS, return
|
||||
if runtime.GOOS != "darwin" {
|
||||
return
|
||||
}
|
||||
|
||||
// Trigger a lookup for captive.apple.com.
|
||||
// This can be done either via a DNS query or an HTTP GET.
|
||||
// Here we use a simple HTTP GET which is what macOS CaptiveNetworkAssistant uses.
|
||||
client := &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
resp, err := client.Get("http://captive.apple.com/generate_204")
|
||||
if err != nil {
|
||||
mainLog.Load().Debug().Msg("failed to trigger captive portal check")
|
||||
return
|
||||
}
|
||||
resp.Body.Close()
|
||||
mainLog.Load().Debug().Msg("triggered captive portal check by querying captive.apple.com")
|
||||
}
|
||||
|
||||
// FlushDNSCache flushes the DNS cache on macOS.
|
||||
func FlushDNSCache() error {
|
||||
// if not Mac OS, return
|
||||
// if not macOS, return
|
||||
if runtime.GOOS != "darwin" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Flush the DNS cache via mDNSResponder.
|
||||
// This is typically needed on modern macOS systems.
|
||||
if err := exec.Command("sudo", "killall", "-HUP", "mDNSResponder").Run(); err != nil {
|
||||
return fmt.Errorf("failed to flush mDNSResponder: %w", err)
|
||||
if out, err := exec.Command("killall", "-HUP", "mDNSResponder").CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("failed to flush mDNSResponder: %w, output: %s", err, string(out))
|
||||
}
|
||||
|
||||
// Optionally, flush the directory services cache.
|
||||
if err := exec.Command("sudo", "dscacheutil", "-flushcache").Run(); err != nil {
|
||||
return fmt.Errorf("failed to flush dscacheutil: %w", err)
|
||||
if out, err := exec.Command("dscacheutil", "-flushcache").CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("failed to flush dscacheutil: %w, output: %s", err, string(out))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user