2010-04-01 00:21:03 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2010-04-01 01:37:49 +01:00
|
|
|
import sys, os, socket, time, traceback
|
2010-04-03 01:16:25 +01:00
|
|
|
from base64 import b64encode, b64decode
|
2010-04-01 00:21:03 +01:00
|
|
|
from select import select
|
|
|
|
|
2010-04-06 05:54:30 +01:00
|
|
|
buffer_size = 65536
|
2010-04-17 23:16:08 +01:00
|
|
|
send_seq = 0
|
2010-04-06 05:54:30 +01:00
|
|
|
|
2010-04-01 00:21:03 +01:00
|
|
|
server_handshake = """HTTP/1.1 101 Web Socket Protocol Handshake\r
|
|
|
|
Upgrade: WebSocket\r
|
|
|
|
Connection: Upgrade\r
|
|
|
|
WebSocket-Origin: %s\r
|
|
|
|
WebSocket-Location: ws://%s%s\r
|
|
|
|
WebSocket-Protocol: sample\r
|
|
|
|
\r
|
|
|
|
"""
|
|
|
|
|
2010-04-16 22:34:19 +01:00
|
|
|
policy_response = """<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>"""
|
|
|
|
|
2010-04-17 23:16:08 +01:00
|
|
|
traffic_legend = """
|
|
|
|
Traffic Legend:
|
|
|
|
} - Client receive
|
|
|
|
}. - Client receive partial
|
|
|
|
{ - Target receive
|
|
|
|
|
|
|
|
> - Target send
|
|
|
|
>. - Target send partial
|
|
|
|
< - Client send
|
|
|
|
<. - Client send partial
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2010-04-01 01:37:49 +01:00
|
|
|
def handshake(client):
|
2010-04-16 22:34:19 +01:00
|
|
|
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
|
2010-04-01 01:37:49 +01:00
|
|
|
req_lines = handshake.split("\r\n")
|
|
|
|
_, path, _ = req_lines[0].split(" ")
|
|
|
|
_, origin = req_lines[4].split(" ")
|
|
|
|
_, host = req_lines[3].split(" ")
|
|
|
|
client.send(server_handshake % (origin, host, path))
|
|
|
|
|
2010-04-01 17:36:22 +01:00
|
|
|
def traffic(token="."):
|
|
|
|
sys.stdout.write(token)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2010-04-16 22:34:19 +01:00
|
|
|
def decode(buf):
|
|
|
|
""" Parse out WebSocket packets. """
|
|
|
|
if buf.count('\xff') > 1:
|
2010-04-17 23:16:08 +01:00
|
|
|
traffic(str(buf.count('\xff')))
|
|
|
|
return [b64decode(d[1:]) for d in buf.split('\xff')]
|
2010-04-16 22:34:19 +01:00
|
|
|
else:
|
|
|
|
return [b64decode(buf[1:-1])]
|
|
|
|
|
2010-04-01 01:37:49 +01:00
|
|
|
def proxy(client, target):
|
2010-04-16 22:34:19 +01:00
|
|
|
""" Proxy WebSocket to normal socket. """
|
2010-04-17 23:16:08 +01:00
|
|
|
global send_seq
|
2010-04-01 01:37:49 +01:00
|
|
|
cqueue = []
|
2010-04-16 22:34:19 +01:00
|
|
|
cpartial = ""
|
2010-04-01 01:37:49 +01:00
|
|
|
tqueue = []
|
|
|
|
socks = [client, target]
|
2010-04-01 00:21:03 +01:00
|
|
|
|
2010-04-01 01:37:49 +01:00
|
|
|
while True:
|
|
|
|
ins, outs, excepts = select(socks, socks, socks, 1)
|
|
|
|
if excepts: raise Exception("Socket exception")
|
|
|
|
|
2010-04-17 23:16:08 +01:00
|
|
|
if tqueue and target in outs:
|
|
|
|
#print "Target send: %s" % repr(tqueue[0])
|
|
|
|
log.write("Target send: %s\n" % map(ord, tqueue[0]))
|
|
|
|
dat = tqueue.pop(0)
|
|
|
|
sent = target.send(dat)
|
|
|
|
if sent == len(dat):
|
|
|
|
traffic(">")
|
|
|
|
else:
|
|
|
|
tqueue.insert(0, dat[sent:])
|
|
|
|
traffic(">.")
|
|
|
|
|
|
|
|
if cqueue and client in outs:
|
|
|
|
dat = cqueue.pop(0)
|
|
|
|
sent = client.send(dat)
|
|
|
|
if sent == len(dat):
|
|
|
|
traffic("<")
|
|
|
|
log.write("Client send: %s\n" % repr(dat))
|
|
|
|
else:
|
|
|
|
cqueue.insert(0, dat[sent:])
|
|
|
|
traffic("<.")
|
|
|
|
log.write("Client send partial: %s\n" % repr(dat[0:send]))
|
|
|
|
|
|
|
|
|
|
|
|
if target in ins:
|
|
|
|
buf = target.recv(buffer_size)
|
|
|
|
if len(buf) == 0: raise Exception("Target closed")
|
|
|
|
|
|
|
|
#enc = b64encode(buf)
|
|
|
|
#chksum = sum([ord(c) for c in enc])
|
|
|
|
#cqueue.append("\x00^" + str(chksum) + "@" + enc + "$\xff")
|
|
|
|
|
|
|
|
cqueue.append("\x00%d:%s\xff" % (send_seq, b64encode(buf)))
|
|
|
|
send_seq += 1
|
|
|
|
|
|
|
|
log.write("Target recv (%d): %s\n" % (len(buf), map(ord, buf)))
|
|
|
|
traffic("{")
|
|
|
|
|
2010-04-01 01:37:49 +01:00
|
|
|
if client in ins:
|
2010-04-06 05:54:30 +01:00
|
|
|
buf = client.recv(buffer_size)
|
2010-04-01 01:37:49 +01:00
|
|
|
if len(buf) == 0: raise Exception("Client closed")
|
2010-04-16 22:34:19 +01:00
|
|
|
|
|
|
|
if buf[-1] == "\xff":
|
2010-04-17 23:16:08 +01:00
|
|
|
traffic("}")
|
|
|
|
log.write("Client recv (%d): %s\n" % (len(buf), repr(buf)))
|
2010-04-16 22:34:19 +01:00
|
|
|
if cpartial:
|
|
|
|
tqueue.extend(decode(cpartial + buf))
|
|
|
|
cpartial = ""
|
|
|
|
else:
|
|
|
|
tqueue.extend(decode(buf))
|
|
|
|
else:
|
2010-04-17 23:16:08 +01:00
|
|
|
traffic("}.")
|
|
|
|
log.write("Client recv partial (%d): %s\n" % (len(buf), repr(buf)))
|
2010-04-16 22:34:19 +01:00
|
|
|
cpartial = cpartial + buf
|
|
|
|
|
2010-04-01 01:37:49 +01:00
|
|
|
|
|
|
|
def start_server(listen_port, target_host, target_port):
|
2010-04-17 23:16:08 +01:00
|
|
|
global send_seq
|
2010-04-01 00:21:03 +01:00
|
|
|
lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
lsock.bind(('', listen_port))
|
|
|
|
lsock.listen(100)
|
2010-04-17 23:16:08 +01:00
|
|
|
print traffic_legend
|
2010-04-01 00:21:03 +01:00
|
|
|
while True:
|
|
|
|
try:
|
2010-04-01 01:37:49 +01:00
|
|
|
csock = tsock = None
|
2010-04-17 23:16:08 +01:00
|
|
|
print 'waiting for connection on port %s' % listen_port
|
2010-04-01 00:21:03 +01:00
|
|
|
csock, address = lsock.accept()
|
2010-04-01 01:37:49 +01:00
|
|
|
print 'Got client connection from %s' % address[0]
|
2010-04-01 00:21:03 +01:00
|
|
|
handshake(csock)
|
|
|
|
print "Connecting to: %s:%s" % (target_host, target_port)
|
|
|
|
tsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
tsock.connect((target_host, target_port))
|
|
|
|
|
2010-04-17 23:16:08 +01:00
|
|
|
send_seq = 0
|
2010-04-01 01:37:49 +01:00
|
|
|
proxy(csock, tsock)
|
2010-04-01 00:21:03 +01:00
|
|
|
|
2010-04-01 01:37:49 +01:00
|
|
|
except Exception:
|
|
|
|
print "Ignoring exception:"
|
|
|
|
print traceback.format_exc()
|
|
|
|
if csock: csock.close()
|
|
|
|
if tsock: tsock.close()
|
2010-04-01 00:21:03 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2010-04-17 23:16:08 +01:00
|
|
|
log = open("ws.log", 'w')
|
2010-04-01 00:21:03 +01:00
|
|
|
try:
|
|
|
|
if len(sys.argv) != 4: raise
|
|
|
|
listen_port = int(sys.argv[1])
|
|
|
|
target_host = sys.argv[2]
|
|
|
|
target_port = int(sys.argv[3])
|
|
|
|
except:
|
|
|
|
print "Usage: <listen_port> <target_host> <target_port>"
|
|
|
|
sys.exit(1)
|
2010-04-01 01:37:49 +01:00
|
|
|
start_server(listen_port, target_host, target_port)
|