dialer.go 6.5 KB

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