condition_test.go 4.9 KB

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