forked from TrueCloudLab/rclone
Check server time against local time #654
This commit is contained in:
parent
83849e0a36
commit
8a56a6836a
1 changed files with 32 additions and 0 deletions
32
fs/http.go
32
fs/http.go
|
@ -143,6 +143,35 @@ func NewTransport(transport *http.Transport, logHeader, logBody bool) *Transport
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
if _, ok := checkedHost[host]; ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dateString := resp.Header.Get("Date")
|
||||||
|
if dateString == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
date, err := http.ParseTime(dateString)
|
||||||
|
if err != nil {
|
||||||
|
Debug(nil, "Couldn't parse Date: from server %s: %q: %v", host, dateString, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dt := time.Since(date)
|
||||||
|
const window = 5 * 60 * time.Second
|
||||||
|
if dt > window || dt < -window {
|
||||||
|
Log(nil, "Time may be set wrong - time from %q is %v different from this computer", host, dt)
|
||||||
|
}
|
||||||
|
checkedHost[host] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
// RoundTrip implements the RoundTripper interface.
|
// RoundTrip implements the RoundTripper interface.
|
||||||
func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
||||||
// Force user agent
|
// Force user agent
|
||||||
|
@ -169,5 +198,8 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
|
||||||
}
|
}
|
||||||
Debug(nil, "%s", separatorResp)
|
Debug(nil, "%s", separatorResp)
|
||||||
}
|
}
|
||||||
|
if err == nil {
|
||||||
|
checkServerTime(req, resp)
|
||||||
|
}
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue