mirror of
https://github.com/rclone/rclone.git
synced 2025-01-13 20:38:12 +02:00
Check server time against local time #654
This commit is contained in:
parent
83849e0a36
commit
8a56a6836a
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.
|
||||
func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
||||
// Force user agent
|
||||
@ -169,5 +198,8 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
|
||||
}
|
||||
Debug(nil, "%s", separatorResp)
|
||||
}
|
||||
if err == nil {
|
||||
checkServerTime(req, resp)
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user