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