condition_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. type ruleTest struct {
  29. input context.Context
  30. output bool
  31. }
  32. cases := []struct {
  33. rule *RoutingRule
  34. test []ruleTest
  35. }{
  36. {
  37. rule: &RoutingRule{
  38. Domain: []*Domain{
  39. {
  40. Value: "v2ray.com",
  41. Type: Domain_Plain,
  42. },
  43. {
  44. Value: "google.com",
  45. Type: Domain_Domain,
  46. },
  47. {
  48. Value: "^facebook\\.com$",
  49. Type: Domain_Regex,
  50. },
  51. },
  52. },
  53. test: []ruleTest{
  54. {
  55. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)}),
  56. output: true,
  57. },
  58. {
  59. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.v2ray.com.www"), 80)}),
  60. output: true,
  61. },
  62. {
  63. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.co"), 80)}),
  64. output: false,
  65. },
  66. {
  67. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.google.com"), 80)}),
  68. output: true,
  69. },
  70. {
  71. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("facebook.com"), 80)}),
  72. output: true,
  73. },
  74. {
  75. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.facebook.com"), 80)}),
  76. output: false,
  77. },
  78. {
  79. input: context.Background(),
  80. output: false,
  81. },
  82. },
  83. },
  84. {
  85. rule: &RoutingRule{
  86. Cidr: []*CIDR{
  87. {
  88. Ip: []byte{8, 8, 8, 8},
  89. Prefix: 32,
  90. },
  91. {
  92. Ip: []byte{8, 8, 8, 8},
  93. Prefix: 32,
  94. },
  95. {
  96. Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
  97. Prefix: 128,
  98. },
  99. },
  100. },
  101. test: []ruleTest{
  102. {
  103. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)}),
  104. output: true,
  105. },
  106. {
  107. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)}),
  108. output: false,
  109. },
  110. {
  111. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)}),
  112. output: true,
  113. },
  114. {
  115. input: context.Background(),
  116. output: false,
  117. },
  118. },
  119. },
  120. {
  121. rule: &RoutingRule{
  122. Geoip: []*GeoIP{
  123. {
  124. Cidr: []*CIDR{
  125. {
  126. Ip: []byte{8, 8, 8, 8},
  127. Prefix: 32,
  128. },
  129. {
  130. Ip: []byte{8, 8, 8, 8},
  131. Prefix: 32,
  132. },
  133. {
  134. Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
  135. Prefix: 128,
  136. },
  137. },
  138. },
  139. },
  140. },
  141. test: []ruleTest{
  142. {
  143. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)}),
  144. output: true,
  145. },
  146. {
  147. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)}),
  148. output: false,
  149. },
  150. {
  151. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)}),
  152. output: true,
  153. },
  154. {
  155. input: context.Background(),
  156. output: false,
  157. },
  158. },
  159. },
  160. {
  161. rule: &RoutingRule{
  162. SourceCidr: []*CIDR{
  163. {
  164. Ip: []byte{192, 168, 0, 0},
  165. Prefix: 16,
  166. },
  167. },
  168. },
  169. test: []ruleTest{
  170. {
  171. input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("192.168.0.1"), 80)}),
  172. output: true,
  173. },
  174. {
  175. input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("10.0.0.1"), 80)}),
  176. output: false,
  177. },
  178. },
  179. },
  180. {
  181. rule: &RoutingRule{
  182. UserEmail: []string{
  183. "admin@v2ray.com",
  184. },
  185. },
  186. test: []ruleTest{
  187. {
  188. input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "admin@v2ray.com"}}),
  189. output: true,
  190. },
  191. {
  192. input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "love@v2ray.com"}}),
  193. output: false,
  194. },
  195. {
  196. input: context.Background(),
  197. output: false,
  198. },
  199. },
  200. },
  201. {
  202. rule: &RoutingRule{
  203. Protocol: []string{"http"},
  204. },
  205. test: []ruleTest{
  206. {
  207. input: dispatcher.ContextWithSniffingResult(context.Background(), &http.SniffHeader{}),
  208. output: true,
  209. },
  210. },
  211. },
  212. {
  213. rule: &RoutingRule{
  214. InboundTag: []string{"test", "test1"},
  215. },
  216. test: []ruleTest{
  217. {
  218. input: withInbound(&session.Inbound{Tag: "test"}),
  219. output: true,
  220. },
  221. {
  222. input: withInbound(&session.Inbound{Tag: "test2"}),
  223. output: false,
  224. },
  225. },
  226. },
  227. }
  228. for _, test := range cases {
  229. cond, err := test.rule.BuildCondition()
  230. common.Must(err)
  231. for _, subtest := range test.test {
  232. actual := cond.Apply(subtest.input)
  233. if actual != subtest.output {
  234. t.Error("test case failed: ", subtest.input, " expected ", subtest.output, " but got ", actual)
  235. }
  236. }
  237. }
  238. }
  239. func loadGeoSite(country string) ([]*Domain, error) {
  240. geositeBytes, err := sysio.ReadAsset("geosite.dat")
  241. if err != nil {
  242. return nil, err
  243. }
  244. var geositeList GeoSiteList
  245. if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
  246. return nil, err
  247. }
  248. for _, site := range geositeList.Entry {
  249. if site.CountryCode == country {
  250. return site.Domain, nil
  251. }
  252. }
  253. return nil, errors.New("country not found: " + country)
  254. }
  255. func TestChinaSites(t *testing.T) {
  256. assert := With(t)
  257. common.Must(sysio.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geosite.dat")))
  258. domains, err := loadGeoSite("CN")
  259. assert(err, IsNil)
  260. matcher, err := NewDomainMatcher(domains)
  261. common.Must(err)
  262. assert(matcher.ApplyDomain("163.com"), IsTrue)
  263. assert(matcher.ApplyDomain("163.com"), IsTrue)
  264. assert(matcher.ApplyDomain("164.com"), IsFalse)
  265. assert(matcher.ApplyDomain("164.com"), IsFalse)
  266. for i := 0; i < 1024; i++ {
  267. assert(matcher.ApplyDomain(strconv.Itoa(i)+".not-exists.com"), IsFalse)
  268. }
  269. }