refactor: move vpn-dns config directory from /etc/coredns to /tmp/nexavpn-vpn-dns

Add configDir, corefilePath, and overridesPath constants pointing to /tmp/nexavpn-vpn-dns directory. Update all file path references in writeCorefile and refreshOverrides to use new constants instead of hardcoded /etc/coredns paths.
This commit is contained in:
2026-03-19 22:27:52 +01:00
parent 5d5f736e1b
commit 74587ec8e1

View File

@@ -14,6 +14,12 @@ import (
"time" "time"
) )
const (
configDir = "/tmp/nexavpn-vpn-dns"
corefilePath = configDir + "/Corefile"
overridesPath = configDir + "/service-overrides.hosts"
)
type dnsResponse struct { type dnsResponse struct {
Records []dnsRecord `json:"records"` Records []dnsRecord `json:"records"`
} }
@@ -26,7 +32,7 @@ type dnsRecord struct {
func main() { func main() {
ctx := context.Background() ctx := context.Background()
if err := os.MkdirAll("/etc/coredns", 0o755); err != nil { if err := os.MkdirAll(configDir, 0o755); err != nil {
log.Fatalf("unable to create coredns config dir: %v", err) log.Fatalf("unable to create coredns config dir: %v", err)
} }
@@ -47,7 +53,7 @@ func main() {
} }
}() }()
cmd := exec.Command("/coredns", "-conf", "/etc/coredns/Corefile") cmd := exec.Command("/coredns", "-conf", corefilePath)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@@ -65,7 +71,7 @@ func writeCorefile() error {
corefile := fmt.Sprintf(`%s { corefile := fmt.Sprintf(`%s {
errors errors
hosts /etc/coredns/service-overrides.hosts { hosts ` + overridesPath + ` {
ttl 30 ttl 30
reload 15s reload 15s
fallthrough fallthrough
@@ -75,7 +81,7 @@ func writeCorefile() error {
} }
`, envOrDefault("NEXAVPN_VPN_DNS_ADDR", ":53"), strings.Join(upstreams, " ")) `, envOrDefault("NEXAVPN_VPN_DNS_ADDR", ":53"), strings.Join(upstreams, " "))
return os.WriteFile("/etc/coredns/Corefile", []byte(corefile), 0o644) return os.WriteFile(corefilePath, []byte(corefile), 0o644)
} }
func refreshOverrides(ctx context.Context) error { func refreshOverrides(ctx context.Context) error {
@@ -116,7 +122,7 @@ func refreshOverrides(ctx context.Context) error {
content += "\n" content += "\n"
} }
return os.WriteFile(filepath.Clean("/etc/coredns/service-overrides.hosts"), []byte(content), 0o644) return os.WriteFile(filepath.Clean(overridesPath), []byte(content), 0o644)
} }
func parseList(raw string) []string { func parseList(raw string) []string {