2020-11-16 19:45:31 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2021-04-07 20:16:06 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2021-05-21 14:55:42 +03:00
|
|
|
"io"
|
2021-02-04 20:35:13 +03:00
|
|
|
"net"
|
2020-11-16 19:45:31 +03:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-12-05 17:24:32 +03:00
|
|
|
"net/netip"
|
|
|
|
"net/url"
|
2021-04-07 20:16:06 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-04-13 13:44:29 +03:00
|
|
|
"strings"
|
2020-11-16 19:45:31 +03:00
|
|
|
"testing"
|
2023-06-30 12:41:10 +03:00
|
|
|
"testing/fstest"
|
2022-12-05 17:24:32 +03:00
|
|
|
"time"
|
2020-11-16 19:45:31 +03:00
|
|
|
|
2022-10-03 18:08:05 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2021-06-01 14:28:34 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2023-06-30 12:41:10 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2023-12-07 16:48:55 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2023-04-07 14:21:37 +03:00
|
|
|
"github.com/AdguardTeam/golibs/httphdr"
|
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"
|
2022-12-05 17:24:32 +03:00
|
|
|
"github.com/miekg/dns"
|
2020-11-16 19:45:31 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-11 17:32:58 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-16 19:45:31 +03:00
|
|
|
)
|
|
|
|
|
2024-03-29 10:45:20 +03:00
|
|
|
// TODO(e.burkov): Use the better approach to testdata with a separate
|
|
|
|
// directory for each test, and a separate file for each subtest. See the
|
|
|
|
// [configmigrate] package.
|
|
|
|
|
2023-10-05 15:26:19 +03:00
|
|
|
// emptySysResolvers is an empty [SystemResolvers] implementation that always
|
|
|
|
// returns nil.
|
|
|
|
type emptySysResolvers struct{}
|
2021-06-01 14:28:34 +03:00
|
|
|
|
2023-10-05 15:26:19 +03:00
|
|
|
// Addrs implements the aghnet.SystemResolvers interface for emptySysResolvers.
|
|
|
|
func (emptySysResolvers) Addrs() (addrs []netip.AddrPort) {
|
2021-06-01 14:28:34 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 14:36:18 +03:00
|
|
|
func loadTestData(t *testing.T, casesFileName string, cases any) {
|
2021-04-07 20:16:06 +03:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
var f *os.File
|
|
|
|
f, err := os.Open(filepath.Join("testdata", casesFileName))
|
|
|
|
require.NoError(t, err)
|
2021-10-22 11:58:18 +03:00
|
|
|
testutil.CleanupAndRequireSuccess(t, f.Close)
|
2021-04-07 20:16:06 +03:00
|
|
|
|
|
|
|
err = json.NewDecoder(f).Decode(cases)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-10-27 20:18:29 +03:00
|
|
|
const (
|
|
|
|
jsonExt = ".json"
|
|
|
|
|
|
|
|
// testBlockedRespTTL is the TTL for blocked responses to use in tests.
|
|
|
|
testBlockedRespTTL = 10
|
|
|
|
)
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2021-07-14 21:03:56 +03:00
|
|
|
func TestDNSForwardHTTP_handleGetConfig(t *testing.T) {
|
2021-05-21 16:15:47 +03:00
|
|
|
filterConf := &filtering.Config{
|
2023-08-30 18:26:02 +03:00
|
|
|
ProtectionEnabled: true,
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
2023-10-27 20:18:29 +03:00
|
|
|
BlockedResponseTTL: testBlockedRespTTL,
|
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-11-15 19:27:13 +03:00
|
|
|
UpstreamDNS: []string{"8.8.8.8:53", "8.8.4.4:53"},
|
|
|
|
FallbackDNS: []string{"9.9.9.10"},
|
|
|
|
RatelimitSubnetLenIPv4: 24,
|
|
|
|
RatelimitSubnetLenIPv6: 56,
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-11-15 19:27:13 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
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
|
|
|
}
|
2024-03-11 18:17:04 +03:00
|
|
|
s := createTestServer(t, filterConf, forwardConf)
|
2023-10-05 15:26:19 +03:00
|
|
|
s.sysResolvers = &emptySysResolvers{}
|
2021-06-01 14:28:34 +03:00
|
|
|
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, s.Start())
|
|
|
|
testutil.CleanupAndRequireSuccess(t, s.Stop)
|
2020-11-16 19:45:31 +03:00
|
|
|
|
|
|
|
defaultConf := s.conf
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
conf func() ServerConfig
|
2021-04-07 20:16:06 +03:00
|
|
|
name string
|
2020-11-16 19:45:31 +03:00
|
|
|
}{{
|
|
|
|
conf: func() ServerConfig {
|
|
|
|
return defaultConf
|
|
|
|
},
|
2021-04-07 20:16:06 +03:00
|
|
|
name: "all_right",
|
2020-11-16 19:45:31 +03:00
|
|
|
}, {
|
|
|
|
conf: func() ServerConfig {
|
|
|
|
conf := defaultConf
|
2023-12-25 14:16:48 +03:00
|
|
|
conf.UpstreamMode = UpstreamModeFastestAddr
|
2021-02-04 20:35:13 +03:00
|
|
|
|
2020-11-16 19:45:31 +03:00
|
|
|
return conf
|
|
|
|
},
|
2021-04-07 20:16:06 +03:00
|
|
|
name: "fastest_addr",
|
2020-11-16 19:45:31 +03:00
|
|
|
}, {
|
|
|
|
conf: func() ServerConfig {
|
|
|
|
conf := defaultConf
|
2023-12-25 14:16:48 +03:00
|
|
|
conf.UpstreamMode = UpstreamModeParallel
|
2021-02-04 20:35:13 +03:00
|
|
|
|
2020-11-16 19:45:31 +03:00
|
|
|
return conf
|
|
|
|
},
|
2021-04-07 20:16:06 +03:00
|
|
|
name: "parallel",
|
2020-11-16 19:45:31 +03:00
|
|
|
}}
|
|
|
|
|
2021-04-07 20:16:06 +03:00
|
|
|
var data map[string]json.RawMessage
|
|
|
|
loadTestData(t, t.Name()+jsonExt, &data)
|
|
|
|
|
2020-11-16 19:45:31 +03:00
|
|
|
for _, tc := range testCases {
|
2021-04-07 20:16:06 +03:00
|
|
|
caseWant, ok := data[tc.name]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2020-11-16 19:45:31 +03:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-03-11 17:32:58 +03:00
|
|
|
t.Cleanup(w.Body.Reset)
|
|
|
|
|
2020-11-16 19:45:31 +03:00
|
|
|
s.conf = tc.conf()
|
|
|
|
s.handleGetConfig(w, nil)
|
|
|
|
|
2023-04-07 14:21:37 +03:00
|
|
|
cType := w.Header().Get(httphdr.ContentType)
|
2022-10-03 18:08:05 +03:00
|
|
|
assert.Equal(t, aghhttp.HdrValApplicationJSON, cType)
|
2021-04-07 20:16:06 +03:00
|
|
|
assert.JSONEq(t, string(caseWant), w.Body.String())
|
2020-11-16 19:45:31 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 21:03:56 +03:00
|
|
|
func TestDNSForwardHTTP_handleSetConfig(t *testing.T) {
|
2021-05-21 16:15:47 +03:00
|
|
|
filterConf := &filtering.Config{
|
2023-08-30 18:26:02 +03:00
|
|
|
ProtectionEnabled: true,
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
2023-10-27 20:18:29 +03:00
|
|
|
BlockedResponseTTL: testBlockedRespTTL,
|
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-11-15 19:27:13 +03:00
|
|
|
UpstreamDNS: []string{"8.8.8.8:53", "8.8.4.4:53"},
|
|
|
|
RatelimitSubnetLenIPv4: 24,
|
|
|
|
RatelimitSubnetLenIPv6: 56,
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-11-15 19:27:13 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
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
|
|
|
}
|
2024-03-11 18:17:04 +03:00
|
|
|
s := createTestServer(t, filterConf, forwardConf)
|
2023-10-05 15:26:19 +03:00
|
|
|
s.sysResolvers = &emptySysResolvers{}
|
2020-11-16 19:45:31 +03:00
|
|
|
|
|
|
|
defaultConf := s.conf
|
|
|
|
|
|
|
|
err := s.Start()
|
2021-10-22 11:58:18 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, s.Stop)
|
2020-11-16 19:45:31 +03:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
wantSet string
|
|
|
|
}{{
|
|
|
|
name: "upstream_dns",
|
|
|
|
wantSet: "",
|
|
|
|
}, {
|
|
|
|
name: "bootstraps",
|
|
|
|
wantSet: "",
|
|
|
|
}, {
|
|
|
|
name: "blocking_mode_good",
|
|
|
|
wantSet: "",
|
|
|
|
}, {
|
2023-11-15 19:27:13 +03:00
|
|
|
name: "blocking_mode_bad",
|
|
|
|
wantSet: "validating dns config: " +
|
|
|
|
"blocking_ipv4 must be valid ipv4 on custom_ip blocking_mode",
|
2020-11-16 19:45:31 +03:00
|
|
|
}, {
|
|
|
|
name: "ratelimit",
|
|
|
|
wantSet: "",
|
2023-11-15 19:27:13 +03:00
|
|
|
}, {
|
|
|
|
name: "ratelimit_subnet_len",
|
|
|
|
wantSet: "",
|
|
|
|
}, {
|
2023-12-05 17:43:50 +03:00
|
|
|
name: "ratelimit_whitelist_not_ip",
|
|
|
|
wantSet: `decoding request: ParseAddr("not.ip"): unexpected character (at "not.ip")`,
|
2020-11-16 19:45:31 +03:00
|
|
|
}, {
|
|
|
|
name: "edns_cs_enabled",
|
|
|
|
wantSet: "",
|
2023-03-22 13:42:20 +03:00
|
|
|
}, {
|
|
|
|
name: "edns_cs_use_custom",
|
|
|
|
wantSet: "",
|
|
|
|
}, {
|
|
|
|
name: "edns_cs_use_custom_bad_ip",
|
|
|
|
wantSet: "decoding request: ParseAddr(\"bad.ip\"): unexpected character (at \"bad.ip\")",
|
2020-11-16 19:45:31 +03:00
|
|
|
}, {
|
|
|
|
name: "dnssec_enabled",
|
|
|
|
wantSet: "",
|
|
|
|
}, {
|
|
|
|
name: "cache_size",
|
|
|
|
wantSet: "",
|
|
|
|
}, {
|
|
|
|
name: "upstream_mode_parallel",
|
|
|
|
wantSet: "",
|
|
|
|
}, {
|
|
|
|
name: "upstream_mode_fastest_addr",
|
|
|
|
wantSet: "",
|
|
|
|
}, {
|
2023-11-15 19:27:13 +03:00
|
|
|
name: "upstream_dns_bad",
|
2024-02-05 16:12:27 +03:00
|
|
|
wantSet: `validating dns config: upstream servers: parsing error at index 0: ` +
|
2024-08-20 18:38:04 +03:00
|
|
|
`cannot prepare the upstream: invalid address !!!: bad domain name "!!!": ` +
|
2024-02-05 16:12:27 +03:00
|
|
|
`bad top-level domain name label "!!!": bad top-level domain name label rune '!'`,
|
2020-11-16 19:45:31 +03:00
|
|
|
}, {
|
2021-04-13 13:44:29 +03:00
|
|
|
name: "bootstraps_bad",
|
2024-01-16 13:18:22 +03:00
|
|
|
wantSet: `validating dns config: checking bootstrap a: not a bootstrap: ParseAddr("a"): ` +
|
|
|
|
`unable to parse IP`,
|
2020-11-16 19:45:31 +03:00
|
|
|
}, {
|
|
|
|
name: "cache_bad_ttl",
|
2023-12-26 16:52:33 +03:00
|
|
|
wantSet: `validating dns config: cache_ttl_min must be less than or equal to cache_ttl_max`,
|
2020-11-16 19:45:31 +03:00
|
|
|
}, {
|
|
|
|
name: "upstream_mode_bad",
|
2023-11-15 19:27:13 +03:00
|
|
|
wantSet: `validating dns config: upstream_mode: incorrect value "somethingelse"`,
|
2021-04-07 20:16:06 +03:00
|
|
|
}, {
|
|
|
|
name: "local_ptr_upstreams_good",
|
|
|
|
wantSet: "",
|
2022-02-14 16:56:14 +03:00
|
|
|
}, {
|
|
|
|
name: "local_ptr_upstreams_bad",
|
2024-04-11 14:03:37 +03:00
|
|
|
wantSet: `validating dns config: private upstream servers: ` +
|
|
|
|
`bad arpa domain name "non.arpa": not a reversed ip network`,
|
2021-04-07 20:16:06 +03:00
|
|
|
}, {
|
|
|
|
name: "local_ptr_upstreams_null",
|
|
|
|
wantSet: "",
|
2023-08-30 13:21:31 +03:00
|
|
|
}, {
|
|
|
|
name: "fallbacks",
|
|
|
|
wantSet: "",
|
2023-09-13 13:58:12 +03:00
|
|
|
}, {
|
|
|
|
name: "blocked_response_ttl",
|
|
|
|
wantSet: "",
|
2023-10-27 20:18:29 +03:00
|
|
|
}, {
|
|
|
|
name: "multiple_domain_specific_upstreams",
|
|
|
|
wantSet: "",
|
2020-11-16 19:45:31 +03:00
|
|
|
}}
|
|
|
|
|
2021-04-07 20:16:06 +03:00
|
|
|
var data map[string]struct {
|
|
|
|
Req json.RawMessage `json:"req"`
|
|
|
|
Want json.RawMessage `json:"want"`
|
|
|
|
}
|
2023-03-22 13:42:20 +03:00
|
|
|
|
|
|
|
testData := t.Name() + jsonExt
|
|
|
|
loadTestData(t, testData, &data)
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2020-11-16 19:45:31 +03:00
|
|
|
for _, tc := range testCases {
|
2023-03-22 13:42:20 +03:00
|
|
|
// NOTE: Do not use require.Contains, because the size of the data
|
|
|
|
// prevents it from printing a meaningful error message.
|
2021-04-07 20:16:06 +03:00
|
|
|
caseData, ok := data[tc.name]
|
2023-03-22 13:42:20 +03:00
|
|
|
require.Truef(t, ok, "%q does not contain test data for test case %s", testData, tc.name)
|
2021-04-07 20:16:06 +03:00
|
|
|
|
2020-11-16 19:45:31 +03:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-03-01 16:16:16 +03:00
|
|
|
t.Cleanup(func() {
|
2023-09-04 17:18:43 +03:00
|
|
|
s.dnsFilter.SetBlockingMode(filtering.BlockingModeDefault, netip.Addr{}, netip.Addr{})
|
2023-03-01 16:16:16 +03:00
|
|
|
s.conf = defaultConf
|
2023-08-30 18:26:02 +03:00
|
|
|
s.conf.Config.EDNSClientSubnet = &EDNSClientSubnet{}
|
2023-10-27 20:18:29 +03:00
|
|
|
s.dnsFilter.SetBlockedResponseTTL(testBlockedRespTTL)
|
2023-03-01 16:16:16 +03:00
|
|
|
})
|
2021-03-11 17:32:58 +03:00
|
|
|
|
2021-05-21 14:55:42 +03:00
|
|
|
rBody := io.NopCloser(bytes.NewReader(caseData.Req))
|
2021-03-11 21:30:52 +03:00
|
|
|
var r *http.Request
|
|
|
|
r, err = http.NewRequest(http.MethodPost, "http://example.com", rBody)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
2020-11-16 19:45:31 +03:00
|
|
|
|
|
|
|
s.handleSetConfig(w, r)
|
2021-04-13 13:44:29 +03:00
|
|
|
assert.Equal(t, tc.wantSet, strings.TrimSuffix(w.Body.String(), "\n"))
|
2020-11-16 19:45:31 +03:00
|
|
|
w.Body.Reset()
|
|
|
|
|
|
|
|
s.handleGetConfig(w, nil)
|
2021-04-07 20:16:06 +03:00
|
|
|
assert.JSONEq(t, string(caseData.Want), w.Body.String())
|
2020-11-16 19:45:31 +03:00
|
|
|
w.Body.Reset()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 16:36:38 +03:00
|
|
|
|
2021-07-29 17:40:31 +03:00
|
|
|
func TestIsCommentOrEmpty(t *testing.T) {
|
2022-01-13 15:05:44 +03:00
|
|
|
for _, tc := range []struct {
|
|
|
|
want assert.BoolAssertionFunc
|
|
|
|
str string
|
|
|
|
}{{
|
|
|
|
want: assert.True,
|
|
|
|
str: "",
|
|
|
|
}, {
|
|
|
|
want: assert.True,
|
|
|
|
str: "# comment",
|
|
|
|
}, {
|
|
|
|
want: assert.False,
|
|
|
|
str: "1.2.3.4",
|
|
|
|
}} {
|
|
|
|
tc.want(t, IsCommentOrEmpty(tc.str))
|
|
|
|
}
|
2021-07-29 17:40:31 +03:00
|
|
|
}
|
|
|
|
|
2023-06-30 12:41:10 +03:00
|
|
|
func newLocalUpstreamListener(t *testing.T, port uint16, handler dns.Handler) (real netip.AddrPort) {
|
|
|
|
t.Helper()
|
|
|
|
|
2022-12-05 17:24:32 +03:00
|
|
|
startCh := make(chan struct{})
|
|
|
|
upsSrv := &dns.Server{
|
2023-06-30 12:41:10 +03:00
|
|
|
Addr: netip.AddrPortFrom(netutil.IPv4Localhost(), port).String(),
|
2022-12-05 17:24:32 +03:00
|
|
|
Net: "tcp",
|
|
|
|
Handler: handler,
|
|
|
|
NotifyStartedFunc: func() { close(startCh) },
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
err := upsSrv.ListenAndServe()
|
2023-06-30 12:41:10 +03:00
|
|
|
require.NoError(testutil.PanicT{}, err)
|
2022-12-05 17:24:32 +03:00
|
|
|
}()
|
2023-06-30 12:41:10 +03:00
|
|
|
|
2022-12-05 17:24:32 +03:00
|
|
|
<-startCh
|
|
|
|
testutil.CleanupAndRequireSuccess(t, upsSrv.Shutdown)
|
|
|
|
|
2023-06-30 12:41:10 +03:00
|
|
|
return testutil.RequireTypeAssert[*net.TCPAddr](t, upsSrv.Listener.Addr()).AddrPort()
|
2022-12-05 17:24:32 +03:00
|
|
|
}
|
|
|
|
|
2023-06-30 12:41:10 +03:00
|
|
|
func TestServer_HandleTestUpstreamDNS(t *testing.T) {
|
2023-11-28 16:40:52 +03:00
|
|
|
hdlr := dns.HandlerFunc(func(w dns.ResponseWriter, m *dns.Msg) {
|
2022-12-05 17:24:32 +03:00
|
|
|
err := w.WriteMsg(new(dns.Msg).SetReply(m))
|
|
|
|
require.NoError(testutil.PanicT{}, err)
|
|
|
|
})
|
|
|
|
|
2023-11-28 16:40:52 +03:00
|
|
|
ups := (&url.URL{
|
2022-12-05 17:24:32 +03:00
|
|
|
Scheme: "tcp",
|
2023-11-28 16:40:52 +03:00
|
|
|
Host: newLocalUpstreamListener(t, 0, hdlr).String(),
|
2022-12-05 17:24:32 +03:00
|
|
|
}).String()
|
|
|
|
|
2023-06-30 12:41:10 +03:00
|
|
|
const (
|
|
|
|
upsTimeout = 100 * time.Millisecond
|
|
|
|
|
|
|
|
hostsFileName = "hosts"
|
|
|
|
upstreamHost = "custom.localhost"
|
|
|
|
)
|
|
|
|
|
2023-11-28 16:40:52 +03:00
|
|
|
hostsListener := newLocalUpstreamListener(t, 0, hdlr)
|
2023-06-30 12:41:10 +03:00
|
|
|
hostsUps := (&url.URL{
|
|
|
|
Scheme: "tcp",
|
2023-10-05 15:26:19 +03:00
|
|
|
Host: netutil.JoinHostPort(upstreamHost, hostsListener.Port()),
|
2023-06-30 12:41:10 +03:00
|
|
|
}).String()
|
|
|
|
|
|
|
|
hc, err := aghnet.NewHostsContainer(
|
|
|
|
fstest.MapFS{
|
|
|
|
hostsFileName: &fstest.MapFile{
|
|
|
|
Data: []byte(hostsListener.Addr().String() + " " + upstreamHost),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&aghtest.FSWatcher{
|
2024-02-13 13:19:22 +03:00
|
|
|
OnStart: func() (_ error) { panic("not implemented") },
|
2023-06-30 12:41:10 +03:00
|
|
|
OnEvents: func() (e <-chan struct{}) { return nil },
|
|
|
|
OnAdd: func(_ string) (err error) { return nil },
|
|
|
|
OnClose: func() (err error) { return nil },
|
|
|
|
},
|
|
|
|
hostsFileName,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2022-12-05 17:24:32 +03:00
|
|
|
|
2023-06-30 12:41:10 +03:00
|
|
|
srv := createTestServer(t, &filtering.Config{
|
2023-09-04 17:18:43 +03:00
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
EtcHosts: hc,
|
2023-06-30 12:41:10 +03:00
|
|
|
}, ServerConfig{
|
2022-12-05 17:24:32 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
|
|
|
UpstreamTimeout: upsTimeout,
|
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,
|
2024-03-11 18:17:04 +03:00
|
|
|
})
|
2023-12-07 16:48:55 +03:00
|
|
|
srv.etcHosts = upstream.NewHostsResolver(hc)
|
2022-12-05 17:24:32 +03:00
|
|
|
startDeferStop(t, srv)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
body map[string]any
|
|
|
|
wantResp map[string]any
|
|
|
|
name string
|
|
|
|
}{{
|
2023-06-30 12:41:10 +03:00
|
|
|
body: map[string]any{
|
|
|
|
"upstream_dns": []string{hostsUps},
|
|
|
|
},
|
|
|
|
wantResp: map[string]any{
|
|
|
|
hostsUps: "OK",
|
|
|
|
},
|
|
|
|
name: "etc_hosts",
|
2023-09-06 12:29:45 +03:00
|
|
|
}, {
|
|
|
|
body: map[string]any{
|
2023-11-28 16:40:52 +03:00
|
|
|
"upstream_dns": []string{ups, "#this.is.comment"},
|
2023-11-10 18:22:09 +03:00
|
|
|
},
|
|
|
|
wantResp: map[string]any{
|
2023-11-28 16:40:52 +03:00
|
|
|
ups: "OK",
|
2023-11-10 18:22:09 +03:00
|
|
|
},
|
2023-11-28 16:40:52 +03:00
|
|
|
name: "comment_mix",
|
2022-12-05 17:24:32 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-06-30 12:41:10 +03:00
|
|
|
var reqBody []byte
|
|
|
|
reqBody, err = json.Marshal(tc.body)
|
2022-12-05 17:24:32 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2023-06-30 12:41:10 +03:00
|
|
|
|
|
|
|
var r *http.Request
|
|
|
|
r, err = http.NewRequest(http.MethodPost, "", bytes.NewReader(reqBody))
|
2022-12-05 17:24:32 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
srv.handleTestUpstreamDNS(w, r)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
|
|
|
resp := map[string]any{}
|
|
|
|
err = json.NewDecoder(w.Body).Decode(&resp)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.wantResp, resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("timeout", func(t *testing.T) {
|
|
|
|
slowHandler := dns.HandlerFunc(func(w dns.ResponseWriter, m *dns.Msg) {
|
|
|
|
time.Sleep(upsTimeout * 2)
|
|
|
|
writeErr := w.WriteMsg(new(dns.Msg).SetReply(m))
|
|
|
|
require.NoError(testutil.PanicT{}, writeErr)
|
|
|
|
})
|
|
|
|
sleepyUps := (&url.URL{
|
|
|
|
Scheme: "tcp",
|
|
|
|
Host: newLocalUpstreamListener(t, 0, slowHandler).String(),
|
|
|
|
}).String()
|
|
|
|
|
|
|
|
req := map[string]any{
|
|
|
|
"upstream_dns": []string{sleepyUps},
|
|
|
|
}
|
2023-06-30 12:41:10 +03:00
|
|
|
|
|
|
|
var reqBody []byte
|
|
|
|
reqBody, err = json.Marshal(req)
|
2022-12-05 17:24:32 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2023-06-30 12:41:10 +03:00
|
|
|
|
|
|
|
var r *http.Request
|
|
|
|
r, err = http.NewRequest(http.MethodPost, "", bytes.NewReader(reqBody))
|
2022-12-05 17:24:32 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
srv.handleTestUpstreamDNS(w, r)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
|
|
|
resp := map[string]any{}
|
|
|
|
err = json.NewDecoder(w.Body).Decode(&resp)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Contains(t, resp, sleepyUps)
|
|
|
|
require.IsType(t, "", resp[sleepyUps])
|
|
|
|
sleepyRes, _ := resp[sleepyUps].(string)
|
|
|
|
|
|
|
|
// TODO(e.burkov): Improve the format of an error in dnsproxy.
|
|
|
|
assert.True(t, strings.HasSuffix(sleepyRes, "i/o timeout"))
|
|
|
|
})
|
|
|
|
}
|