dialer.go 4.5 KB

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