diff --git a/include/websock.js b/include/websock.js index 7d12644e..01a24c3f 100644 --- a/include/websock.js +++ b/include/websock.js @@ -331,6 +331,9 @@ function open(uri, protocols) { websocket = {}; } else { websocket = new WebSocket(uri, protocols); + if (protocols.indexOf('binary') >= 0) { + websocket.binaryType = 'arraybuffer'; + } } websocket.onmessage = recv_message; @@ -343,9 +346,6 @@ function open(uri, protocols) { mode = 'base64'; Util.Error("Server select no sub-protocol!: " + websocket.protocol); } - if (mode === 'binary') { - websocket.binaryType = 'arraybuffer'; - } eventHandlers.open(); Util.Debug("<< WebSock.onopen"); }; diff --git a/utils/websocket.py b/utils/websocket.py index 1a5b9ffa..3f539a1d 100644 --- a/utils/websocket.py +++ b/utils/websocket.py @@ -941,6 +941,11 @@ Sec-WebSocket-Accept: %s\r if startsock: startsock.close() + # Close listen port + self.vmsg("Closing socket listening at %s:%s" + % (self.listen_host, self.listen_port)) + lsock.close() + # HTTP handler with WebSocket upgrade support class WSRequestHandler(SimpleHTTPRequestHandler): diff --git a/utils/websockify b/utils/websockify index 8bd67ec4..1154d925 100755 --- a/utils/websockify +++ b/utils/websockify @@ -14,8 +14,11 @@ as taken from http://docs.python.org/dev/library/ssl.html#certificates import signal, socket, optparse, time, os, sys, subprocess from select import select import websocket -try: from urllib.parse import parse_qs, urlparse -except: from urlparse import parse_qs, urlparse +try: + from urllib.parse import parse_qs, urlparse +except: + from cgi import parse_qs + from urlparse import urlparse class WebSocketProxy(websocket.WebSocketServer): """ @@ -205,7 +208,7 @@ Traffic Legend: # Extract the token parameter from url args = parse_qs(urlparse(path)[4]) # 4 is the query from url - if not len(args['token']): + if not args.has_key('token') or not len(args['token']): raise self.EClose("Token not present") token = args['token'][0].rstrip('\n') @@ -249,6 +252,24 @@ Traffic Legend: ins, outs, excepts = select(rlist, wlist, [], 1) if excepts: raise Exception("Socket exception") + if self.client in outs: + # Send queued target data to the client + c_pend = self.send_frames(cqueue) + + cqueue = [] + + if self.client in ins: + # Receive client data, decode it, and queue for target + bufs, closed = self.recv_frames() + tqueue.extend(bufs) + + if closed: + # TODO: What about blocking on client socket? + self.vmsg("%s:%s: Client closed connection" %( + self.target_host, self.target_port)) + raise self.CClose(closed['code'], closed['reason']) + + if target in outs: # Send queued client data to the target dat = tqueue.pop(0) @@ -273,24 +294,6 @@ Traffic Legend: self.traffic("{") - if self.client in outs: - # Send queued target data to the client - c_pend = self.send_frames(cqueue) - - cqueue = [] - - - if self.client in ins: - # Receive client data, decode it, and queue for target - bufs, closed = self.recv_frames() - tqueue.extend(bufs) - - if closed: - # TODO: What about blocking on client socket? - self.vmsg("%s:%s: Client closed connection" %( - self.target_host, self.target_port)) - raise self.CClose(closed['code'], closed['reason']) - def _subprocess_setup(): # Python installs a SIGPIPE handler by default. This is usually not what