uptime-kuma/server/socket-handlers/cloudflared-socket-handler.js

124 lines
3.5 KiB
JavaScript
Raw Normal View History

const { checkLogin, doubleCheckPassword } = require("../util-server");
const { CloudflaredTunnel } = require("node-cloudflared-tunnel");
const { UptimeKumaServer } = require("../uptime-kuma-server");
2022-12-08 11:13:47 +00:00
const { log } = require("../../src/util");
const { Settings } = require("../settings");
const io = UptimeKumaServer.getInstance().io;
2022-03-29 06:48:02 +00:00
const prefix = "cloudflared_";
const cloudflared = new CloudflaredTunnel();
/**
* Change running state
* @param {string} running Is it running?
* @param {string} message Message to pass
* @returns {void}
*/
cloudflared.change = (running, message) => {
io.to("cloudflared").emit(prefix + "running", running);
io.to("cloudflared").emit(prefix + "message", message);
};
/**
* Emit an error message
* @param {string} errorMessage Error message to send
* @returns {void}
*/
cloudflared.error = (errorMessage) => {
io.to("cloudflared").emit(prefix + "errorMessage", errorMessage);
};
2022-03-29 06:48:02 +00:00
/**
* Handler for cloudflared
* @param {Socket} socket Socket.io instance
* @returns {void}
*/
2022-03-29 06:48:02 +00:00
module.exports.cloudflaredSocketHandler = (socket) => {
socket.on(prefix + "join", async () => {
2022-03-29 06:48:02 +00:00
try {
checkLogin(socket);
socket.join("cloudflared");
io.to(socket.userID).emit(prefix + "installed", cloudflared.checkInstalled());
2022-03-30 03:59:49 +00:00
io.to(socket.userID).emit(prefix + "running", cloudflared.running);
io.to(socket.userID).emit(prefix + "token", await Settings.get("cloudflaredTunnelToken"));
} catch (error) { }
});
2022-03-29 06:48:02 +00:00
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") {
await Settings.set("cloudflaredTunnelToken", token);
cloudflared.token = token;
} else {
cloudflared.token = null;
}
cloudflared.start();
} catch (error) { }
});
2022-03-30 03:59:49 +00:00
socket.on(prefix + "stop", async (currentPassword, callback) => {
try {
checkLogin(socket);
const disabledAuth = await Settings.get("disableAuth");
if (!disabledAuth) {
await doubleCheckPassword(socket, currentPassword);
}
cloudflared.stop();
2022-03-30 03:59:49 +00:00
} catch (error) {
callback({
ok: false,
msg: error.message,
});
}
});
socket.on(prefix + "removeToken", async () => {
try {
checkLogin(socket);
await Settings.set("cloudflaredTunnelToken", "");
} catch (error) { }
2022-03-29 06:48:02 +00:00
});
};
2022-03-30 03:59:49 +00:00
/**
* Automatically start cloudflared
* @param {string} token Cloudflared tunnel token
* @returns {Promise<void>}
*/
module.exports.autoStart = async (token) => {
if (!token) {
token = await Settings.get("cloudflaredTunnelToken");
} else {
// Override the current token via args or env var
await Settings.set("cloudflaredTunnelToken", token);
log.info("cloudflare", "Use cloudflared token from args or env var");
}
2022-03-30 03:59:49 +00:00
if (token) {
log.info("cloudflare", "Start cloudflared");
2022-03-30 03:59:49 +00:00
cloudflared.token = token;
cloudflared.start();
}
};
/**
* Stop cloudflared
* @returns {Promise<void>}
*/
module.exports.stop = async () => {
2022-12-08 11:13:47 +00:00
log.info("cloudflared", "Stop cloudflared");
2022-04-18 16:39:49 +00:00
if (cloudflared) {
cloudflared.stop();
}
};