Commit 5d2c3864 authored by Joel Martin's avatar Joel Martin

Reassemble partial client packets in wsproxy.py

parent af7a3193
...@@ -802,33 +802,39 @@ init_ws: function () { ...@@ -802,33 +802,39 @@ init_ws: function () {
console.log("connecting to " + uri); console.log("connecting to " + uri);
RFB.ws = new WebSocket(uri); RFB.ws = new WebSocket(uri);
RFB.ws.onmessage = function(e) { RFB.ws.onmessage = function(e) {
//console.log(">> onmessage"); //console.log(">> WebSockets.onmessage");
RFB.d = RFB.d.concat(Base64.decode(e.data)); RFB.d = RFB.d.concat(Base64.decode(e.data));
if (RFB.state != 'normal') { if (RFB.state == 'closed') {
RFB.init_msg(); console.log("onmessage while close");
} else { } else if (RFB.state == 'reset') {
RFB.normal_msg();
}
if (RFB.state == 'reset') {
/* close and reset connection */ /* close and reset connection */
RFB.disconnect(); RFB.disconnect();
RFB.init_ws(); RFB.init_ws();
} else if (RFB.state == 'failed') { } else if (RFB.state == 'failed') {
console.log("Giving up!"); console.log("Giving up!");
RFB.disconnect(); RFB.disconnect();
} else if (RFB.state != 'normal') {
RFB.init_msg();
} else {
RFB.normal_msg();
} }
//console.log("<< onmessage"); //console.log("<< WebSockets.onmessage");
}; };
RFB.ws.onopen = function(e) { RFB.ws.onopen = function(e) {
console.log(">> onopen"); console.log(">> WebSockets.onopen");
RFB.state = "ProtocolVersion"; RFB.state = "ProtocolVersion";
console.log("<< onopen"); console.log("<< WebSockets.onopen");
}; };
RFB.ws.onclose = function(e) { RFB.ws.onclose = function(e) {
console.log(">> onclose"); console.log(">> WebSockets.onclose");
RFB.state = "closed"; RFB.state = "closed";
console.log("<< onclose"); console.log("<< WebSockets.onclose");
} };
RFB.ws.onerror = function(e) {
console.log(">> WebSockets.onerror");
console.log(" " + e);
console.log("<< WebSockets.onerror");
};
console.log("<< init_ws"); console.log("<< init_ws");
}, },
...@@ -838,7 +844,7 @@ connect: function () { ...@@ -838,7 +844,7 @@ connect: function () {
RFB.host = $('host').value; RFB.host = $('host').value;
RFB.port = $('port').value; RFB.port = $('port').value;
RFB.password = $('password').value; RFB.password = $('password').value;
if ((!host) || (!port)) { if ((!RFB.host) || (!RFB.port)) {
console.log("must set host and port"); console.log("must set host and port");
return; return;
} }
......
...@@ -15,8 +15,16 @@ WebSocket-Protocol: sample\r ...@@ -15,8 +15,16 @@ WebSocket-Protocol: sample\r
\r \r
""" """
policy_response = """<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>"""
def handshake(client): def handshake(client):
handshake = client.recv(255) handshake = client.recv(1024)
print "Handshake [%s]" % handshake
if handshake.startswith("<policy-file-request/>"):
print "Sending:", policy_response
client.send(policy_response)
handshake = client.recv(1024)
print "Handshake [%s]" % handshake
req_lines = handshake.split("\r\n") req_lines = handshake.split("\r\n")
_, path, _ = req_lines[0].split(" ") _, path, _ = req_lines[0].split(" ")
_, origin = req_lines[4].split(" ") _, origin = req_lines[4].split(" ")
...@@ -27,8 +35,17 @@ def traffic(token="."): ...@@ -27,8 +35,17 @@ def traffic(token="."):
sys.stdout.write(token) sys.stdout.write(token)
sys.stdout.flush() sys.stdout.flush()
def decode(buf):
""" Parse out WebSocket packets. """
if buf.count('\xff') > 1:
return [d[1:] for d in buf.split('\xff')]
else:
return [b64decode(buf[1:-1])]
def proxy(client, target): def proxy(client, target):
""" Proxy WebSocket to normal socket. """
cqueue = [] cqueue = []
cpartial = ""
tqueue = [] tqueue = []
socks = [client, target] socks = [client, target]
...@@ -39,9 +56,19 @@ def proxy(client, target): ...@@ -39,9 +56,19 @@ def proxy(client, target):
if client in ins: if client in ins:
buf = client.recv(buffer_size) buf = client.recv(buffer_size)
if len(buf) == 0: raise Exception("Client closed") if len(buf) == 0: raise Exception("Client closed")
tqueue.append(b64decode(buf[1:-1]))
#print "Client recv: %s (%d)" % (repr(buf[1:-1]), len(buf)) if buf[-1] == "\xff":
if cpartial:
tqueue.extend(decode(cpartial + buf))
cpartial = ""
else:
tqueue.extend(decode(buf))
traffic("}") traffic("}")
else:
traffic(".}")
cpartial = cpartial + buf
#print "Client recv: %s (%d)" % (repr(buf, len(buf))
if target in ins: if target in ins:
buf = target.recv(buffer_size) buf = target.recv(buffer_size)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment