From 2356a7c1552e67178a0553888beed29164e0331a Mon Sep 17 00:00:00 2001 From: NI Date: Fri, 6 Dec 2019 18:58:59 +0800 Subject: [PATCH] Change static assets and websocket interface to `/sshwifty/assets` and `/sshwifty/socket`. This should allow user to map Sshwifty to a subdirectory behind a reverse proxy --- application/controller/controller.go | 38 +++++++++++++++++++++++++--- ui/app.js | 6 +++-- webpack.config.js | 2 +- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/application/controller/controller.go b/application/controller/controller.go index 92e70b3..5deee76 100644 --- a/application/controller/controller.go +++ b/application/controller/controller.go @@ -34,6 +34,11 @@ var ( http.StatusNotFound, "Page not found") ) +const ( + assetsURLPrefix = "/sshwifty/assets/" + assetsURLPrefixLen = len(assetsURLPrefix) +) + // handler is the main service dispatcher type handler struct { hostNameChecker string @@ -73,14 +78,39 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "/": err = serveController(h.homeCtl, w, r, clientLogger) - case "/socket": + case "/sshwifty/socket": err = serveController(h.socketCtl, w, r, clientLogger) + case "/robots.txt": + err = serveStaticCacheData( + "robots.txt", + staticFileExt(".txt"), + w, + r, + clientLogger) + + case "/favicon.ico": + err = serveStaticCacheData( + "favicon.ico", + staticFileExt(".ico"), + w, + r, + clientLogger) + + case "/manifest.json": + err = serveStaticCacheData( + "manifest.json", + staticFileExt(".json"), + w, + r, + clientLogger) + default: - if len(r.URL.Path) > 0 && strings.ToUpper(r.Method) == "GET" { + if strings.HasPrefix(r.URL.Path, assetsURLPrefix) && + strings.ToUpper(r.Method) == "GET" { err = serveStaticCacheData( - r.URL.Path[1:], - staticFileExt(r.URL.Path[1:]), + r.URL.Path[assetsURLPrefixLen:], + staticFileExt(r.URL.Path[assetsURLPrefixLen:]), w, r, clientLogger) diff --git a/ui/app.js b/ui/app.js index 71175d3..bf9d2bd 100644 --- a/ui/app.js +++ b/ui/app.js @@ -65,6 +65,8 @@ const mainTemplate = ` `.trim(); +const socksInterface = "/sshwifty/socket"; + function startApp(rootEl) { const pageTitle = document.title; @@ -149,7 +151,7 @@ function startApp(rootEl) { r = "ws://"; } - r += location.host + "/socket"; + r += location.host + socksInterface; return r; }, @@ -249,7 +251,7 @@ function startApp(rootEl) { ? null : await this.getSocketAuthKey(privateKey, this.key); - let h = await xhr.head("/socket", { + let h = await xhr.head(socksInterface, { "X-Key": authKey ? btoa(String.fromCharCode.apply(null, authKey)) : "" }); diff --git a/webpack.config.js b/webpack.config.js index d1c10dc..30dcfe3 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -185,7 +185,7 @@ module.exports = { devtool: process.env.NODE_ENV === "development" ? "inline-source-map" : "source-map", output: { - publicPath: "/", + publicPath: "/sshwifty/assets/", path: path.join(__dirname, ".tmp", "dist"), filename: process.env.NODE_ENV === "development" ? "[id].js" : "[hash].js" },