router_test.go 8.0 KB

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