router_test.go 8.1 KB

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