mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-28 14:08:49 +03:00
91 lines
2.8 KiB
JavaScript
91 lines
2.8 KiB
JavaScript
const { checkLogin, setSetting, setting, doubleCheckPassword } = require("../util-server");
|
|
const { CloudflaredTunnel } = require("node-cloudflared-tunnel");
|
|
const { io } = require("../server");
|
|
|
|
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);
|
|
};
|
|
|
|
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 setting("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") {
|
|
token = token.trim();
|
|
|
|
// try to strip out "cloudflared.exe service install"
|
|
let array = token.split(" ");
|
|
if (array.length > 1) {
|
|
for (let i = 0; i < array.length - 1; i++) {
|
|
if (array[i] === "install") {
|
|
token = array[i + 1];
|
|
}
|
|
}
|
|
}
|
|
|
|
await setSetting("cloudflaredTunnelToken", token);
|
|
cloudflared.token = token;
|
|
} else {
|
|
cloudflared.token = null;
|
|
}
|
|
cloudflared.start();
|
|
} catch (error) { }
|
|
});
|
|
|
|
socket.on(prefix + "stop", async (currentPassword, callback) => {
|
|
try {
|
|
checkLogin(socket);
|
|
await doubleCheckPassword(socket, currentPassword);
|
|
cloudflared.stop();
|
|
} catch (error) {
|
|
callback({
|
|
ok: false,
|
|
msg: error.message,
|
|
});
|
|
}
|
|
});
|
|
|
|
socket.on(prefix + "removeToken", async () => {
|
|
try {
|
|
checkLogin(socket);
|
|
await setSetting("cloudflaredTunnelToken", "");
|
|
} catch (error) { }
|
|
});
|
|
|
|
};
|
|
|
|
module.exports.autoStart = async () => {
|
|
let token = await setting("cloudflaredTunnelToken");
|
|
|
|
if (token) {
|
|
console.log("Start cloudflared");
|
|
cloudflared.token = token;
|
|
cloudflared.start();
|
|
}
|
|
};
|