condition_test.go 4.9 KB

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