ws_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package websocket_test
  2. import (
  3. "bytes"
  4. "context"
  5. "runtime"
  6. "testing"
  7. "time"
  8. "v2ray.com/core/common/net"
  9. "v2ray.com/core/common/protocol/tls/cert"
  10. "v2ray.com/core/transport/internet"
  11. "v2ray.com/core/transport/internet/tls"
  12. . "v2ray.com/core/transport/internet/websocket"
  13. . "v2ray.com/ext/assert"
  14. )
  15. func Test_listenWSAndDial(t *testing.T) {
  16. assert := With(t)
  17. listen, err := ListenWS(internet.ContextWithTransportSettings(context.Background(), &Config{
  18. Path: "ws",
  19. }), net.DomainAddress("localhost"), 13146, func(conn internet.Connection) {
  20. go func(c internet.Connection) {
  21. defer c.Close()
  22. var b [1024]byte
  23. n, err := c.Read(b[:])
  24. //assert(err, IsNil)
  25. if err != nil {
  26. return
  27. }
  28. assert(bytes.HasPrefix(b[:n], []byte("Test connection")), IsTrue)
  29. _, err = c.Write([]byte("Response"))
  30. assert(err, IsNil)
  31. }(conn)
  32. })
  33. assert(err, IsNil)
  34. ctx := internet.ContextWithTransportSettings(context.Background(), &Config{Path: "ws"})
  35. conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
  36. assert(err, IsNil)
  37. _, err = conn.Write([]byte("Test connection 1"))
  38. assert(err, IsNil)
  39. var b [1024]byte
  40. n, err := conn.Read(b[:])
  41. assert(err, IsNil)
  42. assert(string(b[:n]), Equals, "Response")
  43. assert(conn.Close(), IsNil)
  44. <-time.After(time.Second * 5)
  45. conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
  46. assert(err, IsNil)
  47. _, err = conn.Write([]byte("Test connection 2"))
  48. assert(err, IsNil)
  49. n, err = conn.Read(b[:])
  50. assert(err, IsNil)
  51. assert(string(b[:n]), Equals, "Response")
  52. assert(conn.Close(), IsNil)
  53. assert(listen.Close(), IsNil)
  54. }
  55. func TestDialWithRemoteAddr(t *testing.T) {
  56. assert := With(t)
  57. listen, err := ListenWS(internet.ContextWithTransportSettings(context.Background(), &Config{
  58. Path: "ws",
  59. }), net.DomainAddress("localhost"), 13148, func(conn internet.Connection) {
  60. go func(c internet.Connection) {
  61. defer c.Close()
  62. assert(c.RemoteAddr().String(), HasPrefix, "1.1.1.1")
  63. var b [1024]byte
  64. n, err := c.Read(b[:])
  65. //assert(err, IsNil)
  66. if err != nil {
  67. return
  68. }
  69. assert(bytes.HasPrefix(b[:n], []byte("Test connection")), IsTrue)
  70. _, err = c.Write([]byte("Response"))
  71. assert(err, IsNil)
  72. }(conn)
  73. })
  74. assert(err, IsNil)
  75. ctx := internet.ContextWithTransportSettings(context.Background(), &Config{Path: "ws", Header: []*Header{{Key: "X-Forwarded-For", Value: "1.1.1.1"}}})
  76. conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13148))
  77. assert(err, IsNil)
  78. _, err = conn.Write([]byte("Test connection 1"))
  79. assert(err, IsNil)
  80. var b [1024]byte
  81. n, err := conn.Read(b[:])
  82. assert(err, IsNil)
  83. assert(string(b[:n]), Equals, "Response")
  84. assert(listen.Close(), IsNil)
  85. }
  86. func Test_listenWSAndDial_TLS(t *testing.T) {
  87. if runtime.GOARCH == "arm64" {
  88. return
  89. }
  90. assert := With(t)
  91. start := time.Now()
  92. ctx := internet.ContextWithTransportSettings(context.Background(), &Config{
  93. Path: "wss",
  94. })
  95. ctx = internet.ContextWithSecuritySettings(ctx, &tls.Config{
  96. AllowInsecure: true,
  97. Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
  98. })
  99. listen, err := ListenWS(ctx, net.DomainAddress("localhost"), 13143, func(conn internet.Connection) {
  100. go func() {
  101. _ = conn.Close()
  102. }()
  103. })
  104. assert(err, IsNil)
  105. defer listen.Close()
  106. conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13143))
  107. assert(err, IsNil)
  108. _ = conn.Close()
  109. end := time.Now()
  110. assert(end.Before(start.Add(time.Second*5)), IsTrue)
  111. }