2020-11-25 13:01:53 +02:00
|
|
|
package conf_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/app/dns"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
. "github.com/xtls/xray-core/infra/conf"
|
2023-08-10 07:43:34 +03:00
|
|
|
"google.golang.org/protobuf/proto"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDNSConfigParsing(t *testing.T) {
|
|
|
|
parserCreator := func() func(string) (proto.Message, error) {
|
|
|
|
return func(s string) (proto.Message, error) {
|
|
|
|
config := new(DNSConfig)
|
|
|
|
if err := json.Unmarshal([]byte(s), config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config.Build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runMultiTestCase(t, []TestCase{
|
|
|
|
{
|
|
|
|
Input: `{
|
|
|
|
"servers": [{
|
|
|
|
"address": "8.8.8.8",
|
|
|
|
"port": 5353,
|
2021-10-16 16:02:51 +03:00
|
|
|
"skipFallback": true,
|
2020-11-25 13:01:53 +02:00
|
|
|
"domains": ["domain:example.com"]
|
|
|
|
}],
|
|
|
|
"hosts": {
|
|
|
|
"domain:example.com": "google.com",
|
2021-10-16 16:02:51 +03:00
|
|
|
"example.com": "127.0.0.1",
|
|
|
|
"keyword:google": ["8.8.8.8", "8.8.4.4"],
|
|
|
|
"regexp:.*\\.com": "8.8.4.4",
|
|
|
|
"www.example.org": ["127.0.0.1", "127.0.0.2"]
|
2020-11-25 13:01:53 +02:00
|
|
|
},
|
2021-10-16 16:02:51 +03:00
|
|
|
"clientIp": "10.0.0.1",
|
|
|
|
"queryStrategy": "UseIPv4",
|
|
|
|
"disableCache": true,
|
|
|
|
"disableFallback": true
|
2020-11-25 13:01:53 +02:00
|
|
|
}`,
|
|
|
|
Parser: parserCreator(),
|
|
|
|
Output: &dns.Config{
|
|
|
|
NameServer: []*dns.NameServer{
|
|
|
|
{
|
|
|
|
Address: &net.Endpoint{
|
|
|
|
Address: &net.IPOrDomain{
|
|
|
|
Address: &net.IPOrDomain_Ip{
|
|
|
|
Ip: []byte{8, 8, 8, 8},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Network: net.Network_UDP,
|
|
|
|
Port: 5353,
|
|
|
|
},
|
2021-10-16 16:02:51 +03:00
|
|
|
SkipFallback: true,
|
2020-11-25 13:01:53 +02:00
|
|
|
PrioritizedDomain: []*dns.NameServer_PriorityDomain{
|
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Subdomain,
|
|
|
|
Domain: "example.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
OriginalRules: []*dns.NameServer_OriginalRule{
|
|
|
|
{
|
|
|
|
Rule: "domain:example.com",
|
|
|
|
Size: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
StaticHosts: []*dns.Config_HostMapping{
|
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Subdomain,
|
|
|
|
Domain: "example.com",
|
|
|
|
ProxiedDomain: "google.com",
|
|
|
|
},
|
2021-01-30 15:01:20 +02:00
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Full,
|
|
|
|
Domain: "example.com",
|
|
|
|
Ip: [][]byte{{127, 0, 0, 1}},
|
|
|
|
},
|
2020-11-25 13:01:53 +02:00
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Keyword,
|
|
|
|
Domain: "google",
|
2021-10-16 16:02:51 +03:00
|
|
|
Ip: [][]byte{{8, 8, 8, 8}, {8, 8, 4, 4}},
|
2020-11-25 13:01:53 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Regex,
|
|
|
|
Domain: ".*\\.com",
|
|
|
|
Ip: [][]byte{{8, 8, 4, 4}},
|
|
|
|
},
|
2021-10-16 16:02:51 +03:00
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Full,
|
|
|
|
Domain: "www.example.org",
|
|
|
|
Ip: [][]byte{{127, 0, 0, 1}, {127, 0, 0, 2}},
|
|
|
|
},
|
2020-11-25 13:01:53 +02:00
|
|
|
},
|
2021-10-16 16:02:51 +03:00
|
|
|
ClientIp: []byte{10, 0, 0, 1},
|
|
|
|
QueryStrategy: dns.QueryStrategy_USE_IP4,
|
|
|
|
DisableCache: true,
|
|
|
|
DisableFallback: true,
|
2020-11-25 13:01:53 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|