condition_test.go 11 KB

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