github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/synthetic/router/router_test.go (about)

     1  package router_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  	_ "unsafe"
     8  
     9  	"github.com/golang/protobuf/proto"
    10  
    11  	"github.com/v2fly/v2ray-core/v5/app/router"
    12  	"github.com/v2fly/v2ray-core/v5/app/router/routercommon"
    13  	"github.com/v2fly/v2ray-core/v5/common/net"
    14  	"github.com/v2fly/v2ray-core/v5/common/serial"
    15  	"github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon/testassist"
    16  	_ "github.com/v2fly/v2ray-core/v5/infra/conf/geodata/memconservative"
    17  	_ "github.com/v2fly/v2ray-core/v5/infra/conf/geodata/standard"
    18  	router2 "github.com/v2fly/v2ray-core/v5/infra/conf/synthetic/router"
    19  )
    20  
    21  func TestRouterConfig(t *testing.T) {
    22  	createParser := func() func(string) (proto.Message, error) {
    23  		return func(s string) (proto.Message, error) {
    24  			config := new(router2.RouterConfig)
    25  			if err := json.Unmarshal([]byte(s), config); err != nil {
    26  				return nil, err
    27  			}
    28  			return config.Build()
    29  		}
    30  	}
    31  
    32  	testassist.RunMultiTestCase(t, []testassist.TestCase{
    33  		{
    34  			Input: `{
    35  				"strategy": "rules",
    36  				"settings": {
    37  					"domainStrategy": "AsIs",
    38  					"rules": [
    39  						{
    40  							"type": "field",
    41  							"domain": [
    42  								"baidu.com",
    43  								"qq.com"
    44  							],
    45  							"outboundTag": "direct"
    46  						},
    47  						{
    48  							"type": "field",
    49  							"domains": [
    50  								"v2fly.org",
    51  								"github.com"
    52  							],
    53  							"outboundTag": "direct"
    54  						},
    55  						{
    56  							"type": "field",
    57  							"ip": [
    58  								"10.0.0.0/8",
    59  								"::1/128"
    60  							],
    61  							"outboundTag": "test"
    62  						},{
    63  							"type": "field",
    64  							"port": "53, 443, 1000-2000",
    65  							"outboundTag": "test"
    66  						},{
    67  							"type": "field",
    68  							"port": 123,
    69  							"outboundTag": "test"
    70  						}
    71  					]
    72  				},
    73  				"balancers": [
    74  					{
    75  						"tag": "b1",
    76  						"selector": ["test"]
    77  					},
    78  					{
    79  						"tag": "b2",
    80  						"selector": ["test"],
    81  						"strategy": {
    82  							"type": "leastload",
    83  							"settings": {
    84  								"healthCheck": {
    85  									"interval": "5m0s",
    86  									"sampling": 2,
    87  									"timeout": "5s",
    88  									"destination": "dest",
    89  									"connectivity": "conn"
    90  								},
    91  								"costs": [
    92  									{
    93  										"regexp": true,
    94  										"match": "\\d+(\\.\\d+)",
    95  										"value": 5
    96  									}
    97  								],
    98  								"baselines": ["400ms", "600ms"],
    99  								"expected": 6,
   100  								"maxRTT": "1000ms",
   101  								"tolerance": 0.5
   102  							}
   103  						},
   104  						"fallbackTag": "fall"
   105  					}
   106  				]
   107  			}`,
   108  			Parser: createParser(),
   109  			Output: &router.Config{
   110  				DomainStrategy: router.DomainStrategy_AsIs,
   111  				BalancingRule: []*router.BalancingRule{
   112  					{
   113  						Tag:              "b1",
   114  						OutboundSelector: []string{"test"},
   115  						Strategy:         "random",
   116  						StrategySettings: serial.ToTypedMessage(&router.StrategyRandomConfig{}),
   117  					},
   118  					{
   119  						Tag:              "b2",
   120  						OutboundSelector: []string{"test"},
   121  						Strategy:         "leastload",
   122  						StrategySettings: serial.ToTypedMessage(&router.StrategyLeastLoadConfig{
   123  							Costs: []*router.StrategyWeight{
   124  								{
   125  									Regexp: true,
   126  									Match:  "\\d+(\\.\\d+)",
   127  									Value:  5,
   128  								},
   129  							},
   130  							Baselines: []int64{
   131  								int64(time.Duration(400) * time.Millisecond),
   132  								int64(time.Duration(600) * time.Millisecond),
   133  							},
   134  							Expected:  6,
   135  							MaxRTT:    int64(time.Duration(1000) * time.Millisecond),
   136  							Tolerance: 0.5,
   137  						}),
   138  						FallbackTag: "fall",
   139  					},
   140  				},
   141  				Rule: []*router.RoutingRule{
   142  					{
   143  						Domain: []*routercommon.Domain{
   144  							{
   145  								Type:  routercommon.Domain_Plain,
   146  								Value: "baidu.com",
   147  							},
   148  							{
   149  								Type:  routercommon.Domain_Plain,
   150  								Value: "qq.com",
   151  							},
   152  						},
   153  						TargetTag: &router.RoutingRule_Tag{
   154  							Tag: "direct",
   155  						},
   156  					},
   157  					{
   158  						Domain: []*routercommon.Domain{
   159  							{
   160  								Type:  routercommon.Domain_Plain,
   161  								Value: "v2fly.org",
   162  							},
   163  							{
   164  								Type:  routercommon.Domain_Plain,
   165  								Value: "github.com",
   166  							},
   167  						},
   168  						TargetTag: &router.RoutingRule_Tag{
   169  							Tag: "direct",
   170  						},
   171  					},
   172  					{
   173  						Geoip: []*routercommon.GeoIP{
   174  							{
   175  								Cidr: []*routercommon.CIDR{
   176  									{
   177  										Ip:     []byte{10, 0, 0, 0},
   178  										Prefix: 8,
   179  									},
   180  									{
   181  										Ip:     []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   182  										Prefix: 128,
   183  									},
   184  								},
   185  							},
   186  						},
   187  						TargetTag: &router.RoutingRule_Tag{
   188  							Tag: "test",
   189  						},
   190  					},
   191  					{
   192  						PortList: &net.PortList{
   193  							Range: []*net.PortRange{
   194  								{From: 53, To: 53},
   195  								{From: 443, To: 443},
   196  								{From: 1000, To: 2000},
   197  							},
   198  						},
   199  						TargetTag: &router.RoutingRule_Tag{
   200  							Tag: "test",
   201  						},
   202  					},
   203  					{
   204  						PortList: &net.PortList{
   205  							Range: []*net.PortRange{
   206  								{From: 123, To: 123},
   207  							},
   208  						},
   209  						TargetTag: &router.RoutingRule_Tag{
   210  							Tag: "test",
   211  						},
   212  					},
   213  				},
   214  			},
   215  		},
   216  		{
   217  			Input: `{
   218  				"strategy": "rules",
   219  				"settings": {
   220  					"domainStrategy": "IPIfNonMatch",
   221  					"rules": [
   222  						{
   223  							"type": "field",
   224  							"domain": [
   225  								"baidu.com",
   226  								"qq.com"
   227  							],
   228  							"outboundTag": "direct"
   229  						},
   230  						{
   231  							"type": "field",
   232  							"domains": [
   233  								"v2fly.org",
   234  								"github.com"
   235  							],
   236  							"outboundTag": "direct"
   237  						},
   238  						{
   239  							"type": "field",
   240  							"ip": [
   241  								"10.0.0.0/8",
   242  								"::1/128"
   243  							],
   244  							"outboundTag": "test"
   245  						}
   246  					]
   247  				}
   248  			}`,
   249  			Parser: createParser(),
   250  			Output: &router.Config{
   251  				DomainStrategy: router.DomainStrategy_IpIfNonMatch,
   252  				Rule: []*router.RoutingRule{
   253  					{
   254  						Domain: []*routercommon.Domain{
   255  							{
   256  								Type:  routercommon.Domain_Plain,
   257  								Value: "baidu.com",
   258  							},
   259  							{
   260  								Type:  routercommon.Domain_Plain,
   261  								Value: "qq.com",
   262  							},
   263  						},
   264  						TargetTag: &router.RoutingRule_Tag{
   265  							Tag: "direct",
   266  						},
   267  					},
   268  					{
   269  						Domain: []*routercommon.Domain{
   270  							{
   271  								Type:  routercommon.Domain_Plain,
   272  								Value: "v2fly.org",
   273  							},
   274  							{
   275  								Type:  routercommon.Domain_Plain,
   276  								Value: "github.com",
   277  							},
   278  						},
   279  						TargetTag: &router.RoutingRule_Tag{
   280  							Tag: "direct",
   281  						},
   282  					},
   283  					{
   284  						Geoip: []*routercommon.GeoIP{
   285  							{
   286  								Cidr: []*routercommon.CIDR{
   287  									{
   288  										Ip:     []byte{10, 0, 0, 0},
   289  										Prefix: 8,
   290  									},
   291  									{
   292  										Ip:     []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   293  										Prefix: 128,
   294  									},
   295  								},
   296  							},
   297  						},
   298  						TargetTag: &router.RoutingRule_Tag{
   299  							Tag: "test",
   300  						},
   301  					},
   302  				},
   303  			},
   304  		},
   305  		{
   306  			Input: `{
   307  				"domainStrategy": "AsIs",
   308  				"rules": [
   309  					{
   310  						"type": "field",
   311  						"domain": [
   312  							"baidu.com",
   313  							"qq.com"
   314  						],
   315  						"outboundTag": "direct"
   316  					},
   317  					{
   318  						"type": "field",
   319  						"domains": [
   320  							"v2fly.org",
   321  							"github.com"
   322  						],
   323  						"outboundTag": "direct"
   324  					},
   325  					{
   326  						"type": "field",
   327  						"ip": [
   328  							"10.0.0.0/8",
   329  							"::1/128"
   330  						],
   331  						"outboundTag": "test"
   332  					}
   333  				]
   334  			}`,
   335  			Parser: createParser(),
   336  			Output: &router.Config{
   337  				DomainStrategy: router.DomainStrategy_AsIs,
   338  				Rule: []*router.RoutingRule{
   339  					{
   340  						Domain: []*routercommon.Domain{
   341  							{
   342  								Type:  routercommon.Domain_Plain,
   343  								Value: "baidu.com",
   344  							},
   345  							{
   346  								Type:  routercommon.Domain_Plain,
   347  								Value: "qq.com",
   348  							},
   349  						},
   350  						TargetTag: &router.RoutingRule_Tag{
   351  							Tag: "direct",
   352  						},
   353  					},
   354  					{
   355  						Domain: []*routercommon.Domain{
   356  							{
   357  								Type:  routercommon.Domain_Plain,
   358  								Value: "v2fly.org",
   359  							},
   360  							{
   361  								Type:  routercommon.Domain_Plain,
   362  								Value: "github.com",
   363  							},
   364  						},
   365  						TargetTag: &router.RoutingRule_Tag{
   366  							Tag: "direct",
   367  						},
   368  					},
   369  					{
   370  						Geoip: []*routercommon.GeoIP{
   371  							{
   372  								Cidr: []*routercommon.CIDR{
   373  									{
   374  										Ip:     []byte{10, 0, 0, 0},
   375  										Prefix: 8,
   376  									},
   377  									{
   378  										Ip:     []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   379  										Prefix: 128,
   380  									},
   381  								},
   382  							},
   383  						},
   384  						TargetTag: &router.RoutingRule_Tag{
   385  							Tag: "test",
   386  						},
   387  					},
   388  				},
   389  			},
   390  		},
   391  	})
   392  }