Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kanaka
Created April 19, 2011 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanaka/928289 to your computer and use it in GitHub Desktop.
Save kanaka/928289 to your computer and use it in GitHub Desktop.
Patch websockify to allow port request in WebSockets path
diff --git a/include/rfb.js b/include/rfb.js
index 3c65a5c..0021318 100644
--- a/include/rfb.js
+++ b/include/rfb.js
@@ -124,6 +124,8 @@ var that = {}, // Public API interface
function cdef(v, type, defval, desc) {
Util.conf_default(conf, that, v, type, defval, desc); }
+cdef('path', 'str', "", 'path to request on connect');
+
cdef('target', 'str', null, 'VNC viewport rendering Canvas');
cdef('focusContainer', 'dom', document, 'Area that traps keyboard input');
@@ -239,7 +241,7 @@ function connect() {
} else {
uri = "ws://";
}
- uri += rfb_host + ":" + rfb_port + "/";
+ uri += rfb_host + ":" + rfb_port + "/" + conf.path;
Util.Info("connecting to " + uri);
ws.open(uri);
diff --git a/utils/websockify b/utils/websockify
index 36aba17..4d04b14 100755
--- a/utils/websockify
+++ b/utils/websockify
@@ -13,6 +13,7 @@ as taken from http://docs.python.org/dev/library/ssl.html#certificates
import socket, optparse, time, os, sys, subprocess
from select import select
+from urlparse import urlparse, parse_qs
from websocket import WebSocketServer
class WebSocketProxy(WebSocketServer):
@@ -40,7 +41,7 @@ Traffic Legend:
def __init__(self, *args, **kwargs):
# Save off proxy specific options
self.target_host = kwargs.pop('target_host')
- self.target_port = kwargs.pop('target_port')
+ self.target_port = 5900
self.wrap_cmd = kwargs.pop('wrap_cmd')
self.wrap_mode = kwargs.pop('wrap_mode')
# Last 3 timestamps command was run
@@ -93,9 +94,9 @@ Traffic Legend:
" ".join(self.wrap_cmd), self.target_port)
self.run_wrap_cmd()
else:
- print " - proxying from %s:%s to %s:%s\n" % (
+ print " - proxying from %s:%s to %s:59**\n" % (
self.listen_host, self.listen_port,
- self.target_host, self.target_port)
+ self.target_host)
def poll(self):
# If we are wrapping a command, check it's status
@@ -147,6 +148,20 @@ Traffic Legend:
self.rec = open(fname, 'w+')
self.rec.write("var VNC_frame_data = [\n")
+ # Read port from path
+ qs = parse_qs(urlparse(self.headers['path']).query)
+ port = qs.get('port')
+ if len(port) > 0:
+ port = port[0]
+ if port.isdigit():
+ self.target_port = int(port)
+ else:
+ raise Exception("Invalid port '%s'" % port)
+
+ if self.target_port < 5900 or self.target_port > 5999:
+ raise Exception("Port '%s' out of range"
+ % self.target_port)
+
# Connect to the target
self.msg("connecting to: %s:%s" % (
self.target_host, self.target_port))
@@ -249,7 +264,7 @@ Traffic Legend:
if __name__ == '__main__':
usage = "\n %prog [options]"
- usage += " [source_addr:]source_port target_addr:target_port"
+ usage += " [source_addr:]source_port target_addr"
usage += "\n %prog [options]"
usage += " [source_addr:]source_port -- WRAP_COMMAND_LINE"
parser = optparse.OptionParser(usage=usage)
@@ -297,14 +312,8 @@ if __name__ == '__main__':
if opts.wrap_cmd:
opts.target_host = None
- opts.target_port = None
else:
- if args[1].count(':') > 0:
- opts.target_host, opts.target_port = args[1].split(':')
- else:
- parser.error("Error parsing target")
- try: opts.target_port = int(opts.target_port)
- except: parser.error("Error parsing target port")
+ opts.target_host = args[1]
# Create and start the WebSockets proxy
server = WebSocketProxy(**opts.__dict__)
diff --git a/vnc_auto.html b/vnc_auto.html
index cb69929..07f6740 100644
--- a/vnc_auto.html
+++ b/vnc_auto.html
@@ -93,8 +93,9 @@
$D('sendCtrlAltDelButton').onclick = sendCtrlAltDel;
- host = WebUtil.getQueryVar('host', null);
- port = WebUtil.getQueryVar('port', null);
+ host = window.location.hostname;
+ port = window.location.port;
+ target_port = WebUtil.getQueryVar('port', '5900');
password = WebUtil.getQueryVar('password', '');
if ((!host) || (!port)) {
updateState('failed',
@@ -102,7 +103,8 @@
return;
}
- rfb = new RFB({'target': $D('VNC_canvas'),
+ rfb = new RFB({'path': "?port=" + target_port,
+ 'target': $D('VNC_canvas'),
'encrypt': WebUtil.getQueryVar('encrypt', false),
'true_color': WebUtil.getQueryVar('true_color', true),
'local_cursor': WebUtil.getQueryVar('cursor', true),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment