diff --git a/package.json b/package.json index bb7b1e570..641bcace9 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,7 @@ "jsonwebtoken": "~8.5.1", "jwt-decode": "^3.1.2", "limiter": "^2.1.0", - "node-cloudflared-tunnel": "~1.0.6", + "node-cloudflared-tunnel": "~1.0.7", "nodemailer": "~6.6.5", "notp": "~2.0.3", "password-hash": "~1.2.2", diff --git a/server/server.js b/server/server.js index f3203545b..0734f5277 100644 --- a/server/server.js +++ b/server/server.js @@ -133,7 +133,7 @@ const { statusPageSocketHandler } = require("./socket-handlers/status-page-socke const databaseSocketHandler = require("./socket-handlers/database-socket-handler"); const TwoFA = require("./2fa"); const StatusPage = require("./model/status_page"); -const { cloudflaredSocketHandler } = require("./socket-handlers/cloudflared-socket-handler"); +const { cloudflaredSocketHandler, autoStart: cloudflaredAutoStart } = require("./socket-handlers/cloudflared-socket-handler"); app.use(express.json()); @@ -1406,6 +1406,9 @@ exports.entryPage = "dashboard"; initBackgroundJobs(args); + // Start cloudflared at the end if configured + await cloudflaredAutoStart(); + })(); async function updateMonitorNotification(monitorID, notificationIDList) { diff --git a/server/socket-handlers/cloudflared-socket-handler.js b/server/socket-handlers/cloudflared-socket-handler.js index f7c69ed2a..128c47884 100644 --- a/server/socket-handlers/cloudflared-socket-handler.js +++ b/server/socket-handlers/cloudflared-socket-handler.js @@ -1,17 +1,13 @@ -const { checkLogin, setSetting, setting } = require("../util-server"); +const { checkLogin, setSetting, setting, doubleCheckPassword } = require("../util-server"); const { CloudflaredTunnel } = require("node-cloudflared-tunnel"); const { io } = require("../server"); const prefix = "cloudflared_"; const cloudflared = new CloudflaredTunnel(); -let isRunning; - cloudflared.change = (running, message) => { io.to("cloudflared").emit(prefix + "running", running); io.to("cloudflared").emit(prefix + "message", message); - isRunning = running; - }; cloudflared.error = (errorMessage) => { @@ -25,7 +21,7 @@ module.exports.cloudflaredSocketHandler = (socket) => { checkLogin(socket); socket.join("cloudflared"); io.to(socket.userID).emit(prefix + "installed", cloudflared.checkInstalled()); - io.to(socket.userID).emit(prefix + "running", isRunning); + io.to(socket.userID).emit(prefix + "running", cloudflared.running); io.to(socket.userID).emit(prefix + "token", await setting("cloudflaredTunnelToken")); } catch (error) { } }); @@ -62,11 +58,34 @@ module.exports.cloudflaredSocketHandler = (socket) => { } catch (error) { } }); - socket.on(prefix + "stop", async () => { + socket.on(prefix + "stop", async (currentPassword, callback) => { try { checkLogin(socket); + await doubleCheckPassword(socket, currentPassword); cloudflared.stop(); + } catch (error) { + callback({ + ok: false, + msg: error.message, + }); + } + }); + + socket.on(prefix + "removeToken", async () => { + try { + checkLogin(socket); + await setSetting("cloudflaredTunnelToken", ""); } catch (error) { } }); }; + +module.exports.autoStart = async () => { + let token = await setting("cloudflaredTunnelToken"); + + if (token) { + console.log("Start cloudflared"); + cloudflared.token = token; + cloudflared.start(); + } +}; diff --git a/src/components/settings/ReverseProxy.vue b/src/components/settings/ReverseProxy.vue index fe41644b6..2b5f65d82 100644 --- a/src/components/settings/ReverseProxy.vue +++ b/src/components/settings/ReverseProxy.vue @@ -19,7 +19,7 @@ {{ message }} -
+
Message:
@@ -37,8 +37,13 @@ id="cloudflareTunnelToken" v-model="cloudflareTunnelToken" autocomplete="one-time-code" + :readonly="running" />
+
+ {{ $t("Remove Token") }} +
+ Don't know how to get the token? Please read the guide:
https://github.com/louislam/uptime-kuma/wiki/Reverse-Proxy-with-Cloudflare-Tunnel @@ -46,15 +51,31 @@
-
- + + + The current connection may be lost if you are connecting Cloudflare Tunnel. Are you sure want to stop it? Type your password to confirm it. + +
+ + +
+
@@ -68,14 +89,17 @@ diff --git a/src/mixins/socket.js b/src/mixins/socket.js index f2c4ff6d8..d8b1ad224 100644 --- a/src/mixins/socket.js +++ b/src/mixins/socket.js @@ -48,6 +48,7 @@ export default { running: false, message: "", errorMessage: "", + currentPassword: "", } }; },