condition_test.go 4.8 KB

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