condition_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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/sysio"
  19. )
  20. func withOutbound(outbound *session.Outbound) context.Context {
  21. return session.ContextWithOutbound(context.Background(), outbound)
  22. }
  23. func withInbound(inbound *session.Inbound) context.Context {
  24. return session.ContextWithInbound(context.Background(), inbound)
  25. }
  26. func TestRoutingRule(t *testing.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. Geoip: []*GeoIP{
  122. {
  123. Cidr: []*CIDR{
  124. {
  125. Ip: []byte{8, 8, 8, 8},
  126. Prefix: 32,
  127. },
  128. {
  129. Ip: []byte{8, 8, 8, 8},
  130. Prefix: 32,
  131. },
  132. {
  133. Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
  134. Prefix: 128,
  135. },
  136. },
  137. },
  138. },
  139. },
  140. test: []ruleTest{
  141. {
  142. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)}),
  143. output: true,
  144. },
  145. {
  146. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)}),
  147. output: false,
  148. },
  149. {
  150. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)}),
  151. output: true,
  152. },
  153. {
  154. input: context.Background(),
  155. output: false,
  156. },
  157. },
  158. },
  159. {
  160. rule: &RoutingRule{
  161. SourceCidr: []*CIDR{
  162. {
  163. Ip: []byte{192, 168, 0, 0},
  164. Prefix: 16,
  165. },
  166. },
  167. },
  168. test: []ruleTest{
  169. {
  170. input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("192.168.0.1"), 80)}),
  171. output: true,
  172. },
  173. {
  174. input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("10.0.0.1"), 80)}),
  175. output: false,
  176. },
  177. },
  178. },
  179. {
  180. rule: &RoutingRule{
  181. UserEmail: []string{
  182. "admin@v2ray.com",
  183. },
  184. },
  185. test: []ruleTest{
  186. {
  187. input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "admin@v2ray.com"}}),
  188. output: true,
  189. },
  190. {
  191. input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "love@v2ray.com"}}),
  192. output: false,
  193. },
  194. {
  195. input: context.Background(),
  196. output: false,
  197. },
  198. },
  199. },
  200. {
  201. rule: &RoutingRule{
  202. Protocol: []string{"http"},
  203. },
  204. test: []ruleTest{
  205. {
  206. input: dispatcher.ContextWithSniffingResult(context.Background(), &http.SniffHeader{}),
  207. output: true,
  208. },
  209. },
  210. },
  211. {
  212. rule: &RoutingRule{
  213. InboundTag: []string{"test", "test1"},
  214. },
  215. test: []ruleTest{
  216. {
  217. input: withInbound(&session.Inbound{Tag: "test"}),
  218. output: true,
  219. },
  220. {
  221. input: withInbound(&session.Inbound{Tag: "test2"}),
  222. output: false,
  223. },
  224. },
  225. },
  226. }
  227. for _, test := range cases {
  228. cond, err := test.rule.BuildCondition()
  229. common.Must(err)
  230. for _, subtest := range test.test {
  231. actual := cond.Apply(subtest.input)
  232. if actual != subtest.output {
  233. t.Error("test case failed: ", subtest.input, " expected ", subtest.output, " but got ", actual)
  234. }
  235. }
  236. }
  237. }
  238. func loadGeoSite(country string) ([]*Domain, error) {
  239. geositeBytes, err := sysio.ReadAsset("geosite.dat")
  240. if err != nil {
  241. return nil, err
  242. }
  243. var geositeList GeoSiteList
  244. if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
  245. return nil, err
  246. }
  247. for _, site := range geositeList.Entry {
  248. if site.CountryCode == country {
  249. return site.Domain, nil
  250. }
  251. }
  252. return nil, errors.New("country not found: " + country)
  253. }
  254. func TestChinaSites(t *testing.T) {
  255. common.Must(sysio.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geosite.dat")))
  256. domains, err := loadGeoSite("CN")
  257. common.Must(err)
  258. matcher, err := NewDomainMatcher(domains)
  259. common.Must(err)
  260. type TestCase struct {
  261. Domain string
  262. Output bool
  263. }
  264. testCases := []TestCase{
  265. {
  266. Domain: "163.com",
  267. Output: true,
  268. },
  269. {
  270. Domain: "163.com",
  271. Output: true,
  272. },
  273. {
  274. Domain: "164.com",
  275. Output: false,
  276. },
  277. {
  278. Domain: "164.com",
  279. Output: false,
  280. },
  281. }
  282. for i := 0; i < 1024; i++ {
  283. testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
  284. }
  285. for _, testCase := range testCases {
  286. r := matcher.ApplyDomain(testCase.Domain)
  287. if r != testCase.Output {
  288. t.Error("expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r)
  289. }
  290. }
  291. }
  292. func BenchmarkMultiGeoIPMatcher(b *testing.B) {
  293. common.Must(sysio.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geoip.dat")))
  294. var geoips []*GeoIP
  295. {
  296. ips, err := loadGeoIP("CN")
  297. common.Must(err)
  298. geoips = append(geoips, &GeoIP{
  299. CountryCode: "CN",
  300. Cidr: ips,
  301. })
  302. }
  303. {
  304. ips, err := loadGeoIP("JP")
  305. common.Must(err)
  306. geoips = append(geoips, &GeoIP{
  307. CountryCode: "JP",
  308. Cidr: ips,
  309. })
  310. }
  311. {
  312. ips, err := loadGeoIP("CA")
  313. common.Must(err)
  314. geoips = append(geoips, &GeoIP{
  315. CountryCode: "CA",
  316. Cidr: ips,
  317. })
  318. }
  319. {
  320. ips, err := loadGeoIP("US")
  321. common.Must(err)
  322. geoips = append(geoips, &GeoIP{
  323. CountryCode: "US",
  324. Cidr: ips,
  325. })
  326. }
  327. matcher, err := NewMultiGeoIPMatcher(geoips, false)
  328. common.Must(err)
  329. ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)})
  330. b.ResetTimer()
  331. for i := 0; i < b.N; i++ {
  332. _ = matcher.Apply(ctx)
  333. }
  334. }