diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index 06dffcb..b0c03c3 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -761,6 +761,8 @@ func processListenFlag() { if listenAddress == "" { return } + mainLog.Load().Debug().Str("listen_address", listenAddress).Msg("Processing listen flag") + host, portStr, err := net.SplitHostPort(listenAddress) if err != nil { mainLog.Load().Fatal().Msgf("invalid listener address: %v", err) @@ -776,22 +778,31 @@ func processListenFlag() { v.Set("listener", map[string]*ctrld.ListenerConfig{ "0": lc, }) + + mainLog.Load().Debug().Str("host", host).Int("port", port).Msg("Listen flag processed successfully") } // processLogAndCacheFlags processes log and cache related flags func processLogAndCacheFlags() { + mainLog.Load().Debug().Msg("Processing log and cache flags") + if logPath != "" { cfg.Service.LogPath = logPath + mainLog.Load().Debug().Str("log_path", logPath).Msg("Log path flag processed") } if logPath != "" && cfg.Service.LogLevel == "" { cfg.Service.LogLevel = "debug" + mainLog.Load().Debug().Msg("Log level set to debug") } if cacheSize != 0 { cfg.Service.CacheEnable = true cfg.Service.CacheSize = cacheSize + mainLog.Load().Debug().Int("cache_size", cacheSize).Msg("Cache flag processed") } v.Set("service", cfg.Service) + + mainLog.Load().Debug().Msg("Log and cache flags processed successfully") } // netInterface returns the network interface by name @@ -1075,6 +1086,8 @@ func uninstall(p *prog, s service.Service) { } func validateConfig(cfg *ctrld.Config) error { + mainLog.Load().Debug().Msg("Validating configuration") + if err := ctrld.ValidateConfig(validator.New(), cfg); err != nil { var ve validator.ValidationErrors if errors.As(err, &ve) { @@ -1082,8 +1095,11 @@ func validateConfig(cfg *ctrld.Config) error { mainLog.Load().Error().Msgf("invalid config: %s: %s", fe.Namespace(), fieldErrorMsg(fe)) } } + mainLog.Load().Error().Err(err).Msg("Configuration validation failed") return err } + + mainLog.Load().Debug().Msg("Configuration validation completed successfully") return nil } diff --git a/cmd/cli/os_darwin.go b/cmd/cli/os_darwin.go index 68bd7e1..94e45fd 100644 --- a/cmd/cli/os_darwin.go +++ b/cmd/cli/os_darwin.go @@ -14,21 +14,25 @@ import ( // allocateIP allocates an IP address on the specified interface // sudo ifconfig lo0 alias 127.0.0.2 up func allocateIP(ip string) error { + mainLog.Load().Debug().Str("ip", ip).Msg("Allocating IP address") cmd := exec.Command("ifconfig", "lo0", "alias", ip, "up") if err := cmd.Run(); err != nil { mainLog.Load().Error().Err(err).Msg("allocateIP failed") return err } + mainLog.Load().Debug().Str("ip", ip).Msg("IP address allocated successfully") return nil } // deAllocateIP deallocates an IP address from the specified interface func deAllocateIP(ip string) error { + mainLog.Load().Debug().Str("ip", ip).Msg("Deallocating IP address") cmd := exec.Command("ifconfig", "lo0", "-alias", ip) if err := cmd.Run(); err != nil { mainLog.Load().Error().Err(err).Msg("deAllocateIP failed") return err } + mainLog.Load().Debug().Str("ip", ip).Msg("IP address deallocated successfully") return nil } @@ -48,6 +52,8 @@ func setDnsIgnoreUnusableInterface(iface *net.Interface, nameservers []string) e // networksetup -setdnsservers Wi-Fi 8.8.8.8 1.1.1.1 // TODO(cuonglm): use system API func setDNS(iface *net.Interface, nameservers []string) error { + mainLog.Load().Debug().Str("interface", iface.Name).Strs("nameservers", nameservers).Msg("Setting DNS configuration") + // Note that networksetup won't modify search domains settings, // This assignment is just a placeholder to silent linter. _ = searchDomains @@ -57,6 +63,8 @@ func setDNS(iface *net.Interface, nameservers []string) error { if out, err := exec.Command(cmd, args...).CombinedOutput(); err != nil { return fmt.Errorf("%v: %w", string(out), err) } + + mainLog.Load().Debug().Str("interface", iface.Name).Msg("DNS configuration set successfully") return nil } @@ -74,11 +82,15 @@ func resetDnsIgnoreUnusableInterface(iface *net.Interface) error { // TODO(cuonglm): use system API func resetDNS(iface *net.Interface) error { + mainLog.Load().Debug().Str("interface", iface.Name).Msg("Resetting DNS configuration") + cmd := "networksetup" args := []string{"-setdnsservers", iface.Name, "empty"} if out, err := exec.Command(cmd, args...).CombinedOutput(); err != nil { return fmt.Errorf("%v: %w", string(out), err) } + + mainLog.Load().Debug().Str("interface", iface.Name).Msg("DNS configuration reset successfully") return nil } diff --git a/cmd/cli/os_freebsd.go b/cmd/cli/os_freebsd.go index 65c44b9..9a7777d 100644 --- a/cmd/cli/os_freebsd.go +++ b/cmd/cli/os_freebsd.go @@ -16,21 +16,25 @@ import ( // allocateIP allocates an IP address on the specified interface // sudo ifconfig lo0 127.0.0.53 alias func allocateIP(ip string) error { + mainLog.Load().Debug().Str("ip", ip).Msg("Allocating IP address") cmd := exec.Command("ifconfig", "lo0", ip, "alias") if err := cmd.Run(); err != nil { mainLog.Load().Error().Err(err).Msg("allocateIP failed") return err } + mainLog.Load().Debug().Str("ip", ip).Msg("IP address allocated successfully") return nil } // deAllocateIP deallocates an IP address from the specified interface func deAllocateIP(ip string) error { + mainLog.Load().Debug().Str("ip", ip).Msg("Deallocating IP address") cmd := exec.Command("ifconfig", "lo0", ip, "-alias") if err := cmd.Run(); err != nil { mainLog.Load().Error().Err(err).Msg("deAllocateIP failed") return err } + mainLog.Load().Debug().Str("ip", ip).Msg("IP address deallocated successfully") return nil } @@ -41,6 +45,8 @@ func setDnsIgnoreUnusableInterface(iface *net.Interface, nameservers []string) e // set the dns server for the provided network interface func setDNS(iface *net.Interface, nameservers []string) error { + mainLog.Load().Debug().Str("interface", iface.Name).Strs("nameservers", nameservers).Msg("Setting DNS configuration") + r, err := dns.NewOSConfigurator(logf, &health.Tracker{}, &controlknobs.Knobs{}, iface.Name) if err != nil { mainLog.Load().Error().Err(err).Msg("failed to create DNS OS configurator") @@ -66,6 +72,8 @@ func setDNS(iface *net.Interface, nameservers []string) error { mainLog.Load().Error().Err(err).Msg("failed to set DNS") return err } + + mainLog.Load().Debug().Str("interface", iface.Name).Msg("DNS configuration set successfully") return nil } @@ -76,6 +84,8 @@ func resetDnsIgnoreUnusableInterface(iface *net.Interface) error { // resetDNS resets DNS servers for the specified interface func resetDNS(iface *net.Interface) error { + mainLog.Load().Debug().Str("interface", iface.Name).Msg("Resetting DNS configuration") + r, err := dns.NewOSConfigurator(logf, &health.Tracker{}, &controlknobs.Knobs{}, iface.Name) if err != nil { mainLog.Load().Error().Err(err).Msg("failed to create DNS OS configurator") @@ -86,6 +96,8 @@ func resetDNS(iface *net.Interface) error { mainLog.Load().Error().Err(err).Msg("failed to rollback DNS setting") return err } + + mainLog.Load().Debug().Str("interface", iface.Name).Msg("DNS configuration reset successfully") return nil } diff --git a/cmd/cli/os_linux.go b/cmd/cli/os_linux.go index 0b93b0b..b4fef82 100644 --- a/cmd/cli/os_linux.go +++ b/cmd/cli/os_linux.go @@ -33,20 +33,24 @@ type getDNS func(iface string) []string // allocate loopback ip // sudo ip a add 127.0.0.2/24 dev lo func allocateIP(ip string) error { + mainLog.Load().Debug().Str("ip", ip).Msg("Allocating IP address") cmd := exec.Command("ip", "a", "add", ip+"/24", "dev", "lo") if out, err := cmd.CombinedOutput(); err != nil { mainLog.Load().Error().Err(err).Msgf("allocateIP failed: %s", string(out)) return err } + mainLog.Load().Debug().Str("ip", ip).Msg("IP address allocated successfully") return nil } func deAllocateIP(ip string) error { + mainLog.Load().Debug().Str("ip", ip).Msg("Deallocating IP address") cmd := exec.Command("ip", "a", "del", ip+"/24", "dev", "lo") if err := cmd.Run(); err != nil { mainLog.Load().Error().Err(err).Msg("deAllocateIP failed") return err } + mainLog.Load().Debug().Str("ip", ip).Msg("IP address deallocated successfully") return nil } @@ -58,6 +62,8 @@ func setDnsIgnoreUnusableInterface(iface *net.Interface, nameservers []string) e } func setDNS(iface *net.Interface, nameservers []string) error { + mainLog.Load().Debug().Str("interface", iface.Name).Strs("nameservers", nameservers).Msg("Setting DNS configuration") + r, err := dns.NewOSConfigurator(logf, &health.Tracker{}, &controlknobs.Knobs{}, iface.Name) if err != nil { mainLog.Load().Error().Err(err).Msg("failed to create DNS OS configurator") @@ -119,6 +125,8 @@ systemdResolve: } mainLog.Load().Debug().Msg("DNS was not set for some reason") } + + mainLog.Load().Debug().Str("interface", iface.Name).Msg("DNS configuration set successfully") return nil } @@ -128,6 +136,8 @@ func resetDnsIgnoreUnusableInterface(iface *net.Interface) error { } func resetDNS(iface *net.Interface) (err error) { + mainLog.Load().Debug().Str("interface", iface.Name).Msg("Resetting DNS configuration") + defer func() { if err == nil { return diff --git a/config.go b/config.go index f5e5b86..9880944 100644 --- a/config.go +++ b/config.go @@ -114,6 +114,9 @@ func SetConfigNameWithPath(v *viper.Viper, name, configPath string) { // InitConfig initializes default config values for given *viper.Viper instance. func InitConfig(v *viper.Viper, name string) { + logger := LoggerFromCtx(context.Background()) + Log(context.Background(), logger.Debug(), "Config initialization started") + v.SetDefault("listener", map[string]*ListenerConfig{ "0": { IP: "", @@ -152,6 +155,8 @@ func InitConfig(v *viper.Viper, name string) { Timeout: 3000, }, }) + + Log(context.Background(), logger.Debug(), "Config initialization completed") } // Config represents ctrld supported configuration. @@ -499,7 +504,7 @@ func (uc *UpstreamConfig) ReBootstrap(ctx context.Context) { _, _, _ = uc.g.Do("ReBootstrap", func() (any, error) { if uc.rebootstrap.CompareAndSwap(false, true) { logger := LoggerFromCtx(ctx) - logger.Debug().Msgf("re-bootstrapping upstream ip for %v", uc) + Log(ctx, logger.Debug(), "Re-bootstrapping upstream: %s", uc.Name) } return true, nil }) @@ -823,7 +828,7 @@ func (uc *UpstreamConfig) FallbackToDirectIP(ctx context.Context) bool { return } logger := LoggerFromCtx(ctx) - logger.Warn().Msgf("using direct IP for %q: %s", uc.Endpoint, ip) + Log(ctx, logger.Warn(), "Using direct IP for %q: %s", uc.Endpoint, ip) uc.u.Host = ip done = true }) @@ -832,12 +837,18 @@ func (uc *UpstreamConfig) FallbackToDirectIP(ctx context.Context) bool { // Init initialized necessary values for an ListenerConfig. func (lc *ListenerConfig) Init() { + logger := LoggerFromCtx(context.Background()) + Log(context.Background(), logger.Debug(), "Initializing listener config") + if lc.Policy != nil { lc.Policy.FailoverRcodeNumbers = make([]int, len(lc.Policy.FailoverRcodes)) for i, rcode := range lc.Policy.FailoverRcodes { lc.Policy.FailoverRcodeNumbers[i] = dnsrcode.FromString(rcode) } + Log(context.Background(), logger.Debug(), "Listener policy initialized with %d failover rcodes", len(lc.Policy.FailoverRcodes)) } + + Log(context.Background(), logger.Debug(), "Listener config initialization completed") } // ValidateConfig validates the given config.