2016-09-10 18:17:43 +00:00
|
|
|
// The HTTP based parts of the config, Transport and Client
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2016-11-02 15:53:43 +00:00
|
|
|
"bytes"
|
2016-09-10 18:17:43 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2017-07-13 04:11:24 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/context" // switch to "context" when we stop supporting go1.6
|
|
|
|
"golang.org/x/time/rate"
|
2016-09-10 18:17:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
separatorReq = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
|
|
|
|
separatorResp = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
transport http.RoundTripper
|
|
|
|
noTransport sync.Once
|
2017-07-13 04:11:24 +00:00
|
|
|
tpsBucket *rate.Limiter // for limiting number of http transactions per second
|
2016-09-10 18:17:43 +00:00
|
|
|
)
|
|
|
|
|
2017-07-13 04:11:24 +00:00
|
|
|
// Start the token bucket if necessary
|
|
|
|
func startHTTPTokenBucket() {
|
|
|
|
if Config.TPSLimit > 0 {
|
|
|
|
tpsBurst := Config.TPSLimitBurst
|
|
|
|
if tpsBurst < 1 {
|
|
|
|
tpsBurst = 1
|
|
|
|
}
|
|
|
|
tpsBucket = rate.NewLimiter(rate.Limit(Config.TPSLimit), tpsBurst)
|
|
|
|
Infof(nil, "Starting HTTP transaction limiter: max %g transactions/s with burst %d", Config.TPSLimit, tpsBurst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 18:17:43 +00:00
|
|
|
// A net.Conn that sets a deadline for every Read or Write operation
|
|
|
|
type timeoutConn struct {
|
|
|
|
net.Conn
|
|
|
|
readTimer *time.Timer
|
|
|
|
writeTimer *time.Timer
|
|
|
|
timeout time.Duration
|
|
|
|
off time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a timeoutConn using the timeout
|
|
|
|
func newTimeoutConn(conn net.Conn, timeout time.Duration) *timeoutConn {
|
|
|
|
return &timeoutConn{
|
|
|
|
Conn: conn,
|
|
|
|
timeout: timeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 10:59:47 +00:00
|
|
|
// Nudge the deadline for an idle timeout on by c.timeout if non-zero
|
|
|
|
func (c *timeoutConn) nudgeDeadline() (err error) {
|
|
|
|
if c.timeout == 0 {
|
|
|
|
return nil
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
2016-10-05 10:59:47 +00:00
|
|
|
when := time.Now().Add(c.timeout)
|
|
|
|
return c.Conn.SetDeadline(when)
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 10:59:47 +00:00
|
|
|
// readOrWrite bytes doing idle timeouts
|
|
|
|
func (c *timeoutConn) readOrWrite(f func([]byte) (int, error), b []byte) (n int, err error) {
|
|
|
|
err = c.nudgeDeadline()
|
2016-09-10 18:17:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
2016-10-05 10:59:47 +00:00
|
|
|
n, err = f(b)
|
|
|
|
cerr := c.nudgeDeadline()
|
|
|
|
if err == nil && cerr != nil {
|
2016-09-10 18:17:43 +00:00
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2016-10-05 10:59:47 +00:00
|
|
|
// Read bytes doing idle timeouts
|
|
|
|
func (c *timeoutConn) Read(b []byte) (n int, err error) {
|
|
|
|
return c.readOrWrite(c.Conn.Read, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write bytes doing idle timeouts
|
|
|
|
func (c *timeoutConn) Write(b []byte) (n int, err error) {
|
|
|
|
return c.readOrWrite(c.Conn.Write, b)
|
|
|
|
}
|
|
|
|
|
2016-09-10 18:17:43 +00:00
|
|
|
// setDefaults for a from b
|
|
|
|
//
|
|
|
|
// Copy the public members from b to a. We can't just use a struct
|
|
|
|
// copy as Transport contains a private mutex.
|
|
|
|
func setDefaults(a, b interface{}) {
|
|
|
|
pt := reflect.TypeOf(a)
|
|
|
|
t := pt.Elem()
|
|
|
|
va := reflect.ValueOf(a).Elem()
|
|
|
|
vb := reflect.ValueOf(b).Elem()
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
aField := va.Field(i)
|
|
|
|
// Set a from b if it is public
|
|
|
|
if aField.CanSet() {
|
|
|
|
bField := vb.Field(i)
|
|
|
|
aField.Set(bField)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transport returns an http.RoundTripper with the correct timeouts
|
|
|
|
func (ci *ConfigInfo) Transport() http.RoundTripper {
|
|
|
|
noTransport.Do(func() {
|
|
|
|
// 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)
|
|
|
|
setDefaults(t, http.DefaultTransport.(*http.Transport))
|
|
|
|
t.Proxy = http.ProxyFromEnvironment
|
|
|
|
t.MaxIdleConnsPerHost = 4 * (ci.Checkers + ci.Transfers + 1)
|
|
|
|
t.TLSHandshakeTimeout = ci.ConnectTimeout
|
2016-10-08 09:12:19 +00:00
|
|
|
t.ResponseHeaderTimeout = ci.Timeout
|
2016-09-10 18:17:43 +00:00
|
|
|
t.TLSClientConfig = &tls.Config{InsecureSkipVerify: ci.InsecureSkipVerify}
|
|
|
|
t.DisableCompression = *noGzip
|
|
|
|
// Set in http_old.go initTransport
|
|
|
|
// t.Dial
|
|
|
|
// Set in http_new.go initTransport
|
|
|
|
// t.DialContext
|
|
|
|
// t.IdelConnTimeout
|
|
|
|
// t.ExpectContinueTimeout
|
|
|
|
ci.initTransport(t)
|
|
|
|
// Wrap that http.Transport in our own transport
|
2016-11-02 15:53:43 +00:00
|
|
|
transport = NewTransport(t, ci.DumpHeaders, ci.DumpBodies, ci.DumpAuth)
|
2016-09-10 18:17:43 +00:00
|
|
|
})
|
|
|
|
return transport
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client returns an http.Client with the correct timeouts
|
|
|
|
func (ci *ConfigInfo) Client() *http.Client {
|
|
|
|
return &http.Client{
|
|
|
|
Transport: ci.Transport(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transport is a our http Transport which wraps an http.Transport
|
|
|
|
// * Sets the User Agent
|
|
|
|
// * Does logging
|
|
|
|
type Transport struct {
|
|
|
|
*http.Transport
|
|
|
|
logHeader bool
|
|
|
|
logBody bool
|
2016-11-02 15:53:43 +00:00
|
|
|
logAuth bool
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTransport wraps the http.Transport passed in and logs all
|
|
|
|
// roundtrips including the body if logBody is set.
|
2016-11-02 15:53:43 +00:00
|
|
|
func NewTransport(transport *http.Transport, logHeader, logBody, logAuth bool) *Transport {
|
2016-09-10 18:17:43 +00:00
|
|
|
return &Transport{
|
|
|
|
Transport: transport,
|
|
|
|
logHeader: logHeader,
|
|
|
|
logBody: logBody,
|
2016-11-02 15:53:43 +00:00
|
|
|
logAuth: logAuth,
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-02-09 11:01:20 +00:00
|
|
|
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 {
|
2017-02-09 11:01:20 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-02 15:53:43 +00:00
|
|
|
var authBuf = []byte("Authorization: ")
|
|
|
|
|
|
|
|
// cleanAuth gets rid of one Authorization: header within the first 4k
|
|
|
|
func cleanAuth(buf []byte) []byte {
|
|
|
|
// 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]
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-07-13 04:11:24 +00:00
|
|
|
// Get transactions per second token first if limiting
|
|
|
|
if tpsBucket != nil {
|
|
|
|
tbErr := tpsBucket.Wait(context.Background()) // FIXME switch to req.Context() when we drop go1.6 support
|
|
|
|
if tbErr != nil {
|
|
|
|
Errorf(nil, "HTTP token bucket error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-09-10 18:17:43 +00:00
|
|
|
// Force user agent
|
|
|
|
req.Header.Set("User-Agent", UserAgent)
|
2017-02-09 11:01:20 +00:00
|
|
|
// Logf request
|
2016-11-02 15:53:43 +00:00
|
|
|
if t.logHeader || t.logBody || t.logAuth {
|
2016-09-10 18:17:43 +00:00
|
|
|
buf, _ := httputil.DumpRequestOut(req, t.logBody)
|
2016-11-02 15:53:43 +00:00
|
|
|
if !t.logAuth {
|
|
|
|
buf = cleanAuth(buf)
|
|
|
|
}
|
2017-02-09 11:01:20 +00:00
|
|
|
Debugf(nil, "%s", separatorReq)
|
|
|
|
Debugf(nil, "%s (req %p)", "HTTP REQUEST", req)
|
|
|
|
Debugf(nil, "%s", string(buf))
|
|
|
|
Debugf(nil, "%s", separatorReq)
|
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
|
2016-11-02 15:53:43 +00:00
|
|
|
if t.logHeader || t.logBody || t.logAuth {
|
2017-02-09 11:01:20 +00:00
|
|
|
Debugf(nil, "%s", separatorResp)
|
|
|
|
Debugf(nil, "%s (req %p)", "HTTP RESPONSE", req)
|
2016-09-10 18:17:43 +00:00
|
|
|
if err != nil {
|
2017-02-09 11:01:20 +00:00
|
|
|
Debugf(nil, "Error: %v", err)
|
2016-09-10 18:17:43 +00:00
|
|
|
} else {
|
|
|
|
buf, _ := httputil.DumpResponse(resp, t.logBody)
|
2017-02-09 11:01:20 +00:00
|
|
|
Debugf(nil, "%s", string(buf))
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
2017-02-09 11:01:20 +00:00
|
|
|
Debugf(nil, "%s", separatorResp)
|
2016-09-10 18:17:43 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
2017-07-23 15:10:23 +00:00
|
|
|
|
|
|
|
// NewDialer creates a net.Dialer structure with Timeout, Keepalive
|
|
|
|
// and LocalAddr set from rclone flags.
|
|
|
|
func (ci *ConfigInfo) NewDialer() *net.Dialer {
|
|
|
|
dialer := &net.Dialer{
|
|
|
|
Timeout: ci.ConnectTimeout,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}
|
|
|
|
if ci.BindAddr != nil {
|
|
|
|
dialer.LocalAddr = &net.TCPAddr{IP: ci.BindAddr}
|
|
|
|
}
|
|
|
|
return dialer
|
|
|
|
}
|