mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-05-09 13:48:41 +00:00
Merge branch 'dns' into dns-geo
This commit is contained in:
commit
b0a66e650c
29 changed files with 540 additions and 399 deletions
|
@ -15,11 +15,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 {
|
||||
|
@ -30,16 +31,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
|
||||
|
@ -93,6 +96,7 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
|
|||
Port: uint32(c.Port),
|
||||
},
|
||||
ClientIp: myClientIP,
|
||||
SkipFallback: c.SkipFallback,
|
||||
PrioritizedDomain: domains,
|
||||
Geoip: geoipList,
|
||||
OriginalRules: originalRules,
|
||||
|
@ -101,12 +105,14 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
|
|||
|
||||
// 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"`
|
||||
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 {
|
||||
|
@ -124,8 +130,13 @@ func getHostMapping(addr *Address) *dns.Config_HostMapping {
|
|||
// Build implements Buildable
|
||||
func (c *DNSConfig) Build() (*dns.Config, error) {
|
||||
config := &dns.Config{
|
||||
Tag: c.Tag,
|
||||
DisableCache: c.DisableCache,
|
||||
Tag: c.Tag,
|
||||
CacheStrategy: dns.CacheStrategy_Cache_ALL,
|
||||
DisableFallback: c.DisableFallback,
|
||||
}
|
||||
|
||||
if c.DisableCache {
|
||||
config.CacheStrategy = dns.CacheStrategy_Cache_DISABLE
|
||||
}
|
||||
|
||||
if c.ClientIP != nil {
|
||||
|
@ -145,6 +156,15 @@ func (c *DNSConfig) Build() (*dns.Config, error) {
|
|||
config.QueryStrategy = dns.QueryStrategy_USE_IP6
|
||||
}
|
||||
|
||||
switch strings.ToLower(c.CacheStrategy) {
|
||||
case "noerror":
|
||||
config.CacheStrategy = dns.CacheStrategy_Cache_NOERROR
|
||||
case "all":
|
||||
config.CacheStrategy = dns.CacheStrategy_Cache_ALL
|
||||
case "disable", "none":
|
||||
config.CacheStrategy = dns.CacheStrategy_Cache_DISABLE
|
||||
}
|
||||
|
||||
for _, server := range c.Servers {
|
||||
ns, err := server.Build()
|
||||
if err != nil {
|
||||
|
|
|
@ -70,6 +70,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": {
|
||||
|
@ -81,7 +82,8 @@ func TestDNSConfigParsing(t *testing.T) {
|
|||
},
|
||||
"clientIp": "10.0.0.1",
|
||||
"queryStrategy": "UseIPv4",
|
||||
"disableCache": true
|
||||
"disableCache": true,
|
||||
"disableFallback": true
|
||||
}`,
|
||||
Parser: parserCreator(),
|
||||
Output: &dns.Config{
|
||||
|
@ -96,7 +98,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: []*domain.Domain{
|
||||
{
|
||||
Type: domain.MatchingType_Subdomain,
|
||||
|
@ -138,9 +141,10 @@ func TestDNSConfigParsing(t *testing.T) {
|
|||
Ip: [][]byte{{8, 8, 4, 4}},
|
||||
},
|
||||
},
|
||||
ClientIp: []byte{10, 0, 0, 1},
|
||||
QueryStrategy: dns.QueryStrategy_USE_IP4,
|
||||
DisableCache: true,
|
||||
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