github.com/eagleql/xray-core@v1.4.4/infra/conf/router_test.go (about)

     1  package conf_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/golang/protobuf/proto"
     8  
     9  	"github.com/eagleql/xray-core/app/router"
    10  	"github.com/eagleql/xray-core/common/net"
    11  	. "github.com/eagleql/xray-core/infra/conf"
    12  )
    13  
    14  func TestRouterConfig(t *testing.T) {
    15  	createParser := func() func(string) (proto.Message, error) {
    16  		return func(s string) (proto.Message, error) {
    17  			config := new(RouterConfig)
    18  			if err := json.Unmarshal([]byte(s), config); err != nil {
    19  				return nil, err
    20  			}
    21  			return config.Build()
    22  		}
    23  	}
    24  
    25  	runMultiTestCase(t, []TestCase{
    26  		{
    27  			Input: `{
    28  				"strategy": "rules",
    29  				"settings": {
    30  					"domainStrategy": "AsIs",
    31  					"rules": [
    32  						{
    33  							"type": "field",
    34  							"domain": [
    35  								"baidu.com",
    36  								"qq.com"
    37  							],
    38  							"outboundTag": "direct"
    39  						},
    40  						{
    41  							"type": "field",
    42  							"ip": [
    43  								"10.0.0.0/8",
    44  								"::1/128"
    45  							],
    46  							"outboundTag": "test"
    47  						},{
    48  							"type": "field",
    49  							"port": "53, 443, 1000-2000",
    50  							"outboundTag": "test"
    51  						},{
    52  							"type": "field",
    53  							"port": 123,
    54  							"outboundTag": "test"
    55  						}
    56  					]
    57  				},
    58  				"balancers": [
    59  					{
    60  						"tag": "b1",
    61  						"selector": ["test"]
    62  					}
    63  				]
    64  			}`,
    65  			Parser: createParser(),
    66  			Output: &router.Config{
    67  				DomainStrategy: router.Config_AsIs,
    68  				BalancingRule: []*router.BalancingRule{
    69  					{
    70  						Tag:              "b1",
    71  						OutboundSelector: []string{"test"},
    72  					},
    73  				},
    74  				Rule: []*router.RoutingRule{
    75  					{
    76  						Domain: []*router.Domain{
    77  							{
    78  								Type:  router.Domain_Plain,
    79  								Value: "baidu.com",
    80  							},
    81  							{
    82  								Type:  router.Domain_Plain,
    83  								Value: "qq.com",
    84  							},
    85  						},
    86  						TargetTag: &router.RoutingRule_Tag{
    87  							Tag: "direct",
    88  						},
    89  					},
    90  					{
    91  						Geoip: []*router.GeoIP{
    92  							{
    93  								Cidr: []*router.CIDR{
    94  									{
    95  										Ip:     []byte{10, 0, 0, 0},
    96  										Prefix: 8,
    97  									},
    98  									{
    99  										Ip:     []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   100  										Prefix: 128,
   101  									},
   102  								},
   103  							},
   104  						},
   105  						TargetTag: &router.RoutingRule_Tag{
   106  							Tag: "test",
   107  						},
   108  					},
   109  					{
   110  						PortList: &net.PortList{
   111  							Range: []*net.PortRange{
   112  								{From: 53, To: 53},
   113  								{From: 443, To: 443},
   114  								{From: 1000, To: 2000},
   115  							},
   116  						},
   117  						TargetTag: &router.RoutingRule_Tag{
   118  							Tag: "test",
   119  						},
   120  					},
   121  					{
   122  						PortList: &net.PortList{
   123  							Range: []*net.PortRange{
   124  								{From: 123, To: 123},
   125  							},
   126  						},
   127  						TargetTag: &router.RoutingRule_Tag{
   128  							Tag: "test",
   129  						},
   130  					},
   131  				},
   132  			},
   133  		},
   134  		{
   135  			Input: `{
   136  				"strategy": "rules",
   137  				"settings": {
   138  					"domainStrategy": "IPIfNonMatch",
   139  					"rules": [
   140  						{
   141  							"type": "field",
   142  							"domain": [
   143  								"baidu.com",
   144  								"qq.com"
   145  							],
   146  							"outboundTag": "direct"
   147  						},
   148  						{
   149  							"type": "field",
   150  							"ip": [
   151  								"10.0.0.0/8",
   152  								"::1/128"
   153  							],
   154  							"outboundTag": "test"
   155  						}
   156  					]
   157  				}
   158  			}`,
   159  			Parser: createParser(),
   160  			Output: &router.Config{
   161  				DomainStrategy: router.Config_IpIfNonMatch,
   162  				Rule: []*router.RoutingRule{
   163  					{
   164  						Domain: []*router.Domain{
   165  							{
   166  								Type:  router.Domain_Plain,
   167  								Value: "baidu.com",
   168  							},
   169  							{
   170  								Type:  router.Domain_Plain,
   171  								Value: "qq.com",
   172  							},
   173  						},
   174  						TargetTag: &router.RoutingRule_Tag{
   175  							Tag: "direct",
   176  						},
   177  					},
   178  					{
   179  						Geoip: []*router.GeoIP{
   180  							{
   181  								Cidr: []*router.CIDR{
   182  									{
   183  										Ip:     []byte{10, 0, 0, 0},
   184  										Prefix: 8,
   185  									},
   186  									{
   187  										Ip:     []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   188  										Prefix: 128,
   189  									},
   190  								},
   191  							},
   192  						},
   193  						TargetTag: &router.RoutingRule_Tag{
   194  							Tag: "test",
   195  						},
   196  					},
   197  				},
   198  			},
   199  		},
   200  		{
   201  			Input: `{
   202  				"domainStrategy": "AsIs",
   203  				"rules": [
   204  					{
   205  						"type": "field",
   206  						"domain": [
   207  							"baidu.com",
   208  							"qq.com"
   209  						],
   210  						"outboundTag": "direct"
   211  					},
   212  					{
   213  						"type": "field",
   214  						"ip": [
   215  							"10.0.0.0/8",
   216  							"::1/128"
   217  						],
   218  						"outboundTag": "test"
   219  					}
   220  				]
   221  			}`,
   222  			Parser: createParser(),
   223  			Output: &router.Config{
   224  				DomainStrategy: router.Config_AsIs,
   225  				Rule: []*router.RoutingRule{
   226  					{
   227  						Domain: []*router.Domain{
   228  							{
   229  								Type:  router.Domain_Plain,
   230  								Value: "baidu.com",
   231  							},
   232  							{
   233  								Type:  router.Domain_Plain,
   234  								Value: "qq.com",
   235  							},
   236  						},
   237  						TargetTag: &router.RoutingRule_Tag{
   238  							Tag: "direct",
   239  						},
   240  					},
   241  					{
   242  						Geoip: []*router.GeoIP{
   243  							{
   244  								Cidr: []*router.CIDR{
   245  									{
   246  										Ip:     []byte{10, 0, 0, 0},
   247  										Prefix: 8,
   248  									},
   249  									{
   250  										Ip:     []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   251  										Prefix: 128,
   252  									},
   253  								},
   254  							},
   255  						},
   256  						TargetTag: &router.RoutingRule_Tag{
   257  							Tag: "test",
   258  						},
   259  					},
   260  				},
   261  			},
   262  		},
   263  	})
   264  }