diff --git a/package.json b/package.json index 24558dc5..bb7b1e57 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.0", + "node-cloudflared-tunnel": "~1.0.6", "nodemailer": "~6.6.5", "notp": "~2.0.3", "password-hash": "~1.2.2", diff --git a/server/socket-handlers/cloudflared-socket-handler.js b/server/socket-handlers/cloudflared-socket-handler.js index 95dd4d80..f7c69ed2 100644 --- a/server/socket-handlers/cloudflared-socket-handler.js +++ b/server/socket-handlers/cloudflared-socket-handler.js @@ -1,19 +1,72 @@ -const { checkLogin } = require("../util-server"); +const { checkLogin, setSetting, setting } = 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) => { + io.to("cloudflared").emit(prefix + "errorMessage", errorMessage); +}; module.exports.cloudflaredSocketHandler = (socket) => { - socket.on(prefix + "start", async (callback) => { + socket.on(prefix + "join", async () => { try { 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 + "token", await setting("cloudflaredTunnelToken")); + } catch (error) { } + }); - } catch (error) { - callback({ - ok: false, - msg: error.message, - }); - } + socket.on(prefix + "leave", async () => { + try { + checkLogin(socket); + socket.leave("cloudflared"); + } catch (error) { } + }); + + socket.on(prefix + "start", async (token) => { + try { + checkLogin(socket); + if (token && typeof token === "string") { + token = token.trim(); + + // try to strip out "cloudflared.exe service install" + let array = token.split(" "); + if (array.length > 1) { + for (let i = 0; i < array.length - 1; i++) { + if (array[i] === "install") { + token = array[i + 1]; + } + } + } + + await setSetting("cloudflaredTunnelToken", token); + cloudflared.token = token; + } else { + cloudflared.token = null; + } + cloudflared.start(); + } catch (error) { } + }); + + socket.on(prefix + "stop", async () => { + try { + checkLogin(socket); + cloudflared.stop(); + } catch (error) { } }); }; diff --git a/src/components/settings/ReverseProxy.vue b/src/components/settings/ReverseProxy.vue new file mode 100644 index 00000000..fe41644b --- /dev/null +++ b/src/components/settings/ReverseProxy.vue @@ -0,0 +1,111 @@ + + + Cloudflare Tunnel + + + + cloudflared: + {{ $t("Installed") }} + {{ $t("Not installed") }} + + + + {{ $t("Status") }}: + {{ $t("Running") }} + {{ $t("Not running") }} + + + + {{ message }} + + + + Message: + + + + (Download cloudflared from Cloudflare Website) + + + + + + + Cloudflare Tunnel {{ $t("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 + + + + + + + + {{ $t("Start") }} cloudflared + + + + {{ $t("Stop") }} cloudflared + + + + + Other Software + + For example: nginx, Apache and Traefik. + Please read https://github.com/louislam/uptime-kuma/wiki/Reverse-Proxy. + + + + + + + diff --git a/src/mixins/socket.js b/src/mixins/socket.js index 7d1bbea5..f2c4ff6d 100644 --- a/src/mixins/socket.js +++ b/src/mixins/socket.js @@ -42,6 +42,13 @@ export default { statusPageList: [], connectionErrorMsg: "Cannot connect to the socket server. Reconnecting...", showReverseProxyGuide: true, + cloudflared: { + cloudflareTunnelToken: "", + installed: null, + running: false, + message: "", + errorMessage: "", + } }; }, @@ -231,6 +238,12 @@ export default { this.socket.firstConnect = false; }); + // cloudflared + socket.on("cloudflared_installed", (res) => this.cloudflared.installed = res); + socket.on("cloudflar
(Download cloudflared from Cloudflare Website)