dialer.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package http
  2. import (
  3. "context"
  4. gotls "crypto/tls"
  5. "net/http"
  6. "net/url"
  7. "sync"
  8. "github.com/xtls/xray-core/common"
  9. "github.com/xtls/xray-core/common/buf"
  10. "github.com/xtls/xray-core/common/net"
  11. "github.com/xtls/xray-core/common/net/cnc"
  12. "github.com/xtls/xray-core/common/session"
  13. "github.com/xtls/xray-core/transport/internet"
  14. "github.com/xtls/xray-core/transport/internet/tls"
  15. "github.com/xtls/xray-core/transport/pipe"
  16. "golang.org/x/net/http2"
  17. )
  18. type dialerConf struct {
  19. net.Destination
  20. *internet.SocketConfig
  21. *tls.Config
  22. }
  23. var (
  24. globalDialerMap map[dialerConf]*http.Client
  25. globalDialerAccess sync.Mutex
  26. )
  27. func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.Config, sockopt *internet.SocketConfig) (*http.Client, error) {
  28. globalDialerAccess.Lock()
  29. defer globalDialerAccess.Unlock()
  30. if globalDialerMap == nil {
  31. globalDialerMap = make(map[dialerConf]*http.Client)
  32. }
  33. if client, found := globalDialerMap[dialerConf{dest, sockopt, tlsSettings}]; found {
  34. return client, nil
  35. }
  36. transport := &http2.Transport{
  37. DialTLS: func(network string, addr string, tlsConfig *gotls.Config) (net.Conn, error) {
  38. rawHost, rawPort, err := net.SplitHostPort(addr)
  39. if err != nil {
  40. return nil, err
  41. }
  42. if len(rawPort) == 0 {
  43. rawPort = "443"
  44. }
  45. port, err := net.PortFromString(rawPort)
  46. if err != nil {
  47. return nil, err
  48. }
  49. address := net.ParseAddress(rawHost)
  50. dctx := context.Background()
  51. dctx = session.ContextWithID(dctx, session.IDFromContext(ctx))
  52. dctx = session.ContextWithOutbound(dctx, session.OutboundFromContext(ctx))
  53. pconn, err := internet.DialSystem(dctx, net.TCPDestination(address, port), sockopt)
  54. if err != nil {
  55. newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
  56. return nil, err
  57. }
  58. cn := gotls.Client(pconn, tlsConfig)
  59. if err := cn.Handshake(); err != nil {
  60. newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
  61. return nil, err
  62. }
  63. if !tlsConfig.InsecureSkipVerify {
  64. if err := cn.VerifyHostname(tlsConfig.ServerName); err != nil {
  65. newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
  66. return nil, err
  67. }
  68. }
  69. state := cn.ConnectionState()
  70. if p := state.NegotiatedProtocol; p != http2.NextProtoTLS {
  71. return nil, newError("http2: unexpected ALPN protocol " + p + "; want q" + http2.NextProtoTLS).AtError()
  72. }
  73. if !state.NegotiatedProtocolIsMutual {
  74. return nil, newError("http2: could not negotiate protocol mutually").AtError()
  75. }
  76. return cn, nil
  77. },
  78. TLSClientConfig: tlsSettings.GetTLSConfig(tls.WithDestination(dest)),
  79. }
  80. client := &http.Client{
  81. Transport: transport,
  82. }
  83. globalDialerMap[dialerConf{dest, sockopt, tlsSettings}] = client
  84. return client, nil
  85. }
  86. // Dial dials a new TCP connection to the given destination.
  87. func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
  88. httpSettings := streamSettings.ProtocolSettings.(*Config)
  89. tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
  90. if tlsConfig == nil {
  91. return nil, newError("TLS must be enabled for http transport.").AtWarning()
  92. }
  93. client, err := getHTTPClient(ctx, dest, tlsConfig, streamSettings.SocketSettings)
  94. if err != nil {
  95. return nil, err
  96. }
  97. opts := pipe.OptionsFromContext(ctx)
  98. preader, pwriter := pipe.New(opts...)
  99. breader := &buf.BufferedReader{Reader: preader}
  100. request := &http.Request{
  101. Method: "PUT",
  102. Host: httpSettings.getRandomHost(),
  103. Body: breader,
  104. URL: &url.URL{
  105. Scheme: "https",
  106. Host: dest.NetAddr(),
  107. Path: httpSettings.getNormalizedPath(),
  108. },
  109. Proto: "HTTP/2",
  110. ProtoMajor: 2,
  111. ProtoMinor: 0,
  112. Header: make(http.Header),
  113. }
  114. // Disable any compression method from server.
  115. request.Header.Set("Accept-Encoding", "identity")
  116. response, err := client.Do(request)
  117. if err != nil {
  118. return nil, newError("failed to dial to ", dest).Base(err).AtWarning()
  119. }
  120. if response.StatusCode != 200 {
  121. return nil, newError("unexpected status", response.StatusCode).AtWarning()
  122. }
  123. bwriter := buf.NewBufferedWriter(pwriter)
  124. common.Must(bwriter.SetBuffered(false))
  125. return cnc.NewConnection(
  126. cnc.ConnectionOutput(response.Body),
  127. cnc.ConnectionInput(bwriter),
  128. cnc.ConnectionOnClose(common.ChainedClosable{breader, bwriter, response.Body}),
  129. ), nil
  130. }
  131. func init() {
  132. common.Must(internet.RegisterTransportDialer(protocolName, Dial))
  133. }