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