Add --record option to wsproxy.py.
This commit is contained in:
parent
d41c33e4b7
commit
325d9eb7f3
48
wsproxy.py
48
wsproxy.py
|
@ -11,11 +11,12 @@ as taken from http://docs.python.org/dev/library/ssl.html#certificates
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import sys, socket, ssl
|
import sys, socket, ssl, optparse
|
||||||
from select import select
|
from select import select
|
||||||
from websocket import *
|
from websocket import *
|
||||||
|
|
||||||
buffer_size = 65536
|
buffer_size = 65536
|
||||||
|
rec = None
|
||||||
|
|
||||||
traffic_legend = """
|
traffic_legend = """
|
||||||
Traffic Legend:
|
Traffic Legend:
|
||||||
|
@ -31,6 +32,7 @@ Traffic Legend:
|
||||||
|
|
||||||
def do_proxy(client, target):
|
def do_proxy(client, target):
|
||||||
""" Proxy WebSocket to normal socket. """
|
""" Proxy WebSocket to normal socket. """
|
||||||
|
global rec
|
||||||
cqueue = []
|
cqueue = []
|
||||||
cpartial = ""
|
cpartial = ""
|
||||||
tqueue = []
|
tqueue = []
|
||||||
|
@ -51,18 +53,19 @@ def do_proxy(client, target):
|
||||||
else:
|
else:
|
||||||
tqueue.insert(0, dat[sent:])
|
tqueue.insert(0, dat[sent:])
|
||||||
traffic(".>")
|
traffic(".>")
|
||||||
##log.write("Target send: %s\n" % map(ord, dat))
|
##if rec: rec.write("Target send: %s\n" % map(ord, dat))
|
||||||
|
|
||||||
if client in outs:
|
if client in outs:
|
||||||
dat = cqueue.pop(0)
|
dat = cqueue.pop(0)
|
||||||
sent = client.send(dat)
|
sent = client.send(dat)
|
||||||
if sent == len(dat):
|
if sent == len(dat):
|
||||||
traffic("<")
|
traffic("<")
|
||||||
##log.write("Client send: %s ...\n" % repr(dat[0:80]))
|
##if rec: rec.write("Client send: %s ...\n" % repr(dat[0:80]))
|
||||||
|
if rec: rec.write("%s,\n" % repr(">" + dat[1:-1]))
|
||||||
else:
|
else:
|
||||||
cqueue.insert(0, dat[sent:])
|
cqueue.insert(0, dat[sent:])
|
||||||
traffic("<.")
|
traffic("<.")
|
||||||
##log.write("Client send partial: %s\n" % repr(dat[0:send]))
|
##if rec: rec.write("Client send partial: %s\n" % repr(dat[0:send]))
|
||||||
|
|
||||||
|
|
||||||
if target in ins:
|
if target in ins:
|
||||||
|
@ -71,7 +74,7 @@ def do_proxy(client, target):
|
||||||
|
|
||||||
cqueue.append(encode(buf))
|
cqueue.append(encode(buf))
|
||||||
traffic("{")
|
traffic("{")
|
||||||
##log.write("Target recv (%d): %s\n" % (len(buf), map(ord, buf)))
|
##if rec: rec.write("Target recv (%d): %s\n" % (len(buf), map(ord, buf)))
|
||||||
|
|
||||||
if client in ins:
|
if client in ins:
|
||||||
buf = client.recv(buffer_size)
|
buf = client.recv(buffer_size)
|
||||||
|
@ -81,7 +84,8 @@ def do_proxy(client, target):
|
||||||
if buf.count('\xff') > 1:
|
if buf.count('\xff') > 1:
|
||||||
traffic(str(buf.count('\xff')))
|
traffic(str(buf.count('\xff')))
|
||||||
traffic("}")
|
traffic("}")
|
||||||
##log.write("Client recv (%d): %s\n" % (len(buf), repr(buf)))
|
##if rec: rec.write("Client recv (%d): %s\n" % (len(buf), repr(buf)))
|
||||||
|
if rec: rec.write("%s,\n" % repr(buf[1:-1]))
|
||||||
if cpartial:
|
if cpartial:
|
||||||
tqueue.extend(decode(cpartial + buf))
|
tqueue.extend(decode(cpartial + buf))
|
||||||
cpartial = ""
|
cpartial = ""
|
||||||
|
@ -89,32 +93,42 @@ def do_proxy(client, target):
|
||||||
tqueue.extend(decode(buf))
|
tqueue.extend(decode(buf))
|
||||||
else:
|
else:
|
||||||
traffic(".}")
|
traffic(".}")
|
||||||
##log.write("Client recv partial (%d): %s\n" % (len(buf), repr(buf)))
|
##if rec: rec.write("Client recv partial (%d): %s\n" % (len(buf), repr(buf)))
|
||||||
cpartial = cpartial + buf
|
cpartial = cpartial + buf
|
||||||
|
|
||||||
def proxy_handler(client):
|
def proxy_handler(client):
|
||||||
global target_host, target_port
|
global target_host, target_port, options, rec
|
||||||
|
|
||||||
print "Connecting to: %s:%s" % (target_host, target_port)
|
print "Connecting to: %s:%s" % (target_host, target_port)
|
||||||
tsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
tsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
tsock.connect((target_host, target_port))
|
tsock.connect((target_host, target_port))
|
||||||
|
|
||||||
|
if options.record:
|
||||||
|
print "Opening record file: %s" % options.record
|
||||||
|
rec = open(options.record, 'w')
|
||||||
|
|
||||||
print traffic_legend
|
print traffic_legend
|
||||||
|
|
||||||
try:
|
try:
|
||||||
do_proxy(client, tsock)
|
do_proxy(client, tsock)
|
||||||
except:
|
except:
|
||||||
if tsock: tsock.close()
|
if tsock: tsock.close()
|
||||||
|
if rec: rec.close()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
##log = open("ws.log", 'w')
|
parser = optparse.OptionParser()
|
||||||
try:
|
parser.add_option("--record", dest="record",
|
||||||
if len(sys.argv) != 4: raise
|
help="record session to a file", metavar="FILE")
|
||||||
listen_port = int(sys.argv[1])
|
(options, args) = parser.parse_args()
|
||||||
target_host = sys.argv[2]
|
|
||||||
target_port = int(sys.argv[3])
|
if len(args) > 3: parser.error("Too many arguments")
|
||||||
except:
|
if len(args) < 3: parser.error("Too few arguments")
|
||||||
print "Usage: <listen_port> <target_host> <target_port>"
|
try: listen_port = int(args[0])
|
||||||
sys.exit(1)
|
except: parser.error("Error parsing listen port")
|
||||||
|
try: target_host = args[1]
|
||||||
|
except: parser.error("Error parsing target host")
|
||||||
|
try: target_port = int(args[2])
|
||||||
|
except: parser.error("Error parsing target port")
|
||||||
|
|
||||||
start_server(listen_port, proxy_handler)
|
start_server(listen_port, proxy_handler)
|
||||||
|
|
Loading…
Reference in New Issue