From 8a56a6836a9454d8cafc823639c27c93ce6a6823 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 8 Oct 2016 14:00:50 +0100 Subject: [PATCH] Check server time against local time #654 --- fs/http.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/fs/http.go b/fs/http.go index c8f5f20c8..e2b225e2f 100644 --- a/fs/http.go +++ b/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 }