浏览代码

Pass token into the path variable

If a token is already present in the path, the new variable
is ignored.  In order to properly manipulate the path,
a new method, `WebUtil.injectParamIfMissing` was introduced.

Fixes #536

[@directxman12: fix up path manipulation logic]
Miguel Xavier Penha Neto 9 年之前
父节点
当前提交
c55f05f619
共有 4 个文件被更改,包括 41 次插入3 次删除
  1. 9 0
      include/ui.js
  2. 24 0
      include/webutil.js
  3. 1 0
      vnc.html
  4. 7 3
      vnc_auto.html

+ 9 - 0
include/ui.js

@@ -96,6 +96,7 @@ var UI;
             UI.initSetting('view_only', false);
             UI.initSetting('view_only', false);
             UI.initSetting('path', 'websockify');
             UI.initSetting('path', 'websockify');
             UI.initSetting('repeaterID', '');
             UI.initSetting('repeaterID', '');
+            UI.initSetting('token', '');
 
 
             var autoconnect = WebUtil.getConfigVar('autoconnect', false);
             var autoconnect = WebUtil.getConfigVar('autoconnect', false);
             if (autoconnect === 'true' || autoconnect == '1') {
             if (autoconnect === 'true' || autoconnect == '1') {
@@ -519,6 +520,7 @@ var UI;
                 UI.connSettingsOpen = false;
                 UI.connSettingsOpen = false;
                 UI.saveSetting('host');
                 UI.saveSetting('host');
                 UI.saveSetting('port');
                 UI.saveSetting('port');
+                UI.saveSetting('token');
                 //UI.saveSetting('password');
                 //UI.saveSetting('password');
             } else {
             } else {
                 $D('noVNC_controls').style.display = "block";
                 $D('noVNC_controls').style.display = "block";
@@ -810,7 +812,14 @@ var UI;
             var host = $D('noVNC_host').value;
             var host = $D('noVNC_host').value;
             var port = $D('noVNC_port').value;
             var port = $D('noVNC_port').value;
             var password = $D('noVNC_password').value;
             var password = $D('noVNC_password').value;
+            var token = $D('noVNC_token').value;
             var path = $D('noVNC_path').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)) {
             if ((!host) || (!port)) {
                 throw new Error("Must set host and port");
                 throw new Error("Must set host and port");
             }
             }

+ 24 - 0
include/webutil.js

@@ -260,3 +260,27 @@ WebUtil.selectStylesheet = function (sheet) {
     }
     }
     return 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;
+};

+ 1 - 0
vnc.html

@@ -199,6 +199,7 @@
                 <li><label><strong>Host: </strong><input id="noVNC_host" /></label></li>
                 <li><label><strong>Host: </strong><input id="noVNC_host" /></label></li>
                 <li><label><strong>Port: </strong><input id="noVNC_port" /></label></li>
                 <li><label><strong>Port: </strong><input id="noVNC_port" /></label></li>
                 <li><label><strong>Password: </strong><input id="noVNC_password" type="password" /></label></li>
                 <li><label><strong>Password: </strong><input id="noVNC_password" type="password" /></label></li>
+                <li><label><strong>Token: </strong><input id="noVNC_token"/></label></li>
                 <li><input id="noVNC_connect_button" type="button" value="Connect"></li>
                 <li><input id="noVNC_connect_button" type="button" value="Connect"></li>
             </ul>
             </ul>
         </div>
         </div>

+ 7 - 3
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.
             // If a token variable is passed in, set the parameter in a cookie.
             // This is used by nova-novncproxy.
             // This is used by nova-novncproxy.
             token = WebUtil.getConfigVar('token', null);
             token = WebUtil.getConfigVar('token', null);
             if (token) {
             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)
                 WebUtil.createCookie('token', token, 1)
             }
             }
 
 
-            password = WebUtil.getConfigVar('password', '');
-            path = WebUtil.getConfigVar('path', 'websockify');
-
             if ((!host) || (!port)) {
             if ((!host) || (!port)) {
                 updateState(null, 'fatal', null, 'Must specify host and port in URL');
                 updateState(null, 'fatal', null, 'Must specify host and port in URL');
                 return;
                 return;