condition_test.go 7.9 KB

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