From 2bebe93e47396f7c7bcb414d3c660b0fd4204c89 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 22 Nov 2023 15:35:40 +0700 Subject: [PATCH] internal/router: do not disable cache on EdgeOS The dnsmasq cache-size setting on EdgeOS could be re-generated anytime by vyatta router/dhcp components. This conflicts with setting generated by ctrld, causing dnsmasq fails to start. It's better to keep dnsmasq cache enabled on EdgeOS, we can turn it off again once we find a reliable way to control cache-size setting. --- internal/router/dnsmasq/dnsmasq.go | 14 +++++++++++--- internal/router/edgeos/edgeos.go | 30 +++++++++--------------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/internal/router/dnsmasq/dnsmasq.go b/internal/router/dnsmasq/dnsmasq.go index 52cf1a6..9fab8b6 100644 --- a/internal/router/dnsmasq/dnsmasq.go +++ b/internal/router/dnsmasq/dnsmasq.go @@ -22,7 +22,9 @@ server={{ .IP }}#{{ .Port }} add-mac add-subnet=32,128 {{- end}} +{{- if .CacheDisabled}} cache-size=0 +{{- end}} ` const MerlinPostConfPath = "/jffs/scripts/dnsmasq.postconf" @@ -71,6 +73,10 @@ type Upstream struct { } func ConfTmpl(tmplText string, cfg *ctrld.Config) (string, error) { + return ConfTmplWitchCacheDisabled(tmplText, cfg, true) +} + +func ConfTmplWitchCacheDisabled(tmplText string, cfg *ctrld.Config, cacheDisabled bool) (string, error) { listener := cfg.FirstListener() if listener == nil { return "", errors.New("missing listener") @@ -80,24 +86,26 @@ func ConfTmpl(tmplText string, cfg *ctrld.Config) (string, error) { ip = "127.0.0.1" } upstreams := []Upstream{{IP: ip, Port: listener.Port}} - return confTmpl(tmplText, upstreams, cfg.HasUpstreamSendClientInfo()) + return confTmpl(tmplText, upstreams, cfg.HasUpstreamSendClientInfo(), cacheDisabled) } func FirewallaConfTmpl(tmplText string, cfg *ctrld.Config) (string, error) { if lc := cfg.FirstListener(); lc != nil && (lc.IP == "0.0.0.0" || lc.IP == "") { - return confTmpl(tmplText, firewallaUpstreams(lc.Port), cfg.HasUpstreamSendClientInfo()) + return confTmpl(tmplText, firewallaUpstreams(lc.Port), cfg.HasUpstreamSendClientInfo(), true) } return ConfTmpl(tmplText, cfg) } -func confTmpl(tmplText string, upstreams []Upstream, sendClientInfo bool) (string, error) { +func confTmpl(tmplText string, upstreams []Upstream, sendClientInfo, cacheDisabled bool) (string, error) { tmpl := template.Must(template.New("").Parse(tmplText)) var to = &struct { SendClientInfo bool Upstreams []Upstream + CacheDisabled bool }{ SendClientInfo: sendClientInfo, Upstreams: upstreams, + CacheDisabled: cacheDisabled, } var sb strings.Builder if err := tmpl.Execute(&sb, to); err != nil { diff --git a/internal/router/edgeos/edgeos.go b/internal/router/edgeos/edgeos.go index 0552882..3e7003b 100644 --- a/internal/router/edgeos/edgeos.go +++ b/internal/router/edgeos/edgeos.go @@ -8,19 +8,18 @@ import ( "os/exec" "strings" - "github.com/Control-D-Inc/ctrld/internal/router/dnsmasq" + "github.com/kardianos/service" "github.com/Control-D-Inc/ctrld" - "github.com/kardianos/service" + "github.com/Control-D-Inc/ctrld/internal/router/dnsmasq" ) const ( - Name = "edgeos" - edgeOSDNSMasqDefaultConfigPath = "/etc/dnsmasq.conf" - edgeOSDNSMasqConfigPath = "/etc/dnsmasq.d/dnsmasq-zzz-ctrld.conf" - usgDNSMasqConfigPath = "/etc/dnsmasq.conf" - usgDNSMasqBackupConfigPath = "/etc/dnsmasq.conf.bak" - toggleContentFilteringLink = "https://community.ui.com/questions/UDM-Pro-disable-enable-DNS-filtering/e2cc4060-e56a-4139-b200-62d7f773ff8f" + Name = "edgeos" + edgeOSDNSMasqConfigPath = "/etc/dnsmasq.d/dnsmasq-zzz-ctrld.conf" + usgDNSMasqConfigPath = "/etc/dnsmasq.conf" + usgDNSMasqBackupConfigPath = "/etc/dnsmasq.conf.bak" + toggleContentFilteringLink = "https://community.ui.com/questions/UDM-Pro-disable-enable-DNS-filtering/e2cc4060-e56a-4139-b200-62d7f773ff8f" ) var ErrContentFilteringEnabled = fmt.Errorf(`the "Content Filtering" feature" is enabled, which is conflicted with ctrld.\n @@ -107,13 +106,10 @@ func (e *EdgeOS) setupUSG() error { if strings.HasPrefix(line, "all-servers") { continue } - if strings.HasPrefix(line, "cache-size") { - continue - } sb.WriteString(line) } - data, err := dnsmasq.ConfTmpl(dnsmasq.ConfigContentTmpl, e.cfg) + data, err := dnsmasq.ConfTmplWitchCacheDisabled(dnsmasq.ConfigContentTmpl, e.cfg, false) if err != nil { return err } @@ -131,11 +127,7 @@ func (e *EdgeOS) setupUSG() error { } func (e *EdgeOS) setupUDM() error { - // Disable dnsmasq cache. - if err := dnsmasq.DisableCache(edgeOSDNSMasqDefaultConfigPath); err != nil { - return err - } - data, err := dnsmasq.ConfTmpl(dnsmasq.ConfigContentTmpl, e.cfg) + data, err := dnsmasq.ConfTmplWitchCacheDisabled(dnsmasq.ConfigContentTmpl, e.cfg, false) if err != nil { return err } @@ -161,10 +153,6 @@ func (e *EdgeOS) cleanupUSG() error { } func (e *EdgeOS) cleanupUDM() error { - // Enable dnsmasq cache. - if err := dnsmasq.EnableCache(edgeOSDNSMasqDefaultConfigPath); err != nil { - return err - } // Remove the custom dnsmasq config if err := os.Remove(edgeOSDNSMasqConfigPath); err != nil { return fmt.Errorf("cleanupUDM: os.Remove: %w", err)