2019-02-10 13:04:11 -05:00
|
|
|
package conf_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/dns"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/app/router"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
|
|
|
|
. "github.com/v2fly/v2ray-core/v4/infra/conf"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
common.Must(err)
|
|
|
|
|
2020-09-03 04:47:40 -04:00
|
|
|
if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
|
|
|
|
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
|
2020-09-03 04:00:24 -04:00
|
|
|
}
|
2019-02-26 15:04:00 -05:00
|
|
|
|
2020-09-03 04:47:40 -04:00
|
|
|
geositeFilePath := filepath.Join(wd, "geosite.dat")
|
|
|
|
os.Setenv("v2ray.location.asset", wd)
|
2019-02-26 15:04:00 -05:00
|
|
|
geositeFile, err := os.OpenFile(geositeFilePath, os.O_CREATE|os.O_WRONLY, 0600)
|
|
|
|
common.Must(err)
|
|
|
|
defer geositeFile.Close()
|
|
|
|
|
|
|
|
list := &router.GeoSiteList{
|
|
|
|
Entry: []*router.GeoSite{
|
|
|
|
{
|
|
|
|
CountryCode: "TEST",
|
|
|
|
Domain: []*router.Domain{
|
|
|
|
{Type: router.Domain_Full, Value: "example.com"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
listBytes, err := proto.Marshal(list)
|
|
|
|
common.Must(err)
|
|
|
|
common.Must2(geositeFile.Write(listBytes))
|
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) {
|
2019-02-10 13:04:11 -05:00
|
|
|
geositePath := platform.GetAssetLocation("geosite.dat")
|
|
|
|
defer func() {
|
|
|
|
os.Remove(geositePath)
|
2020-09-03 04:47:40 -04:00
|
|
|
os.Unsetenv("v2ray.location.asset")
|
2019-02-10 13:04:11 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
parserCreator := func() func(string) (proto.Message, error) {
|
|
|
|
return func(s string) (proto.Message, error) {
|
2020-10-11 07:22:46 -04:00
|
|
|
config := new(DNSConfig)
|
2019-02-10 13:04:11 -05:00
|
|
|
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",
|
2020-12-11 18:15:29 -05:00
|
|
|
"clientIp": "10.0.0.1",
|
2019-02-10 13:04:11 -05:00
|
|
|
"port": 5353,
|
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",
|
2019-02-26 15:04:00 -05:00
|
|
|
"domain:example.com": "google.com",
|
|
|
|
"geosite:test": "10.0.0.1",
|
|
|
|
"keyword:google": "8.8.8.8",
|
|
|
|
"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",
|
|
|
|
"disableCache": 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,
|
|
|
|
},
|
2020-12-11 18:15:29 -05:00
|
|
|
ClientIp: []byte{10, 0, 0, 1},
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
StaticHosts: []*dns.Config_HostMapping{
|
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Subdomain,
|
|
|
|
Domain: "example.com",
|
|
|
|
ProxiedDomain: "google.com",
|
|
|
|
},
|
2019-02-26 15:04:00 -05:00
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Full,
|
|
|
|
Domain: "example.com",
|
|
|
|
Ip: [][]byte{{10, 0, 0, 1}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: dns.DomainMatchingType_Keyword,
|
|
|
|
Domain: "google",
|
|
|
|
Ip: [][]byte{{8, 8, 8, 8}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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-03-19 03:55:18 -04:00
|
|
|
ClientIp: []byte{10, 0, 0, 1},
|
|
|
|
QueryStrategy: dns.QueryStrategy_USE_IP4,
|
|
|
|
DisableCache: true,
|
2019-02-10 13:04:11 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|