Reassemble partial client packets in wsproxy.py

This commit is contained in:
Joel Martin 2010-04-16 16:34:19 -05:00
parent af7a319387
commit 5d2c386400
2 changed files with 51 additions and 18 deletions

34
vnc.js
View File

@ -802,33 +802,39 @@ init_ws: function () {
console.log("connecting to " + uri);
RFB.ws = new WebSocket(uri);
RFB.ws.onmessage = function(e) {
//console.log(">> onmessage");
//console.log(">> WebSockets.onmessage");
RFB.d = RFB.d.concat(Base64.decode(e.data));
if (RFB.state != 'normal') {
RFB.init_msg();
} else {
RFB.normal_msg();
}
if (RFB.state == 'reset') {
if (RFB.state == 'closed') {
console.log("onmessage while close");
} else if (RFB.state == 'reset') {
/* close and reset connection */
RFB.disconnect();
RFB.init_ws();
} else if (RFB.state == 'failed') {
console.log("Giving up!");
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) {
console.log(">> onopen");
console.log(">> WebSockets.onopen");
RFB.state = "ProtocolVersion";
console.log("<< onopen");
console.log("<< WebSockets.onopen");
};
RFB.ws.onclose = function(e) {
console.log(">> onclose");
console.log(">> WebSockets.onclose");
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");
},
@ -838,7 +844,7 @@ connect: function () {
RFB.host = $('host').value;
RFB.port = $('port').value;
RFB.password = $('password').value;
if ((!host) || (!port)) {
if ((!RFB.host) || (!RFB.port)) {
console.log("must set host and port");
return;
}

View File

@ -15,8 +15,16 @@ WebSocket-Protocol: sample\r
\r
"""
policy_response = """<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>"""
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")
_, path, _ = req_lines[0].split(" ")
_, origin = req_lines[4].split(" ")
@ -27,8 +35,17 @@ def traffic(token="."):
sys.stdout.write(token)
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):
""" Proxy WebSocket to normal socket. """
cqueue = []
cpartial = ""
tqueue = []
socks = [client, target]
@ -39,9 +56,19 @@ def proxy(client, target):
if client in ins:
buf = client.recv(buffer_size)
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))
traffic("}")
if buf[-1] == "\xff":
if cpartial:
tqueue.extend(decode(cpartial + buf))
cpartial = ""
else:
tqueue.extend(decode(buf))
traffic("}")
else:
traffic(".}")
cpartial = cpartial + buf
#print "Client recv: %s (%d)" % (repr(buf, len(buf))
if target in ins:
buf = target.recv(buffer_size)