github.com/imannamdari/v2ray-core/v5@v5.0.5/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/imannamdari/v2ray-core/v5/app/router" 12 "github.com/imannamdari/v2ray-core/v5/app/router/routercommon" 13 "github.com/imannamdari/v2ray-core/v5/common/net" 14 "github.com/imannamdari/v2ray-core/v5/common/serial" 15 "github.com/imannamdari/v2ray-core/v5/infra/conf/cfgcommon/testassist" 16 _ "github.com/imannamdari/v2ray-core/v5/infra/conf/geodata/memconservative" 17 _ "github.com/imannamdari/v2ray-core/v5/infra/conf/geodata/standard" 18 router2 "github.com/imannamdari/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 }, 117 { 118 Tag: "b2", 119 OutboundSelector: []string{"test"}, 120 Strategy: "leastload", 121 StrategySettings: serial.ToTypedMessage(&router.StrategyLeastLoadConfig{ 122 Costs: []*router.StrategyWeight{ 123 { 124 Regexp: true, 125 Match: "\\d+(\\.\\d+)", 126 Value: 5, 127 }, 128 }, 129 Baselines: []int64{ 130 int64(time.Duration(400) * time.Millisecond), 131 int64(time.Duration(600) * time.Millisecond), 132 }, 133 Expected: 6, 134 MaxRTT: int64(time.Duration(1000) * time.Millisecond), 135 Tolerance: 0.5, 136 }), 137 FallbackTag: "fall", 138 }, 139 }, 140 Rule: []*router.RoutingRule{ 141 { 142 Domain: []*routercommon.Domain{ 143 { 144 Type: routercommon.Domain_Plain, 145 Value: "baidu.com", 146 }, 147 { 148 Type: routercommon.Domain_Plain, 149 Value: "qq.com", 150 }, 151 }, 152 TargetTag: &router.RoutingRule_Tag{ 153 Tag: "direct", 154 }, 155 }, 156 { 157 Domain: []*routercommon.Domain{ 158 { 159 Type: routercommon.Domain_Plain, 160 Value: "v2fly.org", 161 }, 162 { 163 Type: routercommon.Domain_Plain, 164 Value: "github.com", 165 }, 166 }, 167 TargetTag: &router.RoutingRule_Tag{ 168 Tag: "direct", 169 }, 170 }, 171 { 172 Geoip: []*routercommon.GeoIP{ 173 { 174 Cidr: []*routercommon.CIDR{ 175 { 176 Ip: []byte{10, 0, 0, 0}, 177 Prefix: 8, 178 }, 179 { 180 Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 181 Prefix: 128, 182 }, 183 }, 184 }, 185 }, 186 TargetTag: &router.RoutingRule_Tag{ 187 Tag: "test", 188 }, 189 }, 190 { 191 PortList: &net.PortList{ 192 Range: []*net.PortRange{ 193 {From: 53, To: 53}, 194 {From: 443, To: 443}, 195 {From: 1000, To: 2000}, 196 }, 197 }, 198 TargetTag: &router.RoutingRule_Tag{ 199 Tag: "test", 200 }, 201 }, 202 { 203 PortList: &net.PortList{ 204 Range: []*net.PortRange{ 205 {From: 123, To: 123}, 206 }, 207 }, 208 TargetTag: &router.RoutingRule_Tag{ 209 Tag: "test", 210 }, 211 }, 212 }, 213 }, 214 }, 215 { 216 Input: `{ 217 "strategy": "rules", 218 "settings": { 219 "domainStrategy": "IPIfNonMatch", 220 "rules": [ 221 { 222 "type": "field", 223 "domain": [ 224 "baidu.com", 225 "qq.com" 226 ], 227 "outboundTag": "direct" 228 }, 229 { 230 "type": "field", 231 "domains": [ 232 "v2fly.org", 233 "github.com" 234 ], 235 "outboundTag": "direct" 236 }, 237 { 238 "type": "field", 239 "ip": [ 240 "10.0.0.0/8", 241 "::1/128" 242 ], 243 "outboundTag": "test" 244 } 245 ] 246 } 247 }`, 248 Parser: createParser(), 249 Output: &router.Config{ 250 DomainStrategy: router.DomainStrategy_IpIfNonMatch, 251 Rule: []*router.RoutingRule{ 252 { 253 Domain: []*routercommon.Domain{ 254 { 255 Type: routercommon.Domain_Plain, 256 Value: "baidu.com", 257 }, 258 { 259 Type: routercommon.Domain_Plain, 260 Value: "qq.com", 261 }, 262 }, 263 TargetTag: &router.RoutingRule_Tag{ 264 Tag: "direct", 265 }, 266 }, 267 { 268 Domain: []*routercommon.Domain{ 269 { 270 Type: routercommon.Domain_Plain, 271 Value: "v2fly.org", 272 }, 273 { 274 Type: routercommon.Domain_Plain, 275 Value: "github.com", 276 }, 277 }, 278 TargetTag: &router.RoutingRule_Tag{ 279 Tag: "direct", 280 }, 281 }, 282 { 283 Geoip: []*routercommon.GeoIP{ 284 { 285 Cidr: []*routercommon.CIDR{ 286 { 287 Ip: []byte{10, 0, 0, 0}, 288 Prefix: 8, 289 }, 290 { 291 Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 292 Prefix: 128, 293 }, 294 }, 295 }, 296 }, 297 TargetTag: &router.RoutingRule_Tag{ 298 Tag: "test", 299 }, 300 }, 301 }, 302 }, 303 }, 304 { 305 Input: `{ 306 "domainStrategy": "AsIs", 307 "rules": [ 308 { 309 "type": "field", 310 "domain": [ 311 "baidu.com", 312 "qq.com" 313 ], 314 "outboundTag": "direct" 315 }, 316 { 317 "type": "field", 318 "domains": [ 319 "v2fly.org", 320 "github.com" 321 ], 322 "outboundTag": "direct" 323 }, 324 { 325 "type": "field", 326 "ip": [ 327 "10.0.0.0/8", 328 "::1/128" 329 ], 330 "outboundTag": "test" 331 } 332 ] 333 }`, 334 Parser: createParser(), 335 Output: &router.Config{ 336 DomainStrategy: router.DomainStrategy_AsIs, 337 Rule: []*router.RoutingRule{ 338 { 339 Domain: []*routercommon.Domain{ 340 { 341 Type: routercommon.Domain_Plain, 342 Value: "baidu.com", 343 }, 344 { 345 Type: routercommon.Domain_Plain, 346 Value: "qq.com", 347 }, 348 }, 349 TargetTag: &router.RoutingRule_Tag{ 350 Tag: "direct", 351 }, 352 }, 353 { 354 Domain: []*routercommon.Domain{ 355 { 356 Type: routercommon.Domain_Plain, 357 Value: "v2fly.org", 358 }, 359 { 360 Type: routercommon.Domain_Plain, 361 Value: "github.com", 362 }, 363 }, 364 TargetTag: &router.RoutingRule_Tag{ 365 Tag: "direct", 366 }, 367 }, 368 { 369 Geoip: []*routercommon.GeoIP{ 370 { 371 Cidr: []*routercommon.CIDR{ 372 { 373 Ip: []byte{10, 0, 0, 0}, 374 Prefix: 8, 375 }, 376 { 377 Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 378 Prefix: 128, 379 }, 380 }, 381 }, 382 }, 383 TargetTag: &router.RoutingRule_Tag{ 384 Tag: "test", 385 }, 386 }, 387 }, 388 }, 389 }, 390 }) 391 }