2018-12-05 20:13:35 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2023-07-20 14:26:35 +03:00
|
|
|
"context"
|
2019-02-22 15:23:39 +03:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
2020-09-08 17:20:24 +03:00
|
|
|
"fmt"
|
2019-02-22 15:23:39 +03:00
|
|
|
"math/big"
|
2018-12-05 20:13:35 +03:00
|
|
|
"net"
|
2023-03-01 13:27:24 +03:00
|
|
|
"net/netip"
|
2019-02-22 15:23:39 +03:00
|
|
|
"sync"
|
2021-10-14 19:39:21 +03:00
|
|
|
"sync/atomic"
|
2018-12-05 20:13:35 +03:00
|
|
|
"testing"
|
2021-10-14 19:39:21 +03:00
|
|
|
"testing/fstest"
|
2018-12-24 15:19:52 +03:00
|
|
|
"time"
|
|
|
|
|
2022-08-16 13:21:25 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
2021-03-31 15:00:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-02-04 20:35:13 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2023-04-27 16:39:35 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering/hashprefix"
|
2023-03-15 14:31:07 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering/safesearch"
|
2019-05-30 15:36:39 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2019-11-21 16:13:19 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2022-03-18 13:37:27 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-10-22 11:58:18 +03:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-09-30 21:17:54 +03:00
|
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
2018-12-05 20:13:35 +03:00
|
|
|
"github.com/miekg/dns"
|
2019-10-31 12:32:14 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-11 17:32:58 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-05 20:13:35 +03:00
|
|
|
)
|
|
|
|
|
2020-11-16 15:52:05 +03:00
|
|
|
func TestMain(m *testing.M) {
|
2022-10-26 21:12:51 +03:00
|
|
|
testutil.DiscardLogOutput(m)
|
2020-11-16 15:52:05 +03:00
|
|
|
}
|
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
// testTimeout is the common timeout for tests.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Use more.
|
|
|
|
const testTimeout = 1 * time.Second
|
|
|
|
|
|
|
|
// testQuestionTarget is the common question target for tests.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Use more.
|
|
|
|
const testQuestionTarget = "target.example"
|
|
|
|
|
2019-02-22 15:23:39 +03:00
|
|
|
const (
|
|
|
|
tlsServerName = "testdns.adguard.com"
|
|
|
|
testMessagesCount = 10
|
|
|
|
)
|
|
|
|
|
2023-12-05 17:43:50 +03:00
|
|
|
// testClientAddrPort is the common net.Addr for tests.
|
2023-07-18 17:02:07 +03:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Use more.
|
2023-12-05 17:43:50 +03:00
|
|
|
var testClientAddrPort = netip.MustParseAddrPort("1.2.3.4:12345")
|
2023-07-18 17:02:07 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
func startDeferStop(t *testing.T, s *Server) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
err := s.Start()
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "failed to start server: %s", err)
|
2021-02-02 15:13:12 +03:00
|
|
|
|
2021-10-22 11:58:18 +03:00
|
|
|
testutil.CleanupAndRequireSuccess(t, s.Stop)
|
2021-02-02 15:13:12 +03:00
|
|
|
}
|
|
|
|
|
2021-04-09 21:01:21 +03:00
|
|
|
func createTestServer(
|
|
|
|
t *testing.T,
|
2021-05-21 16:15:47 +03:00
|
|
|
filterConf *filtering.Config,
|
2021-04-09 21:01:21 +03:00
|
|
|
forwardConf ServerConfig,
|
|
|
|
localUps upstream.Upstream,
|
|
|
|
) (s *Server) {
|
2021-03-11 17:32:58 +03:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
rules := `||nxdomain.example.org
|
2021-07-12 13:10:47 +03:00
|
|
|
||NULL.example.org^
|
2021-03-11 17:32:58 +03:00
|
|
|
127.0.0.1 host.example.org
|
|
|
|
@@||whitelist.example.org^
|
|
|
|
||127.0.0.255`
|
2021-05-21 16:15:47 +03:00
|
|
|
filters := []filtering.Filter{{
|
2021-03-11 17:32:58 +03:00
|
|
|
ID: 0, Data: []byte(rules),
|
|
|
|
}}
|
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
f, err := filtering.New(filterConf, filters)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-05-24 14:48:42 +03:00
|
|
|
f.SetEnabled(true)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
dhcp := &testDHCP{
|
|
|
|
OnEnabled: func() (ok bool) { return false },
|
|
|
|
OnHostByIP: func(ip netip.Addr) (host string) { return "" },
|
|
|
|
OnIPByHost: func(host string) (ip netip.Addr) { panic("not implemented") },
|
|
|
|
}
|
2021-03-31 15:00:47 +03:00
|
|
|
s, err = NewServer(DNSCreateParams{
|
2023-08-30 14:02:12 +03:00
|
|
|
DHCPServer: dhcp,
|
2022-03-18 13:37:27 +03:00
|
|
|
DNSFilter: f,
|
|
|
|
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
|
2021-03-31 15:00:47 +03:00
|
|
|
})
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-06 17:09:54 +03:00
|
|
|
err = s.Prepare(&forwardConf)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2021-05-26 17:55:19 +03:00
|
|
|
s.serverLock.Lock()
|
|
|
|
defer s.serverLock.Unlock()
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2023-02-06 17:17:51 +03:00
|
|
|
// TODO(e.burkov): Try to move it higher.
|
2021-04-09 21:01:21 +03:00
|
|
|
if localUps != nil {
|
2023-02-06 17:17:51 +03:00
|
|
|
ups := []upstream.Upstream{localUps}
|
|
|
|
s.localResolvers.UpstreamConfig.Upstreams = ups
|
2021-05-26 17:55:19 +03:00
|
|
|
s.conf.UsePrivateRDNS = true
|
2023-02-06 17:17:51 +03:00
|
|
|
s.dnsProxy.PrivateRDNSUpstreamConfig = &proxy.UpstreamConfig{
|
|
|
|
Upstreams: ups,
|
|
|
|
}
|
2021-04-09 21:01:21 +03:00
|
|
|
}
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func createServerTLSConfig(t *testing.T) (*tls.Config, []byte, []byte) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "cannot generate RSA key: %s", err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "failed to generate serial number: %s", err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
notBefore := time.Now()
|
2021-09-30 21:17:54 +03:00
|
|
|
notAfter := notBefore.Add(5 * 365 * timeutil.Day)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
Subject: pkix.Name{
|
|
|
|
Organization: []string{"AdGuard Tests"},
|
|
|
|
},
|
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
IsCA: true,
|
|
|
|
}
|
|
|
|
template.DNSNames = append(template.DNSNames, tlsServerName)
|
|
|
|
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(privateKey), privateKey)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "failed to create certificate: %s", err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
certPem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
|
|
keyPem := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})
|
|
|
|
|
|
|
|
cert, err := tls.X509KeyPair(certPem, keyPem)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "failed to create certificate: %s", err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
return &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
ServerName: tlsServerName,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}, certPem, keyPem
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestTLS(t *testing.T, tlsConf TLSConfig) (s *Server, certPem []byte) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
var keyPem []byte
|
|
|
|
_, certPem, keyPem = createServerTLSConfig(t)
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
s = createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-04-09 21:01:21 +03:00
|
|
|
}, nil)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
tlsConf.CertificateChainData, tlsConf.PrivateKeyData = certPem, keyPem
|
|
|
|
s.conf.TLSConfig = tlsConf
|
|
|
|
|
2022-09-06 17:09:54 +03:00
|
|
|
err := s.Prepare(&s.conf)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "failed to prepare server: %s", err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
return s, certPem
|
|
|
|
}
|
|
|
|
|
2022-10-28 19:35:59 +03:00
|
|
|
const googleDomainName = "google-public-dns-a.google.com."
|
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
func createGoogleATestMessage() *dns.Msg {
|
2022-10-28 19:35:59 +03:00
|
|
|
return createTestMessage(googleDomainName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGoogleUpstream() (u upstream.Upstream) {
|
|
|
|
return &aghtest.UpstreamMock{
|
|
|
|
OnAddress: func() (addr string) { return "google.upstream.example" },
|
|
|
|
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
|
|
|
|
return aghalg.Coalesce(
|
|
|
|
aghtest.MatchedResponse(req, dns.TypeA, googleDomainName, "8.8.8.8"),
|
|
|
|
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
|
|
|
|
), nil
|
|
|
|
},
|
|
|
|
OnClose: func() (err error) { return nil },
|
|
|
|
}
|
2021-03-11 17:32:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func createTestMessage(host string) *dns.Msg {
|
|
|
|
return &dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Id: dns.Id(),
|
|
|
|
RecursionDesired: true,
|
|
|
|
},
|
|
|
|
Question: []dns.Question{{
|
|
|
|
Name: host,
|
|
|
|
Qtype: dns.TypeA,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestMessageWithType(host string, qtype uint16) *dns.Msg {
|
|
|
|
req := createTestMessage(host)
|
|
|
|
req.Question[0].Qtype = qtype
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2023-08-09 16:27:21 +03:00
|
|
|
// newResp returns the new DNS response with response code set to rcode, req
|
|
|
|
// used as request, and rrs added.
|
|
|
|
func newResp(rcode int, req *dns.Msg, ans []dns.RR) (resp *dns.Msg) {
|
|
|
|
resp = (&dns.Msg{}).SetRcode(req, rcode)
|
|
|
|
resp.RecursionAvailable = true
|
|
|
|
resp.Compress = true
|
|
|
|
resp.Answer = ans
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
func assertGoogleAResponse(t *testing.T, reply *dns.Msg) {
|
2023-08-23 16:58:24 +03:00
|
|
|
assertResponse(t, reply, netip.AddrFrom4([4]byte{8, 8, 8, 8}))
|
2021-03-11 17:32:58 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 16:58:24 +03:00
|
|
|
func assertResponse(t *testing.T, reply *dns.Msg, ip netip.Addr) {
|
2021-03-11 17:32:58 +03:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
require.Lenf(t, reply.Answer, 1, "dns server returned reply with wrong number of answers - %d", len(reply.Answer))
|
|
|
|
|
|
|
|
a, ok := reply.Answer[0].(*dns.A)
|
|
|
|
require.Truef(t, ok, "dns server returned wrong answer type instead of A: %v", reply.Answer[0])
|
2023-08-23 16:58:24 +03:00
|
|
|
assert.Equal(t, net.IP(ip.AsSlice()), a.A)
|
2021-03-11 17:32:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendTestMessagesAsync sends messages in parallel to check for race issues.
|
|
|
|
//
|
|
|
|
//lint:ignore U1000 it's called from the function which is skipped for now.
|
|
|
|
func sendTestMessagesAsync(t *testing.T, conn *dns.Conn) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
|
|
|
|
for i := 0; i < testMessagesCount; i++ {
|
|
|
|
msg := createGoogleATestMessage()
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err := conn.WriteMsg(msg)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "cannot write message: %s", err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
res, err := conn.ReadMsg()
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "cannot read response to message: %s", err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
assertGoogleAResponse(t, res)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendTestMessages(t *testing.T, conn *dns.Conn) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
for i := 0; i < testMessagesCount; i++ {
|
|
|
|
req := createGoogleATestMessage()
|
|
|
|
err := conn.WriteMsg(req)
|
2021-04-15 19:00:31 +03:00
|
|
|
assert.NoErrorf(t, err, "cannot write message #%d: %s", i, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
res, err := conn.ReadMsg()
|
2021-04-15 19:00:31 +03:00
|
|
|
assert.NoErrorf(t, err, "cannot read response to message #%d: %s", i, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
assertGoogleAResponse(t, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 20:13:35 +03:00
|
|
|
func TestServer(t *testing.T) {
|
2023-08-30 18:26:02 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-04-09 21:01:21 +03:00
|
|
|
}, nil)
|
2022-10-28 19:35:59 +03:00
|
|
|
s.conf.UpstreamConfig.Upstreams = []upstream.Upstream{newGoogleUpstream()}
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2021-06-29 15:53:28 +03:00
|
|
|
net string
|
|
|
|
proto proxy.Proto
|
2021-02-02 15:13:12 +03:00
|
|
|
}{{
|
|
|
|
name: "message_over_udp",
|
2021-06-29 15:53:28 +03:00
|
|
|
net: "",
|
2021-02-02 15:13:12 +03:00
|
|
|
proto: proxy.ProtoUDP,
|
|
|
|
}, {
|
|
|
|
name: "message_over_tcp",
|
2021-06-29 15:53:28 +03:00
|
|
|
net: "tcp",
|
2021-02-02 15:13:12 +03:00
|
|
|
proto: proxy.ProtoTCP,
|
|
|
|
}}
|
2018-12-05 20:13:35 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
addr := s.dnsProxy.Addr(tc.proto)
|
2021-06-29 15:53:28 +03:00
|
|
|
client := dns.Client{Net: tc.net}
|
2018-12-05 20:13:35 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
reply, _, err := client.Exchange(createGoogleATestMessage(), addr.String())
|
2021-09-13 16:00:36 +03:00
|
|
|
require.NoErrorf(t, err, "couldn't talk to server %s: %s", addr, err)
|
2018-12-05 20:13:35 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
assertGoogleAResponse(t, reply)
|
|
|
|
})
|
2018-12-24 15:19:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 17:36:49 +03:00
|
|
|
func TestServer_timeout(t *testing.T) {
|
|
|
|
t.Run("custom", func(t *testing.T) {
|
|
|
|
srvConf := &ServerConfig{
|
2023-07-18 17:02:07 +03:00
|
|
|
UpstreamTimeout: testTimeout,
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
2022-09-06 17:09:54 +03:00
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-06-15 17:36:49 +03:00
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
s, err := NewServer(DNSCreateParams{DNSFilter: createTestDNSFilter(t)})
|
2021-06-15 17:36:49 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = s.Prepare(srvConf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
assert.Equal(t, testTimeout, s.conf.UpstreamTimeout)
|
2021-06-15 17:36:49 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("default", func(t *testing.T) {
|
2023-09-04 17:18:43 +03:00
|
|
|
s, err := NewServer(DNSCreateParams{DNSFilter: createTestDNSFilter(t)})
|
2021-06-15 17:36:49 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-12-25 14:16:48 +03:00
|
|
|
s.conf.Config.UpstreamMode = UpstreamModeLoadBalance
|
2023-08-30 18:26:02 +03:00
|
|
|
s.conf.Config.EDNSClientSubnet = &EDNSClientSubnet{
|
2023-03-01 16:16:16 +03:00
|
|
|
Enabled: false,
|
|
|
|
}
|
2022-09-06 17:09:54 +03:00
|
|
|
err = s.Prepare(&s.conf)
|
2021-06-15 17:36:49 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, DefaultTimeout, s.conf.UpstreamTimeout)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-08 17:37:28 +03:00
|
|
|
func TestServer_Prepare_fallbacks(t *testing.T) {
|
|
|
|
srvConf := &ServerConfig{
|
|
|
|
Config: Config{
|
|
|
|
FallbackDNS: []string{
|
|
|
|
"#tls://1.1.1.1",
|
|
|
|
"8.8.8.8",
|
|
|
|
},
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-09-08 17:37:28 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2023-09-08 17:37:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s, err := NewServer(DNSCreateParams{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = s.Prepare(srvConf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, s.dnsProxy.Fallbacks)
|
|
|
|
|
|
|
|
assert.Len(t, s.dnsProxy.Fallbacks.Upstreams, 1)
|
|
|
|
}
|
|
|
|
|
2019-08-20 00:55:32 +03:00
|
|
|
func TestServerWithProtectionDisabled(t *testing.T) {
|
2023-09-04 17:18:43 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-04-09 21:01:21 +03:00
|
|
|
}, nil)
|
2022-10-28 19:35:59 +03:00
|
|
|
s.conf.UpstreamConfig.Upstreams = []upstream.Upstream{newGoogleUpstream()}
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
2019-08-20 00:55:32 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Message over UDP.
|
2019-08-20 00:55:32 +03:00
|
|
|
req := createGoogleATestMessage()
|
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
2021-06-29 15:53:28 +03:00
|
|
|
client := &dns.Client{}
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2019-08-20 00:55:32 +03:00
|
|
|
reply, _, err := client.Exchange(req, addr.String())
|
2021-09-13 16:00:36 +03:00
|
|
|
require.NoErrorf(t, err, "couldn't talk to server %s: %s", addr, err)
|
2019-08-20 00:55:32 +03:00
|
|
|
assertGoogleAResponse(t, reply)
|
|
|
|
}
|
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
func TestDoTServer(t *testing.T) {
|
|
|
|
s, certPem := createTestTLS(t, TLSConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
TLSListenAddrs: []*net.TCPAddr{{}},
|
2021-02-02 15:13:12 +03:00
|
|
|
})
|
2022-10-28 19:35:59 +03:00
|
|
|
s.conf.UpstreamConfig.Upstreams = []upstream.Upstream{newGoogleUpstream()}
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
2019-02-22 15:23:39 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Add our self-signed generated config to roots.
|
2019-02-22 15:23:39 +03:00
|
|
|
roots := x509.NewCertPool()
|
|
|
|
roots.AppendCertsFromPEM(certPem)
|
2019-04-17 12:02:56 +03:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: tlsServerName,
|
|
|
|
RootCAs: roots,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
2019-02-22 15:23:39 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Create a DNS-over-TLS client connection.
|
2019-02-22 15:23:39 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoTLS)
|
|
|
|
conn, err := dns.DialWithTLS("tcp-tls", addr.String(), tlsConfig)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "cannot connect to the proxy: %s", err)
|
2019-02-22 15:23:39 +03:00
|
|
|
|
|
|
|
sendTestMessages(t, conn)
|
|
|
|
}
|
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
func TestDoQServer(t *testing.T) {
|
|
|
|
s, _ := createTestTLS(t, TLSConfig{
|
2021-04-21 12:49:41 +03:00
|
|
|
QUICListenAddrs: []*net.UDPAddr{{IP: net.IP{127, 0, 0, 1}}},
|
2021-02-02 15:13:12 +03:00
|
|
|
})
|
2022-10-28 19:35:59 +03:00
|
|
|
s.conf.UpstreamConfig.Upstreams = []upstream.Upstream{newGoogleUpstream()}
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
2020-09-08 17:20:24 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Create a DNS-over-QUIC upstream.
|
2020-09-08 17:20:24 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoQUIC)
|
2021-06-29 15:53:28 +03:00
|
|
|
opts := &upstream.Options{InsecureSkipVerify: true}
|
2021-02-02 15:13:12 +03:00
|
|
|
u, err := upstream.AddressToUpstream(fmt.Sprintf("%s://%s", proxy.ProtoQUIC, addr), opts)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
2020-09-08 17:20:24 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Send the test message.
|
2020-09-08 17:20:24 +03:00
|
|
|
req := createGoogleATestMessage()
|
|
|
|
res, err := u.Exchange(req)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
2020-09-08 17:20:24 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
assertGoogleAResponse(t, res)
|
2020-09-08 17:20:24 +03:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:23:39 +03:00
|
|
|
func TestServerRace(t *testing.T) {
|
2021-02-02 15:13:12 +03:00
|
|
|
t.Skip("TODO(e.burkov): inspect the golibs/cache package for locks")
|
|
|
|
|
2021-05-21 16:15:47 +03:00
|
|
|
filterConf := &filtering.Config{
|
2021-02-04 20:35:13 +03:00
|
|
|
SafeBrowsingEnabled: true,
|
|
|
|
SafeBrowsingCacheSize: 1000,
|
2023-03-15 14:31:07 +03:00
|
|
|
SafeSearchConf: filtering.SafeSearchConfig{Enabled: true},
|
2021-02-04 20:35:13 +03:00
|
|
|
SafeSearchCacheSize: 1000,
|
|
|
|
ParentalCacheSize: 1000,
|
|
|
|
CacheTime: 30,
|
|
|
|
}
|
|
|
|
forwardConf := ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
|
|
|
UpstreamDNS: []string{"8.8.8.8:53", "8.8.4.4:53"},
|
2021-02-04 20:35:13 +03:00
|
|
|
},
|
|
|
|
ConfigModified: func() {},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-02-04 20:35:13 +03:00
|
|
|
}
|
2021-04-09 21:01:21 +03:00
|
|
|
s := createTestServer(t, filterConf, forwardConf, nil)
|
2022-10-28 19:35:59 +03:00
|
|
|
s.conf.UpstreamConfig.Upstreams = []upstream.Upstream{newGoogleUpstream()}
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
2019-02-22 15:23:39 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Message over UDP.
|
2019-02-22 15:23:39 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
2021-06-29 15:53:28 +03:00
|
|
|
conn, err := dns.Dial("udp", addr.String())
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "cannot connect to the proxy: %s", err)
|
2019-02-22 15:23:39 +03:00
|
|
|
|
|
|
|
sendTestMessagesAsync(t, conn)
|
2021-02-02 15:13:12 +03:00
|
|
|
}
|
2019-02-22 15:23:39 +03:00
|
|
|
|
2019-02-25 14:58:54 +03:00
|
|
|
func TestSafeSearch(t *testing.T) {
|
2023-07-20 14:26:35 +03:00
|
|
|
resolver := &aghtest.Resolver{
|
|
|
|
OnLookupIP: func(_ context.Context, _, host string) (ips []net.IP, err error) {
|
|
|
|
ip4, ip6 := aghtest.HostToIPs(host)
|
|
|
|
|
2023-08-23 16:58:24 +03:00
|
|
|
return []net.IP{ip4.AsSlice(), ip6.AsSlice()}, nil
|
2023-07-20 14:26:35 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-15 14:31:07 +03:00
|
|
|
safeSearchConf := filtering.SafeSearchConfig{
|
|
|
|
Enabled: true,
|
|
|
|
Google: true,
|
|
|
|
Yandex: true,
|
|
|
|
CustomResolver: resolver,
|
|
|
|
}
|
|
|
|
|
2021-05-21 16:15:47 +03:00
|
|
|
filterConf := &filtering.Config{
|
2023-09-04 17:18:43 +03:00
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
2023-08-30 18:26:02 +03:00
|
|
|
ProtectionEnabled: true,
|
2023-03-15 14:31:07 +03:00
|
|
|
SafeSearchConf: safeSearchConf,
|
2021-02-04 20:35:13 +03:00
|
|
|
SafeSearchCacheSize: 1000,
|
|
|
|
CacheTime: 30,
|
2019-02-25 14:58:54 +03:00
|
|
|
}
|
2023-04-06 14:12:50 +03:00
|
|
|
safeSearch, err := safesearch.NewDefault(
|
2023-03-15 14:31:07 +03:00
|
|
|
safeSearchConf,
|
2023-04-06 14:12:50 +03:00
|
|
|
"",
|
2023-03-15 14:31:07 +03:00
|
|
|
filterConf.SafeSearchCacheSize,
|
|
|
|
time.Minute*time.Duration(filterConf.CacheTime),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
filterConf.SafeSearch = safeSearch
|
2021-02-04 20:35:13 +03:00
|
|
|
forwardConf := ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
2021-02-04 20:35:13 +03:00
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-02-04 20:35:13 +03:00
|
|
|
}
|
2021-04-09 21:01:21 +03:00
|
|
|
s := createTestServer(t, filterConf, forwardConf, nil)
|
2021-02-04 20:35:13 +03:00
|
|
|
startDeferStop(t, s)
|
2019-02-25 14:58:54 +03:00
|
|
|
|
2021-02-04 20:35:13 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP).String()
|
2021-06-29 15:53:28 +03:00
|
|
|
client := &dns.Client{}
|
2021-02-04 20:35:13 +03:00
|
|
|
|
2023-08-23 16:58:24 +03:00
|
|
|
yandexIP := netip.AddrFrom4([4]byte{213, 180, 193, 56})
|
2023-07-20 14:26:35 +03:00
|
|
|
googleIP, _ := aghtest.HostToIPs("forcesafesearch.google.com")
|
2019-02-25 14:58:54 +03:00
|
|
|
|
2021-02-04 20:35:13 +03:00
|
|
|
testCases := []struct {
|
2023-12-12 16:20:55 +03:00
|
|
|
host string
|
|
|
|
want netip.Addr
|
|
|
|
wantCNAME string
|
2021-02-04 20:35:13 +03:00
|
|
|
}{{
|
2023-12-12 16:20:55 +03:00
|
|
|
host: "yandex.com.",
|
|
|
|
want: yandexIP,
|
|
|
|
wantCNAME: "",
|
2021-02-04 20:35:13 +03:00
|
|
|
}, {
|
2023-12-12 16:20:55 +03:00
|
|
|
host: "yandex.by.",
|
|
|
|
want: yandexIP,
|
|
|
|
wantCNAME: "",
|
2021-02-04 20:35:13 +03:00
|
|
|
}, {
|
2023-12-12 16:20:55 +03:00
|
|
|
host: "yandex.kz.",
|
|
|
|
want: yandexIP,
|
|
|
|
wantCNAME: "",
|
2021-02-04 20:35:13 +03:00
|
|
|
}, {
|
2023-12-12 16:20:55 +03:00
|
|
|
host: "yandex.ru.",
|
|
|
|
want: yandexIP,
|
|
|
|
wantCNAME: "",
|
2021-02-04 20:35:13 +03:00
|
|
|
}, {
|
2023-12-12 16:20:55 +03:00
|
|
|
host: "www.google.com.",
|
|
|
|
want: googleIP,
|
|
|
|
wantCNAME: "forcesafesearch.google.com.",
|
2021-02-04 20:35:13 +03:00
|
|
|
}, {
|
2023-12-12 16:20:55 +03:00
|
|
|
host: "www.google.com.af.",
|
|
|
|
want: googleIP,
|
|
|
|
wantCNAME: "forcesafesearch.google.com.",
|
2021-02-04 20:35:13 +03:00
|
|
|
}, {
|
2023-12-12 16:20:55 +03:00
|
|
|
host: "www.google.be.",
|
|
|
|
want: googleIP,
|
|
|
|
wantCNAME: "forcesafesearch.google.com.",
|
2021-02-04 20:35:13 +03:00
|
|
|
}, {
|
2023-12-12 16:20:55 +03:00
|
|
|
host: "www.google.by.",
|
|
|
|
want: googleIP,
|
|
|
|
wantCNAME: "forcesafesearch.google.com.",
|
2021-02-04 20:35:13 +03:00
|
|
|
}}
|
2019-02-25 14:58:54 +03:00
|
|
|
|
2021-02-04 20:35:13 +03:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.host, func(t *testing.T) {
|
|
|
|
req := createTestMessage(tc.host)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2023-03-15 14:31:07 +03:00
|
|
|
var reply *dns.Msg
|
|
|
|
reply, _, err = client.Exchange(req, addr)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "couldn't talk to server %s: %s", addr, err)
|
2023-12-12 16:20:55 +03:00
|
|
|
|
|
|
|
if tc.wantCNAME != "" {
|
|
|
|
require.Len(t, reply.Answer, 2)
|
|
|
|
|
|
|
|
cname := testutil.RequireTypeAssert[*dns.CNAME](t, reply.Answer[0])
|
|
|
|
assert.Equal(t, tc.wantCNAME, cname.Target)
|
|
|
|
} else {
|
|
|
|
require.Len(t, reply.Answer, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
a := testutil.RequireTypeAssert[*dns.A](t, reply.Answer[len(reply.Answer)-1])
|
|
|
|
assert.Equal(t, net.IP(tc.want.AsSlice()), a.A)
|
2021-02-02 15:13:12 +03:00
|
|
|
})
|
2019-02-25 14:58:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-24 15:19:52 +03:00
|
|
|
func TestInvalidRequest(t *testing.T) {
|
2023-09-04 17:18:43 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-04-09 21:01:21 +03:00
|
|
|
}, nil)
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
2018-12-24 15:19:52 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP).String()
|
|
|
|
req := dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Id: dns.Id(),
|
|
|
|
RecursionDesired: true,
|
|
|
|
},
|
2018-12-24 15:19:52 +03:00
|
|
|
}
|
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Send a DNS request without question.
|
|
|
|
_, _, err := (&dns.Client{
|
2023-07-18 17:02:07 +03:00
|
|
|
Timeout: testTimeout,
|
2021-02-02 15:13:12 +03:00
|
|
|
}).Exchange(&req, addr)
|
|
|
|
|
2021-03-25 16:00:27 +03:00
|
|
|
assert.NoErrorf(t, err, "got a response to an invalid query")
|
2018-12-24 15:19:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockedRequest(t *testing.T) {
|
2021-02-04 20:35:13 +03:00
|
|
|
forwardConf := ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
2021-02-04 20:35:13 +03:00
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-02-04 20:35:13 +03:00
|
|
|
}
|
2023-08-30 18:26:02 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
ProtectionEnabled: true,
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, forwardConf, nil)
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
|
|
|
|
2019-02-22 15:23:39 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
2018-12-24 15:19:52 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Default blocking.
|
|
|
|
req := createTestMessage("nxdomain.example.org.")
|
2018-12-24 15:19:52 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
reply, err := dns.Exchange(req, addr.String())
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "couldn't talk to server %s: %s", addr, err)
|
|
|
|
|
2020-10-02 12:51:55 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, reply.Rcode)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
|
|
|
require.Len(t, reply.Answer, 1)
|
2021-02-02 15:13:12 +03:00
|
|
|
assert.True(t, reply.Answer[0].(*dns.A).A.IsUnspecified())
|
2018-12-24 15:19:52 +03:00
|
|
|
}
|
|
|
|
|
2020-05-14 12:57:41 +03:00
|
|
|
func TestServerCustomClientUpstream(t *testing.T) {
|
2023-11-17 15:51:51 +03:00
|
|
|
const defaultCacheSize = 1024 * 1024
|
|
|
|
|
|
|
|
var upsCalledCounter uint32
|
|
|
|
|
2021-02-04 20:35:13 +03:00
|
|
|
forwardConf := ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
CacheSize: defaultCacheSize,
|
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
2021-02-04 20:35:13 +03:00
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-02-04 20:35:13 +03:00
|
|
|
}
|
2023-09-04 17:18:43 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, forwardConf, nil)
|
2021-05-28 13:02:59 +03:00
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
ups := aghtest.NewUpstreamMock(func(req *dns.Msg) (resp *dns.Msg, err error) {
|
2023-11-17 15:51:51 +03:00
|
|
|
atomic.AddUint32(&upsCalledCounter, 1)
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
return aghalg.Coalesce(
|
|
|
|
aghtest.MatchedResponse(req, dns.TypeA, "host", "192.168.0.1"),
|
|
|
|
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
|
|
|
|
), nil
|
|
|
|
})
|
2023-11-17 15:51:51 +03:00
|
|
|
|
|
|
|
customUpsConf := proxy.NewCustomUpstreamConfig(
|
|
|
|
&proxy.UpstreamConfig{
|
|
|
|
Upstreams: []upstream.Upstream{ups},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
defaultCacheSize,
|
|
|
|
forwardConf.EDNSClientSubnet.Enabled,
|
|
|
|
)
|
|
|
|
|
2023-11-16 14:05:10 +03:00
|
|
|
s.conf.ClientsContainer = &aghtest.ClientsContainer{
|
|
|
|
OnUpstreamConfigByID: func(
|
|
|
|
_ string,
|
|
|
|
_ upstream.Resolver,
|
2023-11-17 15:51:51 +03:00
|
|
|
) (conf *proxy.CustomUpstreamConfig, err error) {
|
|
|
|
return customUpsConf, nil
|
2023-11-16 14:05:10 +03:00
|
|
|
},
|
2020-05-14 12:57:41 +03:00
|
|
|
}
|
2023-11-17 15:51:51 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
2020-08-13 11:49:42 +03:00
|
|
|
|
2023-11-17 15:51:51 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP).String()
|
2020-05-14 12:57:41 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Send test request.
|
|
|
|
req := createTestMessage("host.")
|
2020-05-14 12:57:41 +03:00
|
|
|
|
2023-11-17 15:51:51 +03:00
|
|
|
reply, err := dns.Exchange(req, addr)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.NotEmpty(t, reply.Answer)
|
|
|
|
require.Len(t, reply.Answer, 1)
|
2023-11-17 15:51:51 +03:00
|
|
|
|
|
|
|
assert.Equal(t, dns.RcodeSuccess, reply.Rcode)
|
2021-02-02 15:13:12 +03:00
|
|
|
assert.Equal(t, net.IP{192, 168, 0, 1}, reply.Answer[0].(*dns.A).A)
|
2023-11-17 15:51:51 +03:00
|
|
|
assert.Equal(t, uint32(1), atomic.LoadUint32(&upsCalledCounter))
|
|
|
|
|
|
|
|
_, err = dns.Exchange(req, addr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint32(1), atomic.LoadUint32(&upsCalledCounter))
|
2020-05-14 12:57:41 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:35:13 +03:00
|
|
|
// testCNAMEs is a map of names and CNAMEs necessary for the TestUpstream work.
|
2022-02-03 21:19:32 +03:00
|
|
|
var testCNAMEs = map[string][]string{
|
|
|
|
"badhost.": {"NULL.example.org."},
|
|
|
|
"whitelist.example.org.": {"NULL.example.org."},
|
2019-11-21 16:13:19 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:35:13 +03:00
|
|
|
// testIPv4 is a map of names and IPv4s necessary for the TestUpstream work.
|
2019-11-21 16:13:19 +03:00
|
|
|
var testIPv4 = map[string][]net.IP{
|
2021-07-12 13:10:47 +03:00
|
|
|
"NULL.example.org.": {{1, 2, 3, 4}},
|
2019-11-21 16:13:19 +03:00
|
|
|
"example.org.": {{127, 0, 0, 255}},
|
|
|
|
}
|
|
|
|
|
2020-01-09 19:52:06 +03:00
|
|
|
func TestBlockCNAMEProtectionEnabled(t *testing.T) {
|
2023-09-04 17:18:43 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-04-09 21:01:21 +03:00
|
|
|
}, nil)
|
2022-02-03 21:19:32 +03:00
|
|
|
testUpstm := &aghtest.Upstream{
|
2021-02-04 20:35:13 +03:00
|
|
|
CName: testCNAMEs,
|
|
|
|
IPv4: testIPv4,
|
2021-02-02 15:13:12 +03:00
|
|
|
}
|
2023-08-30 18:26:02 +03:00
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
s.dnsProxy.UpstreamConfig = &proxy.UpstreamConfig{
|
|
|
|
Upstreams: []upstream.Upstream{testUpstm},
|
|
|
|
}
|
|
|
|
startDeferStop(t, s)
|
|
|
|
|
2020-01-09 19:52:06 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
|
|
|
|
2021-07-12 13:10:47 +03:00
|
|
|
// 'badhost' has a canonical name 'NULL.example.org' which should be
|
2021-03-11 17:32:58 +03:00
|
|
|
// blocked by filters, but protection is disabled so it is not.
|
2020-01-09 19:52:06 +03:00
|
|
|
req := createTestMessage("badhost.")
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2020-01-09 19:52:06 +03:00
|
|
|
reply, err := dns.Exchange(req, addr.String())
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-13 20:31:43 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, reply.Rcode)
|
2020-01-09 19:52:06 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 16:13:19 +03:00
|
|
|
func TestBlockCNAME(t *testing.T) {
|
2021-02-04 20:35:13 +03:00
|
|
|
forwardConf := ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
2021-02-04 20:35:13 +03:00
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-02-04 20:35:13 +03:00
|
|
|
}
|
2023-08-30 18:26:02 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
ProtectionEnabled: true,
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, forwardConf, nil)
|
2021-02-02 15:13:12 +03:00
|
|
|
s.conf.UpstreamConfig.Upstreams = []upstream.Upstream{
|
2022-02-03 21:19:32 +03:00
|
|
|
&aghtest.Upstream{
|
2021-02-04 20:35:13 +03:00
|
|
|
CName: testCNAMEs,
|
|
|
|
IPv4: testIPv4,
|
2021-02-02 15:13:12 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
startDeferStop(t, s)
|
|
|
|
|
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP).String()
|
|
|
|
|
|
|
|
testCases := []struct {
|
2022-02-03 21:19:32 +03:00
|
|
|
name string
|
2021-02-02 15:13:12 +03:00
|
|
|
host string
|
|
|
|
want bool
|
|
|
|
}{{
|
2022-02-03 21:19:32 +03:00
|
|
|
name: "block_request",
|
2021-02-02 15:13:12 +03:00
|
|
|
host: "badhost.",
|
2021-07-12 13:10:47 +03:00
|
|
|
// 'badhost' has a canonical name 'NULL.example.org' which is
|
2021-02-02 15:13:12 +03:00
|
|
|
// blocked by filters: response is blocked.
|
|
|
|
want: true,
|
|
|
|
}, {
|
2022-02-03 21:19:32 +03:00
|
|
|
name: "allowed",
|
2021-02-02 15:13:12 +03:00
|
|
|
host: "whitelist.example.org.",
|
|
|
|
// 'whitelist.example.org' has a canonical name
|
2021-07-12 13:10:47 +03:00
|
|
|
// 'NULL.example.org' which is blocked by filters
|
2021-02-02 15:13:12 +03:00
|
|
|
// but 'whitelist.example.org' is in a whitelist:
|
|
|
|
// response isn't blocked.
|
|
|
|
want: false,
|
|
|
|
}, {
|
2022-02-03 21:19:32 +03:00
|
|
|
name: "block_response",
|
2021-02-02 15:13:12 +03:00
|
|
|
host: "example.org.",
|
|
|
|
// 'example.org' has a canonical name 'cname1' with IP
|
|
|
|
// 127.0.0.255 which is blocked by filters: response is blocked.
|
|
|
|
want: true,
|
|
|
|
}}
|
2019-11-21 16:13:19 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
for _, tc := range testCases {
|
2022-02-03 21:19:32 +03:00
|
|
|
req := createTestMessage(tc.host)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2022-02-03 21:19:32 +03:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-02-02 15:13:12 +03:00
|
|
|
reply, err := dns.Exchange(req, addr)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, reply.Rcode)
|
|
|
|
if tc.want {
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Len(t, reply.Answer, 1)
|
2021-07-12 13:10:47 +03:00
|
|
|
|
|
|
|
ans := reply.Answer[0]
|
|
|
|
a, ok := ans.(*dns.A)
|
2022-02-03 21:19:32 +03:00
|
|
|
require.True(t, ok)
|
2021-07-12 13:10:47 +03:00
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
assert.True(t, a.A.IsUnspecified())
|
2021-02-02 15:13:12 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-11-21 16:13:19 +03:00
|
|
|
}
|
|
|
|
|
2019-12-23 13:36:59 +03:00
|
|
|
func TestClientRulesForCNAMEMatching(t *testing.T) {
|
2021-02-04 20:35:13 +03:00
|
|
|
forwardConf := ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-08-23 16:58:24 +03:00
|
|
|
FilterHandler: func(_ netip.Addr, _ string, settings *filtering.Settings) {
|
2021-02-04 20:35:13 +03:00
|
|
|
settings.FilteringEnabled = false
|
|
|
|
},
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
2021-02-04 20:35:13 +03:00
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2019-12-23 13:36:59 +03:00
|
|
|
}
|
2023-09-04 17:18:43 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, forwardConf, nil)
|
2021-02-02 15:13:12 +03:00
|
|
|
s.conf.UpstreamConfig.Upstreams = []upstream.Upstream{
|
2022-02-03 21:19:32 +03:00
|
|
|
&aghtest.Upstream{
|
2021-02-04 20:35:13 +03:00
|
|
|
CName: testCNAMEs,
|
|
|
|
IPv4: testIPv4,
|
2021-02-02 15:13:12 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
startDeferStop(t, s)
|
|
|
|
|
2019-12-23 13:36:59 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
|
|
|
|
2021-07-12 13:10:47 +03:00
|
|
|
// 'badhost' has a canonical name 'NULL.example.org' which is blocked by
|
2021-02-02 15:13:12 +03:00
|
|
|
// filters: response is blocked.
|
|
|
|
req := dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Id: dns.Id(),
|
|
|
|
},
|
|
|
|
Question: []dns.Question{{
|
|
|
|
Name: "badhost.",
|
|
|
|
Qtype: dns.TypeA,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
}},
|
2019-12-23 13:36:59 +03:00
|
|
|
}
|
2021-02-02 15:13:12 +03:00
|
|
|
|
|
|
|
// However, in our case it should not be blocked as filtering is
|
|
|
|
// disabled on the client level.
|
2019-12-23 13:36:59 +03:00
|
|
|
reply, err := dns.Exchange(&req, addr.String())
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-12-23 13:36:59 +03:00
|
|
|
assert.Equal(t, dns.RcodeSuccess, reply.Rcode)
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:53:31 +03:00
|
|
|
func TestNullBlockedRequest(t *testing.T) {
|
2021-02-04 20:35:13 +03:00
|
|
|
forwardConf := ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
2021-02-04 20:35:13 +03:00
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-02-04 20:35:13 +03:00
|
|
|
}
|
2023-08-30 18:26:02 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
ProtectionEnabled: true,
|
|
|
|
BlockingMode: filtering.BlockingModeNullIP,
|
|
|
|
}, forwardConf, nil)
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
2019-05-14 15:53:31 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Nil filter blocking.
|
|
|
|
req := dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Id: dns.Id(),
|
|
|
|
RecursionDesired: true,
|
|
|
|
},
|
|
|
|
Question: []dns.Question{{
|
2021-07-12 13:10:47 +03:00
|
|
|
Name: "NULL.example.org.",
|
2021-02-02 15:13:12 +03:00
|
|
|
Qtype: dns.TypeA,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
}},
|
2019-05-14 15:53:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
reply, err := dns.Exchange(&req, addr.String())
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "couldn't talk to server %s: %s", addr, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Lenf(t, reply.Answer, 1, "dns server %s returned reply with wrong number of answers - %d", addr, len(reply.Answer))
|
2021-02-02 15:13:12 +03:00
|
|
|
a, ok := reply.Answer[0].(*dns.A)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Truef(t, ok, "dns server %s returned wrong answer type instead of A: %v", addr, reply.Answer[0])
|
2021-02-02 15:13:12 +03:00
|
|
|
assert.Truef(t, a.A.IsUnspecified(), "dns server %s returned wrong answer instead of 0.0.0.0: %v", addr, a.A)
|
2019-05-14 15:53:31 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 17:04:30 +03:00
|
|
|
func TestBlockedCustomIP(t *testing.T) {
|
2021-07-12 13:10:47 +03:00
|
|
|
rules := "||nxdomain.example.org^\n||NULL.example.org^\n127.0.0.1 host.example.org\n@@||whitelist.example.org^\n||127.0.0.255\n"
|
2021-05-21 16:15:47 +03:00
|
|
|
filters := []filtering.Filter{{
|
2021-02-02 15:13:12 +03:00
|
|
|
ID: 0,
|
|
|
|
Data: []byte(rules),
|
2020-02-26 19:58:25 +03:00
|
|
|
}}
|
2019-12-16 17:04:30 +03:00
|
|
|
|
2023-08-30 18:26:02 +03:00
|
|
|
f, err := filtering.New(&filtering.Config{
|
|
|
|
ProtectionEnabled: true,
|
|
|
|
BlockingMode: filtering.BlockingModeCustomIP,
|
|
|
|
BlockingIPv4: netip.Addr{},
|
|
|
|
BlockingIPv6: netip.Addr{},
|
|
|
|
}, filters)
|
2022-09-23 13:23:35 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
dhcp := &testDHCP{
|
|
|
|
OnEnabled: func() (ok bool) { return false },
|
|
|
|
OnHostByIP: func(_ netip.Addr) (host string) { panic("not implemented") },
|
|
|
|
OnIPByHost: func(_ string) (ip netip.Addr) { panic("not implemented") },
|
|
|
|
}
|
2022-03-18 13:37:27 +03:00
|
|
|
s, err := NewServer(DNSCreateParams{
|
2023-08-30 14:02:12 +03:00
|
|
|
DHCPServer: dhcp,
|
2022-03-18 13:37:27 +03:00
|
|
|
DNSFilter: f,
|
|
|
|
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
|
2021-02-02 15:13:12 +03:00
|
|
|
})
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
conf := &ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamDNS: []string{"8.8.8.8:53", "8.8.4.4:53"},
|
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
2021-02-02 15:13:12 +03:00
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-02-02 15:13:12 +03:00
|
|
|
}
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Invalid BlockingIPv4.
|
2021-03-25 16:00:27 +03:00
|
|
|
err = s.Prepare(conf)
|
|
|
|
assert.Error(t, err)
|
2019-12-16 17:04:30 +03:00
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
s.dnsFilter.SetBlockingMode(
|
|
|
|
filtering.BlockingModeCustomIP,
|
|
|
|
netip.AddrFrom4([4]byte{0, 0, 0, 1}),
|
|
|
|
netip.MustParseAddr("::1"))
|
2021-03-25 16:00:27 +03:00
|
|
|
|
|
|
|
err = s.Prepare(conf)
|
|
|
|
require.NoError(t, err)
|
2021-02-02 15:13:12 +03:00
|
|
|
|
2021-05-24 14:48:42 +03:00
|
|
|
f.SetEnabled(true)
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
2019-12-16 17:04:30 +03:00
|
|
|
|
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
|
|
|
|
2021-07-12 13:10:47 +03:00
|
|
|
req := createTestMessageWithType("NULL.example.org.", dns.TypeA)
|
2019-12-16 17:04:30 +03:00
|
|
|
reply, err := dns.Exchange(req, addr.String())
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Len(t, reply.Answer, 1)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2019-12-16 17:04:30 +03:00
|
|
|
a, ok := reply.Answer[0].(*dns.A)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.True(t, ok)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-01-20 17:27:53 +03:00
|
|
|
assert.True(t, net.IP{0, 0, 0, 1}.Equal(a.A))
|
2019-12-16 17:04:30 +03:00
|
|
|
|
2021-07-12 13:10:47 +03:00
|
|
|
req = createTestMessageWithType("NULL.example.org.", dns.TypeAAAA)
|
2019-12-16 17:04:30 +03:00
|
|
|
reply, err = dns.Exchange(req, addr.String())
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Len(t, reply.Answer, 1)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2019-12-16 17:04:30 +03:00
|
|
|
a6, ok := reply.Answer[0].(*dns.AAAA)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.True(t, ok)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2020-05-13 20:31:43 +03:00
|
|
|
assert.Equal(t, "::1", a6.AAAA.String())
|
2019-12-16 17:04:30 +03:00
|
|
|
}
|
|
|
|
|
2018-12-24 15:19:52 +03:00
|
|
|
func TestBlockedByHosts(t *testing.T) {
|
2021-02-04 20:35:13 +03:00
|
|
|
forwardConf := ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
2021-02-04 20:35:13 +03:00
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-02-04 20:35:13 +03:00
|
|
|
}
|
2021-05-24 14:48:42 +03:00
|
|
|
|
2023-08-30 18:26:02 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
ProtectionEnabled: true,
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, forwardConf, nil)
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
2019-02-22 15:23:39 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
2018-12-24 15:19:52 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// Hosts blocking.
|
|
|
|
req := createTestMessage("host.example.org.")
|
2018-12-24 15:19:52 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
reply, err := dns.Exchange(req, addr.String())
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "couldn't talk to server %s: %s", addr, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Lenf(t, reply.Answer, 1, "dns server %s returned reply with wrong number of answers - %d", addr, len(reply.Answer))
|
2021-02-02 15:13:12 +03:00
|
|
|
a, ok := reply.Answer[0].(*dns.A)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Truef(t, ok, "dns server %s returned wrong answer type instead of A: %v", addr, reply.Answer[0])
|
2021-02-02 15:13:12 +03:00
|
|
|
assert.Equalf(t, net.IP{127, 0, 0, 1}, a.A, "dns server %s returned wrong answer instead of 8.8.8.8: %v", addr, a.A)
|
2018-12-24 15:19:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockedBySafeBrowsing(t *testing.T) {
|
2023-04-27 16:39:35 +03:00
|
|
|
const (
|
|
|
|
hostname = "wmconvirus.narod.ru"
|
|
|
|
cacheTime = 10 * time.Minute
|
|
|
|
cacheSize = 10000
|
|
|
|
)
|
|
|
|
|
|
|
|
sbChecker := hashprefix.New(&hashprefix.Config{
|
|
|
|
CacheTime: cacheTime,
|
|
|
|
CacheSize: cacheSize,
|
|
|
|
Upstream: aghtest.NewBlockUpstream(hostname, true),
|
|
|
|
})
|
2021-02-02 15:13:12 +03:00
|
|
|
|
2023-07-20 14:26:35 +03:00
|
|
|
ans4, _ := aghtest.HostToIPs(hostname)
|
2021-02-04 20:35:13 +03:00
|
|
|
|
2021-05-21 16:15:47 +03:00
|
|
|
filterConf := &filtering.Config{
|
2023-09-04 17:18:43 +03:00
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
2023-08-30 18:26:02 +03:00
|
|
|
ProtectionEnabled: true,
|
|
|
|
SafeBrowsingEnabled: true,
|
|
|
|
SafeBrowsingChecker: sbChecker,
|
|
|
|
SafeBrowsingBlockHost: ans4.String(),
|
2021-02-04 20:35:13 +03:00
|
|
|
}
|
|
|
|
forwardConf := ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
2021-02-02 15:13:12 +03:00
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2018-12-24 15:19:52 +03:00
|
|
|
}
|
2021-04-09 21:01:21 +03:00
|
|
|
s := createTestServer(t, filterConf, forwardConf, nil)
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
2019-02-22 15:23:39 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
2018-12-24 15:19:52 +03:00
|
|
|
|
2021-02-02 15:13:12 +03:00
|
|
|
// SafeBrowsing blocking.
|
2021-02-04 20:35:13 +03:00
|
|
|
req := createTestMessage(hostname + ".")
|
2021-02-02 15:13:12 +03:00
|
|
|
|
|
|
|
reply, err := dns.Exchange(req, addr.String())
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoErrorf(t, err, "couldn't talk to server %s: %s", addr, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Lenf(t, reply.Answer, 1, "dns server %s returned reply with wrong number of answers - %d", addr, len(reply.Answer))
|
2018-12-24 15:19:52 +03:00
|
|
|
|
2023-08-23 16:58:24 +03:00
|
|
|
assertResponse(t, reply, ans4)
|
2018-12-24 15:19:52 +03:00
|
|
|
}
|
|
|
|
|
2020-06-01 15:23:08 +03:00
|
|
|
func TestRewrite(t *testing.T) {
|
2021-05-21 16:15:47 +03:00
|
|
|
c := &filtering.Config{
|
2023-08-30 18:26:02 +03:00
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
2021-12-27 19:40:39 +03:00
|
|
|
Rewrites: []*filtering.LegacyRewrite{{
|
2020-06-01 15:23:08 +03:00
|
|
|
Domain: "test.com",
|
|
|
|
Answer: "1.2.3.4",
|
|
|
|
Type: dns.TypeA,
|
2021-02-02 15:13:12 +03:00
|
|
|
}, {
|
2020-06-01 15:23:08 +03:00
|
|
|
Domain: "alias.test.com",
|
|
|
|
Answer: "test.com",
|
|
|
|
Type: dns.TypeCNAME,
|
2021-02-02 15:13:12 +03:00
|
|
|
}, {
|
2020-06-01 15:23:08 +03:00
|
|
|
Domain: "my.alias.example.org",
|
|
|
|
Answer: "example.org",
|
|
|
|
Type: dns.TypeCNAME,
|
2021-02-02 15:13:12 +03:00
|
|
|
}},
|
2020-06-01 15:23:08 +03:00
|
|
|
}
|
2022-09-23 13:23:35 +03:00
|
|
|
f, err := filtering.New(c, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
f.SetEnabled(true)
|
2020-06-01 15:23:08 +03:00
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
dhcp := &testDHCP{
|
|
|
|
OnEnabled: func() (ok bool) { return false },
|
|
|
|
OnHostByIP: func(ip netip.Addr) (host string) { panic("not implemented") },
|
|
|
|
OnIPByHost: func(host string) (ip netip.Addr) { panic("not implemented") },
|
|
|
|
}
|
2022-03-18 13:37:27 +03:00
|
|
|
s, err := NewServer(DNSCreateParams{
|
2023-08-30 14:02:12 +03:00
|
|
|
DHCPServer: dhcp,
|
2022-03-18 13:37:27 +03:00
|
|
|
DNSFilter: f,
|
|
|
|
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
|
2021-03-31 15:00:47 +03:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NoError(t, s.Prepare(&ServerConfig{
|
2021-03-23 12:32:07 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamDNS: []string{"8.8.8.8:53"},
|
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
2021-02-02 15:13:12 +03:00
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2021-03-31 15:00:47 +03:00
|
|
|
}))
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2022-10-28 19:35:59 +03:00
|
|
|
ups := aghtest.NewUpstreamMock(func(req *dns.Msg) (resp *dns.Msg, err error) {
|
|
|
|
return aghalg.Coalesce(
|
|
|
|
aghtest.MatchedResponse(req, dns.TypeA, "example.org", "4.3.2.1"),
|
|
|
|
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
|
|
|
|
), nil
|
|
|
|
})
|
|
|
|
s.conf.UpstreamConfig.Upstreams = []upstream.Upstream{ups}
|
2021-02-02 15:13:12 +03:00
|
|
|
startDeferStop(t, s)
|
|
|
|
|
2020-06-01 15:23:08 +03:00
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
subTestFunc := func(t *testing.T) {
|
|
|
|
req := createTestMessageWithType("test.com.", dns.TypeA)
|
|
|
|
reply, eerr := dns.Exchange(req, addr.String())
|
|
|
|
require.NoError(t, eerr)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
require.Len(t, reply.Answer, 1)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
a, ok := reply.Answer[0].(*dns.A)
|
|
|
|
require.True(t, ok)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
assert.True(t, net.IP{1, 2, 3, 4}.Equal(a.A))
|
2020-06-01 15:23:08 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
req = createTestMessageWithType("test.com.", dns.TypeAAAA)
|
|
|
|
reply, eerr = dns.Exchange(req, addr.String())
|
|
|
|
require.NoError(t, eerr)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
assert.Empty(t, reply.Answer)
|
2020-07-24 13:27:14 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
req = createTestMessageWithType("alias.test.com.", dns.TypeA)
|
|
|
|
reply, eerr = dns.Exchange(req, addr.String())
|
|
|
|
require.NoError(t, eerr)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
require.Len(t, reply.Answer, 2)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
assert.Equal(t, "test.com.", reply.Answer[0].(*dns.CNAME).Target)
|
|
|
|
assert.True(t, net.IP{1, 2, 3, 4}.Equal(reply.Answer[1].(*dns.A).A))
|
2020-06-01 15:23:08 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
req = createTestMessageWithType("my.alias.example.org.", dns.TypeA)
|
|
|
|
reply, eerr = dns.Exchange(req, addr.String())
|
|
|
|
require.NoError(t, eerr)
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
// The original question is restored.
|
|
|
|
require.Len(t, reply.Question, 1)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
assert.Equal(t, "my.alias.example.org.", reply.Question[0].Name)
|
|
|
|
|
|
|
|
require.Len(t, reply.Answer, 2)
|
|
|
|
|
|
|
|
assert.Equal(t, "example.org.", reply.Answer[0].(*dns.CNAME).Target)
|
|
|
|
assert.Equal(t, dns.TypeA, reply.Answer[1].Header().Rrtype)
|
|
|
|
}
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
for _, protect := range []bool{true, false} {
|
|
|
|
val := protect
|
|
|
|
conf := s.getDNSConfig()
|
|
|
|
conf.ProtectionEnabled = &val
|
|
|
|
s.setConfig(conf)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
t.Run(fmt.Sprintf("protection_is_%t", val), subTestFunc)
|
|
|
|
}
|
2020-06-01 15:23:08 +03:00
|
|
|
}
|
|
|
|
|
2022-08-03 14:36:18 +03:00
|
|
|
func publicKey(priv any) any {
|
2019-02-22 15:23:39 +03:00
|
|
|
switch k := priv.(type) {
|
|
|
|
case *rsa.PrivateKey:
|
|
|
|
return &k.PublicKey
|
2021-02-04 20:35:13 +03:00
|
|
|
|
2019-02-22 15:23:39 +03:00
|
|
|
case *ecdsa.PrivateKey:
|
|
|
|
return &k.PublicKey
|
2021-02-04 20:35:13 +03:00
|
|
|
|
2019-02-22 15:23:39 +03:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-05-24 14:49:26 +03:00
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
// testDHCP is a mock implementation of the [DHCP] interface.
|
|
|
|
type testDHCP struct {
|
|
|
|
OnHostByIP func(ip netip.Addr) (host string)
|
|
|
|
OnIPByHost func(host string) (ip netip.Addr)
|
|
|
|
OnEnabled func() (ok bool)
|
2020-07-03 18:20:01 +03:00
|
|
|
}
|
2021-06-16 16:48:46 +03:00
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
// type check
|
|
|
|
var _ DHCP = (*testDHCP)(nil)
|
|
|
|
|
|
|
|
// HostByIP implements the [DHCP] interface for *testDHCP.
|
|
|
|
func (d *testDHCP) HostByIP(ip netip.Addr) (host string) { return d.OnHostByIP(ip) }
|
|
|
|
|
|
|
|
// IPByHost implements the [DHCP] interface for *testDHCP.
|
|
|
|
func (d *testDHCP) IPByHost(host string) (ip netip.Addr) { return d.OnIPByHost(host) }
|
|
|
|
|
|
|
|
// IsClientHost implements the [DHCP] interface for *testDHCP.
|
|
|
|
func (d *testDHCP) Enabled() (ok bool) { return d.OnEnabled() }
|
|
|
|
|
2020-09-11 11:53:36 +03:00
|
|
|
func TestPTRResponseFromDHCPLeases(t *testing.T) {
|
2022-06-28 19:09:26 +03:00
|
|
|
const localDomain = "lan"
|
|
|
|
|
2023-08-30 18:26:02 +03:00
|
|
|
flt, err := filtering.New(&filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, nil)
|
2022-09-23 13:23:35 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-18 13:37:27 +03:00
|
|
|
s, err := NewServer(DNSCreateParams{
|
2023-08-30 14:02:12 +03:00
|
|
|
DNSFilter: flt,
|
|
|
|
DHCPServer: &testDHCP{
|
|
|
|
OnEnabled: func() (ok bool) { return true },
|
|
|
|
OnIPByHost: func(host string) (ip netip.Addr) { panic("not implemented") },
|
|
|
|
OnHostByIP: func(ip netip.Addr) (host string) {
|
|
|
|
return "myhost"
|
|
|
|
},
|
|
|
|
},
|
2022-03-18 13:37:27 +03:00
|
|
|
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
|
2022-06-28 19:09:26 +03:00
|
|
|
LocalDomain: localDomain,
|
2021-02-02 15:13:12 +03:00
|
|
|
})
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
2021-02-02 15:13:12 +03:00
|
|
|
|
2021-03-23 12:32:07 +03:00
|
|
|
s.conf.UDPListenAddrs = []*net.UDPAddr{{}}
|
|
|
|
s.conf.TCPListenAddrs = []*net.TCPAddr{{}}
|
2020-06-23 12:13:13 +03:00
|
|
|
s.conf.UpstreamDNS = []string{"127.0.0.1:53"}
|
2023-08-30 18:26:02 +03:00
|
|
|
s.conf.Config.EDNSClientSubnet = &EDNSClientSubnet{Enabled: false}
|
2023-12-25 14:16:48 +03:00
|
|
|
s.conf.Config.UpstreamMode = UpstreamModeLoadBalance
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2022-09-06 17:09:54 +03:00
|
|
|
err = s.Prepare(&s.conf)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = s.Start()
|
|
|
|
require.NoError(t, err)
|
2021-10-22 11:58:18 +03:00
|
|
|
t.Cleanup(s.Close)
|
2020-06-23 12:13:13 +03:00
|
|
|
|
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
2021-04-29 16:00:07 +03:00
|
|
|
req := createTestMessageWithType("34.12.168.192.in-addr.arpa.", dns.TypePTR)
|
2020-06-23 12:13:13 +03:00
|
|
|
|
|
|
|
resp, err := dns.Exchange(req, addr.String())
|
2022-06-28 19:09:26 +03:00
|
|
|
require.NoErrorf(t, err, "%s", addr)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-03-11 17:32:58 +03:00
|
|
|
require.Len(t, resp.Answer, 1)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2022-10-26 21:12:51 +03:00
|
|
|
ans := resp.Answer[0]
|
|
|
|
assert.Equal(t, dns.TypePTR, ans.Header().Rrtype)
|
|
|
|
assert.Equal(t, "34.12.168.192.in-addr.arpa.", ans.Header().Name)
|
|
|
|
|
|
|
|
ptr := testutil.RequireTypeAssert[*dns.PTR](t, ans)
|
2021-02-02 15:13:12 +03:00
|
|
|
|
2022-06-28 19:09:26 +03:00
|
|
|
assert.Equal(t, dns.Fqdn("myhost."+localDomain), ptr.Ptr)
|
2020-06-23 12:13:13 +03:00
|
|
|
}
|
2020-09-11 11:53:36 +03:00
|
|
|
|
|
|
|
func TestPTRResponseFromHosts(t *testing.T) {
|
2021-10-14 19:39:21 +03:00
|
|
|
// Prepare test hosts file.
|
|
|
|
|
|
|
|
const hostsFilename = "hosts"
|
|
|
|
|
|
|
|
testFS := fstest.MapFS{
|
|
|
|
hostsFilename: &fstest.MapFile{Data: []byte(`
|
|
|
|
127.0.0.1 host # comment
|
|
|
|
::1 localhost#comment
|
|
|
|
`)},
|
2020-09-11 11:53:36 +03:00
|
|
|
}
|
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
dhcp := &testDHCP{
|
|
|
|
OnEnabled: func() (ok bool) { return false },
|
|
|
|
OnIPByHost: func(host string) (ip netip.Addr) { panic("not implemented") },
|
|
|
|
OnHostByIP: func(ip netip.Addr) (host string) { return "" },
|
|
|
|
}
|
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
var eventsCalledCounter uint32
|
2023-08-22 16:45:11 +03:00
|
|
|
hc, err := aghnet.NewHostsContainer(testFS, &aghtest.FSWatcher{
|
2024-02-13 13:19:22 +03:00
|
|
|
OnStart: func() (_ error) { panic("not implemented") },
|
2021-10-14 19:39:21 +03:00
|
|
|
OnEvents: func() (e <-chan struct{}) {
|
|
|
|
assert.Equal(t, uint32(1), atomic.AddUint32(&eventsCalledCounter, 1))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
OnAdd: func(name string) (err error) {
|
|
|
|
assert.Equal(t, hostsFilename, name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2021-11-17 17:21:10 +03:00
|
|
|
OnClose: func() (err error) { panic("not implemented") },
|
2021-10-14 19:39:21 +03:00
|
|
|
}, hostsFilename)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
2021-03-11 17:32:58 +03:00
|
|
|
t.Cleanup(func() {
|
2021-10-14 19:39:21 +03:00
|
|
|
assert.Equal(t, uint32(1), atomic.LoadUint32(&eventsCalledCounter))
|
|
|
|
})
|
2020-09-11 11:53:36 +03:00
|
|
|
|
2022-09-23 13:23:35 +03:00
|
|
|
flt, err := filtering.New(&filtering.Config{
|
2023-08-30 18:26:02 +03:00
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
EtcHosts: hc,
|
2021-10-20 19:52:13 +03:00
|
|
|
}, nil)
|
2022-09-23 13:23:35 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
flt.SetEnabled(true)
|
2020-09-11 11:53:36 +03:00
|
|
|
|
2021-03-31 15:00:47 +03:00
|
|
|
var s *Server
|
|
|
|
s, err = NewServer(DNSCreateParams{
|
2023-08-30 14:02:12 +03:00
|
|
|
DHCPServer: dhcp,
|
2022-03-18 13:37:27 +03:00
|
|
|
DNSFilter: flt,
|
|
|
|
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
|
2021-03-31 15:00:47 +03:00
|
|
|
})
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-23 12:32:07 +03:00
|
|
|
s.conf.UDPListenAddrs = []*net.UDPAddr{{}}
|
|
|
|
s.conf.TCPListenAddrs = []*net.TCPAddr{{}}
|
2020-09-11 11:53:36 +03:00
|
|
|
s.conf.UpstreamDNS = []string{"127.0.0.1:53"}
|
2023-08-30 18:26:02 +03:00
|
|
|
s.conf.Config.EDNSClientSubnet = &EDNSClientSubnet{Enabled: false}
|
2023-12-25 14:16:48 +03:00
|
|
|
s.conf.Config.UpstreamMode = UpstreamModeLoadBalance
|
2021-02-02 15:13:12 +03:00
|
|
|
|
2022-09-06 17:09:54 +03:00
|
|
|
err = s.Prepare(&s.conf)
|
2021-03-25 16:00:27 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = s.Start()
|
|
|
|
require.NoError(t, err)
|
2021-10-22 11:58:18 +03:00
|
|
|
t.Cleanup(s.Close)
|
2020-09-11 11:53:36 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
subTestFunc := func(t *testing.T) {
|
|
|
|
addr := s.dnsProxy.Addr(proxy.ProtoUDP)
|
|
|
|
req := createTestMessageWithType("1.0.0.127.in-addr.arpa.", dns.TypePTR)
|
2020-09-11 11:53:36 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
resp, eerr := dns.Exchange(req, addr.String())
|
|
|
|
require.NoError(t, eerr)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
require.Len(t, resp.Answer, 1)
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
assert.Equal(t, dns.TypePTR, resp.Answer[0].Header().Rrtype)
|
|
|
|
assert.Equal(t, "1.0.0.127.in-addr.arpa.", resp.Answer[0].Header().Name)
|
2021-02-02 15:13:12 +03:00
|
|
|
|
2021-10-20 19:52:13 +03:00
|
|
|
ptr, ok := resp.Answer[0].(*dns.PTR)
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, "host.", ptr.Ptr)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, protect := range []bool{true, false} {
|
|
|
|
val := protect
|
|
|
|
conf := s.getDNSConfig()
|
|
|
|
conf.ProtectionEnabled = &val
|
|
|
|
s.setConfig(conf)
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("protection_is_%t", val), subTestFunc)
|
|
|
|
}
|
2020-09-11 11:53:36 +03:00
|
|
|
}
|
2021-03-25 16:00:27 +03:00
|
|
|
|
|
|
|
func TestNewServer(t *testing.T) {
|
2021-08-09 16:03:37 +03:00
|
|
|
// TODO(a.garipov): Consider moving away from the text-based error
|
|
|
|
// checks and onto a more structured approach.
|
2021-03-25 16:00:27 +03:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
in DNSCreateParams
|
|
|
|
wantErrMsg string
|
|
|
|
}{{
|
|
|
|
name: "success",
|
|
|
|
in: DNSCreateParams{},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
2021-04-15 19:00:31 +03:00
|
|
|
name: "success_local_tld",
|
2021-03-25 16:00:27 +03:00
|
|
|
in: DNSCreateParams{
|
2021-04-15 19:00:31 +03:00
|
|
|
LocalDomain: "mynet",
|
2021-03-25 16:00:27 +03:00
|
|
|
},
|
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
2021-04-15 19:00:31 +03:00
|
|
|
name: "success_local_domain",
|
2021-03-25 16:00:27 +03:00
|
|
|
in: DNSCreateParams{
|
2021-04-15 19:00:31 +03:00
|
|
|
LocalDomain: "my.local.net",
|
2021-03-25 16:00:27 +03:00
|
|
|
},
|
2021-04-15 19:00:31 +03:00
|
|
|
wantErrMsg: "",
|
|
|
|
}, {
|
|
|
|
name: "bad_local_domain",
|
|
|
|
in: DNSCreateParams{
|
|
|
|
LocalDomain: "!!!",
|
|
|
|
},
|
2021-08-09 16:03:37 +03:00
|
|
|
wantErrMsg: `local domain: bad domain name "!!!": ` +
|
2023-02-21 16:52:33 +03:00
|
|
|
`bad top-level domain name label "!!!": ` +
|
|
|
|
`bad top-level domain name label rune '!'`,
|
2021-03-25 16:00:27 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
_, err := NewServer(tc.in)
|
2021-10-22 11:58:18 +03:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
2021-03-25 16:00:27 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2023-07-25 14:16:26 +03:00
|
|
|
// doubleTTL is a helper function that returns a clone of DNS PTR with appended
|
|
|
|
// copy of first answer record with doubled TTL.
|
|
|
|
func doubleTTL(msg *dns.Msg) (resp *dns.Msg) {
|
|
|
|
if msg == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(msg.Answer) == 0 {
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := msg.Answer[0]
|
|
|
|
ptr, ok := rec.(*dns.PTR)
|
|
|
|
if !ok {
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
clone := *ptr
|
|
|
|
clone.Hdr.Ttl *= 2
|
|
|
|
msg.Answer = append(msg.Answer, &clone)
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2021-04-07 20:16:06 +03:00
|
|
|
func TestServer_Exchange(t *testing.T) {
|
2022-08-16 13:21:25 +03:00
|
|
|
const (
|
|
|
|
onesHost = "one.one.one.one"
|
2023-07-25 14:16:26 +03:00
|
|
|
twosHost = "two.two.two.two"
|
2022-08-16 13:21:25 +03:00
|
|
|
localDomainHost = "local.domain"
|
2023-07-25 14:16:26 +03:00
|
|
|
|
|
|
|
defaultTTL = time.Second * 60
|
2022-08-16 13:21:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-07-06 17:10:06 +03:00
|
|
|
onesIP = netip.MustParseAddr("1.1.1.1")
|
2023-07-25 14:16:26 +03:00
|
|
|
twosIP = netip.MustParseAddr("2.2.2.2")
|
2023-07-06 17:10:06 +03:00
|
|
|
localIP = netip.MustParseAddr("192.168.1.1")
|
2022-08-16 13:21:25 +03:00
|
|
|
)
|
|
|
|
|
2023-07-25 14:16:26 +03:00
|
|
|
onesRevExtIPv4, err := netutil.IPToReversedAddr(onesIP.AsSlice())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
twosRevExtIPv4, err := netutil.IPToReversedAddr(twosIP.AsSlice())
|
2022-08-16 13:21:25 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
extUpstream := &aghtest.UpstreamMock{
|
|
|
|
OnAddress: func() (addr string) { return "external.upstream.example" },
|
|
|
|
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
|
2022-10-28 19:35:59 +03:00
|
|
|
return aghalg.Coalesce(
|
2023-07-25 14:16:26 +03:00
|
|
|
aghtest.MatchedResponse(req, dns.TypePTR, onesRevExtIPv4, onesHost),
|
|
|
|
doubleTTL(aghtest.MatchedResponse(req, dns.TypePTR, twosRevExtIPv4, twosHost)),
|
2022-08-16 13:21:25 +03:00
|
|
|
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
|
2022-10-28 19:35:59 +03:00
|
|
|
), nil
|
2021-04-07 20:16:06 +03:00
|
|
|
},
|
|
|
|
}
|
2022-08-16 13:21:25 +03:00
|
|
|
|
2023-07-06 17:10:06 +03:00
|
|
|
revLocIPv4, err := netutil.IPToReversedAddr(localIP.AsSlice())
|
2022-08-16 13:21:25 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
locUpstream := &aghtest.UpstreamMock{
|
|
|
|
OnAddress: func() (addr string) { return "local.upstream.example" },
|
|
|
|
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
|
2022-10-28 19:35:59 +03:00
|
|
|
return aghalg.Coalesce(
|
|
|
|
aghtest.MatchedResponse(req, dns.TypePTR, revLocIPv4, localDomainHost),
|
2022-08-16 13:21:25 +03:00
|
|
|
new(dns.Msg).SetRcode(req, dns.RcodeNameError),
|
2022-10-28 19:35:59 +03:00
|
|
|
), nil
|
2021-04-07 20:16:06 +03:00
|
|
|
},
|
|
|
|
}
|
2022-08-16 13:21:25 +03:00
|
|
|
|
|
|
|
errUpstream := aghtest.NewErrorUpstream()
|
|
|
|
nonPtrUpstream := aghtest.NewBlockUpstream("some-host", true)
|
2023-02-10 16:40:36 +03:00
|
|
|
refusingUpstream := aghtest.NewUpstreamMock(func(req *dns.Msg) (resp *dns.Msg, err error) {
|
|
|
|
return new(dns.Msg).SetRcode(req, dns.RcodeRefused), nil
|
|
|
|
})
|
2023-07-27 18:23:23 +03:00
|
|
|
zeroTTLUps := &aghtest.UpstreamMock{
|
|
|
|
OnAddress: func() (addr string) { return "zero.ttl.example" },
|
|
|
|
OnExchange: func(req *dns.Msg) (resp *dns.Msg, err error) {
|
|
|
|
resp = new(dns.Msg).SetReply(req)
|
|
|
|
hdr := dns.RR_Header{
|
|
|
|
Name: req.Question[0].Name,
|
|
|
|
Rrtype: dns.TypePTR,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
Ttl: 0,
|
|
|
|
}
|
|
|
|
resp.Answer = []dns.RR{&dns.PTR{
|
|
|
|
Hdr: hdr,
|
|
|
|
Ptr: localDomainHost,
|
|
|
|
}}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
},
|
|
|
|
}
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2022-08-29 15:54:41 +03:00
|
|
|
srv := &Server{
|
|
|
|
recDetector: newRecursionDetector(0, 1),
|
|
|
|
internalProxy: &proxy.Proxy{
|
|
|
|
Config: proxy.Config{
|
|
|
|
UpstreamConfig: &proxy.UpstreamConfig{
|
|
|
|
Upstreams: []upstream.Upstream{extUpstream},
|
|
|
|
},
|
2021-04-07 20:16:06 +03:00
|
|
|
},
|
|
|
|
},
|
2022-08-29 15:54:41 +03:00
|
|
|
}
|
2021-05-26 17:55:19 +03:00
|
|
|
srv.conf.UsePrivateRDNS = true
|
2022-03-18 13:37:27 +03:00
|
|
|
srv.privateNets = netutil.SubnetSetFunc(netutil.IsLocallyServed)
|
2023-12-25 18:23:10 +03:00
|
|
|
require.NoError(t, srv.internalProxy.Init())
|
2021-04-07 20:16:06 +03:00
|
|
|
|
|
|
|
testCases := []struct {
|
2023-07-25 14:16:26 +03:00
|
|
|
req netip.Addr
|
2021-04-07 20:16:06 +03:00
|
|
|
wantErr error
|
|
|
|
locUpstream upstream.Upstream
|
2023-07-25 14:16:26 +03:00
|
|
|
name string
|
|
|
|
want string
|
|
|
|
wantTTL time.Duration
|
2021-04-07 20:16:06 +03:00
|
|
|
}{{
|
|
|
|
name: "external_good",
|
2022-08-16 13:21:25 +03:00
|
|
|
want: onesHost,
|
2021-04-07 20:16:06 +03:00
|
|
|
wantErr: nil,
|
|
|
|
locUpstream: nil,
|
2022-08-16 13:21:25 +03:00
|
|
|
req: onesIP,
|
2023-07-25 14:16:26 +03:00
|
|
|
wantTTL: defaultTTL,
|
2021-04-07 20:16:06 +03:00
|
|
|
}, {
|
|
|
|
name: "local_good",
|
2022-08-16 13:21:25 +03:00
|
|
|
want: localDomainHost,
|
2021-04-07 20:16:06 +03:00
|
|
|
wantErr: nil,
|
|
|
|
locUpstream: locUpstream,
|
|
|
|
req: localIP,
|
2023-07-25 14:16:26 +03:00
|
|
|
wantTTL: defaultTTL,
|
2021-04-07 20:16:06 +03:00
|
|
|
}, {
|
|
|
|
name: "upstream_error",
|
|
|
|
want: "",
|
2022-08-16 13:21:25 +03:00
|
|
|
wantErr: aghtest.ErrUpstream,
|
2021-04-07 20:16:06 +03:00
|
|
|
locUpstream: errUpstream,
|
|
|
|
req: localIP,
|
2023-07-25 14:16:26 +03:00
|
|
|
wantTTL: 0,
|
2021-04-07 20:16:06 +03:00
|
|
|
}, {
|
|
|
|
name: "empty_answer_error",
|
|
|
|
want: "",
|
2023-02-10 16:40:36 +03:00
|
|
|
wantErr: ErrRDNSNoData,
|
2021-04-07 20:16:06 +03:00
|
|
|
locUpstream: locUpstream,
|
2023-07-06 17:10:06 +03:00
|
|
|
req: netip.MustParseAddr("192.168.1.2"),
|
2023-07-25 14:16:26 +03:00
|
|
|
wantTTL: 0,
|
2021-04-07 20:16:06 +03:00
|
|
|
}, {
|
2023-02-10 16:40:36 +03:00
|
|
|
name: "invalid_answer",
|
2021-04-07 20:16:06 +03:00
|
|
|
want: "",
|
2023-02-10 16:40:36 +03:00
|
|
|
wantErr: ErrRDNSNoData,
|
2021-04-07 20:16:06 +03:00
|
|
|
locUpstream: nonPtrUpstream,
|
|
|
|
req: localIP,
|
2023-07-25 14:16:26 +03:00
|
|
|
wantTTL: 0,
|
2023-02-10 16:40:36 +03:00
|
|
|
}, {
|
|
|
|
name: "refused",
|
|
|
|
want: "",
|
|
|
|
wantErr: ErrRDNSFailed,
|
|
|
|
locUpstream: refusingUpstream,
|
|
|
|
req: localIP,
|
2023-07-25 14:16:26 +03:00
|
|
|
wantTTL: 0,
|
|
|
|
}, {
|
|
|
|
name: "longest_ttl",
|
|
|
|
want: twosHost,
|
|
|
|
wantErr: nil,
|
|
|
|
locUpstream: nil,
|
|
|
|
req: twosIP,
|
|
|
|
wantTTL: defaultTTL * 2,
|
2023-07-27 18:23:23 +03:00
|
|
|
}, {
|
|
|
|
name: "zero_ttl",
|
|
|
|
want: localDomainHost,
|
|
|
|
wantErr: nil,
|
|
|
|
locUpstream: zeroTTLUps,
|
|
|
|
req: localIP,
|
|
|
|
wantTTL: 0,
|
2021-04-07 20:16:06 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2021-04-09 21:01:21 +03:00
|
|
|
pcfg := proxy.Config{
|
|
|
|
UpstreamConfig: &proxy.UpstreamConfig{
|
|
|
|
Upstreams: []upstream.Upstream{tc.locUpstream},
|
|
|
|
},
|
|
|
|
}
|
2021-05-26 17:55:19 +03:00
|
|
|
srv.localResolvers = &proxy.Proxy{
|
2021-04-09 21:01:21 +03:00
|
|
|
Config: pcfg,
|
2021-04-07 20:16:06 +03:00
|
|
|
}
|
2023-12-25 18:23:10 +03:00
|
|
|
require.NoError(t, srv.localResolvers.Init())
|
2021-04-07 20:16:06 +03:00
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-07-25 14:16:26 +03:00
|
|
|
host, ttl, eerr := srv.Exchange(tc.req)
|
2021-04-07 20:16:06 +03:00
|
|
|
|
|
|
|
require.ErrorIs(t, eerr, tc.wantErr)
|
|
|
|
assert.Equal(t, tc.want, host)
|
2023-07-25 14:16:26 +03:00
|
|
|
assert.Equal(t, tc.wantTTL, ttl)
|
2021-04-07 20:16:06 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("resolving_disabled", func(t *testing.T) {
|
2021-05-26 17:55:19 +03:00
|
|
|
srv.conf.UsePrivateRDNS = false
|
2023-07-27 18:23:23 +03:00
|
|
|
t.Cleanup(func() { srv.conf.UsePrivateRDNS = true })
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2023-07-25 14:16:26 +03:00
|
|
|
host, _, eerr := srv.Exchange(localIP)
|
2021-05-26 17:55:19 +03:00
|
|
|
|
|
|
|
require.NoError(t, eerr)
|
|
|
|
assert.Empty(t, host)
|
2021-04-07 20:16:06 +03:00
|
|
|
})
|
|
|
|
}
|