condition_test.go 9.6 KB

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