github.com/xraypb/xray-core@v1.6.6/infra/conf/dns_test.go (about) 1 package conf_test 2 3 import ( 4 "encoding/json" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/golang/protobuf/proto" 10 "github.com/xraypb/xray-core/app/dns" 11 "github.com/xraypb/xray-core/app/router" 12 "github.com/xraypb/xray-core/common" 13 "github.com/xraypb/xray-core/common/net" 14 "github.com/xraypb/xray-core/common/platform" 15 "github.com/xraypb/xray-core/common/platform/filesystem" 16 . "github.com/xraypb/xray-core/infra/conf" 17 ) 18 19 func init() { 20 wd, err := os.Getwd() 21 common.Must(err) 22 23 if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) { 24 common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "resources", "geoip.dat"))) 25 } 26 27 geositeFilePath := filepath.Join(wd, "geosite.dat") 28 os.Setenv("xray.location.asset", wd) 29 geositeFile, err := os.OpenFile(geositeFilePath, os.O_CREATE|os.O_WRONLY, 0o600) 30 common.Must(err) 31 defer geositeFile.Close() 32 33 list := &router.GeoSiteList{ 34 Entry: []*router.GeoSite{ 35 { 36 CountryCode: "TEST", 37 Domain: []*router.Domain{ 38 {Type: router.Domain_Full, Value: "example.com"}, 39 }, 40 }, 41 }, 42 } 43 44 listBytes, err := proto.Marshal(list) 45 common.Must(err) 46 common.Must2(geositeFile.Write(listBytes)) 47 } 48 49 func TestDNSConfigParsing(t *testing.T) { 50 geositePath := platform.GetAssetLocation("geosite.dat") 51 defer func() { 52 os.Remove(geositePath) 53 os.Unsetenv("xray.location.asset") 54 }() 55 56 parserCreator := func() func(string) (proto.Message, error) { 57 return func(s string) (proto.Message, error) { 58 config := new(DNSConfig) 59 if err := json.Unmarshal([]byte(s), config); err != nil { 60 return nil, err 61 } 62 return config.Build() 63 } 64 } 65 66 runMultiTestCase(t, []TestCase{ 67 { 68 Input: `{ 69 "servers": [{ 70 "address": "8.8.8.8", 71 "port": 5353, 72 "skipFallback": true, 73 "domains": ["domain:example.com"] 74 }], 75 "hosts": { 76 "domain:example.com": "google.com", 77 "example.com": "127.0.0.1", 78 "keyword:google": ["8.8.8.8", "8.8.4.4"], 79 "regexp:.*\\.com": "8.8.4.4", 80 "www.example.org": ["127.0.0.1", "127.0.0.2"] 81 }, 82 "clientIp": "10.0.0.1", 83 "queryStrategy": "UseIPv4", 84 "disableCache": true, 85 "disableFallback": true 86 }`, 87 Parser: parserCreator(), 88 Output: &dns.Config{ 89 NameServer: []*dns.NameServer{ 90 { 91 Address: &net.Endpoint{ 92 Address: &net.IPOrDomain{ 93 Address: &net.IPOrDomain_Ip{ 94 Ip: []byte{8, 8, 8, 8}, 95 }, 96 }, 97 Network: net.Network_UDP, 98 Port: 5353, 99 }, 100 SkipFallback: true, 101 PrioritizedDomain: []*dns.NameServer_PriorityDomain{ 102 { 103 Type: dns.DomainMatchingType_Subdomain, 104 Domain: "example.com", 105 }, 106 }, 107 OriginalRules: []*dns.NameServer_OriginalRule{ 108 { 109 Rule: "domain:example.com", 110 Size: 1, 111 }, 112 }, 113 }, 114 }, 115 StaticHosts: []*dns.Config_HostMapping{ 116 { 117 Type: dns.DomainMatchingType_Subdomain, 118 Domain: "example.com", 119 ProxiedDomain: "google.com", 120 }, 121 { 122 Type: dns.DomainMatchingType_Full, 123 Domain: "example.com", 124 Ip: [][]byte{{127, 0, 0, 1}}, 125 }, 126 { 127 Type: dns.DomainMatchingType_Keyword, 128 Domain: "google", 129 Ip: [][]byte{{8, 8, 8, 8}, {8, 8, 4, 4}}, 130 }, 131 { 132 Type: dns.DomainMatchingType_Regex, 133 Domain: ".*\\.com", 134 Ip: [][]byte{{8, 8, 4, 4}}, 135 }, 136 { 137 Type: dns.DomainMatchingType_Full, 138 Domain: "www.example.org", 139 Ip: [][]byte{{127, 0, 0, 1}, {127, 0, 0, 2}}, 140 }, 141 }, 142 ClientIp: []byte{10, 0, 0, 1}, 143 QueryStrategy: dns.QueryStrategy_USE_IP4, 144 DisableCache: true, 145 DisableFallback: true, 146 }, 147 }, 148 }) 149 }