2017-05-01 17:30:52 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
2017-09-24 18:04:23 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2018-01-27 12:57:43 +00:00
|
|
|
"encoding/pem"
|
2017-05-01 17:30:52 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-12-02 18:36:43 +00:00
|
|
|
"os"
|
2018-01-27 12:57:43 +00:00
|
|
|
"strings"
|
2017-05-01 17:30:52 +00:00
|
|
|
"time"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
2024-01-29 03:23:56 +00:00
|
|
|
"github.com/peterbourgon/unixtransport"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2018-01-27 12:57:43 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2024-05-01 18:06:54 +00:00
|
|
|
"github.com/restic/restic/internal/feature"
|
2024-05-01 18:03:31 +00:00
|
|
|
"golang.org/x/net/http2"
|
2017-05-01 17:30:52 +00:00
|
|
|
)
|
|
|
|
|
2018-01-27 12:57:43 +00:00
|
|
|
// TransportOptions collects various options which can be set for an HTTP based
|
|
|
|
// transport.
|
|
|
|
type TransportOptions struct {
|
|
|
|
// contains filenames of PEM encoded root certificates to trust
|
|
|
|
RootCertFilenames []string
|
|
|
|
|
|
|
|
// contains the name of a file containing the TLS client certificate and private key in PEM format
|
|
|
|
TLSClientCertKeyFilename string
|
2019-12-12 22:10:23 +00:00
|
|
|
|
|
|
|
// Skip TLS certificate verification
|
|
|
|
InsecureTLS bool
|
2024-05-26 22:03:11 +00:00
|
|
|
|
|
|
|
// Specify Custom User-Agent for the http Client
|
|
|
|
HTTPUserAgent string
|
2018-01-27 12:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// readPEMCertKey reads a file and returns the PEM encoded certificate and key
|
|
|
|
// blocks.
|
|
|
|
func readPEMCertKey(filename string) (certs []byte, key []byte, err error) {
|
2022-12-02 18:36:43 +00:00
|
|
|
data, err := os.ReadFile(filename)
|
2018-01-27 12:57:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "ReadFile")
|
|
|
|
}
|
|
|
|
|
|
|
|
var block *pem.Block
|
|
|
|
for {
|
|
|
|
if len(data) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
block, data = pem.Decode(data)
|
|
|
|
if block == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.HasSuffix(block.Type, "CERTIFICATE"):
|
|
|
|
certs = append(certs, pem.EncodeToMemory(block)...)
|
|
|
|
case strings.HasSuffix(block.Type, "PRIVATE KEY"):
|
|
|
|
if key != nil {
|
|
|
|
return nil, nil, errors.Errorf("error loading TLS cert and key from %v: more than one private key found", filename)
|
|
|
|
}
|
|
|
|
key = pem.EncodeToMemory(block)
|
|
|
|
default:
|
|
|
|
return nil, nil, errors.Errorf("error loading TLS cert and key from %v: unknown block type %v found", filename, block.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return certs, key, nil
|
|
|
|
}
|
|
|
|
|
2017-09-24 18:04:23 +00:00
|
|
|
// Transport returns a new http.RoundTripper with default settings applied. If
|
|
|
|
// a custom rootCertFilename is non-empty, it must point to a valid PEM file,
|
|
|
|
// otherwise the function will return an error.
|
2018-01-27 12:57:43 +00:00
|
|
|
func Transport(opts TransportOptions) (http.RoundTripper, error) {
|
2017-05-01 17:30:52 +00:00
|
|
|
// copied from net/http
|
|
|
|
tr := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}).DialContext,
|
|
|
|
MaxIdleConns: 100,
|
2017-05-31 17:39:19 +00:00
|
|
|
MaxIdleConnsPerHost: 100,
|
2017-05-01 17:30:52 +00:00
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
2017-12-30 03:51:13 +00:00
|
|
|
TLSClientConfig: &tls.Config{},
|
|
|
|
}
|
|
|
|
|
2024-05-01 18:03:31 +00:00
|
|
|
// ensure that http2 connections are closed if they are broken
|
|
|
|
h2, err := http2.ConfigureTransports(tr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-05-11 18:22:10 +00:00
|
|
|
if feature.Flag.Enabled(feature.BackendErrorRedesign) {
|
2024-05-01 18:03:31 +00:00
|
|
|
h2.WriteByteTimeout = 120 * time.Second
|
|
|
|
h2.ReadIdleTimeout = 60 * time.Second
|
|
|
|
h2.PingTimeout = 60 * time.Second
|
|
|
|
}
|
|
|
|
|
2024-01-29 03:23:56 +00:00
|
|
|
unixtransport.Register(tr)
|
|
|
|
|
2019-12-12 22:10:23 +00:00
|
|
|
if opts.InsecureTLS {
|
|
|
|
tr.TLSClientConfig.InsecureSkipVerify = true
|
|
|
|
}
|
|
|
|
|
2018-01-27 12:57:43 +00:00
|
|
|
if opts.TLSClientCertKeyFilename != "" {
|
|
|
|
certs, key, err := readPEMCertKey(opts.TLSClientCertKeyFilename)
|
2017-12-30 03:51:13 +00:00
|
|
|
if err != nil {
|
2018-01-27 12:57:43 +00:00
|
|
|
return nil, err
|
2017-12-30 03:51:13 +00:00
|
|
|
}
|
2017-09-24 18:04:23 +00:00
|
|
|
|
2018-01-27 12:57:43 +00:00
|
|
|
crt, err := tls.X509KeyPair(certs, key)
|
2017-09-24 18:04:23 +00:00
|
|
|
if err != nil {
|
2018-01-27 12:57:43 +00:00
|
|
|
return nil, errors.Errorf("parse TLS client cert or key: %v", err)
|
2017-09-24 18:04:23 +00:00
|
|
|
}
|
2018-01-27 12:57:43 +00:00
|
|
|
tr.TLSClientConfig.Certificates = []tls.Certificate{crt}
|
2017-09-24 18:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 12:57:43 +00:00
|
|
|
if opts.RootCertFilenames != nil {
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
for _, filename := range opts.RootCertFilenames {
|
|
|
|
if filename == "" {
|
|
|
|
return nil, errors.Errorf("empty filename for root certificate supplied")
|
|
|
|
}
|
2022-12-02 18:36:43 +00:00
|
|
|
b, err := os.ReadFile(filename)
|
2018-01-27 12:57:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("unable to read root certificate: %v", err)
|
|
|
|
}
|
|
|
|
if ok := pool.AppendCertsFromPEM(b); !ok {
|
|
|
|
return nil, errors.Errorf("cannot parse root certificate from %q", filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tr.TLSClientConfig.RootCAs = pool
|
|
|
|
}
|
2017-09-24 18:04:23 +00:00
|
|
|
|
2024-05-01 18:06:54 +00:00
|
|
|
rt := http.RoundTripper(tr)
|
2024-05-26 22:03:11 +00:00
|
|
|
|
|
|
|
// if the userAgent is set in the Transport Options, wrap the
|
|
|
|
// http.RoundTripper
|
|
|
|
if opts.HTTPUserAgent != "" {
|
|
|
|
rt = newCustomUserAgentRoundTripper(rt, opts.HTTPUserAgent)
|
|
|
|
}
|
|
|
|
|
2024-05-11 18:22:10 +00:00
|
|
|
if feature.Flag.Enabled(feature.BackendErrorRedesign) {
|
2024-06-01 16:54:01 +00:00
|
|
|
rt = newWatchdogRoundtripper(rt, 5*time.Minute, 128*1024)
|
2024-05-01 18:06:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 12:57:43 +00:00
|
|
|
// wrap in the debug round tripper (if active)
|
2024-05-01 18:06:54 +00:00
|
|
|
return debug.RoundTripper(rt), nil
|
2017-05-01 17:30:52 +00:00
|
|
|
}
|