2019-10-30 01:52:58 -07:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-02-16 08:46:49 -07:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2019-10-30 01:52:58 -07:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/AdguardTeam/golibs/utils"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
func httpError(r *http.Request, w http.ResponseWriter, code int, format string, args ...interface{}) {
|
|
|
|
text := fmt.Sprintf(format, args...)
|
2020-11-05 05:20:57 -07:00
|
|
|
log.Info("dns: %s %s: %s", r.Method, r.URL, text)
|
2019-10-30 01:52:58 -07:00
|
|
|
http.Error(w, text, code)
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
type dnsConfig struct {
|
|
|
|
Upstreams *[]string `json:"upstream_dns"`
|
|
|
|
UpstreamsFile *string `json:"upstream_dns_file"`
|
|
|
|
Bootstraps *[]string `json:"bootstrap_dns"`
|
|
|
|
|
|
|
|
ProtectionEnabled *bool `json:"protection_enabled"`
|
|
|
|
RateLimit *uint32 `json:"ratelimit"`
|
|
|
|
BlockingMode *string `json:"blocking_mode"`
|
2021-01-13 06:56:05 -07:00
|
|
|
BlockingIPv4 net.IP `json:"blocking_ipv4"`
|
|
|
|
BlockingIPv6 net.IP `json:"blocking_ipv6"`
|
2020-11-20 07:32:41 -07:00
|
|
|
EDNSCSEnabled *bool `json:"edns_cs_enabled"`
|
|
|
|
DNSSECEnabled *bool `json:"dnssec_enabled"`
|
|
|
|
DisableIPv6 *bool `json:"disable_ipv6"`
|
|
|
|
UpstreamMode *string `json:"upstream_mode"`
|
|
|
|
CacheSize *uint32 `json:"cache_size"`
|
|
|
|
CacheMinTTL *uint32 `json:"cache_ttl_min"`
|
|
|
|
CacheMaxTTL *uint32 `json:"cache_ttl_max"`
|
2019-11-08 04:59:12 -07:00
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
func (s *Server) getDNSConfig() dnsConfig {
|
2019-11-08 04:59:12 -07:00
|
|
|
s.RLock()
|
2020-11-20 07:32:41 -07:00
|
|
|
upstreams := stringArrayDup(s.conf.UpstreamDNS)
|
|
|
|
upstreamFile := s.conf.UpstreamDNSFileName
|
|
|
|
bootstraps := stringArrayDup(s.conf.BootstrapDNS)
|
|
|
|
protectionEnabled := s.conf.ProtectionEnabled
|
|
|
|
blockingMode := s.conf.BlockingMode
|
|
|
|
BlockingIPv4 := s.conf.BlockingIPv4
|
|
|
|
BlockingIPv6 := s.conf.BlockingIPv6
|
|
|
|
Ratelimit := s.conf.Ratelimit
|
|
|
|
EnableEDNSClientSubnet := s.conf.EnableEDNSClientSubnet
|
|
|
|
EnableDNSSEC := s.conf.EnableDNSSEC
|
|
|
|
AAAADisabled := s.conf.AAAADisabled
|
|
|
|
CacheSize := s.conf.CacheSize
|
|
|
|
CacheMinTTL := s.conf.CacheMinTTL
|
|
|
|
CacheMaxTTL := s.conf.CacheMaxTTL
|
|
|
|
var upstreamMode string
|
2020-06-10 05:33:23 -07:00
|
|
|
if s.conf.FastestAddr {
|
2020-11-20 07:32:41 -07:00
|
|
|
upstreamMode = "fastest_addr"
|
2020-06-10 05:33:23 -07:00
|
|
|
} else if s.conf.AllServers {
|
2020-11-20 07:32:41 -07:00
|
|
|
upstreamMode = "parallel"
|
2020-06-10 05:33:23 -07:00
|
|
|
}
|
2019-11-08 04:59:12 -07:00
|
|
|
s.RUnlock()
|
2020-11-20 07:32:41 -07:00
|
|
|
return dnsConfig{
|
|
|
|
Upstreams: &upstreams,
|
|
|
|
UpstreamsFile: &upstreamFile,
|
|
|
|
Bootstraps: &bootstraps,
|
|
|
|
ProtectionEnabled: &protectionEnabled,
|
|
|
|
BlockingMode: &blockingMode,
|
2021-01-13 06:56:05 -07:00
|
|
|
BlockingIPv4: BlockingIPv4,
|
|
|
|
BlockingIPv6: BlockingIPv6,
|
2020-11-20 07:32:41 -07:00
|
|
|
RateLimit: &Ratelimit,
|
|
|
|
EDNSCSEnabled: &EnableEDNSClientSubnet,
|
|
|
|
DNSSECEnabled: &EnableDNSSEC,
|
|
|
|
DisableIPv6: &AAAADisabled,
|
|
|
|
CacheSize: &CacheSize,
|
|
|
|
CacheMinTTL: &CacheMinTTL,
|
|
|
|
CacheMaxTTL: &CacheMaxTTL,
|
|
|
|
UpstreamMode: &upstreamMode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleGetConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
resp := s.getDNSConfig()
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2019-11-08 04:59:12 -07:00
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
if err := enc.Encode(resp); err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "json.Encoder: %s", err)
|
2019-11-08 04:59:12 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
func (req *dnsConfig) checkBlockingMode() bool {
|
|
|
|
if req.BlockingMode == nil {
|
|
|
|
return true
|
2019-11-08 06:19:56 -07:00
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
bm := *req.BlockingMode
|
2019-11-08 06:19:56 -07:00
|
|
|
if bm == "custom_ip" {
|
2021-01-13 06:56:05 -07:00
|
|
|
if req.BlockingIPv4.To4() == nil {
|
2019-11-08 06:19:56 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-13 06:56:05 -07:00
|
|
|
return req.BlockingIPv6 != nil
|
2020-11-20 07:32:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, valid := range []string{
|
|
|
|
"default",
|
|
|
|
"refused",
|
|
|
|
"nxdomain",
|
|
|
|
"null_ip",
|
|
|
|
} {
|
|
|
|
if bm == valid {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *dnsConfig) checkUpstreamsMode() bool {
|
|
|
|
if req.UpstreamMode == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, valid := range []string{
|
|
|
|
"",
|
|
|
|
"fastest_addr",
|
|
|
|
"parallel",
|
|
|
|
} {
|
|
|
|
if *req.UpstreamMode == valid {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *dnsConfig) checkBootstrap() (string, error) {
|
|
|
|
if req.Bootstraps == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, boot := range *req.Bootstraps {
|
|
|
|
if boot == "" {
|
|
|
|
return boot, fmt.Errorf("invalid bootstrap server address: empty")
|
|
|
|
}
|
|
|
|
|
2021-03-03 05:27:25 -07:00
|
|
|
if _, err := upstream.NewResolver(boot, upstream.Options{Timeout: 0}); err != nil {
|
2020-11-20 07:32:41 -07:00
|
|
|
return boot, fmt.Errorf("invalid bootstrap server address: %w", err)
|
|
|
|
}
|
2019-11-08 06:19:56 -07:00
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
return "", nil
|
2019-11-08 06:19:56 -07:00
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
func (req *dnsConfig) checkCacheTTL() bool {
|
|
|
|
if req.CacheMinTTL == nil && req.CacheMaxTTL == nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-12-21 05:43:27 -07:00
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
var min, max uint32
|
|
|
|
if req.CacheMinTTL != nil {
|
|
|
|
min = *req.CacheMinTTL
|
2020-08-21 05:54:16 -07:00
|
|
|
}
|
2020-12-21 05:43:27 -07:00
|
|
|
if req.CacheMaxTTL != nil {
|
2020-11-20 07:32:41 -07:00
|
|
|
max = *req.CacheMaxTTL
|
2020-08-21 05:54:16 -07:00
|
|
|
}
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
return min <= max
|
2020-08-21 05:54:16 -07:00
|
|
|
}
|
|
|
|
|
2019-11-08 04:59:12 -07:00
|
|
|
func (s *Server) handleSetConfig(w http.ResponseWriter, r *http.Request) {
|
2020-11-20 07:32:41 -07:00
|
|
|
req := dnsConfig{}
|
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
if err := dec.Decode(&req); err != nil {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "json Encode: %s", err)
|
2019-11-08 04:59:12 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if req.Upstreams != nil {
|
|
|
|
if err := ValidateUpstreams(*req.Upstreams); err != nil {
|
2020-09-22 05:04:17 -07:00
|
|
|
httpError(r, w, http.StatusBadRequest, "wrong upstreams specification: %s", err)
|
|
|
|
return
|
2020-04-22 09:14:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if errBoot, err := req.checkBootstrap(); err != nil {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "%s can not be used as bootstrap dns cause: %s", errBoot, err)
|
|
|
|
return
|
2020-04-22 09:14:04 -07:00
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if !req.checkBlockingMode() {
|
2019-11-08 06:19:56 -07:00
|
|
|
httpError(r, w, http.StatusBadRequest, "blocking_mode: incorrect value")
|
2019-11-08 04:59:12 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if !req.checkUpstreamsMode() {
|
2020-06-10 05:33:23 -07:00
|
|
|
httpError(r, w, http.StatusBadRequest, "upstream_mode: incorrect value")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if !req.checkCacheTTL() {
|
2020-07-03 10:35:04 -07:00
|
|
|
httpError(r, w, http.StatusBadRequest, "cache_ttl_min must be less or equal than cache_ttl_max")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if s.setConfig(req) {
|
|
|
|
if err := s.Reconfigure(nil); err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) setConfig(dc dnsConfig) (restart bool) {
|
2019-11-08 04:59:12 -07:00
|
|
|
s.Lock()
|
2019-11-08 06:19:56 -07:00
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.Upstreams != nil {
|
|
|
|
s.conf.UpstreamDNS = *dc.Upstreams
|
2020-04-22 09:14:04 -07:00
|
|
|
restart = true
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.UpstreamsFile != nil {
|
|
|
|
s.conf.UpstreamDNSFileName = *dc.UpstreamsFile
|
2020-09-01 04:33:35 -07:00
|
|
|
restart = true
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.Bootstraps != nil {
|
|
|
|
s.conf.BootstrapDNS = *dc.Bootstraps
|
2020-04-22 09:14:04 -07:00
|
|
|
restart = true
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.ProtectionEnabled != nil {
|
|
|
|
s.conf.ProtectionEnabled = *dc.ProtectionEnabled
|
2019-11-08 06:19:56 -07:00
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.BlockingMode != nil {
|
|
|
|
s.conf.BlockingMode = *dc.BlockingMode
|
|
|
|
if *dc.BlockingMode == "custom_ip" {
|
2021-01-13 06:56:05 -07:00
|
|
|
s.conf.BlockingIPv4 = dc.BlockingIPv4.To4()
|
|
|
|
s.conf.BlockingIPv6 = dc.BlockingIPv6.To16()
|
2019-11-08 06:19:56 -07:00
|
|
|
}
|
2019-11-08 04:59:12 -07:00
|
|
|
}
|
2019-11-08 06:19:56 -07:00
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.RateLimit != nil {
|
|
|
|
if s.conf.Ratelimit != *dc.RateLimit {
|
2019-11-08 06:19:56 -07:00
|
|
|
restart = true
|
|
|
|
}
|
2020-11-20 07:32:41 -07:00
|
|
|
s.conf.Ratelimit = *dc.RateLimit
|
2019-11-08 06:19:56 -07:00
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.EDNSCSEnabled != nil {
|
|
|
|
s.conf.EnableEDNSClientSubnet = *dc.EDNSCSEnabled
|
2019-11-12 09:45:55 -07:00
|
|
|
restart = true
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.DNSSECEnabled != nil {
|
|
|
|
s.conf.EnableDNSSEC = *dc.DNSSECEnabled
|
2020-02-17 07:34:57 -07:00
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.DisableIPv6 != nil {
|
|
|
|
s.conf.AAAADisabled = *dc.DisableIPv6
|
2019-11-11 09:34:47 -07:00
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.CacheSize != nil {
|
|
|
|
s.conf.CacheSize = *dc.CacheSize
|
2020-07-03 10:35:04 -07:00
|
|
|
restart = true
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.CacheMinTTL != nil {
|
|
|
|
s.conf.CacheMinTTL = *dc.CacheMinTTL
|
2020-07-03 10:35:04 -07:00
|
|
|
restart = true
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.CacheMaxTTL != nil {
|
|
|
|
s.conf.CacheMaxTTL = *dc.CacheMaxTTL
|
2020-07-03 10:35:04 -07:00
|
|
|
restart = true
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if dc.UpstreamMode != nil {
|
|
|
|
switch *dc.UpstreamMode {
|
2020-06-10 05:33:23 -07:00
|
|
|
case "parallel":
|
|
|
|
s.conf.AllServers = true
|
2020-11-20 07:32:41 -07:00
|
|
|
s.conf.FastestAddr = false
|
2020-06-10 05:33:23 -07:00
|
|
|
case "fastest_addr":
|
2020-11-20 07:32:41 -07:00
|
|
|
s.conf.AllServers = false
|
2020-06-10 05:33:23 -07:00
|
|
|
s.conf.FastestAddr = true
|
2020-11-20 07:32:41 -07:00
|
|
|
default:
|
|
|
|
s.conf.AllServers = false
|
|
|
|
s.conf.FastestAddr = false
|
2020-06-10 05:33:23 -07:00
|
|
|
}
|
2020-04-22 09:14:04 -07:00
|
|
|
}
|
2019-11-08 04:59:12 -07:00
|
|
|
s.Unlock()
|
|
|
|
s.conf.ConfigModified()
|
2020-11-20 07:32:41 -07:00
|
|
|
return restart
|
2019-11-08 04:59:12 -07:00
|
|
|
}
|
|
|
|
|
2019-10-30 01:52:58 -07:00
|
|
|
type upstreamJSON struct {
|
|
|
|
Upstreams []string `json:"upstream_dns"` // Upstreams
|
|
|
|
BootstrapDNS []string `json:"bootstrap_dns"` // Bootstrap DNS
|
|
|
|
}
|
|
|
|
|
2019-11-06 07:24:15 -07:00
|
|
|
// ValidateUpstreams validates each upstream and returns an error if any upstream is invalid or if there are no default upstreams specified
|
|
|
|
func ValidateUpstreams(upstreams []string) error {
|
2020-09-22 05:04:17 -07:00
|
|
|
// No need to validate comments
|
|
|
|
upstreams = filterOutComments(upstreams)
|
|
|
|
|
|
|
|
// Consider this case valid because defaultDNS will be used
|
|
|
|
if len(upstreams) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:17:54 -07:00
|
|
|
_, err := proxy.ParseUpstreamsConfig(
|
|
|
|
upstreams,
|
2021-03-03 05:27:25 -07:00
|
|
|
upstream.Options{
|
|
|
|
Bootstrap: []string{},
|
|
|
|
Timeout: DefaultTimeout,
|
|
|
|
},
|
|
|
|
)
|
2021-02-16 08:46:49 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:52:58 -07:00
|
|
|
var defaultUpstreamFound bool
|
|
|
|
for _, u := range upstreams {
|
2021-03-11 11:30:52 -07:00
|
|
|
var ok bool
|
|
|
|
ok, err = validateUpstream(u)
|
2019-10-30 01:52:58 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !defaultUpstreamFound {
|
2021-03-11 11:30:52 -07:00
|
|
|
defaultUpstreamFound = ok
|
2019-10-30 01:52:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !defaultUpstreamFound {
|
|
|
|
return fmt.Errorf("no default upstreams specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-27 05:03:07 -07:00
|
|
|
var protocols = []string{"tls://", "https://", "tcp://", "sdns://", "quic://"}
|
2019-10-30 01:52:58 -07:00
|
|
|
|
|
|
|
func validateUpstream(u string) (bool, error) {
|
|
|
|
// Check if user tries to specify upstream for domain
|
|
|
|
u, defaultUpstream, err := separateUpstream(u)
|
|
|
|
if err != nil {
|
|
|
|
return defaultUpstream, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The special server address '#' means "use the default servers"
|
|
|
|
if u == "#" && !defaultUpstream {
|
|
|
|
return defaultUpstream, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the upstream has a valid protocol prefix
|
|
|
|
for _, proto := range protocols {
|
|
|
|
if strings.HasPrefix(u, proto) {
|
|
|
|
return defaultUpstream, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return error if the upstream contains '://' without any valid protocol
|
|
|
|
if strings.Contains(u, "://") {
|
|
|
|
return defaultUpstream, fmt.Errorf("wrong protocol")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if upstream is valid plain DNS
|
|
|
|
return defaultUpstream, checkPlainDNS(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
// separateUpstream returns upstream without specified domains and a bool flag that indicates if no domains were specified
|
|
|
|
// error will be returned if upstream per domain specification is invalid
|
|
|
|
func separateUpstream(upstream string) (string, bool, error) {
|
|
|
|
defaultUpstream := true
|
|
|
|
if strings.HasPrefix(upstream, "[/") {
|
|
|
|
defaultUpstream = false
|
|
|
|
// split domains and upstream string
|
|
|
|
domainsAndUpstream := strings.Split(strings.TrimPrefix(upstream, "[/"), "/]")
|
|
|
|
if len(domainsAndUpstream) != 2 {
|
2020-11-05 05:20:57 -07:00
|
|
|
return "", defaultUpstream, fmt.Errorf("wrong dns upstream per domain specification: %s", upstream)
|
2019-10-30 01:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// split domains list and validate each one
|
|
|
|
for _, host := range strings.Split(domainsAndUpstream[0], "/") {
|
|
|
|
if host != "" {
|
|
|
|
if err := utils.IsValidHostname(host); err != nil {
|
|
|
|
return "", defaultUpstream, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
upstream = domainsAndUpstream[1]
|
|
|
|
}
|
|
|
|
return upstream, defaultUpstream, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkPlainDNS checks if host is plain DNS
|
|
|
|
func checkPlainDNS(upstream string) error {
|
|
|
|
// Check if host is ip without port
|
|
|
|
if net.ParseIP(upstream) != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if host is ip with port
|
|
|
|
ip, port, err := net.SplitHostPort(upstream)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if net.ParseIP(ip) == nil {
|
|
|
|
return fmt.Errorf("%s is not a valid IP", ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = strconv.ParseInt(port, 0, 64)
|
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("%s is not a valid port: %w", port, err)
|
2019-10-30 01:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleTestUpstreamDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
|
req := upstreamJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "Failed to read request body: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result := map[string]string{}
|
|
|
|
|
|
|
|
for _, host := range req.Upstreams {
|
|
|
|
err = checkDNS(host, req.BootstrapDNS)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("%v", err)
|
|
|
|
result[host] = err.Error()
|
|
|
|
} else {
|
|
|
|
result[host] = "OK"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonVal, err := json.Marshal(result)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "Unable to marshal status json: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, err = w.Write(jsonVal)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "Couldn't write body: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDNS(input string, bootstrap []string) error {
|
2020-09-22 05:04:17 -07:00
|
|
|
if !isUpstream(input) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:52:58 -07:00
|
|
|
// separate upstream from domains list
|
|
|
|
input, defaultUpstream, err := separateUpstream(input)
|
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("wrong upstream format: %w", err)
|
2019-10-30 01:52:58 -07:00
|
|
|
}
|
|
|
|
|
2020-05-14 08:24:59 -07:00
|
|
|
// No need to check this DNS server
|
2020-09-22 05:04:17 -07:00
|
|
|
if !defaultUpstream {
|
2019-10-30 01:52:58 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-11 11:30:52 -07:00
|
|
|
if _, err = validateUpstream(input); err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("wrong upstream format: %w", err)
|
2019-10-30 01:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(bootstrap) == 0 {
|
|
|
|
bootstrap = defaultBootstrap
|
|
|
|
}
|
|
|
|
|
2020-11-05 05:20:57 -07:00
|
|
|
log.Debug("checking if dns %s works...", input)
|
2019-10-30 01:52:58 -07:00
|
|
|
u, err := upstream.AddressToUpstream(input, upstream.Options{Bootstrap: bootstrap, Timeout: DefaultTimeout})
|
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("failed to choose upstream for %s: %w", input, err)
|
2019-10-30 01:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
req := dns.Msg{}
|
|
|
|
req.Id = dns.Id()
|
|
|
|
req.RecursionDesired = true
|
|
|
|
req.Question = []dns.Question{
|
|
|
|
{Name: "google-public-dns-a.google.com.", Qtype: dns.TypeA, Qclass: dns.ClassINET},
|
|
|
|
}
|
|
|
|
reply, err := u.Exchange(&req)
|
|
|
|
if err != nil {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("couldn't communicate with dns server %s: %w", input, err)
|
2019-10-30 01:52:58 -07:00
|
|
|
}
|
|
|
|
if len(reply.Answer) != 1 {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("dns server %s returned wrong answer", input)
|
2019-10-30 01:52:58 -07:00
|
|
|
}
|
|
|
|
if t, ok := reply.Answer[0].(*dns.A); ok {
|
|
|
|
if !net.IPv4(8, 8, 8, 8).Equal(t.A) {
|
2020-11-05 05:20:57 -07:00
|
|
|
return fmt.Errorf("dns server %s returned wrong answer: %v", input, t.A)
|
2019-10-30 01:52:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 05:20:57 -07:00
|
|
|
log.Debug("dns %s works OK", input)
|
2019-10-30 01:52:58 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 01:15:31 -07:00
|
|
|
// Control flow:
|
|
|
|
// web
|
|
|
|
// -> dnsforward.handleDOH -> dnsforward.ServeHTTP
|
|
|
|
// -> proxy.ServeHTTP -> proxy.handleDNSRequest
|
|
|
|
// -> dnsforward.handleDNSRequest
|
2020-02-27 01:48:59 -07:00
|
|
|
func (s *Server) handleDOH(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !s.conf.TLSAllowUnencryptedDOH && r.TLS == nil {
|
|
|
|
httpError(r, w, http.StatusNotFound, "Not Found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.IsRunning() {
|
2020-11-05 05:20:57 -07:00
|
|
|
httpError(r, w, http.StatusInternalServerError, "dns server is not running")
|
2020-02-27 01:48:59 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:52:58 -07:00
|
|
|
func (s *Server) registerHandlers() {
|
2021-01-13 07:26:57 -07:00
|
|
|
s.conf.HTTPRegister(http.MethodGet, "/control/dns_info", s.handleGetConfig)
|
|
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/dns_config", s.handleSetConfig)
|
|
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/test_upstream_dns", s.handleTestUpstreamDNS)
|
2019-10-30 05:11:16 -07:00
|
|
|
|
2021-01-13 07:26:57 -07:00
|
|
|
s.conf.HTTPRegister(http.MethodGet, "/control/access/list", s.handleAccessList)
|
|
|
|
s.conf.HTTPRegister(http.MethodPost, "/control/access/set", s.handleAccessSet)
|
2020-02-27 01:48:59 -07:00
|
|
|
|
2021-02-02 04:15:13 -07:00
|
|
|
// Register both versions, with and without the trailing slash, to
|
|
|
|
// prevent a 301 Moved Permanently redirect when clients request the
|
|
|
|
// path without the trailing slash. Those redirects break some clients.
|
|
|
|
//
|
|
|
|
// See go doc net/http.ServeMux.
|
|
|
|
//
|
|
|
|
// See also https://github.com/AdguardTeam/AdGuardHome/issues/2628.
|
|
|
|
s.conf.HTTPRegister("", "/dns-query", s.handleDOH)
|
2021-01-27 08:32:13 -07:00
|
|
|
s.conf.HTTPRegister("", "/dns-query/", s.handleDOH)
|
2019-10-30 01:52:58 -07:00
|
|
|
}
|