condition_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. for _, test := range cases {
  260. cond, err := test.rule.BuildCondition()
  261. common.Must(err)
  262. for _, subtest := range test.test {
  263. actual := cond.Apply(subtest.input)
  264. if actual != subtest.output {
  265. t.Error("test case failed: ", subtest.input, " expected ", subtest.output, " but got ", actual)
  266. }
  267. }
  268. }
  269. }
  270. func loadGeoSite(country string) ([]*Domain, error) {
  271. geositeBytes, err := filesystem.ReadAsset("geosite.dat")
  272. if err != nil {
  273. return nil, err
  274. }
  275. var geositeList GeoSiteList
  276. if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
  277. return nil, err
  278. }
  279. for _, site := range geositeList.Entry {
  280. if site.CountryCode == country {
  281. return site.Domain, nil
  282. }
  283. }
  284. return nil, errors.New("country not found: " + country)
  285. }
  286. func TestChinaSites(t *testing.T) {
  287. domains, err := loadGeoSite("CN")
  288. common.Must(err)
  289. matcher, err := NewDomainMatcher(domains)
  290. common.Must(err)
  291. type TestCase struct {
  292. Domain string
  293. Output bool
  294. }
  295. testCases := []TestCase{
  296. {
  297. Domain: "163.com",
  298. Output: true,
  299. },
  300. {
  301. Domain: "163.com",
  302. Output: true,
  303. },
  304. {
  305. Domain: "164.com",
  306. Output: false,
  307. },
  308. {
  309. Domain: "164.com",
  310. Output: false,
  311. },
  312. }
  313. for i := 0; i < 1024; i++ {
  314. testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
  315. }
  316. for _, testCase := range testCases {
  317. r := matcher.ApplyDomain(testCase.Domain)
  318. if r != testCase.Output {
  319. t.Error("expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r)
  320. }
  321. }
  322. }
  323. func BenchmarkMultiGeoIPMatcher(b *testing.B) {
  324. var geoips []*GeoIP
  325. {
  326. ips, err := loadGeoIP("CN")
  327. common.Must(err)
  328. geoips = append(geoips, &GeoIP{
  329. CountryCode: "CN",
  330. Cidr: ips,
  331. })
  332. }
  333. {
  334. ips, err := loadGeoIP("JP")
  335. common.Must(err)
  336. geoips = append(geoips, &GeoIP{
  337. CountryCode: "JP",
  338. Cidr: ips,
  339. })
  340. }
  341. {
  342. ips, err := loadGeoIP("CA")
  343. common.Must(err)
  344. geoips = append(geoips, &GeoIP{
  345. CountryCode: "CA",
  346. Cidr: ips,
  347. })
  348. }
  349. {
  350. ips, err := loadGeoIP("US")
  351. common.Must(err)
  352. geoips = append(geoips, &GeoIP{
  353. CountryCode: "US",
  354. Cidr: ips,
  355. })
  356. }
  357. matcher, err := NewMultiGeoIPMatcher(geoips, false)
  358. common.Must(err)
  359. ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)})
  360. b.ResetTimer()
  361. for i := 0; i < b.N; i++ {
  362. _ = matcher.Apply(ctx)
  363. }
  364. }