splithttp_test.go 5.1 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. conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
  59. common.Must(err)
  60. _, err = conn.Write([]byte("Test connection 2"))
  61. common.Must(err)
  62. n, _ = conn.Read(b[:])
  63. common.Must(err)
  64. if string(b[:n]) != "Response" {
  65. t.Error("response: ", string(b[:n]))
  66. }
  67. common.Must(conn.Close())
  68. common.Must(listen.Close())
  69. }
  70. func TestDialWithRemoteAddr(t *testing.T) {
  71. listenPort := tcp.PickPort()
  72. listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, &internet.MemoryStreamConfig{
  73. ProtocolName: "splithttp",
  74. ProtocolSettings: &Config{
  75. Path: "sh",
  76. },
  77. }, func(conn stat.Connection) {
  78. go func(c stat.Connection) {
  79. defer c.Close()
  80. var b [1024]byte
  81. _, err := c.Read(b[:])
  82. // common.Must(err)
  83. if err != nil {
  84. return
  85. }
  86. _, err = c.Write([]byte(c.RemoteAddr().String()))
  87. common.Must(err)
  88. }(conn)
  89. })
  90. common.Must(err)
  91. conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), listenPort), &internet.MemoryStreamConfig{
  92. ProtocolName: "splithttp",
  93. ProtocolSettings: &Config{Path: "sh", Header: map[string]string{"X-Forwarded-For": "1.1.1.1"}},
  94. })
  95. common.Must(err)
  96. _, err = conn.Write([]byte("Test connection 1"))
  97. common.Must(err)
  98. var b [1024]byte
  99. n, _ := conn.Read(b[:])
  100. if string(b[:n]) != "1.1.1.1:0" {
  101. t.Error("response: ", string(b[:n]))
  102. }
  103. common.Must(listen.Close())
  104. }
  105. func Test_listenSHAndDial_TLS(t *testing.T) {
  106. if runtime.GOARCH == "arm64" {
  107. return
  108. }
  109. listenPort := tcp.PickPort()
  110. start := time.Now()
  111. streamSettings := &internet.MemoryStreamConfig{
  112. ProtocolName: "splithttp",
  113. ProtocolSettings: &Config{
  114. Path: "shs",
  115. },
  116. SecurityType: "tls",
  117. SecuritySettings: &tls.Config{
  118. AllowInsecure: true,
  119. Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
  120. },
  121. }
  122. listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) {
  123. go func() {
  124. _ = conn.Close()
  125. }()
  126. })
  127. common.Must(err)
  128. defer listen.Close()
  129. conn, err := Dial(context.Background(), net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
  130. common.Must(err)
  131. _ = conn.Close()
  132. end := time.Now()
  133. if !end.Before(start.Add(time.Second * 5)) {
  134. t.Error("end: ", end, " start: ", start)
  135. }
  136. }
  137. func Test_listenSHAndDial_H2C(t *testing.T) {
  138. if runtime.GOARCH == "arm64" {
  139. return
  140. }
  141. listenPort := tcp.PickPort()
  142. streamSettings := &internet.MemoryStreamConfig{
  143. ProtocolName: "splithttp",
  144. ProtocolSettings: &Config{
  145. Path: "shs",
  146. },
  147. }
  148. listen, err := ListenSH(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) {
  149. go func() {
  150. _ = conn.Close()
  151. }()
  152. })
  153. common.Must(err)
  154. defer listen.Close()
  155. client := http.Client{
  156. Transport: &http2.Transport{
  157. // So http2.Transport doesn't complain the URL scheme isn't 'https'
  158. AllowHTTP: true,
  159. // even with AllowHTTP, http2.Transport will attempt to establish
  160. // the connection using DialTLSContext. Disable TLS with custom
  161. // dial context.
  162. DialTLSContext: func(ctx context.Context, network, addr string, cfg *gotls.Config) (gonet.Conn, error) {
  163. var d gonet.Dialer
  164. return d.DialContext(ctx, network, addr)
  165. },
  166. },
  167. }
  168. resp, err := client.Get("http://" + net.LocalHostIP.String() + ":" + listenPort.String())
  169. common.Must(err)
  170. if resp.StatusCode != 404 {
  171. t.Error("Expected 404 but got:", resp.StatusCode)
  172. }
  173. if resp.ProtoMajor != 2 {
  174. t.Error("Expected h2 but got:", resp.ProtoMajor)
  175. }
  176. }