Allow relative WebSocket URLs

This can be very useful if you have multiple instances of noVNC, and you
want to redirect them to different VNC servers.

The new default settings will have the same behaviour as before for
systems where noVNC is deployed in the root web folder.
This commit is contained in:
Pierre Ossman 2024-08-08 14:53:42 +02:00
parent 074fa1a40f
commit 96c76f7709
1 changed files with 13 additions and 24 deletions

View File

@ -158,20 +158,7 @@ const UI = {
UI.initSetting('logging', 'warn'); UI.initSetting('logging', 'warn');
UI.updateLogging(); UI.updateLogging();
// if port == 80 (or 443) then it won't be present and should be
// set manually
let port = window.location.port;
if (!port) {
if (window.location.protocol.substring(0, 5) == 'https') {
port = 443;
} else if (window.location.protocol.substring(0, 4) == 'http') {
port = 80;
}
}
/* Populate the controls if defaults are provided in the URL */ /* Populate the controls if defaults are provided in the URL */
UI.initSetting('host', window.location.hostname);
UI.initSetting('port', port);
UI.initSetting('encrypt', (window.location.protocol === "https:")); UI.initSetting('encrypt', (window.location.protocol === "https:"));
UI.initSetting('view_clip', false); UI.initSetting('view_clip', false);
UI.initSetting('resize', 'off'); UI.initSetting('resize', 'off');
@ -1021,25 +1008,27 @@ const UI = {
UI.hideStatus(); UI.hideStatus();
if (!host) {
Log.Error("Can't connect when host is: " + host);
UI.showStatus(_("Must set host"), 'error');
return;
}
UI.closeConnectPanel(); UI.closeConnectPanel();
UI.updateVisualState('connecting'); UI.updateVisualState('connecting');
let url; let url;
url = new URL("https://" + host); if (host) {
url = new URL("https://" + host);
url.protocol = UI.getSetting('encrypt') ? 'wss:' : 'ws:'; url.protocol = UI.getSetting('encrypt') ? 'wss:' : 'ws:';
if (port) { if (port) {
url.port = port; url.port = port;
}
url.pathname = '/' + path;
} else {
// Current (May 2024) browsers support relative WebSocket
// URLs natively, but we need to support older browsers for
// some time.
url = new URL(path, location.href);
url.protocol = (window.location.protocol === "https:") ? 'wss:' : 'ws:';
} }
url.pathname = '/' + path;
try { try {
UI.rfb = new RFB(document.getElementById('noVNC_container'), UI.rfb = new RFB(document.getElementById('noVNC_container'),