condition_test.go 5.2 KB

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