dialer.go 4.3 KB

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