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