condition_test.go 12 KB

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