splithttp_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package splithttp_test
  2. import (
  3. "context"
  4. gotls "crypto/tls"
  5. "fmt"
  6. gonet "net"
  7. "net/http"
  8. "runtime"
  9. "testing"
  10. "time"
  11. "github.com/xtls/xray-core/common"
  12. "github.com/xtls/xray-core/common/net"
  13. "github.com/xtls/xray-core/common/protocol/tls/cert"
  14. "github.com/xtls/xray-core/testing/servers/tcp"
  15. "github.com/xtls/xray-core/transport/internet"
  16. . "github.com/xtls/xray-core/transport/internet/splithttp"
  17. "github.com/xtls/xray-core/transport/internet/stat"
  18. "github.com/xtls/xray-core/transport/internet/tls"
  19. "golang.org/x/net/http2"
  20. )
  21. func Test_listenSHAndDial(t *testing.T) {
  22. listenPort := tcp.PickPort()
  23. listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, &internet.MemoryStreamConfig{
  24. ProtocolName: "splithttp",
  25. ProtocolSettings: &Config{
  26. Path: "/sh",
  27. },
  28. }, func(conn stat.Connection) {
  29. go func(c stat.Connection) {
  30. defer c.Close()
  31. var b [1024]byte
  32. c.SetReadDeadline(time.Now().Add(2 * time.Second))
  33. _, err := c.Read(b[:])
  34. if err != nil {
  35. return
  36. }
  37. common.Must2(c.Write([]byte("Response")))
  38. }(conn)
  39. })
  40. common.Must(err)
  41. ctx := context.Background()
  42. streamSettings := &internet.MemoryStreamConfig{
  43. ProtocolName: "splithttp",
  44. ProtocolSettings: &Config{Path: "sh"},
  45. }
  46. conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
  47. common.Must(err)
  48. _, err = conn.Write([]byte("Test connection 1"))
  49. common.Must(err)
  50. var b [1024]byte
  51. fmt.Println("test2")
  52. n, _ := conn.Read(b[:])
  53. fmt.Println("string is", n)
  54. if string(b[:n]) != "Response" {
  55. t.Error("response: ", string(b[:n]))
  56. }
  57. common.Must(conn.Close())
  58. <-time.After(time.Second * 5)
  59. conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
  60. common.Must(err)
  61. _, err = conn.Write([]byte("Test connection 2"))
  62. common.Must(err)
  63. n, _ = conn.Read(b[:])
  64. common.Must(err)
  65. if string(b[:n]) != "Response" {
  66. t.Error("response: ", string(b[:n]))
  67. }
  68. common.Must(conn.Close())
  69. common.Must(listen.Close())
  70. }
  71. func TestDialWithRemoteAddr(t *testing.T) {
  72. listenPort := tcp.PickPort()
  73. listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, &internet.MemoryStreamConfig{
  74. ProtocolName: "splithttp",
  75. ProtocolSettings: &Config{
  76. Path: "sh",
  77. },
  78. }, func(conn stat.Connection) {
  79. go func(c stat.Connection) {
  80. defer c.Close()
  81. var b [1024]byte
  82. _, err := c.Read(b[:])
  83. // common.Must(err)
  84. if err != nil {
  85. return
  86. }
  87. _, err = c.Write([]byte("Response"))
  88. common.Must(err)
  89. }(conn)
  90. })
  91. common.Must(err)
  92. conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), listenPort), &internet.MemoryStreamConfig{
  93. ProtocolName: "splithttp",
  94. ProtocolSettings: &Config{Path: "sh", Header: map[string]string{"X-Forwarded-For": "1.1.1.1"}},
  95. })
  96. common.Must(err)
  97. _, err = conn.Write([]byte("Test connection 1"))
  98. common.Must(err)
  99. var b [1024]byte
  100. n, _ := conn.Read(b[:])
  101. if string(b[:n]) != "Response" {
  102. t.Error("response: ", string(b[:n]))
  103. }
  104. common.Must(listen.Close())
  105. }
  106. func Test_listenSHAndDial_TLS(t *testing.T) {
  107. if runtime.GOARCH == "arm64" {
  108. return
  109. }
  110. listenPort := tcp.PickPort()
  111. start := time.Now()
  112. streamSettings := &internet.MemoryStreamConfig{
  113. ProtocolName: "splithttp",
  114. ProtocolSettings: &Config{
  115. Path: "shs",
  116. },
  117. SecurityType: "tls",
  118. SecuritySettings: &tls.Config{
  119. AllowInsecure: true,
  120. Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
  121. },
  122. }
  123. listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) {
  124. go func() {
  125. _ = conn.Close()
  126. }()
  127. })
  128. common.Must(err)
  129. defer listen.Close()
  130. conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
  131. common.Must(err)
  132. _ = conn.Close()
  133. end := time.Now()
  134. if !end.Before(start.Add(time.Second * 5)) {
  135. t.Error("end: ", end, " start: ", start)
  136. }
  137. }
  138. func Test_listenSHAndDial_H2C(t *testing.T) {
  139. if runtime.GOARCH == "arm64" {
  140. return
  141. }
  142. listenPort := tcp.PickPort()
  143. streamSettings := &internet.MemoryStreamConfig{
  144. ProtocolName: "splithttp",
  145. ProtocolSettings: &Config{
  146. Path: "shs",
  147. },
  148. }
  149. listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) {
  150. go func() {
  151. _ = conn.Close()
  152. }()
  153. })
  154. common.Must(err)
  155. defer listen.Close()
  156. client := http.Client{
  157. Transport: &http2.Transport{
  158. // So http2.Transport doesn't complain the URL scheme isn't 'https'
  159. AllowHTTP: true,
  160. // even with AllowHTTP, http2.Transport will attempt to establish
  161. // the connection using DialTLSContext. Disable TLS with custom
  162. // dial context.
  163. DialTLSContext: func(ctx context.Context, network, addr string, cfg *gotls.Config) (gonet.Conn, error) {
  164. var d gonet.Dialer
  165. return d.DialContext(ctx, network, addr)
  166. },
  167. },
  168. }
  169. resp, err := client.Get("http://" + net.LocalHostIP.String() + ":" + listenPort.String())
  170. common.Must(err)
  171. if resp.StatusCode != 404 {
  172. t.Error("Expected 404 but got:", resp.StatusCode)
  173. }
  174. if resp.ProtoMajor != 2 {
  175. t.Error("Expected h2 but got:", resp.ProtoMajor)
  176. }
  177. }