router_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package conf_test
  2. import (
  3. "context"
  4. "net"
  5. "testing"
  6. v2net "v2ray.com/core/common/net"
  7. "v2ray.com/core/proxy"
  8. "v2ray.com/core/testing/assert"
  9. . "v2ray.com/core/tools/conf"
  10. )
  11. func makeDestination(ip string) v2net.Destination {
  12. return v2net.TCPDestination(v2net.IPAddress(net.ParseIP(ip)), 80)
  13. }
  14. func makeDomainDestination(domain string) v2net.Destination {
  15. return v2net.TCPDestination(v2net.DomainAddress(domain), 80)
  16. }
  17. func TestChinaIPJson(t *testing.T) {
  18. assert := assert.On(t)
  19. rule, err := ParseRule([]byte(`{
  20. "type": "chinaip",
  21. "outboundTag": "x"
  22. }`))
  23. assert.Error(err).IsNil()
  24. assert.String(rule.Tag).Equals("x")
  25. cond, err := rule.BuildCondition()
  26. assert.Error(err).IsNil()
  27. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("121.14.1.189"), 80)))).IsTrue() // sina.com.cn
  28. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("101.226.103.106"), 80)))).IsTrue() // qq.com
  29. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("115.239.210.36"), 80)))).IsTrue() // image.baidu.com
  30. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("120.135.126.1"), 80)))).IsTrue()
  31. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("8.8.8.8"), 80)))).IsFalse()
  32. }
  33. func TestChinaSitesJson(t *testing.T) {
  34. assert := assert.On(t)
  35. rule, err := ParseRule([]byte(`{
  36. "type": "chinasites",
  37. "outboundTag": "y"
  38. }`))
  39. assert.Error(err).IsNil()
  40. assert.String(rule.Tag).Equals("y")
  41. cond, err := rule.BuildCondition()
  42. assert.Error(err).IsNil()
  43. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("v.qq.com"), 80)))).IsTrue()
  44. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.163.com"), 80)))).IsTrue()
  45. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("ngacn.cc"), 80)))).IsTrue()
  46. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("12306.cn"), 80)))).IsTrue()
  47. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("v2ray.com"), 80)))).IsFalse()
  48. }
  49. func TestDomainRule(t *testing.T) {
  50. assert := assert.On(t)
  51. rule, err := ParseRule([]byte(`{
  52. "type": "field",
  53. "domain": [
  54. "ooxx.com",
  55. "oxox.com",
  56. "regexp:\\.cn$"
  57. ],
  58. "network": "tcp",
  59. "outboundTag": "direct"
  60. }`))
  61. assert.Error(err).IsNil()
  62. assert.Pointer(rule).IsNotNil()
  63. cond, err := rule.BuildCondition()
  64. assert.Error(err).IsNil()
  65. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.ooxx.com"), 80)))).IsTrue()
  66. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.aabb.com"), 80)))).IsFalse()
  67. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80)))).IsFalse()
  68. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.12306.cn"), 80)))).IsTrue()
  69. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.acn.com"), 80)))).IsFalse()
  70. }
  71. func TestIPRule(t *testing.T) {
  72. assert := assert.On(t)
  73. rule, err := ParseRule([]byte(`{
  74. "type": "field",
  75. "ip": [
  76. "10.0.0.0/8",
  77. "192.0.0.0/24"
  78. ],
  79. "network": "tcp",
  80. "outboundTag": "direct"
  81. }`))
  82. assert.Error(err).IsNil()
  83. assert.Pointer(rule).IsNotNil()
  84. cond, err := rule.BuildCondition()
  85. assert.Error(err).IsNil()
  86. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80)))).IsFalse()
  87. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80)))).IsTrue()
  88. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80)))).IsFalse()
  89. assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{192, 0, 0, 1}), 80)))).IsTrue()
  90. }
  91. func TestSourceIPRule(t *testing.T) {
  92. assert := assert.On(t)
  93. rule, err := ParseRule([]byte(`{
  94. "type": "field",
  95. "source": [
  96. "10.0.0.0/8",
  97. "192.0.0.0/24"
  98. ],
  99. "outboundTag": "direct"
  100. }`))
  101. assert.Error(err).IsNil()
  102. assert.Pointer(rule).IsNotNil()
  103. cond, err := rule.BuildCondition()
  104. assert.Error(err).IsNil()
  105. assert.Bool(cond.Apply(proxy.ContextWithSource(context.Background(), v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80)))).IsFalse()
  106. assert.Bool(cond.Apply(proxy.ContextWithSource(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80)))).IsTrue()
  107. assert.Bool(cond.Apply(proxy.ContextWithSource(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80)))).IsFalse()
  108. assert.Bool(cond.Apply(proxy.ContextWithSource(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{192, 0, 0, 1}), 80)))).IsTrue()
  109. }