dialer.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package http
  2. import (
  3. "context"
  4. gotls "crypto/tls"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "sync"
  9. "time"
  10. "github.com/xtls/xray-core/common"
  11. "github.com/xtls/xray-core/common/buf"
  12. c "github.com/xtls/xray-core/common/ctx"
  13. "github.com/xtls/xray-core/common/errors"
  14. "github.com/xtls/xray-core/common/net"
  15. "github.com/xtls/xray-core/common/net/cnc"
  16. "github.com/xtls/xray-core/common/session"
  17. "github.com/xtls/xray-core/transport/internet"
  18. "github.com/xtls/xray-core/transport/internet/reality"
  19. "github.com/xtls/xray-core/transport/internet/stat"
  20. "github.com/xtls/xray-core/transport/internet/tls"
  21. "github.com/xtls/xray-core/transport/pipe"
  22. "golang.org/x/net/http2"
  23. )
  24. type dialerConf struct {
  25. net.Destination
  26. *internet.MemoryStreamConfig
  27. }
  28. var (
  29. globalDialerMap map[dialerConf]*http.Client
  30. globalDialerAccess sync.Mutex
  31. )
  32. func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (*http.Client, error) {
  33. globalDialerAccess.Lock()
  34. defer globalDialerAccess.Unlock()
  35. if globalDialerMap == nil {
  36. globalDialerMap = make(map[dialerConf]*http.Client)
  37. }
  38. httpSettings := streamSettings.ProtocolSettings.(*Config)
  39. tlsConfigs := tls.ConfigFromStreamSettings(streamSettings)
  40. realityConfigs := reality.ConfigFromStreamSettings(streamSettings)
  41. if tlsConfigs == nil && realityConfigs == nil {
  42. return nil, errors.New("TLS or REALITY must be enabled for http transport.").AtWarning()
  43. }
  44. sockopt := streamSettings.SocketSettings
  45. if client, found := globalDialerMap[dialerConf{dest, streamSettings}]; found {
  46. return client, nil
  47. }
  48. transport := &http2.Transport{
  49. DialTLSContext: func(hctx context.Context, string, addr string, tlsConfig *gotls.Config) (net.Conn, error) {
  50. rawHost, rawPort, err := net.SplitHostPort(addr)
  51. if err != nil {
  52. return nil, err
  53. }
  54. if len(rawPort) == 0 {
  55. rawPort = "443"
  56. }
  57. port, err := net.PortFromString(rawPort)
  58. if err != nil {
  59. return nil, err
  60. }
  61. address := net.ParseAddress(rawHost)
  62. hctx = c.ContextWithID(hctx, c.IDFromContext(ctx))
  63. hctx = session.ContextWithOutbounds(hctx, session.OutboundsFromContext(ctx))
  64. hctx = session.ContextWithTimeoutOnly(hctx, true)
  65. pconn, err := internet.DialSystem(hctx, net.TCPDestination(address, port), sockopt)
  66. if err != nil {
  67. errors.LogErrorInner(ctx, err, "failed to dial to " + addr)
  68. return nil, err
  69. }
  70. if realityConfigs != nil {
  71. return reality.UClient(pconn, realityConfigs, hctx, dest)
  72. }
  73. var cn tls.Interface
  74. if fingerprint := tls.GetFingerprint(tlsConfigs.Fingerprint); fingerprint != nil {
  75. cn = tls.UClient(pconn, tlsConfig, fingerprint).(*tls.UConn)
  76. } else {
  77. cn = tls.Client(pconn, tlsConfig).(*tls.Conn)
  78. }
  79. if err := cn.HandshakeContext(ctx); err != nil {
  80. errors.LogErrorInner(ctx, err, "failed to dial to " + addr)
  81. return nil, err
  82. }
  83. if !tlsConfig.InsecureSkipVerify {
  84. if err := cn.VerifyHostname(tlsConfig.ServerName); err != nil {
  85. errors.LogErrorInner(ctx, err, "failed to dial to " + addr)
  86. return nil, err
  87. }
  88. }
  89. negotiatedProtocol := cn.NegotiatedProtocol()
  90. if negotiatedProtocol != http2.NextProtoTLS {
  91. return nil, errors.New("http2: unexpected ALPN protocol " + negotiatedProtocol + "; want q" + http2.NextProtoTLS).AtError()
  92. }
  93. return cn, nil
  94. },
  95. }
  96. if tlsConfigs != nil {
  97. transport.TLSClientConfig = tlsConfigs.GetTLSConfig(tls.WithDestination(dest))
  98. }
  99. if httpSettings.IdleTimeout > 0 || httpSettings.HealthCheckTimeout > 0 {
  100. transport.ReadIdleTimeout = time.Second * time.Duration(httpSettings.IdleTimeout)
  101. transport.PingTimeout = time.Second * time.Duration(httpSettings.HealthCheckTimeout)
  102. }
  103. client := &http.Client{
  104. Transport: transport,
  105. }
  106. globalDialerMap[dialerConf{dest, streamSettings}] = client
  107. return client, nil
  108. }
  109. // Dial dials a new TCP connection to the given destination.
  110. func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
  111. httpSettings := streamSettings.ProtocolSettings.(*Config)
  112. client, err := getHTTPClient(ctx, dest, streamSettings)
  113. if err != nil {
  114. return nil, err
  115. }
  116. opts := pipe.OptionsFromContext(ctx)
  117. preader, pwriter := pipe.New(opts...)
  118. breader := &buf.BufferedReader{Reader: preader}
  119. httpMethod := "PUT"
  120. if httpSettings.Method != "" {
  121. httpMethod = httpSettings.Method
  122. }
  123. httpHeaders := make(http.Header)
  124. for _, httpHeader := range httpSettings.Header {
  125. for _, httpHeaderValue := range httpHeader.Value {
  126. httpHeaders.Set(httpHeader.Name, httpHeaderValue)
  127. }
  128. }
  129. request := &http.Request{
  130. Method: httpMethod,
  131. Host: httpSettings.getRandomHost(),
  132. Body: breader,
  133. URL: &url.URL{
  134. Scheme: "https",
  135. Host: dest.NetAddr(),
  136. Path: httpSettings.getNormalizedPath(),
  137. },
  138. Proto: "HTTP/2",
  139. ProtoMajor: 2,
  140. ProtoMinor: 0,
  141. Header: httpHeaders,
  142. }
  143. // Disable any compression method from server.
  144. request.Header.Set("Accept-Encoding", "identity")
  145. wrc := &WaitReadCloser{Wait: make(chan struct{})}
  146. go func() {
  147. response, err := client.Do(request)
  148. if err != nil {
  149. errors.LogWarningInner(ctx, err, "failed to dial to ", dest)
  150. wrc.Close()
  151. {
  152. // Abandon `client` if `client.Do(request)` failed
  153. // See https://github.com/golang/go/issues/30702
  154. globalDialerAccess.Lock()
  155. if globalDialerMap[dialerConf{dest, streamSettings}] == client {
  156. delete(globalDialerMap, dialerConf{dest, streamSettings})
  157. }
  158. globalDialerAccess.Unlock()
  159. }
  160. return
  161. }
  162. if response.StatusCode != 200 {
  163. errors.LogWarning(ctx, "unexpected status", response.StatusCode)
  164. wrc.Close()
  165. return
  166. }
  167. wrc.Set(response.Body)
  168. }()
  169. bwriter := buf.NewBufferedWriter(pwriter)
  170. common.Must(bwriter.SetBuffered(false))
  171. return cnc.NewConnection(
  172. cnc.ConnectionOutput(wrc),
  173. cnc.ConnectionInput(bwriter),
  174. cnc.ConnectionOnClose(common.ChainedClosable{breader, bwriter, wrc}),
  175. ), nil
  176. }
  177. func init() {
  178. common.Must(internet.RegisterTransportDialer(protocolName, Dial))
  179. }
  180. type WaitReadCloser struct {
  181. Wait chan struct{}
  182. io.ReadCloser
  183. }
  184. func (w *WaitReadCloser) Set(rc io.ReadCloser) {
  185. w.ReadCloser = rc
  186. defer func() {
  187. if recover() != nil {
  188. rc.Close()
  189. }
  190. }()
  191. close(w.Wait)
  192. }
  193. func (w *WaitReadCloser) Read(b []byte) (int, error) {
  194. if w.ReadCloser == nil {
  195. if <-w.Wait; w.ReadCloser == nil {
  196. return 0, io.ErrClosedPipe
  197. }
  198. }
  199. return w.ReadCloser.Read(b)
  200. }
  201. func (w *WaitReadCloser) Close() error {
  202. if w.ReadCloser != nil {
  203. return w.ReadCloser.Close()
  204. }
  205. defer func() {
  206. if recover() != nil && w.ReadCloser != nil {
  207. w.ReadCloser.Close()
  208. }
  209. }()
  210. close(w.Wait)
  211. return nil
  212. }