ws_test.go 3.4 KB

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