router_test.go 7.8 KB

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