condition_test.go 8.8 KB

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