mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-05-09 13:48:41 +00:00
Feat: add disableFallback & skipFallback option for DNS client (#489)
This commit is contained in:
parent
70b63e21a5
commit
6e902b24ae
6 changed files with 191 additions and 145 deletions
|
@ -11,11 +11,12 @@ import (
|
|||
)
|
||||
|
||||
type NameServerConfig struct {
|
||||
Address *Address
|
||||
ClientIP *Address
|
||||
Port uint16
|
||||
Domains []string
|
||||
ExpectIPs StringList
|
||||
Address *Address
|
||||
ClientIP *Address
|
||||
Port uint16
|
||||
SkipFallback bool
|
||||
Domains []string
|
||||
ExpectIPs StringList
|
||||
}
|
||||
|
||||
func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
|
||||
|
@ -26,16 +27,18 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
|
|||
}
|
||||
|
||||
var advanced struct {
|
||||
Address *Address `json:"address"`
|
||||
ClientIP *Address `json:"clientIp"`
|
||||
Port uint16 `json:"port"`
|
||||
Domains []string `json:"domains"`
|
||||
ExpectIPs StringList `json:"expectIps"`
|
||||
Address *Address `json:"address"`
|
||||
ClientIP *Address `json:"clientIp"`
|
||||
Port uint16 `json:"port"`
|
||||
SkipFallback bool `json:"skipFallback"`
|
||||
Domains []string `json:"domains"`
|
||||
ExpectIPs StringList `json:"expectIps"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &advanced); err == nil {
|
||||
c.Address = advanced.Address
|
||||
c.ClientIP = advanced.ClientIP
|
||||
c.Port = advanced.Port
|
||||
c.SkipFallback = advanced.SkipFallback
|
||||
c.Domains = advanced.Domains
|
||||
c.ExpectIPs = advanced.ExpectIPs
|
||||
return nil
|
||||
|
@ -104,6 +107,7 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
|
|||
Port: uint32(c.Port),
|
||||
},
|
||||
ClientIp: myClientIP,
|
||||
SkipFallback: c.SkipFallback,
|
||||
PrioritizedDomain: domains,
|
||||
Geoip: geoipList,
|
||||
OriginalRules: originalRules,
|
||||
|
@ -119,13 +123,14 @@ var typeMap = map[router.Domain_Type]dns.DomainMatchingType{
|
|||
|
||||
// DNSConfig is a JSON serializable object for dns.Config.
|
||||
type DNSConfig struct {
|
||||
Servers []*NameServerConfig `json:"servers"`
|
||||
Hosts map[string]*Address `json:"hosts"`
|
||||
ClientIP *Address `json:"clientIp"`
|
||||
Tag string `json:"tag"`
|
||||
QueryStrategy string `json:"queryStrategy"`
|
||||
CacheStrategy string `json:"cacheStrategy"`
|
||||
DisableCache bool `json:"disableCache"`
|
||||
Servers []*NameServerConfig `json:"servers"`
|
||||
Hosts map[string]*Address `json:"hosts"`
|
||||
ClientIP *Address `json:"clientIp"`
|
||||
Tag string `json:"tag"`
|
||||
QueryStrategy string `json:"queryStrategy"`
|
||||
CacheStrategy string `json:"cacheStrategy"`
|
||||
DisableCache bool `json:"disableCache"`
|
||||
DisableFallback bool `json:"disableFallback"`
|
||||
}
|
||||
|
||||
func getHostMapping(addr *Address) *dns.Config_HostMapping {
|
||||
|
@ -143,8 +148,9 @@ func getHostMapping(addr *Address) *dns.Config_HostMapping {
|
|||
// Build implements Buildable
|
||||
func (c *DNSConfig) Build() (*dns.Config, error) {
|
||||
config := &dns.Config{
|
||||
Tag: c.Tag,
|
||||
CacheStrategy: dns.CacheStrategy_Cache_ALL,
|
||||
Tag: c.Tag,
|
||||
CacheStrategy: dns.CacheStrategy_Cache_ALL,
|
||||
DisableFallback: c.DisableFallback,
|
||||
}
|
||||
|
||||
if c.DisableCache {
|
||||
|
|
|
@ -69,6 +69,7 @@ func TestDNSConfigParsing(t *testing.T) {
|
|||
"address": "8.8.8.8",
|
||||
"clientIp": "10.0.0.1",
|
||||
"port": 5353,
|
||||
"skipFallback": true,
|
||||
"domains": ["domain:example.com"]
|
||||
}],
|
||||
"hosts": {
|
||||
|
@ -80,7 +81,8 @@ func TestDNSConfigParsing(t *testing.T) {
|
|||
},
|
||||
"clientIp": "10.0.0.1",
|
||||
"queryStrategy": "UseIPv4",
|
||||
"disableCache": true
|
||||
"disableCache": true,
|
||||
"disableFallback": true
|
||||
}`,
|
||||
Parser: parserCreator(),
|
||||
Output: &dns.Config{
|
||||
|
@ -95,7 +97,8 @@ func TestDNSConfigParsing(t *testing.T) {
|
|||
Network: net.Network_UDP,
|
||||
Port: 5353,
|
||||
},
|
||||
ClientIp: []byte{10, 0, 0, 1},
|
||||
ClientIp: []byte{10, 0, 0, 1},
|
||||
SkipFallback: true,
|
||||
PrioritizedDomain: []*dns.NameServer_PriorityDomain{
|
||||
{
|
||||
Type: dns.DomainMatchingType_Subdomain,
|
||||
|
@ -137,9 +140,10 @@ func TestDNSConfigParsing(t *testing.T) {
|
|||
Ip: [][]byte{{8, 8, 4, 4}},
|
||||
},
|
||||
},
|
||||
ClientIp: []byte{10, 0, 0, 1},
|
||||
QueryStrategy: dns.QueryStrategy_USE_IP4,
|
||||
CacheStrategy: dns.CacheStrategy_Cache_DISABLE,
|
||||
ClientIp: []byte{10, 0, 0, 1},
|
||||
QueryStrategy: dns.QueryStrategy_USE_IP4,
|
||||
CacheStrategy: dns.CacheStrategy_Cache_DISABLE,
|
||||
DisableFallback: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue