condition_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package router_test
  2. import (
  3. "context"
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "testing"
  8. proto "github.com/golang/protobuf/proto"
  9. "v2ray.com/core/app/dispatcher"
  10. . "v2ray.com/core/app/router"
  11. "v2ray.com/core/common"
  12. "v2ray.com/core/common/errors"
  13. "v2ray.com/core/common/net"
  14. "v2ray.com/core/common/platform"
  15. "v2ray.com/core/common/protocol"
  16. "v2ray.com/core/common/protocol/http"
  17. "v2ray.com/core/common/session"
  18. . "v2ray.com/ext/assert"
  19. "v2ray.com/ext/sysio"
  20. )
  21. func withOutbound(outbound *session.Outbound) context.Context {
  22. return session.ContextWithOutbound(context.Background(), outbound)
  23. }
  24. func withInbound(inbound *session.Inbound) context.Context {
  25. return session.ContextWithInbound(context.Background(), inbound)
  26. }
  27. func TestRoutingRule(t *testing.T) {
  28. type ruleTest struct {
  29. input context.Context
  30. output bool
  31. }
  32. cases := []struct {
  33. rule *RoutingRule
  34. test []ruleTest
  35. }{
  36. {
  37. rule: &RoutingRule{
  38. Domain: []*Domain{
  39. {
  40. Value: "v2ray.com",
  41. Type: Domain_Plain,
  42. },
  43. {
  44. Value: "google.com",
  45. Type: Domain_Domain,
  46. },
  47. {
  48. Value: "^facebook\\.com$",
  49. Type: Domain_Regex,
  50. },
  51. },
  52. },
  53. test: []ruleTest{
  54. {
  55. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)}),
  56. output: true,
  57. },
  58. {
  59. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.v2ray.com.www"), 80)}),
  60. output: true,
  61. },
  62. {
  63. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.co"), 80)}),
  64. output: false,
  65. },
  66. {
  67. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.google.com"), 80)}),
  68. output: true,
  69. },
  70. {
  71. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("facebook.com"), 80)}),
  72. output: true,
  73. },
  74. {
  75. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.facebook.com"), 80)}),
  76. output: false,
  77. },
  78. {
  79. input: context.Background(),
  80. output: false,
  81. },
  82. },
  83. },
  84. {
  85. rule: &RoutingRule{
  86. Cidr: []*CIDR{
  87. {
  88. Ip: []byte{8, 8, 8, 8},
  89. Prefix: 32,
  90. },
  91. {
  92. Ip: []byte{8, 8, 8, 8},
  93. Prefix: 32,
  94. },
  95. {
  96. Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
  97. Prefix: 128,
  98. },
  99. },
  100. },
  101. test: []ruleTest{
  102. {
  103. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)}),
  104. output: true,
  105. },
  106. {
  107. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)}),
  108. output: false,
  109. },
  110. {
  111. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)}),
  112. output: true,
  113. },
  114. {
  115. input: context.Background(),
  116. output: false,
  117. },
  118. },
  119. },
  120. {
  121. rule: &RoutingRule{
  122. SourceCidr: []*CIDR{
  123. {
  124. Ip: []byte{192, 168, 0, 0},
  125. Prefix: 16,
  126. },
  127. },
  128. },
  129. test: []ruleTest{
  130. {
  131. input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("192.168.0.1"), 80)}),
  132. output: true,
  133. },
  134. {
  135. input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("10.0.0.1"), 80)}),
  136. output: false,
  137. },
  138. },
  139. },
  140. {
  141. rule: &RoutingRule{
  142. UserEmail: []string{
  143. "admin@v2ray.com",
  144. },
  145. },
  146. test: []ruleTest{
  147. {
  148. input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "admin@v2ray.com"}}),
  149. output: true,
  150. },
  151. {
  152. input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "love@v2ray.com"}}),
  153. output: false,
  154. },
  155. {
  156. input: context.Background(),
  157. output: false,
  158. },
  159. },
  160. },
  161. {
  162. rule: &RoutingRule{
  163. Protocol: []string{"http"},
  164. },
  165. test: []ruleTest{
  166. {
  167. input: dispatcher.ContextWithSniffingResult(context.Background(), &http.SniffHeader{}),
  168. output: true,
  169. },
  170. },
  171. },
  172. {
  173. rule: &RoutingRule{
  174. InboundTag: []string{"test", "test1"},
  175. },
  176. test: []ruleTest{
  177. {
  178. input: withInbound(&session.Inbound{Tag: "test"}),
  179. output: true,
  180. },
  181. {
  182. input: withInbound(&session.Inbound{Tag: "test2"}),
  183. output: false,
  184. },
  185. },
  186. },
  187. }
  188. for _, test := range cases {
  189. cond, err := test.rule.BuildCondition()
  190. common.Must(err)
  191. for _, subtest := range test.test {
  192. actual := cond.Apply(subtest.input)
  193. if actual != subtest.output {
  194. t.Error("test case failed: ", subtest.input, " expected ", subtest.output, " but got ", actual)
  195. }
  196. }
  197. }
  198. }
  199. func loadGeoSite(country string) ([]*Domain, error) {
  200. geositeBytes, err := sysio.ReadAsset("geosite.dat")
  201. if err != nil {
  202. return nil, err
  203. }
  204. var geositeList GeoSiteList
  205. if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
  206. return nil, err
  207. }
  208. for _, site := range geositeList.Entry {
  209. if site.CountryCode == country {
  210. return site.Domain, nil
  211. }
  212. }
  213. return nil, errors.New("country not found: " + country)
  214. }
  215. func TestChinaSites(t *testing.T) {
  216. assert := With(t)
  217. common.Must(sysio.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geosite.dat")))
  218. domains, err := loadGeoSite("CN")
  219. assert(err, IsNil)
  220. matcher, err := NewDomainMatcher(domains)
  221. common.Must(err)
  222. assert(matcher.ApplyDomain("163.com"), IsTrue)
  223. assert(matcher.ApplyDomain("163.com"), IsTrue)
  224. assert(matcher.ApplyDomain("164.com"), IsFalse)
  225. assert(matcher.ApplyDomain("164.com"), IsFalse)
  226. for i := 0; i < 1024; i++ {
  227. assert(matcher.ApplyDomain(strconv.Itoa(i)+".not-exists.com"), IsFalse)
  228. }
  229. }