mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-17 04:34:35 +03:00
feat(dns): add optimistic caching (#5237)
This commit is contained in:
@@ -16,19 +16,21 @@ import (
|
||||
)
|
||||
|
||||
type NameServerConfig struct {
|
||||
Address *Address `json:"address"`
|
||||
ClientIP *Address `json:"clientIp"`
|
||||
Port uint16 `json:"port"`
|
||||
SkipFallback bool `json:"skipFallback"`
|
||||
Domains []string `json:"domains"`
|
||||
ExpectedIPs StringList `json:"expectedIPs"`
|
||||
ExpectIPs StringList `json:"expectIPs"`
|
||||
QueryStrategy string `json:"queryStrategy"`
|
||||
Tag string `json:"tag"`
|
||||
TimeoutMs uint64 `json:"timeoutMs"`
|
||||
DisableCache bool `json:"disableCache"`
|
||||
FinalQuery bool `json:"finalQuery"`
|
||||
UnexpectedIPs StringList `json:"unexpectedIPs"`
|
||||
Address *Address `json:"address"`
|
||||
ClientIP *Address `json:"clientIp"`
|
||||
Port uint16 `json:"port"`
|
||||
SkipFallback bool `json:"skipFallback"`
|
||||
Domains []string `json:"domains"`
|
||||
ExpectedIPs StringList `json:"expectedIPs"`
|
||||
ExpectIPs StringList `json:"expectIPs"`
|
||||
QueryStrategy string `json:"queryStrategy"`
|
||||
Tag string `json:"tag"`
|
||||
TimeoutMs uint64 `json:"timeoutMs"`
|
||||
DisableCache bool `json:"disableCache"`
|
||||
ServeStale bool `json:"serveStale"`
|
||||
ServeExpiredTTL *uint32 `json:"serveExpiredTTL"`
|
||||
FinalQuery bool `json:"finalQuery"`
|
||||
UnexpectedIPs StringList `json:"unexpectedIPs"`
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
|
||||
@@ -40,19 +42,21 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
|
||||
var advanced struct {
|
||||
Address *Address `json:"address"`
|
||||
ClientIP *Address `json:"clientIp"`
|
||||
Port uint16 `json:"port"`
|
||||
SkipFallback bool `json:"skipFallback"`
|
||||
Domains []string `json:"domains"`
|
||||
ExpectedIPs StringList `json:"expectedIPs"`
|
||||
ExpectIPs StringList `json:"expectIPs"`
|
||||
QueryStrategy string `json:"queryStrategy"`
|
||||
Tag string `json:"tag"`
|
||||
TimeoutMs uint64 `json:"timeoutMs"`
|
||||
DisableCache bool `json:"disableCache"`
|
||||
FinalQuery bool `json:"finalQuery"`
|
||||
UnexpectedIPs StringList `json:"unexpectedIPs"`
|
||||
Address *Address `json:"address"`
|
||||
ClientIP *Address `json:"clientIp"`
|
||||
Port uint16 `json:"port"`
|
||||
SkipFallback bool `json:"skipFallback"`
|
||||
Domains []string `json:"domains"`
|
||||
ExpectedIPs StringList `json:"expectedIPs"`
|
||||
ExpectIPs StringList `json:"expectIPs"`
|
||||
QueryStrategy string `json:"queryStrategy"`
|
||||
Tag string `json:"tag"`
|
||||
TimeoutMs uint64 `json:"timeoutMs"`
|
||||
DisableCache bool `json:"disableCache"`
|
||||
ServeStale bool `json:"serveStale"`
|
||||
ServeExpiredTTL *uint32 `json:"serveExpiredTTL"`
|
||||
FinalQuery bool `json:"finalQuery"`
|
||||
UnexpectedIPs StringList `json:"unexpectedIPs"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &advanced); err == nil {
|
||||
c.Address = advanced.Address
|
||||
@@ -66,6 +70,8 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
|
||||
c.Tag = advanced.Tag
|
||||
c.TimeoutMs = advanced.TimeoutMs
|
||||
c.DisableCache = advanced.DisableCache
|
||||
c.ServeStale = advanced.ServeStale
|
||||
c.ServeExpiredTTL = advanced.ServeExpiredTTL
|
||||
c.FinalQuery = advanced.FinalQuery
|
||||
c.UnexpectedIPs = advanced.UnexpectedIPs
|
||||
return nil
|
||||
@@ -173,6 +179,8 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
|
||||
Tag: c.Tag,
|
||||
TimeoutMs: c.TimeoutMs,
|
||||
DisableCache: c.DisableCache,
|
||||
ServeStale: c.ServeStale,
|
||||
ServeExpiredTTL: c.ServeExpiredTTL,
|
||||
FinalQuery: c.FinalQuery,
|
||||
UnexpectedGeoip: unexpectedGeoipList,
|
||||
ActUnprior: actUnprior,
|
||||
@@ -194,6 +202,8 @@ type DNSConfig struct {
|
||||
Tag string `json:"tag"`
|
||||
QueryStrategy string `json:"queryStrategy"`
|
||||
DisableCache bool `json:"disableCache"`
|
||||
ServeStale bool `json:"serveStale"`
|
||||
ServeExpiredTTL uint32 `json:"serveExpiredTTL"`
|
||||
DisableFallback bool `json:"disableFallback"`
|
||||
DisableFallbackIfMatch bool `json:"disableFallbackIfMatch"`
|
||||
UseSystemHosts bool `json:"useSystemHosts"`
|
||||
@@ -391,6 +401,8 @@ func (c *DNSConfig) Build() (*dns.Config, error) {
|
||||
config := &dns.Config{
|
||||
Tag: c.Tag,
|
||||
DisableCache: c.DisableCache,
|
||||
ServeStale: c.ServeStale,
|
||||
ServeExpiredTTL: c.ServeExpiredTTL,
|
||||
DisableFallback: c.DisableFallback,
|
||||
DisableFallbackIfMatch: c.DisableFallbackIfMatch,
|
||||
QueryStrategy: resolveQueryStrategy(c.QueryStrategy),
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestDNSConfigParsing(t *testing.T) {
|
||||
return config.Build()
|
||||
}
|
||||
}
|
||||
|
||||
expectedServeExpiredTTL := uint32(172800)
|
||||
runMultiTestCase(t, []TestCase{
|
||||
{
|
||||
Input: `{
|
||||
@@ -28,7 +28,9 @@ func TestDNSConfigParsing(t *testing.T) {
|
||||
"address": "8.8.8.8",
|
||||
"port": 5353,
|
||||
"skipFallback": true,
|
||||
"domains": ["domain:example.com"]
|
||||
"domains": ["domain:example.com"],
|
||||
"serveStale": true,
|
||||
"serveExpiredTTL": 172800
|
||||
}],
|
||||
"hosts": {
|
||||
"domain:example.com": "google.com",
|
||||
@@ -40,6 +42,8 @@ func TestDNSConfigParsing(t *testing.T) {
|
||||
"clientIp": "10.0.0.1",
|
||||
"queryStrategy": "UseIPv4",
|
||||
"disableCache": true,
|
||||
"serveStale": false,
|
||||
"serveExpiredTTL": 86400,
|
||||
"disableFallback": true
|
||||
}`,
|
||||
Parser: parserCreator(),
|
||||
@@ -68,6 +72,8 @@ func TestDNSConfigParsing(t *testing.T) {
|
||||
Size: 1,
|
||||
},
|
||||
},
|
||||
ServeStale: true,
|
||||
ServeExpiredTTL: &expectedServeExpiredTTL,
|
||||
},
|
||||
},
|
||||
StaticHosts: []*dns.Config_HostMapping{
|
||||
@@ -100,6 +106,8 @@ func TestDNSConfigParsing(t *testing.T) {
|
||||
ClientIp: []byte{10, 0, 0, 1},
|
||||
QueryStrategy: dns.QueryStrategy_USE_IP4,
|
||||
DisableCache: true,
|
||||
ServeStale: false,
|
||||
ServeExpiredTTL: 86400,
|
||||
DisableFallback: true,
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user