github.com/eagleql/xray-core@v1.4.4/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/eagleql/xray-core/app/dns"
    10  	"github.com/eagleql/xray-core/app/router"
    11  	"github.com/eagleql/xray-core/common"
    12  	"github.com/eagleql/xray-core/common/net"
    13  	"github.com/eagleql/xray-core/common/platform"
    14  	"github.com/eagleql/xray-core/common/platform/filesystem"
    15  	. "github.com/eagleql/xray-core/infra/conf"
    16  	"github.com/golang/protobuf/proto"
    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, 0600)
    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  func TestDNSConfigParsing(t *testing.T) {
    49  	geositePath := platform.GetAssetLocation("geosite.dat")
    50  	defer func() {
    51  		os.Remove(geositePath)
    52  		os.Unsetenv("xray.location.asset")
    53  	}()
    54  
    55  	parserCreator := func() func(string) (proto.Message, error) {
    56  		return func(s string) (proto.Message, error) {
    57  			config := new(DNSConfig)
    58  			if err := json.Unmarshal([]byte(s), config); err != nil {
    59  				return nil, err
    60  			}
    61  			return config.Build()
    62  		}
    63  	}
    64  
    65  	runMultiTestCase(t, []TestCase{
    66  		{
    67  			Input: `{
    68  				"servers": [{
    69  					"address": "8.8.8.8",
    70  					"port": 5353,
    71  					"domains": ["domain:example.com"]
    72  				}],
    73  				"hosts": {
    74  					"example.com": "127.0.0.1",
    75  					"domain:example.com": "google.com",
    76  					"geosite:test": "10.0.0.1",
    77  					"keyword:google": "8.8.8.8",
    78  					"regexp:.*\\.com": "8.8.4.4"
    79  				},
    80  				"clientIp": "10.0.0.1"
    81  			}`,
    82  			Parser: parserCreator(),
    83  			Output: &dns.Config{
    84  				NameServer: []*dns.NameServer{
    85  					{
    86  						Address: &net.Endpoint{
    87  							Address: &net.IPOrDomain{
    88  								Address: &net.IPOrDomain_Ip{
    89  									Ip: []byte{8, 8, 8, 8},
    90  								},
    91  							},
    92  							Network: net.Network_UDP,
    93  							Port:    5353,
    94  						},
    95  						PrioritizedDomain: []*dns.NameServer_PriorityDomain{
    96  							{
    97  								Type:   dns.DomainMatchingType_Subdomain,
    98  								Domain: "example.com",
    99  							},
   100  						},
   101  						OriginalRules: []*dns.NameServer_OriginalRule{
   102  							{
   103  								Rule: "domain:example.com",
   104  								Size: 1,
   105  							},
   106  						},
   107  					},
   108  				},
   109  				StaticHosts: []*dns.Config_HostMapping{
   110  					{
   111  						Type:          dns.DomainMatchingType_Subdomain,
   112  						Domain:        "example.com",
   113  						ProxiedDomain: "google.com",
   114  					},
   115  					{
   116  						Type:   dns.DomainMatchingType_Full,
   117  						Domain: "example.com",
   118  						Ip:     [][]byte{{127, 0, 0, 1}},
   119  					},
   120  					{
   121  						Type:   dns.DomainMatchingType_Full,
   122  						Domain: "example.com",
   123  						Ip:     [][]byte{{10, 0, 0, 1}},
   124  					},
   125  					{
   126  						Type:   dns.DomainMatchingType_Keyword,
   127  						Domain: "google",
   128  						Ip:     [][]byte{{8, 8, 8, 8}},
   129  					},
   130  					{
   131  						Type:   dns.DomainMatchingType_Regex,
   132  						Domain: ".*\\.com",
   133  						Ip:     [][]byte{{8, 8, 4, 4}},
   134  					},
   135  				},
   136  				ClientIp: []byte{10, 0, 0, 1},
   137  			},
   138  		},
   139  	})
   140  }