1
0
mirror of https://github.com/louislam/uptime-kuma.git synced 2024-12-24 22:14:47 +02:00
uptime-kuma/server/socket-handlers/cloudflared-socket-handler.js

86 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-03-30 05:59:49 +02:00
const { checkLogin, setSetting, setting, doubleCheckPassword } = require("../util-server");
const { CloudflaredTunnel } = require("node-cloudflared-tunnel");
const { io } = require("../server");
2022-03-29 08:48:02 +02:00
const prefix = "cloudflared_";
const cloudflared = new CloudflaredTunnel();
cloudflared.change = (running, message) => {
io.to("cloudflared").emit(prefix + "running", running);
io.to("cloudflared").emit(prefix + "message", message);
};
cloudflared.error = (errorMessage) => {
io.to("cloudflared").emit(prefix + "errorMessage", errorMessage);
};
2022-03-29 08:48:02 +02:00
module.exports.cloudflaredSocketHandler = (socket) => {
socket.on(prefix + "join", async () => {
2022-03-29 08:48:02 +02:00
try {
checkLogin(socket);
socket.join("cloudflared");
io.to(socket.userID).emit(prefix + "installed", cloudflared.checkInstalled());
2022-03-30 05:59:49 +02:00
io.to(socket.userID).emit(prefix + "running", cloudflared.running);
io.to(socket.userID).emit(prefix + "token", await setting("cloudflaredTunnelToken"));
} catch (error) { }
});
2022-03-29 08:48:02 +02: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 setSetting("cloudflaredTunnelToken", token);
cloudflared.token = token;
} else {
cloudflared.token = null;
}
cloudflared.start();
} catch (error) { }
});
2022-03-30 05:59:49 +02:00
socket.on(prefix + "stop", async (currentPassword, callback) => {
try {
checkLogin(socket);
2022-03-30 05:59:49 +02:00
await doubleCheckPassword(socket, currentPassword);
cloudflared.stop();
2022-03-30 05:59:49 +02:00
} catch (error) {
callback({
ok: false,
msg: error.message,
});
}
});
socket.on(prefix + "removeToken", async () => {
try {
checkLogin(socket);
await setSetting("cloudflaredTunnelToken", "");
} catch (error) { }
2022-03-29 08:48:02 +02:00
});
};
2022-03-30 05:59:49 +02:00
module.exports.autoStart = async (token) => {
if (!token) {
token = await setting("cloudflaredTunnelToken");
} else {
// Override the current token via args or env var
await setSetting("cloudflaredTunnelToken", token);
console.log("Use cloudflared token from args or env var");
}
2022-03-30 05:59:49 +02:00
if (token) {
console.log("Start cloudflared");
cloudflared.token = token;
cloudflared.start();
}
};