From de24fa293ec6b5dc93b85095b676494eae1fd7c4 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 8 Jul 2025 20:40:24 +0700 Subject: [PATCH] internal/router: support Ubios 4.3+ This change improves compatibility with newer UniFi OS versions while maintaining backward compatibility with UniFi OS 4.2 and earlier. The refactoring also reduces code duplication and improves maintainability by centralizing dnsmasq configuration path logic. --- internal/router/dnsmasq/dnsmasq.go | 25 +++++++++++++++++++++++++ internal/router/edgeos/edgeos.go | 3 ++- internal/router/service_ubios.go | 7 +++---- internal/router/ubios/ubios.go | 21 +++++++++++---------- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/internal/router/dnsmasq/dnsmasq.go b/internal/router/dnsmasq/dnsmasq.go index a690ee4..058b0b5 100644 --- a/internal/router/dnsmasq/dnsmasq.go +++ b/internal/router/dnsmasq/dnsmasq.go @@ -4,6 +4,7 @@ import ( "errors" "html/template" "net" + "os" "path/filepath" "strings" @@ -163,3 +164,27 @@ func FirewallaSelfInterfaces() []*net.Interface { } return ifaces } + +const ( + ubios43ConfPath = "/run/dnsmasq.dhcp.conf.d" + ubios42ConfPath = "/run/dnsmasq.conf.d" + ubios43PidFile = "/run/dnsmasq-main.pid" + ubios42PidFile = "/run/dnsmasq.pid" + UbiosConfName = "zzzctrld.conf" +) + +// UbiosConfPath returns the appropriate configuration path based on the system's directory structure. +func UbiosConfPath() string { + if st, _ := os.Stat(ubios43ConfPath); st != nil && st.IsDir() { + return ubios43ConfPath + } + return ubios42ConfPath +} + +// UbiosPidFile returns the appropriate dnsmasq pid file based on the system's directory structure. +func UbiosPidFile() string { + if st, _ := os.Stat(ubios43PidFile); st != nil && !st.IsDir() { + return ubios43PidFile + } + return ubios42PidFile +} diff --git a/internal/router/edgeos/edgeos.go b/internal/router/edgeos/edgeos.go index 2e229ac..7364ac1 100644 --- a/internal/router/edgeos/edgeos.go +++ b/internal/router/edgeos/edgeos.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "strings" "github.com/kardianos/service" @@ -181,7 +182,7 @@ func ContentFilteringEnabled() bool { // DnsShieldEnabled reports whether DNS Shield is enabled. // See: https://community.ui.com/releases/UniFi-OS-Dream-Machines-3-2-7/251dfc1e-f4dd-4264-a080-3be9d8b9e02b func DnsShieldEnabled() bool { - buf, err := os.ReadFile("/var/run/dnsmasq.conf.d/dns.conf") + buf, err := os.ReadFile(filepath.Join(dnsmasq.UbiosConfPath(), "dns.conf")) if err != nil { return false } diff --git a/internal/router/service_ubios.go b/internal/router/service_ubios.go index 8077c07..9ad971d 100644 --- a/internal/router/service_ubios.go +++ b/internal/router/service_ubios.go @@ -13,14 +13,13 @@ import ( "time" "github.com/kardianos/service" + + "github.com/Control-D-Inc/ctrld/internal/router/dnsmasq" ) // This is a copy of https://github.com/kardianos/service/blob/v1.2.1/service_sysv_linux.go, // with modification for supporting ubios v1 init system. -// Keep in sync with ubios.ubiosDNSMasqConfigPath -const ubiosDNSMasqConfigPath = "/run/dnsmasq.conf.d/zzzctrld.conf" - type ubiosSvc struct { i service.Interface platform string @@ -86,7 +85,7 @@ func (s *ubiosSvc) Install() error { }{ s.Config, path, - ubiosDNSMasqConfigPath, + filepath.Join(dnsmasq.UbiosConfPath(), dnsmasq.UbiosConfName), } if err := s.template().Execute(f, to); err != nil { diff --git a/internal/router/ubios/ubios.go b/internal/router/ubios/ubios.go index a1f0b6c..cba6842 100644 --- a/internal/router/ubios/ubios.go +++ b/internal/router/ubios/ubios.go @@ -3,6 +3,7 @@ package ubios import ( "bytes" "os" + "path/filepath" "strconv" "github.com/kardianos/service" @@ -12,19 +13,19 @@ import ( "github.com/Control-D-Inc/ctrld/internal/router/edgeos" ) -const ( - Name = "ubios" - ubiosDNSMasqConfigPath = "/run/dnsmasq.conf.d/zzzctrld.conf" - ubiosDNSMasqDnsConfigPath = "/run/dnsmasq.conf.d/dns.conf" -) +const Name = "ubios" type Ubios struct { - cfg *ctrld.Config + cfg *ctrld.Config + dnsmasqConfPath string } // New returns a router.Router for configuring/setup/run ctrld on Ubios routers. func New(cfg *ctrld.Config) *Ubios { - return &Ubios{cfg: cfg} + return &Ubios{ + cfg: cfg, + dnsmasqConfPath: filepath.Join(dnsmasq.UbiosConfPath(), dnsmasq.UbiosConfName), + } } func (u *Ubios) ConfigureService(config *service.Config) error { @@ -59,7 +60,7 @@ func (u *Ubios) Setup() error { if err != nil { return err } - if err := os.WriteFile(ubiosDNSMasqConfigPath, []byte(data), 0600); err != nil { + if err := os.WriteFile(u.dnsmasqConfPath, []byte(data), 0600); err != nil { return err } // Restart dnsmasq service. @@ -74,7 +75,7 @@ func (u *Ubios) Cleanup() error { return nil } // Remove the custom dnsmasq config - if err := os.Remove(ubiosDNSMasqConfigPath); err != nil { + if err := os.Remove(u.dnsmasqConfPath); err != nil { return err } // Restart dnsmasq service. @@ -85,7 +86,7 @@ func (u *Ubios) Cleanup() error { } func restartDNSMasq() error { - buf, err := os.ReadFile("/run/dnsmasq.pid") + buf, err := os.ReadFile(dnsmasq.UbiosPidFile()) if err != nil { return err }