vmess_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_och", func(space app.Space, config interface{}) (proxy.InboundHandler, error) {
  34. ich.PacketDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
  35. return ich, nil
  36. })
  37. assert.Error(err).IsNil()
  38. configA := &point.Config{
  39. Port: portA,
  40. DNSConfig: &dns.Config{
  41. NameServers: []v2net.Destination{
  42. v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
  43. },
  44. },
  45. InboundConfig: &point.ConnectionConfig{
  46. Protocol: protocol,
  47. Settings: nil,
  48. },
  49. OutboundConfig: &point.ConnectionConfig{
  50. Protocol: "vmess",
  51. Settings: []byte(`{
  52. "vnext": [
  53. {
  54. "address": "127.0.0.1",
  55. "port": ` + portB.String() + `,
  56. "users": [
  57. {"id": "` + testAccount.String() + `"}
  58. ]
  59. }
  60. ]
  61. }`),
  62. },
  63. }
  64. pointA, err := point.NewPoint(configA)
  65. assert.Error(err).IsNil()
  66. err = pointA.Start()
  67. assert.Error(err).IsNil()
  68. ochConnInput := []byte("The data to be returned to inbound server.")
  69. ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  70. och := &proxymocks.OutboundConnectionHandler{
  71. ConnInput: bytes.NewReader(ochConnInput),
  72. ConnOutput: ochConnOutput,
  73. }
  74. protocol, err = proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
  75. return och, nil
  76. })
  77. assert.Error(err).IsNil()
  78. configB := &point.Config{
  79. Port: portB,
  80. DNSConfig: &dns.Config{
  81. NameServers: []v2net.Destination{
  82. v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
  83. },
  84. },
  85. InboundConfig: &point.ConnectionConfig{
  86. Protocol: "vmess",
  87. Settings: []byte(`{
  88. "clients": [
  89. {"id": "` + testAccount.String() + `"}
  90. ]
  91. }`),
  92. },
  93. OutboundConfig: &point.ConnectionConfig{
  94. Protocol: protocol,
  95. Settings: nil,
  96. },
  97. }
  98. pointB, err := point.NewPoint(configB)
  99. assert.Error(err).IsNil()
  100. err = pointB.Start()
  101. assert.Error(err).IsNil()
  102. dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
  103. ich.Communicate(dest)
  104. assert.Bytes(ichConnInput).Equals(ochConnOutput.Bytes())
  105. assert.Bytes(ichConnOutput.Bytes()).Equals(ochConnInput)
  106. }