vmess_test.go 3.7 KB

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