WIP -- single binary -- works, replies to DNS, but need to check what got broken

This commit is contained in:
Eugene Bujak 2018-10-11 20:52:23 +03:00
parent 838406353b
commit bad88961e9
6 changed files with 174 additions and 175 deletions

View File

@ -166,7 +166,7 @@ func writeAllConfigs() error {
return nil
}
const coreDNSConfigTemplate = `. {
const coreDNSConfigTemplate = `.:{{.Port}} {
{{if .ProtectionEnabled}}dnsfilter {{if .FilteringEnabled}}{{.FilterFile}}{{end}} {
{{if .SafeBrowsingEnabled}}safebrowsing{{end}}
{{if .ParentalEnabled}}parental {{.ParentalSensitivity}}{{end}}

View File

@ -9,14 +9,13 @@ import (
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"time"
coredns_plugin "github.com/AdguardTeam/AdguardDNS/coredns_plugin"
"github.com/AdguardTeam/AdguardDNS/dnsfilter"
"github.com/miekg/dns"
"gopkg.in/asaskevich/govalidator.v4"
@ -24,8 +23,6 @@ import (
const updatePeriod = time.Minute * 30
var coreDNSCommand *exec.Cmd
var filterTitle = regexp.MustCompile(`^! Title: +(.*)$`)
// cached version.json to avoid hammering github.io for each page reload
@ -43,26 +40,7 @@ var client = &http.Client{
// coredns run control
// -------------------
func tellCoreDNSToReload() {
// not running -- cheap check
if coreDNSCommand == nil && coreDNSCommand.Process == nil {
return
}
// not running -- more expensive check
if !isRunning() {
return
}
pid := coreDNSCommand.Process.Pid
process, err := os.FindProcess(pid)
if err != nil {
log.Printf("os.FindProcess(%d) returned err: %v\n", pid, err)
return
}
err = process.Signal(syscall.SIGUSR1)
if err != nil {
log.Printf("process.Signal on pid %d returned: %v\n", pid, err)
return
}
coredns_plugin.Reload <- true
}
func writeAllConfigsAndReloadCoreDNS() error {
@ -95,126 +73,6 @@ func returnOK(w http.ResponseWriter, r *http.Request) {
}
}
func isRunning() bool {
if coreDNSCommand != nil && coreDNSCommand.Process != nil {
pid := coreDNSCommand.Process.Pid
process, err := os.FindProcess(pid)
if err != nil {
log.Printf("os.FindProcess(%d) returned err: %v\n", pid, err)
} else {
err := process.Signal(syscall.Signal(0))
if err != nil {
log.Printf("process.Signal on pid %d returned: %v\n", pid, err)
}
if err == nil {
return true
}
}
}
return false
}
func startDNSServer() error {
if isRunning() {
return fmt.Errorf("Unable to start coreDNS: Already running")
}
err := writeCoreDNSConfig()
if err != nil {
errortext := fmt.Errorf("Unable to write coredns config: %s", err)
log.Println(errortext)
return errortext
}
err = writeFilterFile()
if err != nil {
errortext := fmt.Errorf("Couldn't write filter file: %s", err)
log.Println(errortext)
return errortext
}
binarypath := filepath.Join(config.ourBinaryDir, config.CoreDNS.binaryFile)
configpath := filepath.Join(config.ourBinaryDir, config.CoreDNS.coreFile)
coreDNSCommand = exec.Command(binarypath, "-conf", configpath, "-dns.port", fmt.Sprintf("%d", config.CoreDNS.Port))
coreDNSCommand.Stdout = os.Stdout
coreDNSCommand.Stderr = os.Stderr
err = coreDNSCommand.Start()
if err != nil {
errortext := fmt.Errorf("Unable to start coreDNS: %s", err)
log.Println(errortext)
return errortext
}
log.Printf("coredns PID: %v\n", coreDNSCommand.Process.Pid)
go childwaiter()
return nil
}
func handleStart(w http.ResponseWriter, r *http.Request) {
if isRunning() {
http.Error(w, fmt.Sprintf("Unable to start coreDNS: Already running"), 400)
return
}
err := startDNSServer()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = fmt.Fprintf(w, "OK, PID %d\n", coreDNSCommand.Process.Pid)
if err != nil {
log.Printf("Couldn't write body in %s(): %s", _Func(), err)
return
}
}
func childwaiter() {
err := coreDNSCommand.Wait()
log.Printf("coredns unexpectedly died: %s\n", err)
coreDNSCommand.Process.Release()
log.Printf("restarting coredns")
err = startDNSServer()
if err != nil {
log.Printf("Couldn't restart DNS server: %s\n", err)
return
}
}
func handleStop(w http.ResponseWriter, r *http.Request) {
if coreDNSCommand == nil || coreDNSCommand.Process == nil {
http.Error(w, fmt.Sprintf("Unable to start coreDNS: Not running"), 400)
return
}
if isRunning() {
http.Error(w, fmt.Sprintf("Unable to start coreDNS: Not running"), 400)
return
}
cmd := coreDNSCommand
// TODO: send SIGTERM first, then SIGKILL
err := cmd.Process.Kill()
if err != nil {
errortext := fmt.Sprintf("Unable to stop coreDNS:\nGot error %T\n%v\n%s", err, err, err)
log.Println(errortext)
http.Error(w, errortext, 500)
return
}
exitstatus := cmd.Wait()
err = cmd.Process.Release()
if err != nil {
errortext := fmt.Sprintf("Unable to release process resources: %s", err)
log.Println(errortext)
http.Error(w, errortext, 500)
return
}
_, err = fmt.Fprintf(w, "OK\n%s\n", exitstatus)
if err != nil {
log.Printf("Couldn't write body in %s(): %s", _Func(), err)
return
}
}
func handleRestart(w http.ResponseWriter, r *http.Request) {
handleStop(w, r)
handleStart(w, r)
}
func handleStatus(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{
"dns_address": config.BindHost,
@ -1246,9 +1104,6 @@ func handleSafeSearchStatus(w http.ResponseWriter, r *http.Request) {
}
func registerControlHandlers() {
http.HandleFunc("/control/start", optionalAuth(ensurePOST(handleStart)))
http.HandleFunc("/control/stop", optionalAuth(ensurePOST(handleStop)))
http.HandleFunc("/control/restart", optionalAuth(ensurePOST(handleRestart)))
http.HandleFunc("/control/status", optionalAuth(ensureGET(handleStatus)))
http.HandleFunc("/control/enable_protection", optionalAuth(ensurePOST(handleProtectionEnable)))
http.HandleFunc("/control/disable_protection", optionalAuth(ensurePOST(handleProtectionDisable)))

127
coredns.go Normal file
View File

@ -0,0 +1,127 @@
package main
import (
"fmt"
"log"
"sync"
// Include all plugins.
_ "github.com/AdguardTeam/AdguardDNS/coredns_plugin"
_ "github.com/coredns/coredns/plugin/auto"
_ "github.com/coredns/coredns/plugin/autopath"
_ "github.com/coredns/coredns/plugin/bind"
_ "github.com/coredns/coredns/plugin/cache"
_ "github.com/coredns/coredns/plugin/chaos"
_ "github.com/coredns/coredns/plugin/debug"
_ "github.com/coredns/coredns/plugin/dnssec"
_ "github.com/coredns/coredns/plugin/dnstap"
_ "github.com/coredns/coredns/plugin/erratic"
_ "github.com/coredns/coredns/plugin/errors"
_ "github.com/coredns/coredns/plugin/file"
_ "github.com/coredns/coredns/plugin/forward"
_ "github.com/coredns/coredns/plugin/health"
_ "github.com/coredns/coredns/plugin/hosts"
_ "github.com/coredns/coredns/plugin/loadbalance"
_ "github.com/coredns/coredns/plugin/log"
_ "github.com/coredns/coredns/plugin/loop"
_ "github.com/coredns/coredns/plugin/metadata"
_ "github.com/coredns/coredns/plugin/metrics"
_ "github.com/coredns/coredns/plugin/nsid"
_ "github.com/coredns/coredns/plugin/pprof"
_ "github.com/coredns/coredns/plugin/proxy"
_ "github.com/coredns/coredns/plugin/reload"
_ "github.com/coredns/coredns/plugin/rewrite"
_ "github.com/coredns/coredns/plugin/root"
_ "github.com/coredns/coredns/plugin/secondary"
_ "github.com/coredns/coredns/plugin/template"
_ "github.com/coredns/coredns/plugin/tls"
_ "github.com/coredns/coredns/plugin/whoami"
_ "github.com/mholt/caddy/onevent"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/coremain"
)
// Directives are registered in the order they should be
// executed.
//
// Ordering is VERY important. Every plugin will
// feel the effects of all other plugin below
// (after) them during a request, but they must not
// care what plugin above them are doing.
var directives = []string{
"metadata",
"tls",
"reload",
"nsid",
"root",
"bind",
"debug",
"health",
"pprof",
"prometheus",
"errors",
"log",
"dnsfilter",
"dnstap",
"chaos",
"loadbalance",
"cache",
"rewrite",
"dnssec",
"autopath",
"template",
"hosts",
"file",
"auto",
"secondary",
"loop",
"forward",
"proxy",
"erratic",
"whoami",
"on",
}
func init() {
dnsserver.Directives = directives
}
var (
isCoreDNSRunningLock sync.Mutex
isCoreDNSRunning = false
)
func isRunning() bool {
isCoreDNSRunningLock.Lock()
value := isCoreDNSRunning
isCoreDNSRunningLock.Unlock()
return value
}
func startDNSServer() error {
isCoreDNSRunningLock.Lock()
if isCoreDNSRunning {
isCoreDNSRunningLock.Unlock()
return fmt.Errorf("Unable to start coreDNS: Already running")
}
isCoreDNSRunning = true
isCoreDNSRunningLock.Unlock()
err := writeCoreDNSConfig()
if err != nil {
errortext := fmt.Errorf("Unable to write coredns config: %s", err)
log.Println(errortext)
return errortext
}
err = writeFilterFile()
if err != nil {
errortext := fmt.Errorf("Couldn't write filter file: %s", err)
log.Println(errortext)
return errortext
}
go coremain.Run()
return nil
}

View File

@ -185,6 +185,10 @@ func setupPlugin(c *caddy.Controller) (*plug, error) {
})
}
onceHook.Do(func() {
caddy.RegisterEventHook("dnsfilter-reload", hook)
})
p.upstream, err = upstream.New(nil)
if err != nil {
return nil, err
@ -573,4 +577,5 @@ func (p *plug) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
// Name returns name of the plugin as seen in Corefile and plugin.cfg
func (p *plug) Name() string { return "dnsfilter" }
var onceHook sync.Once
var onceQueryLog sync.Once

39
coredns_plugin/reload.go Normal file
View File

@ -0,0 +1,39 @@
package dnsfilter
import (
"log"
"github.com/mholt/caddy"
)
var Reload = make(chan bool)
func hook(event caddy.EventName, info interface{}) error {
if event != caddy.InstanceStartupEvent {
return nil
}
// this should be an instance. ok to panic if not
instance := info.(*caddy.Instance)
go func() {
trace("Will wait for Reload channel")
for range Reload {
trace("Got message on Reload, restarting coredns")
corefile, err := caddy.LoadCaddyfile(instance.Caddyfile().ServerType())
if err != nil {
continue
}
_, err = instance.Restart(corefile)
if err != nil {
log.Printf("Corefile changed but reload failed: %s", err)
continue
}
// hook will be called again from new instance
return
}
}()
return nil
}

View File

@ -25,33 +25,6 @@ tags:
name: safesearch
description: 'Enforce family-friendly results in search engines'
paths:
/start:
post:
tags:
- global
operationId: start
summary: 'Start DNS server'
responses:
200:
description: OK
/stop:
post:
tags:
- global
operationId: stop
summary: 'Stop DNS server'
responses:
200:
description: OK
/restart:
post:
tags:
- global
operationId: restart
summary: 'Restart DNS server'
responses:
200:
description: OK
/status:
get:
tags: