dialer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // +build !confonly
  2. package http
  3. import (
  4. "context"
  5. gotls "crypto/tls"
  6. "net/http"
  7. "net/url"
  8. "sync"
  9. "golang.org/x/net/http2"
  10. "v2ray.com/core/common"
  11. "v2ray.com/core/common/buf"
  12. "v2ray.com/core/common/net"
  13. "v2ray.com/core/transport/internet"
  14. "v2ray.com/core/transport/internet/tls"
  15. "v2ray.com/core/transport/pipe"
  16. )
  17. var (
  18. globalDialerMap map[net.Destination]*http.Client
  19. globalDailerAccess sync.Mutex
  20. )
  21. func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.Config) (*http.Client, error) {
  22. globalDailerAccess.Lock()
  23. defer globalDailerAccess.Unlock()
  24. if globalDialerMap == nil {
  25. globalDialerMap = make(map[net.Destination]*http.Client)
  26. }
  27. if client, found := globalDialerMap[dest]; found {
  28. return client, nil
  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. pconn, err := internet.DialSystem(context.Background(), net.TCPDestination(address, port), nil)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return gotls.Client(pconn, tlsConfig), nil
  49. },
  50. TLSClientConfig: tlsSettings.GetTLSConfig(tls.WithDestination(dest), tls.WithNextProto("h2")),
  51. }
  52. client := &http.Client{
  53. Transport: transport,
  54. }
  55. globalDialerMap[dest] = client
  56. return client, nil
  57. }
  58. // Dial dials a new TCP connection to the given destination.
  59. func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
  60. httpSettings := streamSettings.ProtocolSettings.(*Config)
  61. tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
  62. if tlsConfig == nil {
  63. return nil, newError("TLS must be enabled for http transport.").AtWarning()
  64. }
  65. client, err := getHTTPClient(ctx, dest, tlsConfig)
  66. if err != nil {
  67. return nil, err
  68. }
  69. opts := pipe.OptionsFromContext(ctx)
  70. preader, pwriter := pipe.New(opts...)
  71. breader := &buf.BufferedReader{Reader: preader}
  72. request := &http.Request{
  73. Method: "PUT",
  74. Host: httpSettings.getRandomHost(),
  75. Body: breader,
  76. URL: &url.URL{
  77. Scheme: "https",
  78. Host: dest.NetAddr(),
  79. Path: httpSettings.getNormalizedPath(),
  80. },
  81. Proto: "HTTP/2",
  82. ProtoMajor: 2,
  83. ProtoMinor: 0,
  84. Header: make(http.Header),
  85. }
  86. // Disable any compression method from server.
  87. request.Header.Set("Accept-Encoding", "identity")
  88. response, err := client.Do(request)
  89. if err != nil {
  90. return nil, newError("failed to dial to ", dest).Base(err).AtWarning()
  91. }
  92. if response.StatusCode != 200 {
  93. return nil, newError("unexpected status", response.StatusCode).AtWarning()
  94. }
  95. bwriter := buf.NewBufferedWriter(pwriter)
  96. common.Must(bwriter.SetBuffered(false))
  97. return net.NewConnection(
  98. net.ConnectionOutput(response.Body),
  99. net.ConnectionInput(bwriter),
  100. net.ConnectionOnClose(common.ChainedClosable{breader, bwriter, response.Body}),
  101. ), nil
  102. }
  103. func init() {
  104. common.Must(internet.RegisterTransportDialer(protocolName, Dial))
  105. }