2022-03-29 18:49:45 +01:00
|
|
|
const { checkLogin, setSetting, setting } = require("../util-server");
|
|
|
|
const { CloudflaredTunnel } = require("node-cloudflared-tunnel");
|
|
|
|
const { io } = require("../server");
|
2022-03-29 07:48:02 +01:00
|
|
|
|
|
|
|
const prefix = "cloudflared_";
|
2022-03-29 18:49:45 +01:00
|
|
|
const cloudflared = new CloudflaredTunnel();
|
|
|
|
|
|
|
|
let isRunning;
|
|
|
|
|
|
|
|
cloudflared.change = (running, message) => {
|
|
|
|
io.to("cloudflared").emit(prefix + "running", running);
|
|
|
|
io.to("cloudflared").emit(prefix + "message", message);
|
|
|
|
isRunning = running;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
cloudflared.error = (errorMessage) => {
|
|
|
|
io.to("cloudflared").emit(prefix + "errorMessage", errorMessage);
|
|
|
|
};
|
2022-03-29 07:48:02 +01:00
|
|
|
|
|
|
|
module.exports.cloudflaredSocketHandler = (socket) => {
|
|
|
|
|
2022-03-29 18:49:45 +01:00
|
|
|
socket.on(prefix + "join", async () => {
|
2022-03-29 07:48:02 +01:00
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
2022-03-29 18:49:45 +01:00
|
|
|
socket.join("cloudflared");
|
|
|
|
io.to(socket.userID).emit(prefix + "installed", cloudflared.checkInstalled());
|
|
|
|
io.to(socket.userID).emit(prefix + "running", isRunning);
|
|
|
|
io.to(socket.userID).emit(prefix + "token", await setting("cloudflaredTunnelToken"));
|
|
|
|
} catch (error) { }
|
|
|
|
});
|
2022-03-29 07:48:02 +01:00
|
|
|
|
2022-03-29 18:49:45 +01: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") {
|
|
|
|
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 () => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
cloudflared.stop();
|
|
|
|
} catch (error) { }
|
2022-03-29 07:48:02 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|