mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
internal/router/dnsmasq: disable cache
So multiple upstreams config could work properly.
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
package dnsmasq
|
package dnsmasq
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -19,6 +22,7 @@ server={{ .IP }}#{{ .Port }}
|
|||||||
add-mac
|
add-mac
|
||||||
add-subnet=32,128
|
add-subnet=32,128
|
||||||
{{- end}}
|
{{- end}}
|
||||||
|
cache-size=0
|
||||||
`
|
`
|
||||||
|
|
||||||
const MerlinPostConfPath = "/jffs/scripts/dnsmasq.postconf"
|
const MerlinPostConfPath = "/jffs/scripts/dnsmasq.postconf"
|
||||||
@@ -47,6 +51,8 @@ if [ -n "$pid" ] && [ -f "/proc/${pid}/cmdline" ]; then
|
|||||||
{{- end}}
|
{{- end}}
|
||||||
pc_delete "dnssec" "$config_file" # disable DNSSEC
|
pc_delete "dnssec" "$config_file" # disable DNSSEC
|
||||||
pc_delete "trust-anchor=" "$config_file" # disable DNSSEC
|
pc_delete "trust-anchor=" "$config_file" # disable DNSSEC
|
||||||
|
pc_delete "cache-size=" "$config_file"
|
||||||
|
pc_append "cache-size=0" "$config_file" # disable cache
|
||||||
|
|
||||||
# For John fork
|
# For John fork
|
||||||
pc_delete "resolv-file" "$config_file" # no WAN DNS settings
|
pc_delete "resolv-file" "$config_file" # no WAN DNS settings
|
||||||
@@ -117,9 +123,28 @@ func firewallaUpstreams(port int) []Upstream {
|
|||||||
return upstreams
|
return upstreams
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// firewallaDnsmasqConfFiles returns dnsmasq config files of all firewalla interfaces.
|
||||||
|
func firewallaDnsmasqConfFiles() ([]string, error) {
|
||||||
|
return filepath.Glob("/home/pi/firerouter/etc/dnsmasq.dns.*.conf")
|
||||||
|
}
|
||||||
|
|
||||||
|
// firewallUpdateConf updates all firewall config files using given function.
|
||||||
|
func firewallUpdateConf(update func(conf string) error) error {
|
||||||
|
confFiles, err := firewallaDnsmasqConfFiles()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, conf := range confFiles {
|
||||||
|
if err := update(conf); err != nil {
|
||||||
|
return fmt.Errorf("%s: %w", conf, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// FirewallaSelfInterfaces returns list of interfaces that will be configured with default dnsmasq setup on Firewalla.
|
// FirewallaSelfInterfaces returns list of interfaces that will be configured with default dnsmasq setup on Firewalla.
|
||||||
func FirewallaSelfInterfaces() []*net.Interface {
|
func FirewallaSelfInterfaces() []*net.Interface {
|
||||||
matches, err := filepath.Glob("/home/pi/firerouter/etc/dnsmasq.dns.*.conf")
|
matches, err := firewallaDnsmasqConfFiles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -133,3 +158,32 @@ func FirewallaSelfInterfaces() []*net.Interface {
|
|||||||
}
|
}
|
||||||
return ifaces
|
return ifaces
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FirewallaDisableCache comments out "cache-size" line in all firewalla dnsmasq config files.
|
||||||
|
func FirewallaDisableCache() error {
|
||||||
|
return firewallUpdateConf(DisableCache)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FirewallaEnableCache un-comments out "cache-size" line in all firewalla dnsmasq config files.
|
||||||
|
func FirewallaEnableCache() error {
|
||||||
|
return firewallUpdateConf(EnableCache)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableCache comments out "cache-size" line in dnsmasq config file.
|
||||||
|
func DisableCache(conf string) error {
|
||||||
|
return replaceFileContent(conf, "\ncache-size=", "\n#cache-size=")
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableCache un-comments "cache-size" line in dnsmasq config file.
|
||||||
|
func EnableCache(conf string) error {
|
||||||
|
return replaceFileContent(conf, "\n#cache-size=", "\ncache-size=")
|
||||||
|
}
|
||||||
|
|
||||||
|
func replaceFileContent(filename, old, new string) error {
|
||||||
|
content, err := os.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
content = bytes.ReplaceAll(content, []byte(old), []byte(new))
|
||||||
|
return os.WriteFile(filename, content, 0644)
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,11 +15,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Name = "edgeos"
|
Name = "edgeos"
|
||||||
edgeOSDNSMasqConfigPath = "/etc/dnsmasq.d/dnsmasq-zzz-ctrld.conf"
|
edgeOSDNSMasqDefaultConfigPath = "/etc/dnsmasq.conf"
|
||||||
usgDNSMasqConfigPath = "/etc/dnsmasq.conf"
|
edgeOSDNSMasqConfigPath = "/etc/dnsmasq.d/dnsmasq-zzz-ctrld.conf"
|
||||||
usgDNSMasqBackupConfigPath = "/etc/dnsmasq.conf.bak"
|
usgDNSMasqConfigPath = "/etc/dnsmasq.conf"
|
||||||
toggleContentFilteringLink = "https://community.ui.com/questions/UDM-Pro-disable-enable-DNS-filtering/e2cc4060-e56a-4139-b200-62d7f773ff8f"
|
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
|
var ErrContentFilteringEnabled = fmt.Errorf(`the "Content Filtering" feature" is enabled, which is conflicted with ctrld.\n
|
||||||
@@ -95,7 +96,7 @@ func (e *EdgeOS) setupUSG() error {
|
|||||||
return fmt.Errorf("setupUSG: backup current config: %w", err)
|
return fmt.Errorf("setupUSG: backup current config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removing all configured upstreams.
|
// Removing all configured upstreams and cache config.
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
scanner := bufio.NewScanner(bytes.NewReader(buf))
|
scanner := bufio.NewScanner(bytes.NewReader(buf))
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
@@ -106,6 +107,9 @@ func (e *EdgeOS) setupUSG() error {
|
|||||||
if strings.HasPrefix(line, "all-servers") {
|
if strings.HasPrefix(line, "all-servers") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if strings.HasPrefix(line, "cache-size") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
sb.WriteString(line)
|
sb.WriteString(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,6 +131,10 @@ func (e *EdgeOS) setupUSG() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *EdgeOS) setupUDM() 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.ConfTmpl(dnsmasq.ConfigContentTmpl, e.cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -153,6 +161,10 @@ func (e *EdgeOS) cleanupUSG() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *EdgeOS) cleanupUDM() error {
|
func (e *EdgeOS) cleanupUDM() error {
|
||||||
|
// Enable dnsmasq cache.
|
||||||
|
if err := dnsmasq.EnableCache(edgeOSDNSMasqDefaultConfigPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// Remove the custom dnsmasq config
|
// Remove the custom dnsmasq config
|
||||||
if err := os.Remove(edgeOSDNSMasqConfigPath); err != nil {
|
if err := os.Remove(edgeOSDNSMasqConfigPath); err != nil {
|
||||||
return fmt.Errorf("cleanupUDM: os.Remove: %w", err)
|
return fmt.Errorf("cleanupUDM: os.Remove: %w", err)
|
||||||
|
|||||||
@@ -65,6 +65,11 @@ func (f *Firewalla) Setup() error {
|
|||||||
return fmt.Errorf("writing ctrld config: %w", err)
|
return fmt.Errorf("writing ctrld config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Disable dnsmasq cache.
|
||||||
|
if err := dnsmasq.FirewallaDisableCache(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Restart dnsmasq service.
|
// Restart dnsmasq service.
|
||||||
if err := restartDNSMasq(); err != nil {
|
if err := restartDNSMasq(); err != nil {
|
||||||
return fmt.Errorf("restartDNSMasq: %w", err)
|
return fmt.Errorf("restartDNSMasq: %w", err)
|
||||||
@@ -82,6 +87,11 @@ func (f *Firewalla) Cleanup() error {
|
|||||||
return fmt.Errorf("removing ctrld config: %w", err)
|
return fmt.Errorf("removing ctrld config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enable dnsmasq cache.
|
||||||
|
if err := dnsmasq.FirewallaEnableCache(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Restart dnsmasq service.
|
// Restart dnsmasq service.
|
||||||
if err := restartDNSMasq(); err != nil {
|
if err := restartDNSMasq(); err != nil {
|
||||||
return fmt.Errorf("restartDNSMasq: %w", err)
|
return fmt.Errorf("restartDNSMasq: %w", err)
|
||||||
|
|||||||
@@ -1,18 +1,14 @@
|
|||||||
package openwrt
|
package openwrt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/Control-D-Inc/ctrld/internal/router/dnsmasq"
|
|
||||||
|
|
||||||
"github.com/kardianos/service"
|
"github.com/kardianos/service"
|
||||||
|
|
||||||
"github.com/Control-D-Inc/ctrld"
|
"github.com/Control-D-Inc/ctrld"
|
||||||
|
"github.com/Control-D-Inc/ctrld/internal/router/dnsmasq"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -20,8 +16,6 @@ const (
|
|||||||
openwrtDNSMasqConfigPath = "/tmp/dnsmasq.d/ctrld.conf"
|
openwrtDNSMasqConfigPath = "/tmp/dnsmasq.d/ctrld.conf"
|
||||||
)
|
)
|
||||||
|
|
||||||
var errUCIEntryNotFound = errors.New("uci: Entry not found")
|
|
||||||
|
|
||||||
type Openwrt struct {
|
type Openwrt struct {
|
||||||
cfg *ctrld.Config
|
cfg *ctrld.Config
|
||||||
}
|
}
|
||||||
@@ -59,10 +53,6 @@ func (o *Openwrt) Setup() error {
|
|||||||
if err := os.WriteFile(openwrtDNSMasqConfigPath, []byte(data), 0600); err != nil {
|
if err := os.WriteFile(openwrtDNSMasqConfigPath, []byte(data), 0600); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Commit.
|
|
||||||
if _, err := uci("commit"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Restart dnsmasq service.
|
// Restart dnsmasq service.
|
||||||
if err := restartDNSMasq(); err != nil {
|
if err := restartDNSMasq(); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -91,17 +81,3 @@ func restartDNSMasq() error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func uci(args ...string) (string, error) {
|
|
||||||
cmd := exec.Command("uci", args...)
|
|
||||||
var stdout, stderr bytes.Buffer
|
|
||||||
cmd.Stdout = &stdout
|
|
||||||
cmd.Stderr = &stderr
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
if strings.HasPrefix(stderr.String(), errUCIEntryNotFound.Error()) {
|
|
||||||
return "", errUCIEntryNotFound
|
|
||||||
}
|
|
||||||
return "", fmt.Errorf("%s:%w", stderr.String(), err)
|
|
||||||
}
|
|
||||||
return strings.TrimSpace(stdout.String()), nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,16 +5,17 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/Control-D-Inc/ctrld/internal/router/dnsmasq"
|
"github.com/kardianos/service"
|
||||||
|
|
||||||
"github.com/Control-D-Inc/ctrld"
|
"github.com/Control-D-Inc/ctrld"
|
||||||
|
"github.com/Control-D-Inc/ctrld/internal/router/dnsmasq"
|
||||||
"github.com/Control-D-Inc/ctrld/internal/router/edgeos"
|
"github.com/Control-D-Inc/ctrld/internal/router/edgeos"
|
||||||
"github.com/kardianos/service"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Name = "ubios"
|
Name = "ubios"
|
||||||
ubiosDNSMasqConfigPath = "/run/dnsmasq.conf.d/zzzctrld.conf"
|
ubiosDNSMasqConfigPath = "/run/dnsmasq.conf.d/zzzctrld.conf"
|
||||||
|
ubiosDNSMasqDnsConfigPath = "/run/dnsmasq.conf.d/dns.conf"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Ubios struct {
|
type Ubios struct {
|
||||||
@@ -57,6 +58,10 @@ func (u *Ubios) Setup() error {
|
|||||||
if err := os.WriteFile(ubiosDNSMasqConfigPath, []byte(data), 0600); err != nil {
|
if err := os.WriteFile(ubiosDNSMasqConfigPath, []byte(data), 0600); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Disable dnsmasq cache.
|
||||||
|
if err := dnsmasq.DisableCache(ubiosDNSMasqDnsConfigPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// Restart dnsmasq service.
|
// Restart dnsmasq service.
|
||||||
if err := restartDNSMasq(); err != nil {
|
if err := restartDNSMasq(); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -72,6 +77,10 @@ func (u *Ubios) Cleanup() error {
|
|||||||
if err := os.Remove(ubiosDNSMasqConfigPath); err != nil {
|
if err := os.Remove(ubiosDNSMasqConfigPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Enable dnsmasq cache.
|
||||||
|
if err := dnsmasq.EnableCache(ubiosDNSMasqDnsConfigPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// Restart dnsmasq service.
|
// Restart dnsmasq service.
|
||||||
if err := restartDNSMasq(); err != nil {
|
if err := restartDNSMasq(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user