Removes more Wild Cloud cruft.

This commit is contained in:
2026-07-11 23:21:23 +00:00
parent ac66ba653d
commit 5d8fe6a754
21 changed files with 196 additions and 1391 deletions

View File

@@ -100,19 +100,6 @@ log-dhcp
)
}
// WriteConfig writes the dnsmasq configuration to the specified path
func (g *ConfigGenerator) WriteConfig(cfg *config.State, entries []DNSEntry, configPath string) error {
configContent := g.Generate(cfg, entries)
slog.Info("writing dnsmasq config", "component", "dnsmasq", "path", configPath)
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
return fmt.Errorf("writing dnsmasq config: %w", err)
}
return nil
}
// RestartService reloads the dnsmasq service (SIGHUP — no downtime).
// Falls back to restart if reload fails.
func (g *ConfigGenerator) RestartService() error {
@@ -223,6 +210,95 @@ func (g *ConfigGenerator) UpdateConfig(cfg *config.State, entries []DNSEntry, re
return nil
}
// GenerateMainConfig creates the main dnsmasq configuration with global settings.
// Used by the DnsmasqGenerate handler to preview/apply the base config independently
// of domain-specific DNS entries (which are handled by Generate + UpdateConfig).
func (g *ConfigGenerator) GenerateMainConfig(cfg *config.State) string {
dnsIP, err := network.GetWildCentralIP()
if err != nil {
slog.Error("failed to detect Wild Central IP", "component", "dnsmasq", "error", err)
dnsIP = ""
}
var sb strings.Builder
fmt.Fprintf(&sb, `# Wild Cloud DNS Configuration (Main)
# This file contains global settings. Domain-specific DNS entries are
# generated by reconciliation into the monolithic config.
# Basic Settings
listen-address=%s
bind-interfaces
domain-needed
bogus-priv
no-resolv
# Upstream DNS servers
server=1.1.1.1
server=8.8.8.8
# Logging
log-queries
log-dhcp
`, dnsIP)
// DHCP section (only when explicitly enabled)
if cfg != nil && cfg.Cloud.Dnsmasq.DHCP.Enabled {
dhcp := cfg.Cloud.Dnsmasq.DHCP
sb.WriteString("\n# DHCP Configuration\n")
leaseTime := dhcp.LeaseTime
if leaseTime == "" {
leaseTime = "24h"
}
fmt.Fprintf(&sb, "dhcp-range=%s,%s,%s\n", dhcp.RangeStart, dhcp.RangeEnd, leaseTime)
gateway := dhcp.Gateway
if gateway != "" {
fmt.Fprintf(&sb, "dhcp-option=3,%s\n", gateway)
}
if dnsIP != "" {
fmt.Fprintf(&sb, "dhcp-option=6,%s\n", dnsIP)
}
for _, lease := range dhcp.StaticLeases {
if lease.Hostname != "" {
fmt.Fprintf(&sb, "dhcp-host=%s,%s,%s\n", lease.MAC, lease.IP, lease.Hostname)
} else {
fmt.Fprintf(&sb, "dhcp-host=%s,%s\n", lease.MAC, lease.IP)
}
}
}
return sb.String()
}
// ValidateConfig tests a dnsmasq configuration file for syntax errors
func (g *ConfigGenerator) ValidateConfig(configPath string) error {
cmd := exec.Command("dnsmasq", "--test", "-C", configPath)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("config validation failed: %w (output: %s)", err, string(output))
}
if !strings.Contains(string(output), "syntax check OK") {
return fmt.Errorf("config validation did not report OK: %s", string(output))
}
return nil
}
// ReloadService sends a SIGHUP to dnsmasq to reload configuration.
// Lighter weight than a full restart. Falls back to RestartService on failure.
func (g *ConfigGenerator) ReloadService() error {
cmd := exec.Command("systemctl", "reload", "dnsmasq.service")
_, err := cmd.CombinedOutput()
if err != nil {
slog.Error("reload failed, attempting restart", "component", "dnsmasq", "error", err)
return g.RestartService()
}
slog.Info("dnsmasq service reloaded", "component", "dnsmasq")
return nil
}
// ConfigureSystemDNS configures systemd-resolved to use the local dnsmasq server
// This should only be called on first start of dnsmasq
func (g *ConfigGenerator) ConfigureSystemDNS() error {