condition_test.go 11 KB

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