condition_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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/net"
  12. "github.com/xtls/xray-core/common/platform"
  13. "github.com/xtls/xray-core/common/platform/filesystem"
  14. "github.com/xtls/xray-core/common/protocol"
  15. "github.com/xtls/xray-core/common/protocol/http"
  16. "github.com/xtls/xray-core/common/session"
  17. "github.com/xtls/xray-core/features/routing"
  18. routing_session "github.com/xtls/xray-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: "example.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("example.com"), 80)}),
  71. output: true,
  72. },
  73. {
  74. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.example.com.www"), 80)}),
  75. output: true,
  76. },
  77. {
  78. input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("example.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. "[email protected]",
  199. },
  200. },
  201. test: []ruleTest{
  202. {
  203. input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "[email protected]"}}),
  204. output: true,
  205. },
  206. {
  207. input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "[email protected]"}}),
  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: map[string]string{
  302. ":path": "/test",
  303. },
  304. },
  305. test: []ruleTest{
  306. {
  307. input: withContent(&session.Content{Protocol: "http/1.1", Attributes: map[string]string{":path": "/test/1"}}),
  308. output: true,
  309. },
  310. },
  311. },
  312. {
  313. rule: &RoutingRule{
  314. Attributes: map[string]string{
  315. "Custom": "p([a-z]+)ch",
  316. },
  317. },
  318. test: []ruleTest{
  319. {
  320. input: withContent(&session.Content{Attributes: map[string]string{"custom": "peach"}}),
  321. output: true,
  322. },
  323. },
  324. },
  325. }
  326. for _, test := range cases {
  327. cond, err := test.rule.BuildCondition()
  328. common.Must(err)
  329. for _, subtest := range test.test {
  330. actual := cond.Apply(subtest.input)
  331. if actual != subtest.output {
  332. t.Error("test case failed: ", subtest.input, " expected ", subtest.output, " but got ", actual)
  333. }
  334. }
  335. }
  336. }
  337. func loadGeoSite(country string) ([]*Domain, error) {
  338. geositeBytes, err := filesystem.ReadAsset("geosite.dat")
  339. if err != nil {
  340. return nil, err
  341. }
  342. var geositeList GeoSiteList
  343. if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
  344. return nil, err
  345. }
  346. for _, site := range geositeList.Entry {
  347. if site.CountryCode == country {
  348. return site.Domain, nil
  349. }
  350. }
  351. return nil, errors.New("country not found: " + country)
  352. }
  353. func TestChinaSites(t *testing.T) {
  354. domains, err := loadGeoSite("CN")
  355. common.Must(err)
  356. matcher, err := NewDomainMatcher(domains)
  357. common.Must(err)
  358. acMatcher, err := NewMphMatcherGroup(domains)
  359. common.Must(err)
  360. type TestCase struct {
  361. Domain string
  362. Output bool
  363. }
  364. testCases := []TestCase{
  365. {
  366. Domain: "163.com",
  367. Output: true,
  368. },
  369. {
  370. Domain: "163.com",
  371. Output: true,
  372. },
  373. {
  374. Domain: "164.com",
  375. Output: false,
  376. },
  377. {
  378. Domain: "164.com",
  379. Output: false,
  380. },
  381. }
  382. for i := 0; i < 1024; i++ {
  383. testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
  384. }
  385. for _, testCase := range testCases {
  386. r1 := matcher.ApplyDomain(testCase.Domain)
  387. r2 := acMatcher.ApplyDomain(testCase.Domain)
  388. if r1 != testCase.Output {
  389. t.Error("DomainMatcher expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r1)
  390. } else if r2 != testCase.Output {
  391. t.Error("ACDomainMatcher expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r2)
  392. }
  393. }
  394. }
  395. func BenchmarkMphDomainMatcher(b *testing.B) {
  396. domains, err := loadGeoSite("CN")
  397. common.Must(err)
  398. matcher, err := NewMphMatcherGroup(domains)
  399. common.Must(err)
  400. type TestCase struct {
  401. Domain string
  402. Output bool
  403. }
  404. testCases := []TestCase{
  405. {
  406. Domain: "163.com",
  407. Output: true,
  408. },
  409. {
  410. Domain: "163.com",
  411. Output: true,
  412. },
  413. {
  414. Domain: "164.com",
  415. Output: false,
  416. },
  417. {
  418. Domain: "164.com",
  419. Output: false,
  420. },
  421. }
  422. for i := 0; i < 1024; i++ {
  423. testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
  424. }
  425. b.ResetTimer()
  426. for i := 0; i < b.N; i++ {
  427. for _, testCase := range testCases {
  428. _ = matcher.ApplyDomain(testCase.Domain)
  429. }
  430. }
  431. }
  432. func BenchmarkDomainMatcher(b *testing.B) {
  433. domains, err := loadGeoSite("CN")
  434. common.Must(err)
  435. matcher, err := NewDomainMatcher(domains)
  436. common.Must(err)
  437. type TestCase struct {
  438. Domain string
  439. Output bool
  440. }
  441. testCases := []TestCase{
  442. {
  443. Domain: "163.com",
  444. Output: true,
  445. },
  446. {
  447. Domain: "163.com",
  448. Output: true,
  449. },
  450. {
  451. Domain: "164.com",
  452. Output: false,
  453. },
  454. {
  455. Domain: "164.com",
  456. Output: false,
  457. },
  458. }
  459. for i := 0; i < 1024; i++ {
  460. testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
  461. }
  462. b.ResetTimer()
  463. for i := 0; i < b.N; i++ {
  464. for _, testCase := range testCases {
  465. _ = matcher.ApplyDomain(testCase.Domain)
  466. }
  467. }
  468. }
  469. func BenchmarkMultiGeoIPMatcher(b *testing.B) {
  470. var geoips []*GeoIP
  471. {
  472. ips, err := loadGeoIP("CN")
  473. common.Must(err)
  474. geoips = append(geoips, &GeoIP{
  475. CountryCode: "CN",
  476. Cidr: ips,
  477. })
  478. }
  479. {
  480. ips, err := loadGeoIP("JP")
  481. common.Must(err)
  482. geoips = append(geoips, &GeoIP{
  483. CountryCode: "JP",
  484. Cidr: ips,
  485. })
  486. }
  487. {
  488. ips, err := loadGeoIP("CA")
  489. common.Must(err)
  490. geoips = append(geoips, &GeoIP{
  491. CountryCode: "CA",
  492. Cidr: ips,
  493. })
  494. }
  495. {
  496. ips, err := loadGeoIP("US")
  497. common.Must(err)
  498. geoips = append(geoips, &GeoIP{
  499. CountryCode: "US",
  500. Cidr: ips,
  501. })
  502. }
  503. matcher, err := NewMultiGeoIPMatcher(geoips, false)
  504. common.Must(err)
  505. ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)})
  506. b.ResetTimer()
  507. for i := 0; i < b.N; i++ {
  508. _ = matcher.Apply(ctx)
  509. }
  510. }