http2_config.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package httpclient
  2. import (
  3. stdTLS "crypto/tls"
  4. "net/http"
  5. "time"
  6. "github.com/sagernet/sing-box/option"
  7. E "github.com/sagernet/sing/common/exceptions"
  8. "golang.org/x/net/http2"
  9. )
  10. func CloneHTTP2Transport(transport *http2.Transport) *http2.Transport {
  11. return &http2.Transport{
  12. ReadIdleTimeout: transport.ReadIdleTimeout,
  13. PingTimeout: transport.PingTimeout,
  14. DialTLSContext: transport.DialTLSContext,
  15. }
  16. }
  17. func ConfigureHTTP2Transport(options option.HTTP2Options) (*http2.Transport, error) {
  18. stdTransport := &http.Transport{
  19. TLSClientConfig: &stdTLS.Config{},
  20. HTTP2: &http.HTTP2Config{
  21. MaxReceiveBufferPerStream: int(options.StreamReceiveWindow.Value()),
  22. MaxReceiveBufferPerConnection: int(options.ConnectionReceiveWindow.Value()),
  23. MaxConcurrentStreams: options.MaxConcurrentStreams,
  24. SendPingTimeout: time.Duration(options.KeepAlivePeriod),
  25. PingTimeout: time.Duration(options.IdleTimeout),
  26. },
  27. }
  28. h2Transport, err := http2.ConfigureTransports(stdTransport)
  29. if err != nil {
  30. return nil, E.Cause(err, "configure HTTP/2 transport")
  31. }
  32. // ConfigureTransports binds ConnPool to the throwaway http.Transport; sever it so DialTLSContext is used directly.
  33. h2Transport.ConnPool = nil
  34. h2Transport.ReadIdleTimeout = time.Duration(options.KeepAlivePeriod)
  35. h2Transport.PingTimeout = time.Duration(options.IdleTimeout)
  36. return h2Transport, nil
  37. }