router_test.go 5.2 KB

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