router_test.go 4.1 KB

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