2022-03-30 06:59:49 +03:00
|
|
|
const { checkLogin, setSetting, setting, doubleCheckPassword } = require("../util-server");
|
2022-03-29 20:49:45 +03:00
|
|
|
const { CloudflaredTunnel } = require("node-cloudflared-tunnel");
|
2022-04-19 10:38:59 +03:00
|
|
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
|
|
|
const io = UptimeKumaServer.getInstance().io;
|
2022-03-29 09:48:02 +03:00
|
|
|
|
|
|
|
const prefix = "cloudflared_";
|
2022-03-29 20:49:45 +03:00
|
|
|
const cloudflared = new CloudflaredTunnel();
|
|
|
|
|
2022-04-21 19:10:23 +03:00
|
|
|
/**
|
|
|
|
* Change running state
|
|
|
|
* @param {string} running Is it running?
|
|
|
|
* @param {string} message Message to pass
|
|
|
|
*/
|
2022-03-29 20:49:45 +03:00
|
|
|
cloudflared.change = (running, message) => {
|
|
|
|
io.to("cloudflared").emit(prefix + "running", running);
|
|
|
|
io.to("cloudflared").emit(prefix + "message", message);
|
|
|
|
};
|
|
|
|
|
2022-04-21 19:10:23 +03:00
|
|
|
/**
|
|
|
|
* Emit an error message
|
|
|
|
* @param {string} errorMessage
|
|
|
|
*/
|
2022-03-29 20:49:45 +03:00
|
|
|
cloudflared.error = (errorMessage) => {
|
|
|
|
io.to("cloudflared").emit(prefix + "errorMessage", errorMessage);
|
|
|
|
};
|
2022-03-29 09:48:02 +03:00
|
|
|
|
2022-04-21 19:10:23 +03:00
|
|
|
/**
|
|
|
|
* Handler for cloudflared
|
|
|
|
* @param {Socket} socket Socket.io instance
|
|
|
|
*/
|
2022-03-29 09:48:02 +03:00
|
|
|
module.exports.cloudflaredSocketHandler = (socket) => {
|
|
|
|
|
2022-03-29 20:49:45 +03:00
|
|
|
socket.on(prefix + "join", async () => {
|
2022-03-29 09:48:02 +03:00
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
2022-03-29 20:49:45 +03:00
|
|
|
socket.join("cloudflared");
|
|
|
|
io.to(socket.userID).emit(prefix + "installed", cloudflared.checkInstalled());
|
2022-03-30 06:59:49 +03:00
|
|
|
io.to(socket.userID).emit(prefix + "running", cloudflared.running);
|
2022-03-29 20:49:45 +03:00
|
|
|
io.to(socket.userID).emit(prefix + "token", await setting("cloudflaredTunnelToken"));
|
|
|
|
} catch (error) { }
|
|
|
|
});
|
2022-03-29 09:48:02 +03:00
|
|
|
|
2022-03-29 20:49:45 +03: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") {
|
2022-03-31 10:58:39 +03:00
|
|
|
await setSetting("cloudflaredTunnelToken", token);
|
2022-03-29 20:49:45 +03:00
|
|
|
cloudflared.token = token;
|
|
|
|
} else {
|
|
|
|
cloudflared.token = null;
|
|
|
|
}
|
|
|
|
cloudflared.start();
|
|
|
|
} catch (error) { }
|
|
|
|
});
|
|
|
|
|
2022-03-30 06:59:49 +03:00
|
|
|
socket.on(prefix + "stop", async (currentPassword, callback) => {
|
2022-03-29 20:49:45 +03:00
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
2022-06-18 14:06:03 +03:00
|
|
|
const disabledAuth = await setting("disableAuth");
|
|
|
|
if (!disabledAuth) {
|
|
|
|
await doubleCheckPassword(socket, currentPassword);
|
|
|
|
}
|
2022-03-29 20:49:45 +03:00
|
|
|
cloudflared.stop();
|
2022-03-30 06:59:49 +03:00
|
|
|
} catch (error) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: error.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on(prefix + "removeToken", async () => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
await setSetting("cloudflaredTunnelToken", "");
|
2022-03-29 20:49:45 +03:00
|
|
|
} catch (error) { }
|
2022-03-29 09:48:02 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|
2022-03-30 06:59:49 +03:00
|
|
|
|
2022-04-21 19:10:23 +03:00
|
|
|
/**
|
|
|
|
* Automatically start cloudflared
|
|
|
|
* @param {string} token Cloudflared tunnel token
|
|
|
|
*/
|
2022-03-30 15:08:26 +03: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 06:59:49 +03:00
|
|
|
|
|
|
|
if (token) {
|
|
|
|
console.log("Start cloudflared");
|
|
|
|
cloudflared.token = token;
|
|
|
|
cloudflared.start();
|
|
|
|
}
|
|
|
|
};
|
2022-04-05 14:41:29 +03:00
|
|
|
|
2022-04-21 19:10:23 +03:00
|
|
|
/** Stop cloudflared */
|
2022-04-05 14:41:29 +03:00
|
|
|
module.exports.stop = async () => {
|
|
|
|
console.log("Stop cloudflared");
|
2022-04-18 19:39:49 +03:00
|
|
|
if (cloudflared) {
|
|
|
|
cloudflared.stop();
|
|
|
|
}
|
2022-04-05 14:41:29 +03:00
|
|
|
};
|