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
Cassandra b287a25de7
Some checks failed
Merge Conflict Labeler / Labeling (push) Has been skipped
json-yaml-validate / json-yaml-validate (push) Successful in 12s
Auto Test / check-linters (push) Failing after 22m10s
Auto Test / auto-test (18, ARM64) (push) Has been cancelled
Auto Test / auto-test (18, macos-latest) (push) Has been cancelled
Auto Test / auto-test (18, ubuntu-latest) (push) Has been cancelled
Auto Test / auto-test (18, windows-latest) (push) Has been cancelled
Auto Test / auto-test (20.5, ARM64) (push) Has been cancelled
Auto Test / auto-test (20.5, macos-latest) (push) Has been cancelled
Auto Test / auto-test (20.5, ubuntu-latest) (push) Has been cancelled
Auto Test / auto-test (20.5, windows-latest) (push) Has been cancelled
Auto Test / armv7-simple-test (18, ARMv7) (push) Has been cancelled
Auto Test / armv7-simple-test (20, ARMv7) (push) Has been cancelled
Auto Test / e2e-test (push) Has been cancelled
Automatically close stale issues / stale (push) Successful in 16s
CodeQL / Analyze (go) (push) Failing after 56s
CodeQL / Analyze (javascript-typescript) (push) Failing after 27s
feat: structured logging (JSON) (#5118)
2024-09-24 17:51:21 +08:00

124 lines
3.5 KiB
JavaScript

const { checkLogin, doubleCheckPassword } = require("../util-server");
const { CloudflaredTunnel } = require("node-cloudflared-tunnel");
const { UptimeKumaServer } = require("../uptime-kuma-server");
const { log } = require("../../src/util");
const { Settings } = require("../settings");
const io = UptimeKumaServer.getInstance().io;
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);
};
/**
* Handler for cloudflared
* @param {Socket} socket Socket.io instance
* @returns {void}
*/
module.exports.cloudflaredSocketHandler = (socket) => {
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", cloudflared.running);
io.to(socket.userID).emit(prefix + "token", await Settings.get("cloudflaredTunnelToken"));
} catch (error) { }
});
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) { }
});
socket.on(prefix + "stop", async (currentPassword, callback) => {
try {
checkLogin(socket);
const disabledAuth = await Settings.get("disableAuth");
if (!disabledAuth) {
await doubleCheckPassword(socket, currentPassword);
}
cloudflared.stop();
} catch (error) {
callback({
ok: false,
msg: error.message,
});
}
});
socket.on(prefix + "removeToken", async () => {
try {
checkLogin(socket);
await Settings.set("cloudflaredTunnelToken", "");
} catch (error) { }
});
};
/**
* 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");
}
if (token) {
log.info("cloudflare", "Start cloudflared");
cloudflared.token = token;
cloudflared.start();
}
};
/**
* Stop cloudflared
* @returns {Promise<void>}
*/
module.exports.stop = async () => {
log.info("cloudflared", "Stop cloudflared");
if (cloudflared) {
cloudflared.stop();
}
};