Xray-core/infra/conf/dns_test.go

158 lines
4 KiB
Go
Raw Normal View History

2020-11-25 11:01:53 +00:00
package conf_test
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/golang/protobuf/proto"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/app/dns"
"github.com/xtls/xray-core/common"
2021-03-24 15:01:20 +00:00
"github.com/xtls/xray-core/common/matcher/domain"
"github.com/xtls/xray-core/common/matcher/geosite"
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/platform"
"github.com/xtls/xray-core/common/platform/filesystem"
. "github.com/xtls/xray-core/infra/conf"
2020-11-25 11:01:53 +00:00
)
func init() {
wd, err := os.Getwd()
common.Must(err)
if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "resources", "geoip.dat")))
2020-11-25 11:01:53 +00:00
}
geositeFilePath := filepath.Join(wd, "geosite.dat")
os.Setenv("xray.location.asset", wd)
geositeFile, err := os.OpenFile(geositeFilePath, os.O_CREATE|os.O_WRONLY, 0600)
common.Must(err)
defer geositeFile.Close()
2021-03-24 15:01:20 +00:00
list := &geosite.GeoSiteList{
Entry: []*geosite.GeoSite{
2020-11-25 11:01:53 +00:00
{
CountryCode: "TEST",
2021-03-24 15:01:20 +00:00
Domain: []*geosite.Domain{
2021-03-27 09:52:15 +00:00
{Type: geosite.Domain_Full, Value: "example.com"},
2020-11-25 11:01:53 +00:00
},
},
},
}
listBytes, err := proto.Marshal(list)
common.Must(err)
common.Must2(geositeFile.Write(listBytes))
}
func TestDNSConfigParsing(t *testing.T) {
geositePath := platform.GetAssetLocation("geosite.dat")
defer func() {
os.Remove(geositePath)
os.Unsetenv("xray.location.asset")
}()
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",
2021-04-02 08:57:31 +00:00
"clientIp": "10.0.0.1",
2020-11-25 11:01:53 +00:00
"port": 5353,
"skipFallback": true,
2020-11-25 11:01:53 +00:00
"domains": ["domain:example.com"]
}],
"hosts": {
"example.com": "127.0.0.1",
"xtls.github.io": ["1.2.3.4", "5.6.7.8"],
2020-11-25 11:01:53 +00:00
"domain:example.com": "google.com",
"geosite:test": ["127.0.0.1", "127.0.0.2"],
"keyword:google": ["8.8.8.8", "8.8.4.4"],
2020-11-25 11:01:53 +00:00
"regexp:.*\\.com": "8.8.4.4"
},
2021-03-19 15:33:07 +00:00
"clientIp": "10.0.0.1",
"queryStrategy": "UseIPv4",
2021-04-10 09:22:44 +00:00
"cacheStrategy": "disable",
"disableFallback": true
2020-11-25 11:01:53 +00: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,
},
ClientIp: []byte{10, 0, 0, 1},
SkipFallback: true,
2021-03-24 15:01:20 +00:00
PrioritizedDomain: []*domain.Domain{
2020-11-25 11:01:53 +00:00
{
2021-03-24 15:01:20 +00:00
Type: domain.MatchingType_Subdomain,
Value: "example.com",
2020-11-25 11:01:53 +00:00
},
},
OriginalRules: []*dns.NameServer_OriginalRule{
{
Rule: "domain:example.com",
Size: 1,
},
},
},
},
StaticHosts: []*dns.Config_HostMapping{
{
2021-03-24 15:01:20 +00:00
Type: domain.MatchingType_Subdomain,
2020-11-25 11:01:53 +00:00
Domain: "example.com",
ProxiedDomain: "google.com",
},
{
2021-03-24 15:01:20 +00:00
Type: domain.MatchingType_Full,
Domain: "example.com",
Ip: [][]byte{{127, 0, 0, 1}},
},
2020-11-25 11:01:53 +00:00
{
2021-03-24 15:01:20 +00:00
Type: domain.MatchingType_Full,
2020-11-25 11:01:53 +00:00
Domain: "example.com",
Ip: [][]byte{{127, 0, 0, 1}, {127, 0, 0, 2}},
2020-11-25 11:01:53 +00:00
},
{
2021-03-24 15:01:20 +00:00
Type: domain.MatchingType_Keyword,
2020-11-25 11:01:53 +00:00
Domain: "google",
Ip: [][]byte{{8, 8, 8, 8}, {8, 8, 4, 4}},
2020-11-25 11:01:53 +00:00
},
{
2021-03-24 15:01:20 +00:00
Type: domain.MatchingType_Regex,
2020-11-25 11:01:53 +00:00
Domain: ".*\\.com",
Ip: [][]byte{{8, 8, 4, 4}},
},
{
2021-04-14 05:04:07 +00:00
Type: domain.MatchingType_Full,
Domain: "xtls.github.io",
Ip: [][]byte{{1, 2, 3, 4}, {5, 6, 7, 8}},
},
2020-11-25 11:01:53 +00:00
},
ClientIp: []byte{10, 0, 0, 1},
QueryStrategy: dns.QueryStrategy_USE_IP4,
CacheStrategy: dns.CacheStrategy_Cache_DISABLE,
DisableFallback: true,
2020-11-25 11:01:53 +00:00
},
},
})
}