1
0

ws_test.go 3.6 KB

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