2018-10-11 20:52:23 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-11-28 16:45:30 +03:00
|
|
|
"net"
|
2018-10-11 20:52:23 +03:00
|
|
|
|
2018-11-28 16:45:30 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/dnsforward"
|
|
|
|
"github.com/joomcode/errorx"
|
2018-10-11 20:52:23 +03:00
|
|
|
)
|
|
|
|
|
2018-11-28 16:45:30 +03:00
|
|
|
var dnsServer = dnsforward.Server{}
|
2018-10-11 20:52:23 +03:00
|
|
|
|
|
|
|
func isRunning() bool {
|
2018-11-28 16:45:30 +03:00
|
|
|
return dnsServer.IsRunning()
|
2018-10-11 20:52:23 +03:00
|
|
|
}
|
|
|
|
|
2018-11-28 18:57:20 +03:00
|
|
|
func generateServerConfig() dnsforward.ServerConfig {
|
2018-11-28 16:45:30 +03:00
|
|
|
filters := []dnsforward.Filter{}
|
2018-11-28 20:19:09 +03:00
|
|
|
userFilter := userFilter()
|
|
|
|
filters = append(filters, dnsforward.Filter{
|
|
|
|
ID: userFilter.ID,
|
|
|
|
Rules: userFilter.Rules,
|
|
|
|
})
|
2018-11-28 16:45:30 +03:00
|
|
|
for _, filter := range config.Filters {
|
|
|
|
filters = append(filters, dnsforward.Filter{
|
|
|
|
ID: filter.ID,
|
|
|
|
Rules: filter.Rules,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
newconfig := dnsforward.ServerConfig{
|
2018-11-28 18:14:12 +03:00
|
|
|
UDPListenAddr: &net.UDPAddr{Port: config.CoreDNS.Port},
|
|
|
|
BlockedResponseTTL: config.CoreDNS.BlockedResponseTTL,
|
|
|
|
Filters: filters,
|
2018-11-28 16:45:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, u := range config.CoreDNS.UpstreamDNS {
|
|
|
|
upstream, err := dnsforward.GetUpstream(u)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Couldn't get upstream: %s", err)
|
|
|
|
// continue, just ignore the upstream
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newconfig.Upstreams = append(newconfig.Upstreams, upstream)
|
|
|
|
}
|
2018-11-28 18:57:20 +03:00
|
|
|
return newconfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func startDNSServer() error {
|
|
|
|
if isRunning() {
|
|
|
|
return fmt.Errorf("Unable to start forwarding DNS server: Already running")
|
|
|
|
}
|
2018-10-17 20:43:26 +03:00
|
|
|
|
2018-11-28 18:57:20 +03:00
|
|
|
newconfig := generateServerConfig()
|
2018-11-28 16:45:30 +03:00
|
|
|
err := dnsServer.Start(&newconfig)
|
2018-10-11 20:52:23 +03:00
|
|
|
if err != nil {
|
2018-11-28 16:45:30 +03:00
|
|
|
return errorx.Decorate(err, "Couldn't start forwarding DNS server")
|
2018-10-11 20:52:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-28 18:57:20 +03:00
|
|
|
|
|
|
|
func reconfigureDNSServer() error {
|
|
|
|
if !isRunning() {
|
|
|
|
return fmt.Errorf("Refusing to reconfigure forwarding DNS server: not running")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := dnsServer.Reconfigure(generateServerConfig())
|
|
|
|
if err != nil {
|
|
|
|
return errorx.Decorate(err, "Couldn't start forwarding DNS server")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|