request_builder.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package fetcher // import "miniflux.app/v2/internal/reader/fetcher"
  4. import (
  5. "crypto/tls"
  6. "encoding/base64"
  7. "log/slog"
  8. "net"
  9. "net/http"
  10. "net/url"
  11. "time"
  12. "miniflux.app/v2/internal/proxyrotator"
  13. )
  14. const (
  15. defaultHTTPClientTimeout = 20
  16. defaultHTTPClientMaxBodySize = 15 * 1024 * 1024
  17. defaultAcceptHeader = "application/xml, application/atom+xml, application/rss+xml, application/rdf+xml, application/feed+json, text/html, */*;q=0.9"
  18. )
  19. type RequestBuilder struct {
  20. headers http.Header
  21. clientProxyURL *url.URL
  22. useClientProxy bool
  23. clientTimeout int
  24. withoutRedirects bool
  25. ignoreTLSErrors bool
  26. disableHTTP2 bool
  27. proxyRotator *proxyrotator.ProxyRotator
  28. }
  29. func NewRequestBuilder() *RequestBuilder {
  30. return &RequestBuilder{
  31. headers: make(http.Header),
  32. clientTimeout: defaultHTTPClientTimeout,
  33. }
  34. }
  35. func (r *RequestBuilder) WithHeader(key, value string) *RequestBuilder {
  36. r.headers.Set(key, value)
  37. return r
  38. }
  39. func (r *RequestBuilder) WithETag(etag string) *RequestBuilder {
  40. if etag != "" {
  41. r.headers.Set("If-None-Match", etag)
  42. }
  43. return r
  44. }
  45. func (r *RequestBuilder) WithLastModified(lastModified string) *RequestBuilder {
  46. if lastModified != "" {
  47. r.headers.Set("If-Modified-Since", lastModified)
  48. }
  49. return r
  50. }
  51. func (r *RequestBuilder) WithUserAgent(userAgent string, defaultUserAgent string) *RequestBuilder {
  52. if userAgent != "" {
  53. r.headers.Set("User-Agent", userAgent)
  54. } else {
  55. r.headers.Set("User-Agent", defaultUserAgent)
  56. }
  57. return r
  58. }
  59. func (r *RequestBuilder) WithCookie(cookie string) *RequestBuilder {
  60. if cookie != "" {
  61. r.headers.Set("Cookie", cookie)
  62. }
  63. return r
  64. }
  65. func (r *RequestBuilder) WithUsernameAndPassword(username, password string) *RequestBuilder {
  66. if username != "" && password != "" {
  67. r.headers.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
  68. }
  69. return r
  70. }
  71. func (r *RequestBuilder) WithProxyRotator(proxyRotator *proxyrotator.ProxyRotator) *RequestBuilder {
  72. r.proxyRotator = proxyRotator
  73. return r
  74. }
  75. func (r *RequestBuilder) WithCustomApplicationProxyURL(proxyURL *url.URL) *RequestBuilder {
  76. r.clientProxyURL = proxyURL
  77. return r
  78. }
  79. func (r *RequestBuilder) UseCustomApplicationProxyURL(value bool) *RequestBuilder {
  80. r.useClientProxy = value
  81. return r
  82. }
  83. func (r *RequestBuilder) WithTimeout(timeout int) *RequestBuilder {
  84. r.clientTimeout = timeout
  85. return r
  86. }
  87. func (r *RequestBuilder) WithoutRedirects() *RequestBuilder {
  88. r.withoutRedirects = true
  89. return r
  90. }
  91. func (r *RequestBuilder) DisableHTTP2(value bool) *RequestBuilder {
  92. r.disableHTTP2 = value
  93. return r
  94. }
  95. func (r *RequestBuilder) IgnoreTLSErrors(value bool) *RequestBuilder {
  96. r.ignoreTLSErrors = value
  97. return r
  98. }
  99. func (r *RequestBuilder) ExecuteRequest(requestURL string) (*http.Response, error) {
  100. // We get the safe ciphers
  101. ciphers := tls.CipherSuites()
  102. if r.ignoreTLSErrors {
  103. // and the insecure ones if we are ignoring TLS errors. This allows to connect to badly configured servers anyway
  104. ciphers = append(ciphers, tls.InsecureCipherSuites()...)
  105. }
  106. cipherSuites := []uint16{}
  107. for _, cipher := range ciphers {
  108. cipherSuites = append(cipherSuites, cipher.ID)
  109. }
  110. transport := &http.Transport{
  111. Proxy: http.ProxyFromEnvironment,
  112. // Setting `DialContext` disables HTTP/2, this option forces the transport to try HTTP/2 regardless.
  113. ForceAttemptHTTP2: true,
  114. DialContext: (&net.Dialer{
  115. // Default is 30s.
  116. Timeout: 10 * time.Second,
  117. // Default is 30s.
  118. KeepAlive: 15 * time.Second,
  119. }).DialContext,
  120. // Default is 100.
  121. MaxIdleConns: 50,
  122. // Default is 90s.
  123. IdleConnTimeout: 10 * time.Second,
  124. TLSClientConfig: &tls.Config{
  125. CipherSuites: cipherSuites,
  126. InsecureSkipVerify: r.ignoreTLSErrors,
  127. },
  128. }
  129. if r.disableHTTP2 {
  130. transport.ForceAttemptHTTP2 = false
  131. // https://pkg.go.dev/net/http#hdr-HTTP_2
  132. // Programs that must disable HTTP/2 can do so by setting [Transport.TLSNextProto] (for clients) or [Server.TLSNextProto] (for servers) to a non-nil, empty map.
  133. transport.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{}
  134. }
  135. var clientProxyURL *url.URL
  136. if r.useClientProxy && r.clientProxyURL != nil {
  137. clientProxyURL = r.clientProxyURL
  138. } else if r.proxyRotator != nil && r.proxyRotator.HasProxies() {
  139. clientProxyURL = r.proxyRotator.GetNextProxy()
  140. }
  141. var clientProxyURLRedacted string
  142. if clientProxyURL != nil {
  143. transport.Proxy = http.ProxyURL(clientProxyURL)
  144. clientProxyURLRedacted = clientProxyURL.Redacted()
  145. }
  146. client := &http.Client{
  147. Timeout: time.Duration(r.clientTimeout) * time.Second,
  148. }
  149. if r.withoutRedirects {
  150. client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
  151. return http.ErrUseLastResponse
  152. }
  153. }
  154. client.Transport = transport
  155. req, err := http.NewRequest("GET", requestURL, nil)
  156. if err != nil {
  157. return nil, err
  158. }
  159. req.Header = r.headers
  160. req.Header.Set("Accept-Encoding", "br, gzip")
  161. req.Header.Set("Accept", defaultAcceptHeader)
  162. req.Header.Set("Connection", "close")
  163. slog.Debug("Making outgoing request", slog.Group("request",
  164. slog.String("method", req.Method),
  165. slog.String("url", req.URL.String()),
  166. slog.Any("headers", req.Header),
  167. slog.Bool("without_redirects", r.withoutRedirects),
  168. slog.Bool("use_app_client_proxy", r.useClientProxy),
  169. slog.String("client_proxy_url", clientProxyURLRedacted),
  170. slog.Bool("ignore_tls_errors", r.ignoreTLSErrors),
  171. slog.Bool("disable_http2", r.disableHTTP2),
  172. ))
  173. return client.Do(req)
  174. }