2021-06-19 08:41:32 -04:00
|
|
|
package dns_test
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-04-06 15:08:03 -04:00
|
|
|
"errors"
|
2021-06-19 08:28:20 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/testassist"
|
2021-04-11 22:01:30 -04:00
|
|
|
"io/fs"
|
2019-02-10 13:04:11 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
"google.golang.org/protobuf/runtime/protoiface"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/dns"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
|
2021-05-03 22:15:11 -04:00
|
|
|
_ "github.com/v2fly/v2ray-core/v4/infra/conf/geodata/standard"
|
2021-06-19 08:41:32 -04:00
|
|
|
|
|
|
|
dns2 "github.com/v2fly/v2ray-core/v4/infra/conf/synthetic/dns"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-05-04 09:51:38 -04:00
|
|
|
const (
|
|
|
|
geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
|
|
|
|
geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
|
|
|
|
)
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-06-19 08:41:32 -04:00
|
|
|
tempPath := filepath.Join(wd, "..", "..", "..", "..", "testing", "temp")
|
2021-05-04 09:51:38 -04:00
|
|
|
geoipPath := filepath.Join(tempPath, "geoip.dat")
|
|
|
|
geositePath := filepath.Join(tempPath, "geosite.dat")
|
2019-02-26 15:04:00 -05:00
|
|
|
|
2021-05-04 09:51:38 -04:00
|
|
|
os.Setenv("v2ray.location.asset", tempPath)
|
2021-05-04 14:07:35 -04:00
|
|
|
|
2021-05-04 14:13:52 -04:00
|
|
|
if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
|
2021-05-19 17:28:52 -04:00
|
|
|
common.Must(os.MkdirAll(tempPath, 0o755))
|
2021-05-04 14:13:52 -04:00
|
|
|
geoipBytes, err := common.FetchHTTPContent(geoipURL)
|
|
|
|
common.Must(err)
|
|
|
|
common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) {
|
2021-05-19 17:28:52 -04:00
|
|
|
common.Must(os.MkdirAll(tempPath, 0o755))
|
2021-05-04 14:13:52 -04:00
|
|
|
geositeBytes, err := common.FetchHTTPContent(geositeURL)
|
|
|
|
common.Must(err)
|
|
|
|
common.Must(filesystem.WriteFile(geositePath, geositeBytes))
|
2021-04-06 15:08:03 -04:00
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
2020-12-11 18:15:29 -05:00
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func TestDNSConfigParsing(t *testing.T) {
|
2021-04-06 15:08:03 -04:00
|
|
|
parserCreator := func() func(string) (protoiface.MessageV1, error) {
|
|
|
|
return func(s string) (protoiface.MessageV1, error) {
|
2021-06-19 08:41:32 -04:00
|
|
|
config := new(dns2.DNSConfig)
|
2019-02-10 13:04:11 -05:00
|
|
|
if err := json.Unmarshal([]byte(s), config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config.Build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 08:28:20 -04:00
|
|
|
testassist.RunMultiTestCase(t, []testassist.TestCase{
|
2019-02-10 13:04:11 -05:00
|
|
|
{
|
|
|
|
Input: `{
|
|
|
|
"servers": [{
|
|
|
|
"address": "8.8.8.8",
|
2020-12-11 18:15:29 -05:00
|
|
|
"clientIp": "10.0.0.1",
|
2019-02-10 13:04:11 -05:00
|
|
|
"port": 5353,
|
2021-04-08 22:35:26 -04:00
|
|
|
"skipFallback": true,
|
2021-02-16 15:31:50 -05:00
|
|
|
"domains": ["domain:v2fly.org"]
|
2019-02-10 13:04:11 -05:00
|
|
|
}],
|
|
|
|
"hosts": {
|
2021-02-16 15:31:50 -05:00
|
|
|
"v2fly.org": "127.0.0.1",
|
2021-04-10 09:56:08 -04:00
|
|
|
"www.v2fly.org": ["1.2.3.4", "5.6.7.8"],
|
2019-02-26 15:04:00 -05:00
|
|
|
"domain:example.com": "google.com",
|
2021-04-10 09:56:08 -04:00
|
|
|
"geosite:test": ["127.0.0.1", "127.0.0.2"],
|
|
|
|
"keyword:google": ["8.8.8.8", "8.8.4.4"],
|
2019-02-26 15:04:00 -05:00
|
|
|
"regexp:.*\\.com": "8.8.4.4"
|
2019-02-10 13:04:11 -05:00
|
|
|
},
|
2021-03-19 03:55:18 -04:00
|
|
|
"clientIp": "10.0.0.1",
|
|
|
|
"queryStrategy": "UseIPv4",
|
2021-04-08 22:35:26 -04:00
|
|
|
"disableCache": true,
|
|
|
|
"disableFallback": true
|
2019-02-10 13:04:11 -05: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-04-08 22:35:26 -04:00
|
|
|
ClientIp: []byte{10, 0, 0, 1},
|
|
|
|
SkipFallback: true,
|
2019-02-10 13:04:11 -05:00
|
|
|
PrioritizedDomain: []*dns.NameServer_PriorityDomain{
|
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Subdomain,
|
2021-02-16 15:31:50 -05:00
|
|
|
Domain: "v2fly.org",
|
2019-02-10 13:04:11 -05:00
|
|
|
},
|
|
|
|
},
|
2020-08-15 09:22:32 -04:00
|
|
|
OriginalRules: []*dns.NameServer_OriginalRule{
|
|
|
|
{
|
2021-02-16 15:31:50 -05:00
|
|
|
Rule: "domain:v2fly.org",
|
2020-08-15 09:22:32 -04:00
|
|
|
Size: 1,
|
|
|
|
},
|
|
|
|
},
|
2019-02-10 13:04:11 -05:00
|
|
|
},
|
|
|
|
},
|
2021-09-07 08:43:28 -04:00
|
|
|
StaticHosts: []*dns.HostMapping{
|
2019-02-10 13:04:11 -05:00
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Subdomain,
|
|
|
|
Domain: "example.com",
|
|
|
|
ProxiedDomain: "google.com",
|
|
|
|
},
|
2019-02-26 15:04:00 -05:00
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Full,
|
2021-04-06 15:08:03 -04:00
|
|
|
Domain: "test.example.com",
|
2021-04-10 09:56:08 -04:00
|
|
|
Ip: [][]byte{{127, 0, 0, 1}, {127, 0, 0, 2}},
|
2019-02-26 15:04:00 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Keyword,
|
|
|
|
Domain: "google",
|
2021-04-10 09:56:08 -04:00
|
|
|
Ip: [][]byte{{8, 8, 8, 8}, {8, 8, 4, 4}},
|
2019-02-26 15:04:00 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Regex,
|
|
|
|
Domain: ".*\\.com",
|
|
|
|
Ip: [][]byte{{8, 8, 4, 4}},
|
|
|
|
},
|
2019-02-10 13:04:11 -05:00
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Full,
|
2021-02-16 15:31:50 -05:00
|
|
|
Domain: "v2fly.org",
|
2019-02-10 13:04:11 -05:00
|
|
|
Ip: [][]byte{{127, 0, 0, 1}},
|
|
|
|
},
|
2021-04-10 09:56:08 -04:00
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Full,
|
|
|
|
Domain: "www.v2fly.org",
|
|
|
|
Ip: [][]byte{{1, 2, 3, 4}, {5, 6, 7, 8}},
|
|
|
|
},
|
2019-02-10 13:04:11 -05:00
|
|
|
},
|
2021-04-08 22:35:26 -04:00
|
|
|
ClientIp: []byte{10, 0, 0, 1},
|
|
|
|
QueryStrategy: dns.QueryStrategy_USE_IP4,
|
|
|
|
DisableCache: true,
|
|
|
|
DisableFallback: true,
|
2019-02-10 13:04:11 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|