router_test.go 4.6 KB

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