mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-04 01:07:49 +02:00
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.
This commit is contained in:
committed by
Cuong Manh Le
parent
6663925c4d
commit
de24fa293e
@@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -163,3 +164,27 @@ func FirewallaSelfInterfaces() []*net.Interface {
|
|||||||
}
|
}
|
||||||
return ifaces
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/kardianos/service"
|
"github.com/kardianos/service"
|
||||||
@@ -181,7 +182,7 @@ func ContentFilteringEnabled() bool {
|
|||||||
// DnsShieldEnabled reports whether DNS Shield is enabled.
|
// 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
|
// See: https://community.ui.com/releases/UniFi-OS-Dream-Machines-3-2-7/251dfc1e-f4dd-4264-a080-3be9d8b9e02b
|
||||||
func DnsShieldEnabled() bool {
|
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 {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,14 +13,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kardianos/service"
|
"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,
|
// 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.
|
// 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 {
|
type ubiosSvc struct {
|
||||||
i service.Interface
|
i service.Interface
|
||||||
platform string
|
platform string
|
||||||
@@ -86,7 +85,7 @@ func (s *ubiosSvc) Install() error {
|
|||||||
}{
|
}{
|
||||||
s.Config,
|
s.Config,
|
||||||
path,
|
path,
|
||||||
ubiosDNSMasqConfigPath,
|
filepath.Join(dnsmasq.UbiosConfPath(), dnsmasq.UbiosConfName),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.template().Execute(f, to); err != nil {
|
if err := s.template().Execute(f, to); err != nil {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package ubios
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/kardianos/service"
|
"github.com/kardianos/service"
|
||||||
@@ -12,19 +13,19 @@ import (
|
|||||||
"github.com/Control-D-Inc/ctrld/internal/router/edgeos"
|
"github.com/Control-D-Inc/ctrld/internal/router/edgeos"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const Name = "ubios"
|
||||||
Name = "ubios"
|
|
||||||
ubiosDNSMasqConfigPath = "/run/dnsmasq.conf.d/zzzctrld.conf"
|
|
||||||
ubiosDNSMasqDnsConfigPath = "/run/dnsmasq.conf.d/dns.conf"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Ubios struct {
|
type Ubios struct {
|
||||||
cfg *ctrld.Config
|
cfg *ctrld.Config
|
||||||
|
dnsmasqConfPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a router.Router for configuring/setup/run ctrld on Ubios routers.
|
// New returns a router.Router for configuring/setup/run ctrld on Ubios routers.
|
||||||
func New(cfg *ctrld.Config) *Ubios {
|
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 {
|
func (u *Ubios) ConfigureService(config *service.Config) error {
|
||||||
@@ -59,7 +60,7 @@ func (u *Ubios) Setup() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
return err
|
||||||
}
|
}
|
||||||
// Restart dnsmasq service.
|
// Restart dnsmasq service.
|
||||||
@@ -74,7 +75,7 @@ func (u *Ubios) Cleanup() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Remove the custom dnsmasq config
|
// Remove the custom dnsmasq config
|
||||||
if err := os.Remove(ubiosDNSMasqConfigPath); err != nil {
|
if err := os.Remove(u.dnsmasqConfPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Restart dnsmasq service.
|
// Restart dnsmasq service.
|
||||||
@@ -85,7 +86,7 @@ func (u *Ubios) Cleanup() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func restartDNSMasq() error {
|
func restartDNSMasq() error {
|
||||||
buf, err := os.ReadFile("/run/dnsmasq.pid")
|
buf, err := os.ReadFile(dnsmasq.UbiosPidFile())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user