diff --git a/include/ui.js b/include/ui.js index 38fbe4bd..1a7f803a 100644 --- a/include/ui.js +++ b/include/ui.js @@ -96,6 +96,7 @@ var UI; UI.initSetting('view_only', false); UI.initSetting('path', 'websockify'); UI.initSetting('repeaterID', ''); + UI.initSetting('token', ''); var autoconnect = WebUtil.getConfigVar('autoconnect', false); if (autoconnect === 'true' || autoconnect == '1') { @@ -519,6 +520,7 @@ var UI; UI.connSettingsOpen = false; UI.saveSetting('host'); UI.saveSetting('port'); + UI.saveSetting('token'); //UI.saveSetting('password'); } else { $D('noVNC_controls').style.display = "block"; @@ -810,7 +812,14 @@ var UI; var host = $D('noVNC_host').value; var port = $D('noVNC_port').value; var password = $D('noVNC_password').value; + var token = $D('noVNC_token').value; var path = $D('noVNC_path').value; + + //if token is in path then ignore the new token variable + if (token) { + path = WebUtil.injectParamIfMissing(path, "token", token); + } + if ((!host) || (!port)) { throw new Error("Must set host and port"); } diff --git a/include/webutil.js b/include/webutil.js index f10aa0d7..4289aa6b 100644 --- a/include/webutil.js +++ b/include/webutil.js @@ -260,3 +260,27 @@ WebUtil.selectStylesheet = function (sheet) { } return sheet; }; + +WebUtil.injectParamIfMissing = function (path, param, value) { + // force pretend that we're dealing with a relative path + // (assume that we wanted an extra if we pass one in) + path = "/" + path; + + var elem = document.createElement('a'); + elem.href = path; + + var param_eq = encodeURIComponent(param) + "="; + var query; + if (elem.search) { + query = elem.search.slice(1).split('&'); + } else { + query = []; + } + + if (!query.some(function (v) { return v.startsWith(param_eq); })) { + query.push(param_eq + encodeURIComponent(value)); + elem.search = "?" + query.join("&"); + } + + return elem.pathname.slice(1) + elem.search + elem.hash; +}; diff --git a/vnc.html b/vnc.html index e2250f5d..f64c750c 100644 --- a/vnc.html +++ b/vnc.html @@ -199,6 +199,7 @@
+ diff --git a/vnc_auto.html b/vnc_auto.html index 04803223..73174713 100644 --- a/vnc_auto.html +++ b/vnc_auto.html @@ -202,16 +202,20 @@ } } + password = WebUtil.getConfigVar('password', ''); + path = WebUtil.getConfigVar('path', 'websockify'); + // If a token variable is passed in, set the parameter in a cookie. // This is used by nova-novncproxy. token = WebUtil.getConfigVar('token', null); if (token) { + + // if token is already present in the path we should use it + path = WebUtil.injectParamIfMissing(path, "token", token); + WebUtil.createCookie('token', token, 1) } - password = WebUtil.getConfigVar('password', ''); - path = WebUtil.getConfigVar('path', 'websockify'); - if ((!host) || (!port)) { updateState(null, 'fatal', null, 'Must specify host and port in URL'); return;