Skip to content

Commit

Permalink
all: replace exchanger with proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Apr 9, 2021
1 parent 0247a2d commit bbc292a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 205 deletions.
86 changes: 0 additions & 86 deletions internal/aghnet/exchanger.go

This file was deleted.

64 changes: 0 additions & 64 deletions internal/aghnet/exchanger_test.go

This file was deleted.

16 changes: 6 additions & 10 deletions internal/dnsforward/dns.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dnsforward

import (
"errors"
"net"
"strings"
"time"
Expand Down Expand Up @@ -403,21 +402,18 @@ func (s *Server) processLocalPTR(ctx *dnsContext) (rc resultCode) {
return resultCodeSuccess
}

req := d.Req
resp, err := s.localResolvers.Exchange(req)
err := s.localResolvers.Resolve(d)
if err != nil {
if errors.Is(err, aghnet.NoUpstreamsErr) {
d.Res = s.genNXDomain(req)

return resultCodeFinish
}

ctx.err = err

return resultCodeError
}

d.Res = resp
if d.Res == nil {
d.Res = s.genNXDomain(d.Req)

return resultCodeFinish
}

return resultCodeSuccess
}
Expand Down
9 changes: 4 additions & 5 deletions internal/dnsforward/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,16 @@ func TestServer_ProcessInternalHosts(t *testing.T) {
}

func TestLocalRestriction(t *testing.T) {
s := createTestServer(t, &dnsfilter.Config{}, ServerConfig{
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
})
ups := &aghtest.TestUpstream{
Reverse: map[string][]string{
"251.252.253.254.in-addr.arpa.": {"host1.example.net."},
"1.1.168.192.in-addr.arpa.": {"some.local-client."},
},
}
s.localResolvers = &aghtest.Exchanger{Ups: ups}
s := createTestServer(t, &dnsfilter.Config{}, ServerConfig{
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
}, ups)
s.conf.UpstreamConfig.Upstreams = []upstream.Upstream{ups}
startDeferStop(t, s)

Expand Down
47 changes: 28 additions & 19 deletions internal/dnsforward/dnsforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
"github.com/AdguardTeam/AdGuardHome/internal/stats"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
)
Expand Down Expand Up @@ -66,7 +67,7 @@ type Server struct {

ipset ipsetCtx
subnetDetector *aghnet.SubnetDetector
localResolvers aghnet.Exchanger
localResolvers *proxy.Proxy

tableHostToIP map[string]net.IP // "hostname -> IP" table for internal addresses (DHCP)
tableHostToIPLock sync.Mutex
Expand Down Expand Up @@ -243,24 +244,24 @@ func (s *Server) Exchange(ip net.IP) (host string, err error) {
Qclass: dns.ClassINET,
}},
}
ctx := &proxy.DNSContext{
Proto: "udp",
Req: req,
StartTime: time.Now(),
}

var resp *dns.Msg
if s.subnetDetector.IsLocallyServedNetwork(ip) {
resp, err = s.localResolvers.Exchange(req)
err = s.localResolvers.Resolve(ctx)
} else {
ctx := &proxy.DNSContext{
Proto: "udp",
Req: req,
StartTime: time.Now(),
}
err = s.internalProxy.Resolve(ctx)

resp = ctx.Res
}
if err != nil {
return "", err
}

resp = ctx.Res

if len(resp.Answer) == 0 {
return "", fmt.Errorf("lookup for %q: %w", arpa, rDNSEmptyAnswerErr)
}
Expand Down Expand Up @@ -376,18 +377,26 @@ func (s *Server) setupResolvers(localAddrs []string) (err error) {
return err
}

// TODO(e.burkov): The approach of subtracting sets of strings
// is not really applicable here since in case of listening on
// all network interfaces we should check the whole interface's
// network to cut off all the loopback addresses as well.
// TODO(e.burkov): The approach of subtracting sets of strings is not
// really applicable here since in case of listening on all network
// interfaces we should check the whole interface's network to cut off
// all the loopback addresses as well.
localAddrs = stringSetSubtract(localAddrs, ourAddrs)

if s.localResolvers, err = aghnet.NewMultiAddrExchanger(
localAddrs,
bootstraps,
defaultLocalTimeout,
); err != nil {
return err
var upsConfig proxy.UpstreamConfig
upsConfig, err = proxy.ParseUpstreamsConfig(localAddrs, upstream.Options{
Bootstrap: bootstraps,
Timeout: defaultLocalTimeout,
// TODO(e.burkov): Should we verify server's ceritificates?
})
if err != nil {
return fmt.Errorf("parsing upstreams: %w", err)
}

s.localResolvers = &proxy.Proxy{
Config: proxy.Config{
UpstreamConfig: &upsConfig,
},
}

return nil
Expand Down
Loading

0 comments on commit bbc292a

Please sign in to comment.