Files
BetterDesk/web-nodejs/services/wsRelay.js
T
UNITRONIX 12d195ff78 Release v2.3.0: Node.js console & security
Bump to v2.3.0 and switch to Node.js web console as the primary console (Flask deprecated). Add comprehensive security and feature updates: CSRF protection, session-fixation prevention, timing-safe auth, WebSocket auth, TOTP 2FA, operator/admin roles, address-book sync, desktop connect (URI handler), and SSL certificate configuration in installers. Introduce a dedicated RustDesk Client API (WAN port 21121) and related web-nodejs middleware, routes, views, protos, and client JS (rdclient) plus vendor libs. Add developer tooling and scripts (dev_modules), update hbbs-patch-v2 Rust sources, README and changelog/docs, and ignore Node.js artifacts in .gitignore.
2026-02-22 03:44:00 +01:00

228 lines
7.3 KiB
JavaScript

/**
* BetterDesk Console - WebSocket Relay Proxy
* Bridges browser WebSocket connections to TCP connections for hbbs/hbbr
*
* Provides two WebSocket endpoints:
* /ws/rendezvous - proxies to hbbs TCP (port 21116)
* /ws/relay - proxies to hbbr TCP (port 21117)
*
* IMPORTANT: hbbr treats loopback TCP connections as admin command interface
* (relay_server.rs: `if !ws && ip.is_loopback()`). The relay proxy must
* connect via a non-loopback IP so hbbr handles it as a relay request.
*/
const WebSocket = require('ws');
const net = require('net');
const os = require('os');
const config = require('../config/config');
// Maximum concurrent relay connections per IP
const MAX_CONNECTIONS_PER_IP = 5;
// Connection timeout (no data for 2 minutes = close)
const IDLE_TIMEOUT_MS = 120000;
// Track connections per IP
const connectionsPerIp = new Map();
/**
* Check if a hostname resolves to a loopback address
* @param {string} host
* @returns {boolean}
*/
function isLoopbackHost(host) {
if (!host) return false;
const lower = host.toLowerCase();
return lower === 'localhost' || lower === '127.0.0.1' || lower === '::1'
|| lower.startsWith('127.');
}
/**
* Get first non-loopback IPv4 address of the machine.
* Used to avoid hbbr's loopback command-mode check when proxying relay connections.
* @returns {string|null}
*/
function getNonLoopbackIp() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return null;
}
/**
* Initialize WebSocket proxy servers and attach to HTTP server
* @param {http.Server} server - The HTTP/HTTPS server instance
*/
function initWsProxy(server) {
// Rendezvous proxy (hbbs)
const rendezvousWss = new WebSocket.Server({ noServer: true });
// Relay proxy (hbbr)
const relayWss = new WebSocket.Server({ noServer: true });
// Handle upgrade requests — verify session cookie before allowing WebSocket
server.on('upgrade', (request, socket, head) => {
const url = new URL(request.url, `http://${request.headers.host}`);
const pathname = url.pathname;
// Parse session cookie to verify authentication
const cookies = {};
const cookieHeader = request.headers.cookie || '';
cookieHeader.split(';').forEach(c => {
const [name, ...rest] = c.trim().split('=');
if (name) cookies[name] = rest.join('=');
});
// Require valid session cookie for WebSocket connections
if (!cookies['betterdesk.sid']) {
console.warn(`WS proxy: Rejected upgrade to ${pathname} — no session cookie`);
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
if (pathname === '/ws/rendezvous') {
rendezvousWss.handleUpgrade(request, socket, head, (ws) => {
rendezvousWss.emit('connection', ws, request);
});
} else if (pathname === '/ws/relay') {
relayWss.handleUpgrade(request, socket, head, (ws) => {
relayWss.emit('connection', ws, request);
});
} else {
socket.destroy();
}
});
// Parse target host/port from config
const hbbsHost = config.wsProxy?.hbbsHost || 'localhost';
const hbbsPort = config.wsProxy?.hbbsPort || 21116;
let hbbrHost = config.wsProxy?.hbbrHost || 'localhost';
const hbbrPort = config.wsProxy?.hbbrPort || 21117;
// CRITICAL: hbbr treats loopback TCP connections as admin command interface
// and will NOT process relay requests from 127.0.0.0/8.
// If hbbrHost is loopback, replace with the machine's non-loopback IP.
if (isLoopbackHost(hbbrHost)) {
const nonLoopback = getNonLoopbackIp();
if (nonLoopback) {
console.log(` WebSocket proxy: hbbr host changed from '${hbbrHost}' to '${nonLoopback}' (avoiding loopback command mode)`);
hbbrHost = nonLoopback;
} else {
console.warn(' WebSocket proxy: WARNING - could not find non-loopback IP for hbbr, relay connections may fail!');
}
}
// Rendezvous connections
rendezvousWss.on('connection', (ws, req) => {
handleProxyConnection(ws, req, hbbsHost, hbbsPort, 'rendezvous');
});
// Relay connections
relayWss.on('connection', (ws, req) => {
handleProxyConnection(ws, req, hbbrHost, hbbrPort, 'relay');
});
console.log(` WebSocket proxy: /ws/rendezvous -> ${hbbsHost}:${hbbsPort}`);
console.log(` WebSocket proxy: /ws/relay -> ${hbbrHost}:${hbbrPort}`);
return { rendezvousWss, relayWss };
}
/**
* Handle a single proxied WebSocket connection
*/
function handleProxyConnection(ws, req, targetHost, targetPort, label) {
const clientIp = req.headers['x-forwarded-for']?.split(',')[0]?.trim()
|| req.socket.remoteAddress
|| 'unknown';
// Rate limit connections per IP
const currentCount = connectionsPerIp.get(clientIp) || 0;
if (currentCount >= MAX_CONNECTIONS_PER_IP) {
console.warn(`WS proxy [${label}]: Connection limit reached for ${clientIp}`);
ws.close(1008, 'Too many connections');
return;
}
connectionsPerIp.set(clientIp, currentCount + 1);
// Idle timeout
let idleTimer = null;
const resetIdleTimer = () => {
if (idleTimer) clearTimeout(idleTimer);
idleTimer = setTimeout(() => {
console.log(`WS proxy [${label}]: Idle timeout for ${clientIp}`);
cleanup();
}, IDLE_TIMEOUT_MS);
};
// Connect to target TCP server
const tcp = net.createConnection({ host: targetHost, port: targetPort }, () => {
resetIdleTimer();
});
tcp.on('error', (err) => {
console.error(`WS proxy [${label}]: TCP error (${targetHost}:${targetPort}):`, err.message);
cleanup();
});
tcp.on('close', () => {
cleanup();
});
// TCP -> WebSocket
tcp.on('data', (data) => {
resetIdleTimer();
if (ws.readyState === WebSocket.OPEN) {
ws.send(data);
}
});
// WebSocket -> TCP
ws.on('message', (data) => {
resetIdleTimer();
if (!tcp.destroyed) {
// Ensure we send Buffer, not string
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
tcp.write(buf);
}
});
ws.on('close', () => {
cleanup();
});
ws.on('error', (err) => {
console.error(`WS proxy [${label}]: WebSocket error:`, err.message);
cleanup();
});
let cleaned = false;
function cleanup() {
if (cleaned) return;
cleaned = true;
if (idleTimer) clearTimeout(idleTimer);
if (!tcp.destroyed) {
tcp.destroy();
}
if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
ws.close();
}
// Decrement connection counter
const count = connectionsPerIp.get(clientIp) || 1;
if (count <= 1) {
connectionsPerIp.delete(clientIp);
} else {
connectionsPerIp.set(clientIp, count - 1);
}
}
}
module.exports = { initWsProxy };