condition_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package router_test
  2. import (
  3. "context"
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "testing"
  8. "time"
  9. proto "github.com/golang/protobuf/proto"
  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/proxy"
  17. . "v2ray.com/ext/assert"
  18. "v2ray.com/ext/sysio"
  19. )
  20. func TestRoutingRule(t *testing.T) {
  21. assert := With(t)
  22. type ruleTest struct {
  23. input context.Context
  24. output bool
  25. }
  26. cases := []struct {
  27. rule *RoutingRule
  28. test []ruleTest
  29. }{
  30. {
  31. rule: &RoutingRule{
  32. Domain: []*Domain{
  33. {
  34. Value: "v2ray.com",
  35. Type: Domain_Plain,
  36. },
  37. {
  38. Value: "google.com",
  39. Type: Domain_Domain,
  40. },
  41. {
  42. Value: "^facebook\\.com$",
  43. Type: Domain_Regex,
  44. },
  45. },
  46. },
  47. test: []ruleTest{
  48. {
  49. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.com"), 80)),
  50. output: true,
  51. },
  52. {
  53. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.v2ray.com.www"), 80)),
  54. output: true,
  55. },
  56. {
  57. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.co"), 80)),
  58. output: false,
  59. },
  60. {
  61. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.google.com"), 80)),
  62. output: true,
  63. },
  64. {
  65. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("facebook.com"), 80)),
  66. output: true,
  67. },
  68. {
  69. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.facebook.com"), 80)),
  70. output: false,
  71. },
  72. {
  73. input: context.Background(),
  74. output: false,
  75. },
  76. },
  77. },
  78. {
  79. rule: &RoutingRule{
  80. Cidr: []*CIDR{
  81. {
  82. Ip: []byte{8, 8, 8, 8},
  83. Prefix: 32,
  84. },
  85. {
  86. Ip: []byte{8, 8, 8, 8},
  87. Prefix: 32,
  88. },
  89. {
  90. Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
  91. Prefix: 128,
  92. },
  93. },
  94. },
  95. test: []ruleTest{
  96. {
  97. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)),
  98. output: true,
  99. },
  100. {
  101. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)),
  102. output: false,
  103. },
  104. {
  105. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)),
  106. output: true,
  107. },
  108. {
  109. input: context.Background(),
  110. output: false,
  111. },
  112. },
  113. },
  114. {
  115. rule: &RoutingRule{
  116. UserEmail: []string{
  117. "admin@v2ray.com",
  118. },
  119. },
  120. test: []ruleTest{
  121. {
  122. input: protocol.ContextWithUser(context.Background(), &protocol.User{Email: "admin@v2ray.com"}),
  123. output: true,
  124. },
  125. {
  126. input: protocol.ContextWithUser(context.Background(), &protocol.User{Email: "love@v2ray.com"}),
  127. output: false,
  128. },
  129. {
  130. input: context.Background(),
  131. output: false,
  132. },
  133. },
  134. },
  135. }
  136. for _, test := range cases {
  137. cond, err := test.rule.BuildCondition()
  138. assert(err, IsNil)
  139. for _, t := range test.test {
  140. assert(cond.Apply(t.input), Equals, t.output)
  141. }
  142. }
  143. }
  144. func loadGeoSite(country string) ([]*Domain, error) {
  145. geositeBytes, err := sysio.ReadAsset("geosite.dat")
  146. if err != nil {
  147. return nil, err
  148. }
  149. var geositeList GeoSiteList
  150. if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
  151. return nil, err
  152. }
  153. for _, site := range geositeList.Entry {
  154. if site.CountryCode == country {
  155. return site.Domain, nil
  156. }
  157. }
  158. return nil, errors.New("country not found: " + country)
  159. }
  160. func TestChinaSites(t *testing.T) {
  161. assert := With(t)
  162. common.Must(sysio.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geosite.dat")))
  163. domains, err := loadGeoSite("CN")
  164. assert(err, IsNil)
  165. matcher := NewCachableDomainMatcher()
  166. for _, d := range domains {
  167. assert(matcher.Add(d), IsNil)
  168. }
  169. assert(matcher.ApplyDomain("163.com"), IsTrue)
  170. assert(matcher.ApplyDomain("163.com"), IsTrue)
  171. assert(matcher.ApplyDomain("164.com"), IsFalse)
  172. assert(matcher.ApplyDomain("164.com"), IsFalse)
  173. for i := 0; i < 1024; i++ {
  174. assert(matcher.ApplyDomain(strconv.Itoa(i)+".not-exists.com"), IsFalse)
  175. }
  176. time.Sleep(time.Second * 10)
  177. for i := 0; i < 1024; i++ {
  178. assert(matcher.ApplyDomain(strconv.Itoa(i)+".not-exists2.com"), IsFalse)
  179. }
  180. }