1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package fetcher // import "miniflux.app/v2/internal/reader/fetcher"
import (
"crypto/tls"
"encoding/base64"
"fmt"
"log/slog"
"net"
"net/http"
"net/url"
"slices"
"time"
"miniflux.app/v2/internal/proxyrotator"
)
const (
defaultHTTPClientTimeout = 20 * time.Second
defaultAcceptHeader = "application/xml, application/atom+xml, application/rss+xml, application/rdf+xml, application/feed+json, text/html, */*;q=0.9"
)
type RequestBuilder struct {
headers http.Header
clientProxyURL *url.URL
clientTimeout time.Duration
useClientProxy bool
withoutRedirects bool
ignoreTLSErrors bool
disableHTTP2 bool
disableCompression bool
proxyRotator *proxyrotator.ProxyRotator
feedProxyURL string
}
func NewRequestBuilder() *RequestBuilder {
return &RequestBuilder{
headers: make(http.Header),
clientTimeout: defaultHTTPClientTimeout,
}
}
func (r *RequestBuilder) WithHeader(key, value string) *RequestBuilder {
r.headers.Set(key, value)
return r
}
func (r *RequestBuilder) WithETag(etag string) *RequestBuilder {
if etag != "" {
r.headers.Set("If-None-Match", etag)
}
return r
}
func (r *RequestBuilder) WithLastModified(lastModified string) *RequestBuilder {
if lastModified != "" {
r.headers.Set("If-Modified-Since", lastModified)
}
return r
}
func (r *RequestBuilder) WithUserAgent(userAgent string, defaultUserAgent string) *RequestBuilder {
if userAgent != "" {
r.headers.Set("User-Agent", userAgent)
} else {
r.headers.Set("User-Agent", defaultUserAgent)
}
return r
}
func (r *RequestBuilder) WithCookie(cookie string) *RequestBuilder {
if cookie != "" {
r.headers.Set("Cookie", cookie)
}
return r
}
func (r *RequestBuilder) WithUsernameAndPassword(username, password string) *RequestBuilder {
if username != "" && password != "" {
r.headers.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
}
return r
}
func (r *RequestBuilder) WithProxyRotator(proxyRotator *proxyrotator.ProxyRotator) *RequestBuilder {
r.proxyRotator = proxyRotator
return r
}
func (r *RequestBuilder) WithCustomApplicationProxyURL(proxyURL *url.URL) *RequestBuilder {
r.clientProxyURL = proxyURL
return r
}
func (r *RequestBuilder) UseCustomApplicationProxyURL(value bool) *RequestBuilder {
r.useClientProxy = value
return r
}
func (r *RequestBuilder) WithCustomFeedProxyURL(proxyURL string) *RequestBuilder {
r.feedProxyURL = proxyURL
return r
}
func (r *RequestBuilder) WithTimeout(timeout time.Duration) *RequestBuilder {
r.clientTimeout = timeout
return r
}
func (r *RequestBuilder) WithoutRedirects() *RequestBuilder {
r.withoutRedirects = true
return r
}
func (r *RequestBuilder) DisableHTTP2(value bool) *RequestBuilder {
r.disableHTTP2 = value
return r
}
func (r *RequestBuilder) IgnoreTLSErrors(value bool) *RequestBuilder {
r.ignoreTLSErrors = value
return r
}
func (r *RequestBuilder) WithoutCompression() *RequestBuilder {
r.disableCompression = true
return r
}
func (r *RequestBuilder) ExecuteRequest(requestURL string) (*http.Response, error) {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
// Setting `DialContext` disables HTTP/2, this option forces the transport to try HTTP/2 regardless.
ForceAttemptHTTP2: true,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second, // Default is 30s.
KeepAlive: 15 * time.Second, // Default is 30s.
}).DialContext,
MaxIdleConns: 50, // Default is 100.
IdleConnTimeout: 10 * time.Second, // Default is 90s.
}
if r.ignoreTLSErrors {
// Add insecure ciphers if we are ignoring TLS errors. This allows to connect to badly configured servers anyway
ciphers := slices.Concat(tls.CipherSuites(), tls.InsecureCipherSuites())
cipherSuites := make([]uint16, 0, len(ciphers))
for _, cipher := range ciphers {
cipherSuites = append(cipherSuites, cipher.ID)
}
transport.TLSClientConfig = &tls.Config{
CipherSuites: cipherSuites,
InsecureSkipVerify: true,
}
}
if r.disableHTTP2 {
transport.ForceAttemptHTTP2 = false
// https://pkg.go.dev/net/http#hdr-HTTP_2
// 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.
transport.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{}
}
var clientProxyURL *url.URL
switch {
case r.feedProxyURL != "":
var err error
clientProxyURL, err = url.Parse(r.feedProxyURL)
if err != nil {
return nil, fmt.Errorf(`fetcher: invalid feed proxy URL %q: %w`, r.feedProxyURL, err)
}
case r.useClientProxy && r.clientProxyURL != nil:
clientProxyURL = r.clientProxyURL
case r.proxyRotator != nil && r.proxyRotator.HasProxies():
clientProxyURL = r.proxyRotator.GetNextProxy()
}
var clientProxyURLRedacted string
if clientProxyURL != nil {
transport.Proxy = http.ProxyURL(clientProxyURL)
clientProxyURLRedacted = clientProxyURL.Redacted()
}
client := &http.Client{
Timeout: r.clientTimeout,
}
if r.withoutRedirects {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
client.Transport = transport
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return nil, err
}
req.Header = r.headers
if r.disableCompression {
req.Header.Set("Accept-Encoding", "identity")
} else {
req.Header.Set("Accept-Encoding", "br, gzip")
}
// Set default Accept header if not already set.
// Note that for the media proxy requests, we need to forward the browser Accept header.
if req.Header.Get("Accept") == "" {
req.Header.Set("Accept", defaultAcceptHeader)
}
req.Header.Set("Connection", "close")
slog.Debug("Making outgoing request", slog.Group("request",
slog.String("method", req.Method),
slog.String("url", req.URL.String()),
slog.Any("headers", req.Header),
slog.Bool("without_redirects", r.withoutRedirects),
slog.Bool("use_app_client_proxy", r.useClientProxy),
slog.String("client_proxy_url", clientProxyURLRedacted),
slog.Bool("ignore_tls_errors", r.ignoreTLSErrors),
slog.Bool("disable_http2", r.disableHTTP2),
))
return client.Do(req)
}
|