tls_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package scenarios
  2. import (
  3. "net"
  4. "path/filepath"
  5. "testing"
  6. "io/ioutil"
  7. "os"
  8. "time"
  9. "v2ray.com/core"
  10. v2net "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/protocol"
  12. "v2ray.com/core/common/serial"
  13. "v2ray.com/core/common/uuid"
  14. "v2ray.com/core/proxy/dokodemo"
  15. "v2ray.com/core/proxy/freedom"
  16. "v2ray.com/core/proxy/vmess"
  17. "v2ray.com/core/proxy/vmess/inbound"
  18. "v2ray.com/core/proxy/vmess/outbound"
  19. "v2ray.com/core/testing/assert"
  20. "v2ray.com/core/testing/servers/tcp"
  21. "v2ray.com/core/transport/internet"
  22. "v2ray.com/core/transport/internet/tls"
  23. )
  24. func mustReadFile(name string) []byte {
  25. content, err := ioutil.ReadFile(name)
  26. if err != nil {
  27. panic(err)
  28. }
  29. return content
  30. }
  31. func TestSimpleTLSConnection(t *testing.T) {
  32. assert := assert.On(t)
  33. tcpServer := tcp.Server{
  34. MsgProcessor: xor,
  35. }
  36. dest, err := tcpServer.Start()
  37. assert.Error(err).IsNil()
  38. userID := protocol.NewID(uuid.New())
  39. serverPort := pickPort()
  40. serverConfig := &core.Config{
  41. Inbound: []*core.InboundConnectionConfig{
  42. {
  43. PortRange: v2net.SinglePortRange(serverPort),
  44. ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
  45. Settings: serial.ToTypedMessage(&inbound.Config{
  46. User: []*protocol.User{
  47. {
  48. Account: serial.ToTypedMessage(&vmess.Account{
  49. Id: userID.String(),
  50. }),
  51. },
  52. },
  53. }),
  54. StreamSettings: &internet.StreamConfig{
  55. SecurityType: serial.GetMessageType(&tls.Config{}),
  56. SecuritySettings: []*serial.TypedMessage{
  57. serial.ToTypedMessage(&tls.Config{
  58. Certificate: []*tls.Certificate{
  59. {
  60. Certificate: mustReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "tls", "cert.pem")),
  61. Key: mustReadFile(filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "testing", "tls", "key.pem")),
  62. },
  63. },
  64. }),
  65. },
  66. },
  67. },
  68. },
  69. Outbound: []*core.OutboundConnectionConfig{
  70. {
  71. Settings: serial.ToTypedMessage(&freedom.Config{}),
  72. },
  73. },
  74. }
  75. clientPort := pickPort()
  76. clientConfig := &core.Config{
  77. Inbound: []*core.InboundConnectionConfig{
  78. {
  79. PortRange: v2net.SinglePortRange(clientPort),
  80. ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP),
  81. Settings: serial.ToTypedMessage(&dokodemo.Config{
  82. Address: v2net.NewIPOrDomain(dest.Address),
  83. Port: uint32(dest.Port),
  84. NetworkList: &v2net.NetworkList{
  85. Network: []v2net.Network{v2net.Network_TCP},
  86. },
  87. }),
  88. },
  89. },
  90. Outbound: []*core.OutboundConnectionConfig{
  91. {
  92. Settings: serial.ToTypedMessage(&outbound.Config{
  93. Receiver: []*protocol.ServerEndpoint{
  94. {
  95. Address: v2net.NewIPOrDomain(v2net.LocalHostIP),
  96. Port: uint32(serverPort),
  97. User: []*protocol.User{
  98. {
  99. Account: serial.ToTypedMessage(&vmess.Account{
  100. Id: userID.String(),
  101. }),
  102. },
  103. },
  104. },
  105. },
  106. }),
  107. StreamSettings: &internet.StreamConfig{
  108. SecurityType: serial.GetMessageType(&tls.Config{}),
  109. SecuritySettings: []*serial.TypedMessage{
  110. serial.ToTypedMessage(&tls.Config{
  111. AllowInsecure: true,
  112. }),
  113. },
  114. },
  115. },
  116. },
  117. }
  118. assert.Error(InitializeServerConfig(serverConfig)).IsNil()
  119. assert.Error(InitializeServerConfig(clientConfig)).IsNil()
  120. conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  121. IP: []byte{127, 0, 0, 1},
  122. Port: int(clientPort),
  123. })
  124. payload := "dokodemo request."
  125. nBytes, err := conn.Write([]byte(payload))
  126. assert.Error(err).IsNil()
  127. assert.Int(nBytes).Equals(len(payload))
  128. conn.CloseWrite()
  129. response := readFrom(conn, time.Second*2, len(payload))
  130. assert.Bytes(response).Equals(xor([]byte(payload)))
  131. conn.Close()
  132. CloseAllServers()
  133. }