dialer.go 3.9 KB

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