condition_test.go 5.0 KB

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