2010-03-31 18:21:03 -05:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2010-04-30 16:41:09 -05:00
|
|
|
'''
|
|
|
|
A WebSocket to TCP socket proxy with support for "wss://" encryption.
|
2010-05-26 15:43:00 -05:00
|
|
|
Copyright 2010 Joel Martin
|
2010-07-17 12:05:58 -05:00
|
|
|
Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3)
|
2010-04-30 16:41:09 -05:00
|
|
|
|
|
|
|
You can make a cert/key with openssl using:
|
|
|
|
openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem
|
|
|
|
as taken from http://docs.python.org/dev/library/ssl.html#certificates
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2010-06-01 17:58:14 -05:00
|
|
|
import sys, socket, ssl, optparse
|
2010-03-31 18:21:03 -05:00
|
|
|
from select import select
|
2010-05-06 10:32:07 -05:00
|
|
|
from websocket import *
|
2010-03-31 18:21:03 -05:00
|
|
|
|
2010-04-05 23:54:30 -05:00
|
|
|
buffer_size = 65536
|
2010-06-01 17:58:14 -05:00
|
|
|
rec = None
|
2010-04-16 16:34:19 -05:00
|
|
|
|
2010-04-17 17:16:08 -05:00
|
|
|
traffic_legend = """
|
|
|
|
Traffic Legend:
|
|
|
|
} - Client receive
|
|
|
|
}. - Client receive partial
|
|
|
|
{ - Target receive
|
|
|
|
|
|
|
|
> - Target send
|
|
|
|
>. - Target send partial
|
|
|
|
< - Client send
|
|
|
|
<. - Client send partial
|
|
|
|
"""
|
|
|
|
|
2010-05-06 10:32:07 -05:00
|
|
|
def do_proxy(client, target):
|
2010-04-16 16:34:19 -05:00
|
|
|
""" Proxy WebSocket to normal socket. """
|
2010-06-01 17:58:14 -05:00
|
|
|
global rec
|
2010-03-31 19:37:49 -05:00
|
|
|
cqueue = []
|
2010-04-16 16:34:19 -05:00
|
|
|
cpartial = ""
|
2010-03-31 19:37:49 -05:00
|
|
|
tqueue = []
|
2010-05-11 09:25:29 -05:00
|
|
|
rlist = [client, target]
|
2010-03-31 18:21:03 -05:00
|
|
|
|
2010-03-31 19:37:49 -05:00
|
|
|
while True:
|
2010-05-11 09:25:29 -05:00
|
|
|
wlist = []
|
|
|
|
if tqueue: wlist.append(target)
|
|
|
|
if cqueue: wlist.append(client)
|
|
|
|
ins, outs, excepts = select(rlist, wlist, [], 1)
|
2010-03-31 19:37:49 -05:00
|
|
|
if excepts: raise Exception("Socket exception")
|
|
|
|
|
2010-05-11 09:25:29 -05:00
|
|
|
if target in outs:
|
2010-04-17 17:16:08 -05:00
|
|
|
dat = tqueue.pop(0)
|
|
|
|
sent = target.send(dat)
|
|
|
|
if sent == len(dat):
|
|
|
|
traffic(">")
|
|
|
|
else:
|
|
|
|
tqueue.insert(0, dat[sent:])
|
2010-05-06 10:32:07 -05:00
|
|
|
traffic(".>")
|
2010-06-01 17:58:14 -05:00
|
|
|
##if rec: rec.write("Target send: %s\n" % map(ord, dat))
|
2010-04-17 17:16:08 -05:00
|
|
|
|
2010-05-11 09:25:29 -05:00
|
|
|
if client in outs:
|
2010-04-17 17:16:08 -05:00
|
|
|
dat = cqueue.pop(0)
|
|
|
|
sent = client.send(dat)
|
|
|
|
if sent == len(dat):
|
|
|
|
traffic("<")
|
2010-06-01 17:58:14 -05:00
|
|
|
##if rec: rec.write("Client send: %s ...\n" % repr(dat[0:80]))
|
|
|
|
if rec: rec.write("%s,\n" % repr(">" + dat[1:-1]))
|
2010-04-17 17:16:08 -05:00
|
|
|
else:
|
|
|
|
cqueue.insert(0, dat[sent:])
|
|
|
|
traffic("<.")
|
2010-06-01 17:58:14 -05:00
|
|
|
##if rec: rec.write("Client send partial: %s\n" % repr(dat[0:send]))
|
2010-04-17 17:16:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
if target in ins:
|
|
|
|
buf = target.recv(buffer_size)
|
|
|
|
if len(buf) == 0: raise Exception("Target closed")
|
|
|
|
|
2010-05-06 10:32:07 -05:00
|
|
|
cqueue.append(encode(buf))
|
2010-04-17 17:16:08 -05:00
|
|
|
traffic("{")
|
2010-06-01 17:58:14 -05:00
|
|
|
##if rec: rec.write("Target recv (%d): %s\n" % (len(buf), map(ord, buf)))
|
2010-04-17 17:16:08 -05:00
|
|
|
|
2010-03-31 19:37:49 -05:00
|
|
|
if client in ins:
|
2010-04-05 23:54:30 -05:00
|
|
|
buf = client.recv(buffer_size)
|
2010-03-31 19:37:49 -05:00
|
|
|
if len(buf) == 0: raise Exception("Client closed")
|
2010-04-16 16:34:19 -05:00
|
|
|
|
2010-08-03 13:21:00 -05:00
|
|
|
if buf == '\xff\x00':
|
|
|
|
raise Exception("Client sent orderly close frame")
|
|
|
|
elif buf[-1] == '\xff':
|
2010-05-06 10:32:07 -05:00
|
|
|
if buf.count('\xff') > 1:
|
|
|
|
traffic(str(buf.count('\xff')))
|
2010-04-17 17:16:08 -05:00
|
|
|
traffic("}")
|
2010-06-01 17:58:14 -05:00
|
|
|
##if rec: rec.write("Client recv (%d): %s\n" % (len(buf), repr(buf)))
|
|
|
|
if rec: rec.write("%s,\n" % repr(buf[1:-1]))
|
2010-04-16 16:34:19 -05:00
|
|
|
if cpartial:
|
|
|
|
tqueue.extend(decode(cpartial + buf))
|
|
|
|
cpartial = ""
|
|
|
|
else:
|
|
|
|
tqueue.extend(decode(buf))
|
|
|
|
else:
|
2010-05-06 10:32:07 -05:00
|
|
|
traffic(".}")
|
2010-06-01 17:58:14 -05:00
|
|
|
##if rec: rec.write("Client recv partial (%d): %s\n" % (len(buf), repr(buf)))
|
2010-04-16 16:34:19 -05:00
|
|
|
cpartial = cpartial + buf
|
|
|
|
|
2010-05-06 10:32:07 -05:00
|
|
|
def proxy_handler(client):
|
2010-06-01 17:58:14 -05:00
|
|
|
global target_host, target_port, options, rec
|
2010-05-06 10:32:07 -05:00
|
|
|
|
2010-06-17 16:06:18 -05:00
|
|
|
if settings['record']:
|
|
|
|
print "Opening record file: %s" % settings['record']
|
2010-06-17 17:05:33 -05:00
|
|
|
rec = open(settings['record'], 'a')
|
2010-06-17 16:06:18 -05:00
|
|
|
|
2010-05-06 10:32:07 -05:00
|
|
|
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-03-31 19:37:49 -05:00
|
|
|
|
2010-04-17 17:16:08 -05:00
|
|
|
print traffic_legend
|
2010-05-06 10:32:07 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
do_proxy(client, tsock)
|
|
|
|
except:
|
|
|
|
if tsock: tsock.close()
|
2010-06-01 17:58:14 -05:00
|
|
|
if rec: rec.close()
|
2010-05-06 10:32:07 -05:00
|
|
|
raise
|
2010-03-31 18:21:03 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2010-06-16 12:37:03 -05:00
|
|
|
usage = "%prog [--record FILE]"
|
|
|
|
usage += " [source_addr:]source_port target_addr:target_port"
|
|
|
|
parser = optparse.OptionParser(usage=usage)
|
2010-06-16 13:58:00 -05:00
|
|
|
parser.add_option("--record",
|
2010-06-01 17:58:14 -05:00
|
|
|
help="record session to a file", metavar="FILE")
|
2010-06-17 16:06:18 -05:00
|
|
|
parser.add_option("--foreground", "-f",
|
|
|
|
dest="daemon", default=True, action="store_false",
|
|
|
|
help="stay in foreground, do not daemonize")
|
2010-06-16 13:58:00 -05:00
|
|
|
parser.add_option("--ssl-only", action="store_true",
|
|
|
|
help="disallow non-encrypted connections")
|
2010-06-17 16:06:18 -05:00
|
|
|
parser.add_option("--cert", default="self.pem",
|
|
|
|
help="SSL certificate")
|
2010-06-01 17:58:14 -05:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2010-06-16 12:37:03 -05:00
|
|
|
if len(args) > 2: parser.error("Too many arguments")
|
|
|
|
if len(args) < 2: parser.error("Too few arguments")
|
|
|
|
if args[0].count(':') > 0:
|
2010-06-17 16:06:18 -05:00
|
|
|
host,port = args[0].split(':')
|
2010-06-16 12:37:03 -05:00
|
|
|
else:
|
2010-06-17 16:06:18 -05:00
|
|
|
host,port = '',args[0]
|
2010-06-16 12:37:03 -05:00
|
|
|
if args[1].count(':') > 0:
|
|
|
|
target_host,target_port = args[1].split(':')
|
|
|
|
else:
|
|
|
|
parser.error("Error parsing target")
|
2010-06-17 16:06:18 -05:00
|
|
|
try: port = int(port)
|
2010-06-01 17:58:14 -05:00
|
|
|
except: parser.error("Error parsing listen port")
|
2010-06-16 12:37:03 -05:00
|
|
|
try: target_port = int(target_port)
|
2010-06-01 17:58:14 -05:00
|
|
|
except: parser.error("Error parsing target port")
|
|
|
|
|
2010-06-17 17:24:54 -05:00
|
|
|
if options.ssl_only and not os.path.exists(options.cert):
|
|
|
|
parser.error("SSL only and %s not found" % options.cert)
|
|
|
|
|
2010-06-17 16:06:18 -05:00
|
|
|
settings['listen_host'] = host
|
|
|
|
settings['listen_port'] = port
|
|
|
|
settings['handler'] = proxy_handler
|
|
|
|
settings['cert'] = os.path.abspath(options.cert)
|
|
|
|
settings['ssl_only'] = options.ssl_only
|
|
|
|
settings['daemon'] = options.daemon
|
2010-06-17 17:05:33 -05:00
|
|
|
if options.record:
|
|
|
|
settings['record'] = os.path.abspath(options.record)
|
2010-06-17 16:06:18 -05:00
|
|
|
start_server()
|