dokodemo_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package scenarios
  2. import (
  3. "net"
  4. "testing"
  5. "v2ray.com/core"
  6. v2net "v2ray.com/core/common/net"
  7. "v2ray.com/core/common/protocol"
  8. "v2ray.com/core/common/serial"
  9. "v2ray.com/core/common/uuid"
  10. "v2ray.com/core/proxy/dokodemo"
  11. "v2ray.com/core/proxy/freedom"
  12. "v2ray.com/core/proxy/vmess"
  13. "v2ray.com/core/proxy/vmess/inbound"
  14. "v2ray.com/core/proxy/vmess/outbound"
  15. "v2ray.com/core/testing/assert"
  16. "v2ray.com/core/testing/servers/tcp"
  17. )
  18. func TestDokodemoTCP(t *testing.T) {
  19. assert := assert.On(t)
  20. tcpServer := tcp.Server{
  21. MsgProcessor: xor,
  22. }
  23. dest, err := tcpServer.Start()
  24. assert.Error(err).IsNil()
  25. defer tcpServer.Close()
  26. userID := protocol.NewID(uuid.New())
  27. serverPort := pickPort()
  28. serverConfig := &core.Config{
  29. Inbound: []*core.InboundConnectionConfig{
  30. {
  31. PortRange: v2net.SinglePortRange(serverPort),
  32. ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
  33. Settings: serial.ToTypedMessage(&inbound.Config{
  34. User: []*protocol.User{
  35. {
  36. Account: serial.ToTypedMessage(&vmess.Account{
  37. Id: userID.String(),
  38. }),
  39. },
  40. },
  41. }),
  42. },
  43. },
  44. Outbound: []*core.OutboundConnectionConfig{
  45. {
  46. Settings: serial.ToTypedMessage(&freedom.Config{}),
  47. },
  48. },
  49. }
  50. clientPort := uint32(pickPort())
  51. clientPortRange := uint32(5)
  52. clientConfig := &core.Config{
  53. Inbound: []*core.InboundConnectionConfig{
  54. {
  55. PortRange: &v2net.PortRange{From: clientPort, To: clientPort + clientPortRange},
  56. ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
  57. Settings: serial.ToTypedMessage(&dokodemo.Config{
  58. Address: v2net.NewIPOrDomain(dest.Address),
  59. Port: uint32(dest.Port),
  60. NetworkList: &v2net.NetworkList{
  61. Network: []v2net.Network{v2net.Network_TCP},
  62. },
  63. }),
  64. },
  65. },
  66. Outbound: []*core.OutboundConnectionConfig{
  67. {
  68. Settings: serial.ToTypedMessage(&outbound.Config{
  69. Receiver: []*protocol.ServerEndpoint{
  70. {
  71. Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
  72. Port: uint32(serverPort),
  73. User: []*protocol.User{
  74. {
  75. Account: serial.ToTypedMessage(&vmess.Account{
  76. Id: userID.String(),
  77. }),
  78. },
  79. },
  80. },
  81. },
  82. }),
  83. },
  84. },
  85. }
  86. assert.Error(InitializeServerConfig(serverConfig)).IsNil()
  87. assert.Error(InitializeServerConfig(clientConfig)).IsNil()
  88. for port := clientPort; port <= clientPort+clientPortRange; port++ {
  89. conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  90. IP: []byte{127, 0, 0, 1},
  91. Port: int(port),
  92. })
  93. payload := "dokodemo request."
  94. nBytes, err := conn.Write([]byte(payload))
  95. assert.Error(err).IsNil()
  96. assert.Int(nBytes).Equals(len(payload))
  97. response := make([]byte, 1024)
  98. nBytes, err = conn.Read(response)
  99. assert.Error(err).IsNil()
  100. assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload)))
  101. assert.Error(conn.Close()).IsNil()
  102. }
  103. CloseAllServers()
  104. }