condition_test.go 13 KB

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