condition_test.go 12 KB

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