From 6295be786fc15cd8b2ec8b4c98632719deb5b0f7 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Mon, 6 Nov 2023 19:58:39 +0100 Subject: [PATCH] WebClient: add a ping URL Signed-off-by: Nicola Murino --- internal/httpd/httpd.go | 3 +++ internal/httpd/httpd_test.go | 19 ++++++++++++++++++- internal/httpd/server.go | 1 + internal/httpd/webclient.go | 7 +++++++ templates/webclient/editfile.html | 5 +++-- templates/webclient/files.html | 5 +++-- 6 files changed, 35 insertions(+), 5 deletions(-) diff --git a/internal/httpd/httpd.go b/internal/httpd/httpd.go index c54e8927..b1ad48bd 100644 --- a/internal/httpd/httpd.go +++ b/internal/httpd/httpd.go @@ -165,6 +165,7 @@ const ( webClientDirsPathDefault = "/web/client/dirs" webClientDownloadZipPathDefault = "/web/client/downloadzip" webClientProfilePathDefault = "/web/client/profile" + webClientPingPathDefault = "/web/client/ping" webClientMFAPathDefault = "/web/client/mfa" webClientTOTPGeneratePathDefault = "/web/client/totp/generate" webClientTOTPValidatePathDefault = "/web/client/totp/validate" @@ -264,6 +265,7 @@ var ( webClientDirsPath string webClientDownloadZipPath string webClientProfilePath string + webClientPingPath string webChangeClientPwdPath string webClientMFAPath string webClientTOTPGeneratePath string @@ -1083,6 +1085,7 @@ func updateWebClientURLs(baseURL string) { webClientDirsPath = path.Join(baseURL, webClientDirsPathDefault) webClientDownloadZipPath = path.Join(baseURL, webClientDownloadZipPathDefault) webClientProfilePath = path.Join(baseURL, webClientProfilePathDefault) + webClientPingPath = path.Join(baseURL, webClientPingPathDefault) webChangeClientPwdPath = path.Join(baseURL, webChangeClientPwdPathDefault) webClientLogoutPath = path.Join(baseURL, webClientLogoutPathDefault) webClientMFAPath = path.Join(baseURL, webClientMFAPathDefault) diff --git a/internal/httpd/httpd_test.go b/internal/httpd/httpd_test.go index 7fde2c28..bfadf5cd 100644 --- a/internal/httpd/httpd_test.go +++ b/internal/httpd/httpd_test.go @@ -180,6 +180,7 @@ const ( webClientDownloadZipPath = "/web/client/downloadzip" webChangeClientPwdPath = "/web/client/changepwd" webClientProfilePath = "/web/client/profile" + webClientPingPath = "/web/client/ping" webClientTwoFactorPath = "/web/client/twofactor" webClientTwoFactorRecoveryPath = "/web/client/twofactor-recovery" webClientLogoutPath = "/web/client/logout" @@ -12247,12 +12248,23 @@ func TestWebClientLoginMock(t *testing.T) { rr = executeRequest(req) checkResponseCode(t, http.StatusFound, rr) assert.Equal(t, webClientLoginPath, rr.Header().Get("Location")) + req, _ = http.NewRequest(http.MethodGet, webClientPingPath, nil) + req.RemoteAddr = defaultRemoteAddr + setBearerForReq(req, webToken) + rr = executeRequest(req) + checkResponseCode(t, http.StatusFound, rr) + assert.Equal(t, webClientLoginPath, rr.Header().Get("Location")) // now try to render client pages req, _ = http.NewRequest(http.MethodGet, webClientProfilePath, nil) req.RemoteAddr = defaultRemoteAddr setJWTCookieForReq(req, webToken) rr = executeRequest(req) checkResponseCode(t, http.StatusOK, rr) + req, _ = http.NewRequest(http.MethodGet, webClientPingPath, nil) + req.RemoteAddr = defaultRemoteAddr + setJWTCookieForReq(req, webToken) + rr = executeRequest(req) + checkResponseCode(t, http.StatusOK, rr) req, _ = http.NewRequest(http.MethodGet, webClientFilesPath, nil) req.RemoteAddr = defaultRemoteAddr setJWTCookieForReq(req, webToken) @@ -12269,7 +12281,12 @@ func TestWebClientLoginMock(t *testing.T) { rr = executeRequest(req) checkResponseCode(t, http.StatusFound, rr) assert.Equal(t, webClientLoginPath, rr.Header().Get("Location")) - + req, _ = http.NewRequest(http.MethodGet, webClientPingPath, nil) + req.RemoteAddr = defaultRemoteAddr + setJWTCookieForReq(req, webToken) + rr = executeRequest(req) + checkResponseCode(t, http.StatusFound, rr) + assert.Equal(t, webClientLoginPath, rr.Header().Get("Location")) // get a new token and use it after removing the user webToken, err = getJWTWebClientTokenFromTestServer(defaultUsername, defaultPassword) assert.NoError(t, err) diff --git a/internal/httpd/server.go b/internal/httpd/server.go index 757a1815..d48743bf 100644 --- a/internal/httpd/server.go +++ b/internal/httpd/server.go @@ -1550,6 +1550,7 @@ func (s *httpdServer) setupWebClientRoutes() { Post(webClientFileActionsPath+"/copy", copyUserFsEntry) router.With(s.checkAuthRequirements, s.refreshCookie). Post(webClientDownloadZipPath, s.handleWebClientDownloadZip) + router.With(s.checkAuthRequirements, s.refreshCookie).Get(webClientPingPath, s.handleClientPing) router.With(s.checkAuthRequirements, s.refreshCookie).Get(webClientProfilePath, s.handleClientGetProfile) router.With(s.checkAuthRequirements).Post(webClientProfilePath, s.handleWebClientProfilePost) diff --git a/internal/httpd/webclient.go b/internal/httpd/webclient.go index 9703f00c..285ba105 100644 --- a/internal/httpd/webclient.go +++ b/internal/httpd/webclient.go @@ -102,6 +102,7 @@ type baseClientPage struct { SharesURL string ShareURL string ProfileURL string + PingURL string ChangePwdURL string StaticURL string LogoutURL string @@ -540,6 +541,7 @@ func (s *httpdServer) getBaseClientPageData(title, currentURL string, r *http.Re SharesURL: webClientSharesPath, ShareURL: webClientSharePath, ProfileURL: webClientProfilePath, + PingURL: webClientPingPath, ChangePwdURL: webChangeClientPwdPath, StaticURL: webStaticFilesPath, LogoutURL: webClientLogoutPath, @@ -1792,3 +1794,8 @@ func (s *httpdServer) handleClientShareLoginPost(w http.ResponseWriter, r *http. s.renderClientMessagePage(w, r, "Share Login OK", "Share login successful, you can now use your link", http.StatusOK, nil, "") } + +func (s *httpdServer) handleClientPing(w http.ResponseWriter, r *http.Request) { + r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize) + render.PlainText(w, r, "PONG") +} diff --git a/templates/webclient/editfile.html b/templates/webclient/editfile.html index a61075aa..ad6f6045 100644 --- a/templates/webclient/editfile.html +++ b/templates/webclient/editfile.html @@ -105,8 +105,9 @@ explicit grant from the SFTPGo Team (support@sftpgo.com). var cmView; function keepAlive() { - axios.get('{{.ProfileURL}}',{ - timeout: 15000 + axios.get('{{.PingURL}}',{ + timeout: 15000, + responseType: 'text' }).catch(function (error){}); } diff --git a/templates/webclient/files.html b/templates/webclient/files.html index ca0f003b..5a8c33a4 100644 --- a/templates/webclient/files.html +++ b/templates/webclient/files.html @@ -208,8 +208,9 @@ explicit grant from the SFTPGo Team (support@sftpgo.com).