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