condition_test.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. package router_test
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strconv"
  6. "testing"
  7. "github.com/golang/protobuf/proto"
  8. . "v2ray.com/core/app/router"
  9. "v2ray.com/core/common"
  10. "v2ray.com/core/common/errors"
  11. "v2ray.com/core/common/net"
  12. "v2ray.com/core/common/platform"
  13. "v2ray.com/core/common/platform/filesystem"
  14. "v2ray.com/core/common/protocol"
  15. "v2ray.com/core/common/protocol/http"
  16. "v2ray.com/core/common/session"
  17. "v2ray.com/core/features/routing"
  18. routing_session "v2ray.com/core/features/routing/session"
  19. )
  20. func init() {
  21. wd, err := os.Getwd()
  22. common.Must(err)
  23. if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
  24. common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
  25. }
  26. if _, err := os.Stat(platform.GetAssetLocation("geosite.dat")); err != nil && os.IsNotExist(err) {
  27. common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "release", "config", "geosite.dat")))
  28. }
  29. }
  30. func withBackground() routing.Context {
  31. return &routing_session.Context{}
  32. }
  33. func withOutbound(outbound *session.Outbound) routing.Context {
  34. return &routing_session.Context{Outbound: outbound}
  35. }
  36. func withInbound(inbound *session.Inbound) routing.Context {
  37. return &routing_session.Context{Inbound: inbound}
  38. }
  39. func withContent(content *session.Content) routing.Context {
  40. return &routing_session.Context{Content: content}
  41. }
  42. func TestRoutingRule(t *testing.T) {
  43. type ruleTest struct {
  44. input routing.Context
  45. output bool
  46. }
  47. cases := []struct {
  48. rule *RoutingRule
  49. test []ruleTest
  50. }{
  51. {
  52. rule: &RoutingRule{
  53. Domain: []*Domain{
  54. {
  55. Value: "v2ray.com",
  56. Type: Domain_Plain,
  57. },
  58. {
  59. Value: "google.com",
  60. Type: Domain_Domain,
  61. },
  62. {
  63. Value: "^facebook\\.com$",
  64. Type: Domain_Regex,
  65. },
  66. },
  67. },
  68. test: []ruleTest{
  69. {
  70. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)}),
  71. output: true,
  72. },
  73. {
  74. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.v2ray.com.www"), 80)}),
  75. output: true,
  76. },
  77. {
  78. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.co"), 80)}),
  79. output: false,
  80. },
  81. {
  82. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.google.com"), 80)}),
  83. output: true,
  84. },
  85. {
  86. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("facebook.com"), 80)}),
  87. output: true,
  88. },
  89. {
  90. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.facebook.com"), 80)}),
  91. output: false,
  92. },
  93. {
  94. input: withBackground(),
  95. output: false,
  96. },
  97. },
  98. },
  99. {
  100. rule: &RoutingRule{
  101. Cidr: []*CIDR{
  102. {
  103. Ip: []byte{8, 8, 8, 8},
  104. Prefix: 32,
  105. },
  106. {
  107. Ip: []byte{8, 8, 8, 8},
  108. Prefix: 32,
  109. },
  110. {
  111. Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
  112. Prefix: 128,
  113. },
  114. },
  115. },
  116. test: []ruleTest{
  117. {
  118. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)}),
  119. output: true,
  120. },
  121. {
  122. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)}),
  123. output: false,
  124. },
  125. {
  126. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)}),
  127. output: true,
  128. },
  129. {
  130. input: withBackground(),
  131. output: false,
  132. },
  133. },
  134. },
  135. {
  136. rule: &RoutingRule{
  137. Geoip: []*GeoIP{
  138. {
  139. Cidr: []*CIDR{
  140. {
  141. Ip: []byte{8, 8, 8, 8},
  142. Prefix: 32,
  143. },
  144. {
  145. Ip: []byte{8, 8, 8, 8},
  146. Prefix: 32,
  147. },
  148. {
  149. Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
  150. Prefix: 128,
  151. },
  152. },
  153. },
  154. },
  155. },
  156. test: []ruleTest{
  157. {
  158. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)}),
  159. output: true,
  160. },
  161. {
  162. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)}),
  163. output: false,
  164. },
  165. {
  166. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)}),
  167. output: true,
  168. },
  169. {
  170. input: withBackground(),
  171. output: false,
  172. },
  173. },
  174. },
  175. {
  176. rule: &RoutingRule{
  177. SourceCidr: []*CIDR{
  178. {
  179. Ip: []byte{192, 168, 0, 0},
  180. Prefix: 16,
  181. },
  182. },
  183. },
  184. test: []ruleTest{
  185. {
  186. input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("192.168.0.1"), 80)}),
  187. output: true,
  188. },
  189. {
  190. input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("10.0.0.1"), 80)}),
  191. output: false,
  192. },
  193. },
  194. },
  195. {
  196. rule: &RoutingRule{
  197. UserEmail: []string{
  198. "admin@v2ray.com",
  199. },
  200. },
  201. test: []ruleTest{
  202. {
  203. input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "admin@v2ray.com"}}),
  204. output: true,
  205. },
  206. {
  207. input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "love@v2ray.com"}}),
  208. output: false,
  209. },
  210. {
  211. input: withBackground(),
  212. output: false,
  213. },
  214. },
  215. },
  216. {
  217. rule: &RoutingRule{
  218. Protocol: []string{"http"},
  219. },
  220. test: []ruleTest{
  221. {
  222. input: withContent(&session.Content{Protocol: (&http.SniffHeader{}).Protocol()}),
  223. output: true,
  224. },
  225. },
  226. },
  227. {
  228. rule: &RoutingRule{
  229. InboundTag: []string{"test", "test1"},
  230. },
  231. test: []ruleTest{
  232. {
  233. input: withInbound(&session.Inbound{Tag: "test"}),
  234. output: true,
  235. },
  236. {
  237. input: withInbound(&session.Inbound{Tag: "test2"}),
  238. output: false,
  239. },
  240. },
  241. },
  242. {
  243. rule: &RoutingRule{
  244. PortList: &net.PortList{
  245. Range: []*net.PortRange{
  246. {From: 443, To: 443},
  247. {From: 1000, To: 1100},
  248. },
  249. },
  250. },
  251. test: []ruleTest{
  252. {
  253. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 443)}),
  254. output: true,
  255. },
  256. {
  257. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 1100)}),
  258. output: true,
  259. },
  260. {
  261. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 1005)}),
  262. output: true,
  263. },
  264. {
  265. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 53)}),
  266. output: false,
  267. },
  268. },
  269. },
  270. {
  271. rule: &RoutingRule{
  272. SourcePortList: &net.PortList{
  273. Range: []*net.PortRange{
  274. {From: 123, To: 123},
  275. {From: 9993, To: 9999},
  276. },
  277. },
  278. },
  279. test: []ruleTest{
  280. {
  281. input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 123)}),
  282. output: true,
  283. },
  284. {
  285. input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 9999)}),
  286. output: true,
  287. },
  288. {
  289. input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 9994)}),
  290. output: true,
  291. },
  292. {
  293. input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 53)}),
  294. output: false,
  295. },
  296. },
  297. },
  298. {
  299. rule: &RoutingRule{
  300. Protocol: []string{"http"},
  301. Attributes: "attrs[':path'].startswith('/test')",
  302. },
  303. test: []ruleTest{
  304. {
  305. input: withContent(&session.Content{Protocol: "http/1.1", Attributes: map[string]string{":path": "/test/1"}}),
  306. output: true,
  307. },
  308. },
  309. },
  310. }
  311. for _, test := range cases {
  312. cond, err := test.rule.BuildCondition()
  313. common.Must(err)
  314. for _, subtest := range test.test {
  315. actual := cond.Apply(subtest.input)
  316. if actual != subtest.output {
  317. t.Error("test case failed: ", subtest.input, " expected ", subtest.output, " but got ", actual)
  318. }
  319. }
  320. }
  321. }
  322. func loadGeoSite(country string) ([]*Domain, error) {
  323. geositeBytes, err := filesystem.ReadAsset("geosite.dat")
  324. if err != nil {
  325. return nil, err
  326. }
  327. var geositeList GeoSiteList
  328. if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
  329. return nil, err
  330. }
  331. for _, site := range geositeList.Entry {
  332. if site.CountryCode == country {
  333. return site.Domain, nil
  334. }
  335. }
  336. return nil, errors.New("country not found: " + country)
  337. }
  338. func TestChinaSites(t *testing.T) {
  339. domains, err := loadGeoSite("CN")
  340. common.Must(err)
  341. matcher, err := NewDomainMatcher(domains)
  342. common.Must(err)
  343. type TestCase struct {
  344. Domain string
  345. Output bool
  346. }
  347. testCases := []TestCase{
  348. {
  349. Domain: "163.com",
  350. Output: true,
  351. },
  352. {
  353. Domain: "163.com",
  354. Output: true,
  355. },
  356. {
  357. Domain: "164.com",
  358. Output: false,
  359. },
  360. {
  361. Domain: "164.com",
  362. Output: false,
  363. },
  364. }
  365. for i := 0; i < 1024; i++ {
  366. testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
  367. }
  368. for _, testCase := range testCases {
  369. r := matcher.ApplyDomain(testCase.Domain)
  370. if r != testCase.Output {
  371. t.Error("expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r)
  372. }
  373. }
  374. }
  375. func BenchmarkMultiGeoIPMatcher(b *testing.B) {
  376. var geoips []*GeoIP
  377. {
  378. ips, err := loadGeoIP("CN")
  379. common.Must(err)
  380. geoips = append(geoips, &GeoIP{
  381. CountryCode: "CN",
  382. Cidr: ips,
  383. })
  384. }
  385. {
  386. ips, err := loadGeoIP("JP")
  387. common.Must(err)
  388. geoips = append(geoips, &GeoIP{
  389. CountryCode: "JP",
  390. Cidr: ips,
  391. })
  392. }
  393. {
  394. ips, err := loadGeoIP("CA")
  395. common.Must(err)
  396. geoips = append(geoips, &GeoIP{
  397. CountryCode: "CA",
  398. Cidr: ips,
  399. })
  400. }
  401. {
  402. ips, err := loadGeoIP("US")
  403. common.Must(err)
  404. geoips = append(geoips, &GeoIP{
  405. CountryCode: "US",
  406. Cidr: ips,
  407. })
  408. }
  409. matcher, err := NewMultiGeoIPMatcher(geoips, false)
  410. common.Must(err)
  411. ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)})
  412. b.ResetTimer()
  413. for i := 0; i < b.N; i++ {
  414. _ = matcher.Apply(ctx)
  415. }
  416. }