router_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package conf_test
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/golang/protobuf/proto"
  6. "v2ray.com/core/app/router"
  7. . "v2ray.com/core/infra/conf"
  8. )
  9. func TestRouterConfig(t *testing.T) {
  10. createParser := func() func(string) (proto.Message, error) {
  11. return func(s string) (proto.Message, error) {
  12. config := new(RouterConfig)
  13. if err := json.Unmarshal([]byte(s), config); err != nil {
  14. return nil, err
  15. }
  16. return config.Build()
  17. }
  18. }
  19. runMultiTestCase(t, []TestCase{
  20. {
  21. Input: `{
  22. "strategy": "rules",
  23. "settings": {
  24. "domainStrategy": "AsIs",
  25. "rules": [
  26. {
  27. "type": "field",
  28. "domain": [
  29. "baidu.com",
  30. "qq.com"
  31. ],
  32. "outboundTag": "direct"
  33. },
  34. {
  35. "type": "field",
  36. "ip": [
  37. "10.0.0.0/8",
  38. "::1/128"
  39. ],
  40. "outboundTag": "test"
  41. }
  42. ]
  43. },
  44. "balancers": [
  45. {
  46. "tag": "b1",
  47. "selector": ["test"]
  48. }
  49. ]
  50. }`,
  51. Parser: createParser(),
  52. Output: &router.Config{
  53. DomainStrategy: router.Config_AsIs,
  54. BalancingRule: []*router.BalancingRule{
  55. {
  56. Tag: "b1",
  57. OutboundSelector: []string{"test"},
  58. },
  59. },
  60. Rule: []*router.RoutingRule{
  61. {
  62. Domain: []*router.Domain{
  63. {
  64. Type: router.Domain_Plain,
  65. Value: "baidu.com",
  66. },
  67. {
  68. Type: router.Domain_Plain,
  69. Value: "qq.com",
  70. },
  71. },
  72. TargetTag: &router.RoutingRule_Tag{
  73. Tag: "direct",
  74. },
  75. },
  76. {
  77. Geoip: []*router.GeoIP{
  78. {
  79. Cidr: []*router.CIDR{
  80. {
  81. Ip: []byte{10, 0, 0, 0},
  82. Prefix: 8,
  83. },
  84. {
  85. Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  86. Prefix: 128,
  87. },
  88. },
  89. },
  90. },
  91. TargetTag: &router.RoutingRule_Tag{
  92. Tag: "test",
  93. },
  94. },
  95. },
  96. },
  97. },
  98. {
  99. Input: `{
  100. "strategy": "rules",
  101. "settings": {
  102. "domainStrategy": "IPIfNonMatch",
  103. "rules": [
  104. {
  105. "type": "field",
  106. "domain": [
  107. "baidu.com",
  108. "qq.com"
  109. ],
  110. "outboundTag": "direct"
  111. },
  112. {
  113. "type": "field",
  114. "ip": [
  115. "10.0.0.0/8",
  116. "::1/128"
  117. ],
  118. "outboundTag": "test"
  119. }
  120. ]
  121. }
  122. }`,
  123. Parser: createParser(),
  124. Output: &router.Config{
  125. DomainStrategy: router.Config_IpIfNonMatch,
  126. Rule: []*router.RoutingRule{
  127. {
  128. Domain: []*router.Domain{
  129. {
  130. Type: router.Domain_Plain,
  131. Value: "baidu.com",
  132. },
  133. {
  134. Type: router.Domain_Plain,
  135. Value: "qq.com",
  136. },
  137. },
  138. TargetTag: &router.RoutingRule_Tag{
  139. Tag: "direct",
  140. },
  141. },
  142. {
  143. Geoip: []*router.GeoIP{
  144. {
  145. Cidr: []*router.CIDR{
  146. {
  147. Ip: []byte{10, 0, 0, 0},
  148. Prefix: 8,
  149. },
  150. {
  151. Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  152. Prefix: 128,
  153. },
  154. },
  155. },
  156. },
  157. TargetTag: &router.RoutingRule_Tag{
  158. Tag: "test",
  159. },
  160. },
  161. },
  162. },
  163. },
  164. {
  165. Input: `{
  166. "domainStrategy": "AsIs",
  167. "rules": [
  168. {
  169. "type": "field",
  170. "domain": [
  171. "baidu.com",
  172. "qq.com"
  173. ],
  174. "outboundTag": "direct"
  175. },
  176. {
  177. "type": "field",
  178. "ip": [
  179. "10.0.0.0/8",
  180. "::1/128"
  181. ],
  182. "outboundTag": "test"
  183. }
  184. ]
  185. }`,
  186. Parser: createParser(),
  187. Output: &router.Config{
  188. DomainStrategy: router.Config_AsIs,
  189. Rule: []*router.RoutingRule{
  190. {
  191. Domain: []*router.Domain{
  192. {
  193. Type: router.Domain_Plain,
  194. Value: "baidu.com",
  195. },
  196. {
  197. Type: router.Domain_Plain,
  198. Value: "qq.com",
  199. },
  200. },
  201. TargetTag: &router.RoutingRule_Tag{
  202. Tag: "direct",
  203. },
  204. },
  205. {
  206. Geoip: []*router.GeoIP{
  207. {
  208. Cidr: []*router.CIDR{
  209. {
  210. Ip: []byte{10, 0, 0, 0},
  211. Prefix: 8,
  212. },
  213. {
  214. Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  215. Prefix: 128,
  216. },
  217. },
  218. },
  219. },
  220. TargetTag: &router.RoutingRule_Tag{
  221. Tag: "test",
  222. },
  223. },
  224. },
  225. },
  226. },
  227. })
  228. }