websockify/wsproxy.py

99 lines
3.0 KiB
Python
Raw Normal View History

#!/usr/bin/python
2010-04-01 01:37:49 +01:00
import sys, os, socket, time, traceback
from base64 import b64encode, b64decode
from select import select
buffer_size = 65536
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-01 01:37:49 +01:00
def handshake(client):
handshake = client.recv(255)
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))
def traffic(token="."):
sys.stdout.write(token)
sys.stdout.flush()
2010-04-01 01:37:49 +01:00
def proxy(client, target):
cqueue = []
tqueue = []
socks = [client, target]
2010-04-01 01:37:49 +01:00
while True:
ins, outs, excepts = select(socks, socks, socks, 1)
if excepts: raise Exception("Socket exception")
if client in ins:
buf = client.recv(buffer_size)
2010-04-01 01:37:49 +01:00
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("}")
2010-04-01 01:37:49 +01:00
if target in ins:
buf = target.recv(buffer_size)
2010-04-01 01:37:49 +01:00
if len(buf) == 0: raise Exception("Target closed")
cqueue.append("\x00" + b64encode(buf) + "\xff")
#print "Target recv: %s (%d)" % (repr(buf), len(buf))
traffic("{")
2010-04-01 01:37:49 +01:00
if cqueue and client in outs:
while cqueue:
#print "Client send: %s" % repr(cqueue[0])
2010-04-01 01:37:49 +01:00
client.send(cqueue.pop(0))
traffic("<")
2010-04-01 01:37:49 +01:00
if tqueue and target in outs:
while tqueue:
#print "Target send: %s" % repr(tqueue[0])
2010-04-01 01:37:49 +01:00
target.send(tqueue.pop(0))
traffic(">")
2010-04-01 01:37:49 +01:00
def start_server(listen_port, target_host, target_port):
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)
while True:
try:
2010-04-01 01:37:49 +01:00
csock = tsock = None
print 'listening on port %s' % listen_port
csock, address = lsock.accept()
2010-04-01 01:37:49 +01:00
print 'Got client connection from %s' % address[0]
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-01 01:37:49 +01:00
proxy(csock, tsock)
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()
if __name__ == '__main__':
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)