ws_test.go 3.6 KB

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