mirror of
https://github.com/khairul169/home-lab.git
synced 2025-04-28 16:49:36 +07:00
feat: start vnc server if not started yet
This commit is contained in:
parent
f465a5602f
commit
c7454c13cc
@ -9,3 +9,4 @@ PC_MAC_ADDR=
|
|||||||
TERMINAL_SHELL=
|
TERMINAL_SHELL=
|
||||||
FILE_DIRS="/some/path;/another/path"
|
FILE_DIRS="/some/path;/another/path"
|
||||||
VNC_PORT=5901
|
VNC_PORT=5901
|
||||||
|
VNC_START_CMD=vncserver :1
|
||||||
|
38
backend/lib/vnc.ts
Normal file
38
backend/lib/vnc.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import type { WebSocket } from "ws";
|
||||||
|
import { websockify } from "./websockify";
|
||||||
|
import net from "node:net";
|
||||||
|
import { spawn } from "node:child_process";
|
||||||
|
|
||||||
|
const VNC_PORT = parseInt(process.env.VNC_PORT || "") || 5901;
|
||||||
|
const VNC_START_CMD = process.env.VNC_START_CMD;
|
||||||
|
|
||||||
|
export const createVNCSession = async (ws: WebSocket) => {
|
||||||
|
const isVncStarted = await isPortOpen(VNC_PORT);
|
||||||
|
|
||||||
|
// VNC is not started, start it.
|
||||||
|
if (!isVncStarted && VNC_START_CMD) {
|
||||||
|
const cmd = VNC_START_CMD.split(" ");
|
||||||
|
const child = spawn(cmd[0], cmd.slice(1));
|
||||||
|
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
child.on("close", () => resolve(null));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
websockify(ws, "localhost", VNC_PORT);
|
||||||
|
};
|
||||||
|
|
||||||
|
async function isPortOpen(port: number) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const tester = net
|
||||||
|
.createServer()
|
||||||
|
.once("error", function (err: any) {
|
||||||
|
resolve(err.code === "EADDRINUSE");
|
||||||
|
})
|
||||||
|
.once("listening", function () {
|
||||||
|
tester.close();
|
||||||
|
resolve(false);
|
||||||
|
})
|
||||||
|
.listen(port);
|
||||||
|
});
|
||||||
|
}
|
@ -1,9 +1,7 @@
|
|||||||
import { WebSocketServer } from "ws";
|
import { WebSocketServer } from "ws";
|
||||||
import { verifyToken } from "./lib/jwt";
|
import { verifyToken } from "./lib/jwt";
|
||||||
import { createTerminalSession } from "./lib/terminal";
|
import { createTerminalSession } from "./lib/terminal";
|
||||||
import { websockify } from "./lib/websockify";
|
import { createVNCSession } from "./lib/vnc";
|
||||||
|
|
||||||
const VNC_PORT = parseInt(process.env.VNC_PORT || "") || 5901;
|
|
||||||
|
|
||||||
const createWsServer = (server: any) => {
|
const createWsServer = (server: any) => {
|
||||||
const wss = new WebSocketServer({ server: server as never });
|
const wss = new WebSocketServer({ server: server as never });
|
||||||
@ -25,7 +23,7 @@ const createWsServer = (server: any) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (url.pathname === "/vnc") {
|
if (url.pathname === "/vnc") {
|
||||||
websockify(ws, "localhost", VNC_PORT);
|
createVNCSession(ws);
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.on("error", console.error);
|
ws.on("error", console.error);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user