Skip to content

Commit

Permalink
dnsforward: add todo
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Mar 7, 2023
1 parent b58255e commit 66835a9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 38 deletions.
15 changes: 4 additions & 11 deletions internal/dnsforward/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ type FilteringConfig struct {
// EDNSClientSubnet is the settings list for EDNS Client Subnet.
type EDNSClientSubnet struct {
// CustomIP for EDNS Client Subnet.
CustomIP string `yaml:"custom_ip"`
CustomIP netip.Addr `yaml:"custom_ip"`

// Enabled defines if EDNS Client Subnet is enabled.
Enabled bool `yaml:"enabled"`
Expand Down Expand Up @@ -340,15 +340,8 @@ func (s *Server) createProxyConfig() (conf proxy.Config, err error) {
}

if srvConf.EDNSClientSubnet.UseCustom {
// TODO(s.chzhen): Add wrapper around netip.Addr.
var ip net.IP
ip, err = netutil.ParseIP(srvConf.EDNSClientSubnet.CustomIP)
if err != nil {
return conf, fmt.Errorf("edns: %w", err)
}

// TODO(s.chzhen): Use netip.Addr instead of net.IP inside dnsproxy.
conf.EDNSAddr = ip
conf.EDNSAddr = net.IP(srvConf.EDNSClientSubnet.CustomIP.AsSlice())
}

if srvConf.CacheSize != 0 {
Expand Down Expand Up @@ -377,7 +370,7 @@ func (s *Server) createProxyConfig() (conf proxy.Config, err error) {

err = s.prepareTLS(&conf)
if err != nil {
return conf, fmt.Errorf("validating tls: %w", err)
return proxy.Config{}, fmt.Errorf("validating tls: %w", err)
}

if c := srvConf.DNSCryptConfig; c.Enabled {
Expand All @@ -388,7 +381,7 @@ func (s *Server) createProxyConfig() (conf proxy.Config, err error) {
}

if conf.UpstreamConfig == nil || len(conf.UpstreamConfig.Upstreams) == 0 {
return conf, errors.Error("no default upstream servers configured")
return proxy.Config{}, errors.Error("no default upstream servers configured")
}

return conf, nil
Expand Down
31 changes: 5 additions & 26 deletions internal/dnsforward/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
)

// jsonDNSConfig is the JSON representation of the DNS server configuration.
//
// TODO(s.chzhen): Split it into smaller pieces.
type jsonDNSConfig struct {
// Upstreams is the list of upstream DNS servers.
Upstreams *[]string `json:"upstream_dns"`
Expand Down Expand Up @@ -86,7 +88,7 @@ type jsonDNSConfig struct {
BlockingIPv6 net.IP `json:"blocking_ipv6"`

// EDNSCSCustomIP is custom IP for EDNS Client Subnet.
EDNSCSCustomIP net.IP `json:"edns_cs_custom_ip"`
EDNSCSCustomIP netip.Addr `json:"edns_cs_custom_ip"`

// DefaultLocalPTRUpstreams is used to pass the addresses from
// systemResolvers to the front-end. It's not a pointer to the slice since
Expand All @@ -107,19 +109,10 @@ func (s *Server) getDNSConfig() (c *jsonDNSConfig, err error) {
blockingIPv6 := s.conf.BlockingIPv6
ratelimit := s.conf.Ratelimit

var customIP net.IP
customIP := s.conf.EDNSClientSubnet.CustomIP
enableEDNSClientSubnet := s.conf.EDNSClientSubnet.Enabled
useCustom := s.conf.EDNSClientSubnet.UseCustom

if useCustom {
customIP, err = netutil.ParseIP(s.conf.EDNSClientSubnet.CustomIP)
if err != nil {
log.Debug("getting edns client subnet: %s", err)

return nil, err
}
}

enableDNSSEC := s.conf.EnableDNSSEC
aaaaDisabled := s.conf.AAAADisabled
cacheSize := s.conf.CacheSize
Expand Down Expand Up @@ -232,11 +225,6 @@ func (req *jsonDNSConfig) validate(privateNets netutil.SubnetSet) (err error) {
}
}

err = validateEDNSCustomIP(req.EDNSCSUseCustom, req.EDNSCSCustomIP)
if err != nil {
return fmt.Errorf("validating edns client subnet: %w", err)
}

err = req.checkBootstrap()
if err != nil {
return err
Expand Down Expand Up @@ -321,7 +309,7 @@ func (s *Server) setConfig(dc *jsonDNSConfig) (shouldRestart bool) {
}

if dc.EDNSCSUseCustom != nil && *dc.EDNSCSUseCustom {
s.conf.EDNSClientSubnet.CustomIP = dc.EDNSCSCustomIP.String()
s.conf.EDNSClientSubnet.CustomIP = dc.EDNSCSCustomIP
}

setIfNotNil(&s.conf.ProtectionEnabled, dc.ProtectionEnabled)
Expand Down Expand Up @@ -530,15 +518,6 @@ func validateUpstream(u string, domains []string) (useDefault bool, err error) {
return false, err
}

// validateEDNSCustomIP validates parameters for EDNS Client Subnet.
func validateEDNSCustomIP(useCustom *bool, customIP net.IP) (err error) {
if useCustom == nil || !*useCustom {
return nil
}

return netutil.ValidateIP(customIP)
}

// separateUpstream returns the upstream and the specified domains. domains is
// nil when the upstream is not domains-specific. Otherwise it may also be
// empty.
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestDNSForwardHTTP_handleSetConfig(t *testing.T) {
wantSet: "",
}, {
name: "edns_cs_use_custom_bad_ip",
wantSet: "decoding request: invalid IP address: bad.ip",
wantSet: "decoding request: ParseAddr(\"bad.ip\"): unexpected character (at \"bad.ip\")",
}, {
name: "dnssec_enabled",
wantSet: "",
Expand Down

0 comments on commit 66835a9

Please sign in to comment.