dialer.go 3.1 KB

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