2018-01-12 16:30:54 +00:00
|
|
|
// Package fshttp contains the common http parts of the config, Transport and Client
|
|
|
|
package fshttp
|
2016-09-10 18:17:43 +00:00
|
|
|
|
|
|
|
import (
|
2016-11-02 15:53:43 +00:00
|
|
|
"bytes"
|
2018-04-06 18:13:27 +00:00
|
|
|
"context"
|
2016-09-10 18:17:43 +00:00
|
|
|
"crypto/tls"
|
2019-02-11 13:09:32 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"log"
|
2016-09-10 18:17:43 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-01-11 17:35:29 +00:00
|
|
|
"net/http/cookiejar"
|
2016-09-10 18:17:43 +00:00
|
|
|
"net/http/httputil"
|
2022-08-20 14:38:02 +00:00
|
|
|
"os"
|
2016-09-10 18:17:43 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-07-13 04:11:24 +00:00
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2021-01-06 12:06:00 +00:00
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
2020-06-05 11:15:07 +00:00
|
|
|
"github.com/rclone/rclone/lib/structs"
|
2019-01-11 17:35:29 +00:00
|
|
|
"golang.org/x/net/publicsuffix"
|
2016-09-10 18:17:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
separatorReq = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
|
|
|
|
separatorResp = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-01-11 17:35:29 +00:00
|
|
|
transport http.RoundTripper
|
2019-03-08 20:33:22 +00:00
|
|
|
noTransport = new(sync.Once)
|
2019-01-11 17:35:29 +00:00
|
|
|
cookieJar, _ = cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
2020-12-14 19:10:11 +00:00
|
|
|
logMutex sync.Mutex
|
2016-09-10 18:17:43 +00:00
|
|
|
)
|
|
|
|
|
2019-03-08 20:33:22 +00:00
|
|
|
// ResetTransport resets the existing transport, allowing it to take new settings.
|
|
|
|
// Should only be used for testing.
|
|
|
|
func ResetTransport() {
|
|
|
|
noTransport = new(sync.Once)
|
|
|
|
}
|
|
|
|
|
2019-10-16 10:21:26 +00:00
|
|
|
// NewTransportCustom returns an http.RoundTripper with the correct timeouts.
|
|
|
|
// The customize function is called if set to give the caller an opportunity to
|
|
|
|
// customize any defaults in the Transport.
|
2020-11-13 15:24:43 +00:00
|
|
|
func NewTransportCustom(ctx context.Context, customize func(*http.Transport)) http.RoundTripper {
|
|
|
|
ci := fs.GetConfig(ctx)
|
2019-10-16 10:21:26 +00:00
|
|
|
// Start with a sensible set of defaults then override.
|
|
|
|
// This also means we get new stuff when it gets added to go
|
|
|
|
t := new(http.Transport)
|
2020-06-05 11:15:07 +00:00
|
|
|
structs.SetDefaults(t, http.DefaultTransport.(*http.Transport))
|
2019-10-16 10:21:26 +00:00
|
|
|
t.Proxy = http.ProxyFromEnvironment
|
|
|
|
t.MaxIdleConnsPerHost = 2 * (ci.Checkers + ci.Transfers + 1)
|
|
|
|
t.MaxIdleConns = 2 * t.MaxIdleConnsPerHost
|
|
|
|
t.TLSHandshakeTimeout = ci.ConnectTimeout
|
|
|
|
t.ResponseHeaderTimeout = ci.Timeout
|
2022-05-30 09:14:37 +00:00
|
|
|
t.DisableKeepAlives = ci.DisableHTTPKeepAlives
|
2019-10-16 10:21:26 +00:00
|
|
|
|
|
|
|
// TLS Config
|
|
|
|
t.TLSClientConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: ci.InsecureSkipVerify,
|
|
|
|
}
|
2019-02-11 13:09:32 +00:00
|
|
|
|
2019-10-16 10:21:26 +00:00
|
|
|
// Load client certs
|
|
|
|
if ci.ClientCert != "" || ci.ClientKey != "" {
|
|
|
|
if ci.ClientCert == "" || ci.ClientKey == "" {
|
|
|
|
log.Fatalf("Both --client-cert and --client-key must be set")
|
2019-02-11 13:09:32 +00:00
|
|
|
}
|
2019-10-16 10:21:26 +00:00
|
|
|
cert, err := tls.LoadX509KeyPair(ci.ClientCert, ci.ClientKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to load --client-cert/--client-key pair: %v", err)
|
2019-02-11 13:09:32 +00:00
|
|
|
}
|
2019-10-16 10:21:26 +00:00
|
|
|
t.TLSClientConfig.Certificates = []tls.Certificate{cert}
|
|
|
|
}
|
2019-02-11 13:09:32 +00:00
|
|
|
|
2023-01-11 07:59:51 +00:00
|
|
|
// Load CA certs
|
|
|
|
if len(ci.CaCert) != 0 {
|
|
|
|
|
2019-10-16 10:21:26 +00:00
|
|
|
caCertPool := x509.NewCertPool()
|
2023-01-11 07:59:51 +00:00
|
|
|
|
|
|
|
for _, cert := range ci.CaCert {
|
|
|
|
caCert, err := os.ReadFile(cert)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to read --ca-cert file %q : %v", cert, err)
|
|
|
|
}
|
|
|
|
ok := caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
if !ok {
|
|
|
|
log.Fatalf("Failed to add certificates from --ca-cert file %q", cert)
|
|
|
|
}
|
2018-04-06 19:33:51 +00:00
|
|
|
}
|
2019-10-16 10:21:26 +00:00
|
|
|
t.TLSClientConfig.RootCAs = caCertPool
|
|
|
|
}
|
|
|
|
|
|
|
|
t.DisableCompression = ci.NoGzip
|
2023-11-24 17:53:33 +00:00
|
|
|
t.DialContext = func(reqCtx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
return NewDialer(ctx).DialContext(reqCtx, network, addr)
|
2019-10-16 10:21:26 +00:00
|
|
|
}
|
|
|
|
t.IdleConnTimeout = 60 * time.Second
|
2020-01-09 14:00:46 +00:00
|
|
|
t.ExpectContinueTimeout = ci.ExpectContinueTimeout
|
2019-10-16 10:21:26 +00:00
|
|
|
|
2020-01-21 16:34:29 +00:00
|
|
|
if ci.Dump&(fs.DumpHeaders|fs.DumpBodies|fs.DumpAuth|fs.DumpRequests|fs.DumpResponses) != 0 {
|
|
|
|
fs.Debugf(nil, "You have specified to dump information. Please be noted that the "+
|
|
|
|
"Accept-Encoding as shown may not be correct in the request and the response may not show "+
|
|
|
|
"Content-Encoding if the go standard libraries auto gzip encoding was in effect. In this case"+
|
|
|
|
" the body of the request will be gunzipped before showing it.")
|
|
|
|
}
|
|
|
|
|
2021-04-19 16:00:07 +00:00
|
|
|
if ci.DisableHTTP2 {
|
|
|
|
t.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{}
|
|
|
|
}
|
|
|
|
|
2019-10-16 10:21:26 +00:00
|
|
|
// customize the transport if required
|
|
|
|
if customize != nil {
|
|
|
|
customize(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap that http.Transport in our own transport
|
|
|
|
return newTransport(ci, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTransport returns an http.RoundTripper with the correct timeouts
|
2020-11-13 15:24:43 +00:00
|
|
|
func NewTransport(ctx context.Context) http.RoundTripper {
|
2019-10-16 10:21:26 +00:00
|
|
|
(*noTransport).Do(func() {
|
2020-11-13 15:24:43 +00:00
|
|
|
transport = NewTransportCustom(ctx, nil)
|
2016-09-10 18:17:43 +00:00
|
|
|
})
|
|
|
|
return transport
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// NewClient returns an http.Client with the correct timeouts
|
2020-11-13 15:24:43 +00:00
|
|
|
func NewClient(ctx context.Context) *http.Client {
|
|
|
|
ci := fs.GetConfig(ctx)
|
2019-10-16 10:21:26 +00:00
|
|
|
client := &http.Client{
|
2020-11-13 15:24:43 +00:00
|
|
|
Transport: NewTransport(ctx),
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
2019-01-11 17:35:29 +00:00
|
|
|
if ci.Cookie {
|
2019-10-16 10:21:26 +00:00
|
|
|
client.Jar = cookieJar
|
2019-01-11 17:35:29 +00:00
|
|
|
}
|
2019-10-16 10:21:26 +00:00
|
|
|
return client
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 10:39:20 +00:00
|
|
|
// Transport is our http Transport which wraps an http.Transport
|
2016-09-10 18:17:43 +00:00
|
|
|
// * Sets the User Agent
|
|
|
|
// * Does logging
|
2021-09-06 14:42:23 +00:00
|
|
|
// * Updates metrics
|
2016-09-10 18:17:43 +00:00
|
|
|
type Transport struct {
|
|
|
|
*http.Transport
|
2018-01-12 16:30:54 +00:00
|
|
|
dump fs.DumpFlags
|
2017-11-24 09:07:56 +00:00
|
|
|
filterRequest func(req *http.Request)
|
2018-01-12 16:30:54 +00:00
|
|
|
userAgent string
|
2020-04-23 14:24:21 +00:00
|
|
|
headers []*fs.HTTPOption
|
2021-09-06 14:42:23 +00:00
|
|
|
metrics *Metrics
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// newTransport wraps the http.Transport passed in and logs all
|
2016-09-10 18:17:43 +00:00
|
|
|
// roundtrips including the body if logBody is set.
|
2018-01-12 16:30:54 +00:00
|
|
|
func newTransport(ci *fs.ConfigInfo, transport *http.Transport) *Transport {
|
2016-09-10 18:17:43 +00:00
|
|
|
return &Transport{
|
|
|
|
Transport: transport,
|
2018-01-12 16:30:54 +00:00
|
|
|
dump: ci.Dump,
|
|
|
|
userAgent: ci.UserAgent,
|
2020-04-23 14:24:21 +00:00
|
|
|
headers: ci.Headers,
|
2021-09-06 14:42:23 +00:00
|
|
|
metrics: DefaultMetrics,
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 09:07:56 +00:00
|
|
|
// SetRequestFilter sets a filter to be used on each request
|
|
|
|
func (t *Transport) SetRequestFilter(f func(req *http.Request)) {
|
|
|
|
t.filterRequest = f
|
|
|
|
}
|
|
|
|
|
2016-10-30 19:16:27 +00:00
|
|
|
// A mutex to protect this map
|
|
|
|
var checkedHostMu sync.RWMutex
|
|
|
|
|
2016-10-08 13:00:50 +00:00
|
|
|
// A map of servers we have checked for time
|
|
|
|
var checkedHost = make(map[string]struct{}, 1)
|
|
|
|
|
|
|
|
// Check the server time is the same as ours, once for each server
|
|
|
|
func checkServerTime(req *http.Request, resp *http.Response) {
|
|
|
|
host := req.URL.Host
|
|
|
|
if req.Host != "" {
|
|
|
|
host = req.Host
|
|
|
|
}
|
2016-10-30 19:16:27 +00:00
|
|
|
checkedHostMu.RLock()
|
|
|
|
_, ok := checkedHost[host]
|
|
|
|
checkedHostMu.RUnlock()
|
|
|
|
if ok {
|
2016-10-08 13:00:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dateString := resp.Header.Get("Date")
|
|
|
|
if dateString == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
date, err := http.ParseTime(dateString)
|
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.Debugf(nil, "Couldn't parse Date: from server %s: %q: %v", host, dateString, err)
|
2016-10-08 13:00:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dt := time.Since(date)
|
|
|
|
const window = 5 * 60 * time.Second
|
|
|
|
if dt > window || dt < -window {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.Logf(nil, "Time may be set wrong - time from %q is %v different from this computer", host, dt)
|
2016-10-08 13:00:50 +00:00
|
|
|
}
|
2016-10-30 19:16:27 +00:00
|
|
|
checkedHostMu.Lock()
|
2016-10-08 13:00:50 +00:00
|
|
|
checkedHost[host] = struct{}{}
|
2016-10-30 19:16:27 +00:00
|
|
|
checkedHostMu.Unlock()
|
2016-10-08 13:00:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 12:14:52 +00:00
|
|
|
// cleanAuth gets rid of one authBuf header within the first 4k
|
|
|
|
func cleanAuth(buf, authBuf []byte) []byte {
|
2016-11-02 15:53:43 +00:00
|
|
|
// Find how much buffer to check
|
|
|
|
n := 4096
|
|
|
|
if len(buf) < n {
|
|
|
|
n = len(buf)
|
|
|
|
}
|
|
|
|
// See if there is an Authorization: header
|
|
|
|
i := bytes.Index(buf[:n], authBuf)
|
|
|
|
if i < 0 {
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
i += len(authBuf)
|
|
|
|
// Overwrite the next 4 chars with 'X'
|
|
|
|
for j := 0; i < len(buf) && j < 4; j++ {
|
|
|
|
if buf[i] == '\n' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
buf[i] = 'X'
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
// Snip out to the next '\n'
|
|
|
|
j := bytes.IndexByte(buf[i:], '\n')
|
|
|
|
if j < 0 {
|
|
|
|
return buf[:i]
|
|
|
|
}
|
|
|
|
n = copy(buf[i:], buf[i+j:])
|
|
|
|
return buf[:i+n]
|
|
|
|
}
|
|
|
|
|
2017-11-21 12:14:52 +00:00
|
|
|
var authBufs = [][]byte{
|
|
|
|
[]byte("Authorization: "),
|
|
|
|
[]byte("X-Auth-Token: "),
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanAuths gets rid of all the possible Auth headers
|
|
|
|
func cleanAuths(buf []byte) []byte {
|
|
|
|
for _, authBuf := range authBufs {
|
|
|
|
buf = cleanAuth(buf, authBuf)
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2016-09-10 18:17:43 +00:00
|
|
|
// RoundTrip implements the RoundTripper interface.
|
|
|
|
func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
2021-01-06 12:06:00 +00:00
|
|
|
// Limit transactions per second if required
|
|
|
|
accounting.LimitTPS(req.Context())
|
2016-09-10 18:17:43 +00:00
|
|
|
// Force user agent
|
2018-01-12 16:30:54 +00:00
|
|
|
req.Header.Set("User-Agent", t.userAgent)
|
2020-04-23 14:24:21 +00:00
|
|
|
// Set user defined headers
|
|
|
|
for _, option := range t.headers {
|
|
|
|
req.Header.Set(option.Key, option.Value)
|
|
|
|
}
|
2017-11-24 09:07:56 +00:00
|
|
|
// Filter the request if required
|
|
|
|
if t.filterRequest != nil {
|
|
|
|
t.filterRequest(req)
|
|
|
|
}
|
2017-02-09 11:01:20 +00:00
|
|
|
// Logf request
|
2018-01-12 16:30:54 +00:00
|
|
|
if t.dump&(fs.DumpHeaders|fs.DumpBodies|fs.DumpAuth|fs.DumpRequests|fs.DumpResponses) != 0 {
|
|
|
|
buf, _ := httputil.DumpRequestOut(req, t.dump&(fs.DumpBodies|fs.DumpRequests) != 0)
|
|
|
|
if t.dump&fs.DumpAuth == 0 {
|
2017-11-21 12:14:52 +00:00
|
|
|
buf = cleanAuths(buf)
|
2016-11-02 15:53:43 +00:00
|
|
|
}
|
2020-12-14 19:10:11 +00:00
|
|
|
logMutex.Lock()
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.Debugf(nil, "%s", separatorReq)
|
|
|
|
fs.Debugf(nil, "%s (req %p)", "HTTP REQUEST", req)
|
|
|
|
fs.Debugf(nil, "%s", string(buf))
|
|
|
|
fs.Debugf(nil, "%s", separatorReq)
|
2020-12-14 19:10:11 +00:00
|
|
|
logMutex.Unlock()
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
|
|
|
// Do round trip
|
|
|
|
resp, err = t.Transport.RoundTrip(req)
|
2017-02-09 11:01:20 +00:00
|
|
|
// Logf response
|
2018-01-12 16:30:54 +00:00
|
|
|
if t.dump&(fs.DumpHeaders|fs.DumpBodies|fs.DumpAuth|fs.DumpRequests|fs.DumpResponses) != 0 {
|
2020-12-14 19:10:11 +00:00
|
|
|
logMutex.Lock()
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.Debugf(nil, "%s", separatorResp)
|
|
|
|
fs.Debugf(nil, "%s (req %p)", "HTTP RESPONSE", req)
|
2016-09-10 18:17:43 +00:00
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.Debugf(nil, "Error: %v", err)
|
2016-09-10 18:17:43 +00:00
|
|
|
} else {
|
2018-01-12 16:30:54 +00:00
|
|
|
buf, _ := httputil.DumpResponse(resp, t.dump&(fs.DumpBodies|fs.DumpResponses) != 0)
|
|
|
|
fs.Debugf(nil, "%s", string(buf))
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.Debugf(nil, "%s", separatorResp)
|
2020-12-14 19:10:11 +00:00
|
|
|
logMutex.Unlock()
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
2021-09-06 14:42:23 +00:00
|
|
|
// Update metrics
|
|
|
|
t.metrics.onResponse(req, resp)
|
|
|
|
|
2016-10-08 13:00:50 +00:00
|
|
|
if err == nil {
|
|
|
|
checkServerTime(req, resp)
|
|
|
|
}
|
2016-09-10 18:17:43 +00:00
|
|
|
return resp, err
|
|
|
|
}
|