vmess_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package vmess_test
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/v2ray/v2ray-core/app"
  6. "github.com/v2ray/v2ray-core/app/dispatcher"
  7. "github.com/v2ray/v2ray-core/app/dns"
  8. v2net "github.com/v2ray/v2ray-core/common/net"
  9. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  10. "github.com/v2ray/v2ray-core/common/protocol"
  11. "github.com/v2ray/v2ray-core/common/uuid"
  12. "github.com/v2ray/v2ray-core/proxy"
  13. proxytesting "github.com/v2ray/v2ray-core/proxy/testing"
  14. proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
  15. _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
  16. _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  17. "github.com/v2ray/v2ray-core/shell/point"
  18. v2testing "github.com/v2ray/v2ray-core/testing"
  19. "github.com/v2ray/v2ray-core/testing/assert"
  20. )
  21. func TestVMessInAndOut(t *testing.T) {
  22. v2testing.Current(t)
  23. id, err := uuid.ParseString("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
  24. assert.Error(err).IsNil()
  25. testAccount := protocol.NewID(id)
  26. portA := v2nettesting.PickPort()
  27. portB := v2nettesting.PickPort()
  28. ichConnInput := []byte("The data to be send to outbound server.")
  29. ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  30. ich := &proxymocks.InboundConnectionHandler{
  31. ConnInput: bytes.NewReader(ichConnInput),
  32. ConnOutput: ichConnOutput,
  33. }
  34. protocol, err := proxytesting.RegisterInboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (proxy.InboundHandler, error) {
  35. ich.PacketDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
  36. return ich, nil
  37. })
  38. assert.Error(err).IsNil()
  39. configA := &point.Config{
  40. Port: portA,
  41. DNSConfig: &dns.Config{
  42. NameServers: []v2net.Destination{
  43. v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
  44. },
  45. },
  46. InboundConfig: &point.ConnectionConfig{
  47. Protocol: protocol,
  48. Settings: nil,
  49. },
  50. OutboundConfig: &point.ConnectionConfig{
  51. Protocol: "vmess",
  52. Settings: []byte(`{
  53. "vnext": [
  54. {
  55. "address": "127.0.0.1",
  56. "port": ` + portB.String() + `,
  57. "users": [
  58. {"id": "` + testAccount.String() + `"}
  59. ]
  60. }
  61. ]
  62. }`),
  63. },
  64. }
  65. pointA, err := point.NewPoint(configA)
  66. assert.Error(err).IsNil()
  67. err = pointA.Start()
  68. assert.Error(err).IsNil()
  69. ochConnInput := []byte("The data to be returned to inbound server.")
  70. ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  71. och := &proxymocks.OutboundConnectionHandler{
  72. ConnInput: bytes.NewReader(ochConnInput),
  73. ConnOutput: ochConnOutput,
  74. }
  75. protocol, err = proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
  76. return och, nil
  77. })
  78. assert.Error(err).IsNil()
  79. configB := &point.Config{
  80. Port: portB,
  81. DNSConfig: &dns.Config{
  82. NameServers: []v2net.Destination{
  83. v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
  84. },
  85. },
  86. InboundConfig: &point.ConnectionConfig{
  87. Protocol: "vmess",
  88. Settings: []byte(`{
  89. "clients": [
  90. {"id": "` + testAccount.String() + `"}
  91. ]
  92. }`),
  93. },
  94. OutboundConfig: &point.ConnectionConfig{
  95. Protocol: protocol,
  96. Settings: nil,
  97. },
  98. }
  99. pointB, err := point.NewPoint(configB)
  100. assert.Error(err).IsNil()
  101. err = pointB.Start()
  102. assert.Error(err).IsNil()
  103. dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
  104. ich.Communicate(dest)
  105. assert.Bytes(ichConnInput).Equals(ochConnOutput.Bytes())
  106. assert.Bytes(ichConnOutput.Bytes()).Equals(ochConnInput)
  107. }