Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ› Fix port issue on some systems #44

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion server/controllers/guacamoleProxy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const WebSocket = require("ws");
const { getGuacamolePort } = require("../utils/guacamoleStarter");

module.exports = async (ws, token) => {
try {
const guacdUrl = `ws://localhost:58391/?token=${token}`;
const guacdUrl = `ws://localhost:${getGuacamolePort()}/?token=${token}`;
const guacdSocket = new WebSocket(guacdUrl);

ws.on("message", (message) => {
Expand Down
9 changes: 3 additions & 6 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const express = require("express");
const path = require("path");
const db = require("./utils/database");
const { authenticate } = require("./middlewares/auth");
const GuacamoleLite = require("guacamole-lite");
const expressWs = require("express-ws");
const { startPVEUpdater } = require("./utils/pveUpdater");
const { startGuacamole } = require("./utils/guacamoleStarter");

require("./utils/folder"); // Create needed data folders

Expand Down Expand Up @@ -36,11 +36,6 @@ app.use("/api/servers", authenticate, require("./routes/server"));
app.use("/api/pve-servers", authenticate, require("./routes/pveServer"));
app.use("/api/identities", authenticate, require("./routes/identity"));

new GuacamoleLite({ port: 58391 }, { port: 4822 }, {
crypt: { cypher: "AES-256-CBC", key: module.exports.GUACD_TOKEN },
log: { level: 0 },
});

if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "../dist")));

Expand All @@ -55,6 +50,8 @@ db.authenticate().catch(err => {
}).then(async () => {
console.log("Successfully connected to the database " + (process.env.DB_TYPE === "mysql" ? "server" : "file"));

await startGuacamole();

await db.sync({ alter: true, force: false });

startPVEUpdater();
Expand Down
42 changes: 42 additions & 0 deletions server/utils/guacamoleStarter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const GuacamoleLite = require("guacamole-lite");
const net = require("net");
const { GUACD_TOKEN } = require("../index");

let GUACAMOLE_LITE_PORT = 58390;

const checkFreePort = async (port) => {
return new Promise((resolve) => {
const server = net.createServer();
server.once("error", () => resolve(false));
server.once("listening", () => {
server.close();
resolve(true);
});
server.listen(port, "::");
});
};

const getFreePort = async () => {
for (let i = 0; i < 10; i++) {
const port = Math.floor(Math.random() * 10000) + 50000;
if (await checkFreePort(port)) {
return port;
}
}
throw new Error("Could not find a free port for Guacamole");
};

module.exports.startGuacamole = async () => {
GUACAMOLE_LITE_PORT = await getFreePort();

console.log("Starting Guacamole on port " + GUACAMOLE_LITE_PORT);

new GuacamoleLite({ port: GUACAMOLE_LITE_PORT, host: "::" }, { port: 4822 }, {
crypt: { cypher: "AES-256-CBC", key: GUACD_TOKEN },
log: { level: 0 },
}).on("error", () => {
console.error("Could not connect to guacd. Is it running?");
});
};

module.exports.getGuacamolePort = () => GUACAMOLE_LITE_PORT;