I'm trying to test out a secure websocket with SSL proxied with http-proxy in JS, but I'm getting the error:
"websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost."
I'm using Python websockets for the server side and websocket-client for the client side (since apparently websockets doesn't support proxies. All the files are in the same directory, and I want them to read from the same cert.pem and key.pem files to get the SSL information. I'd also like to use TLSv1.2 if possible. As a note, the proxy is there for logging purposes, to log the messages sent over the websocket connection, so they can't just be passed through encrypted.
wsserver.py:
...
async def server_main(PORT, ssl_context):
async with websockets.serve(handler, "127.0.0.1", PORT, ssl=ssl_context):
print('Server started')
await asyncio.Future()
if __name__ == '__main__':
print('Server starting...')
PORT = 9003
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
ssl_context.load_cert_chain(pathlib.Path(__file__).with_name('cert.pem'), pathlib.Path(__file__).with_name('key.pem'), "asdf")
asyncio.run(server_main(PORT, ssl_context))
wsclient.py:
...
async def client_main(PORT):
websocket.enableTrace = True
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
ssl_context.load_cert_chain(pathlib.Path(__file__).with_name('cert.pem'), pathlib.Path(__file__).with_name('key.pem'), "asdf")
ws = websocket.create_connection(f"ws://localhost:{PORT}", sllopt={"context": ssl_context})
proxy.js:
...
var proxy = new httpProxy.createProxyServer({
target: 'ws://localhost:9003',
ws: true,
ssl: {
key: fs.readFileSync('key.pem', 'utf8'),
cert: fs.readFileSync('cert.pem', 'utf8'),
passphrase: "asdf"
},
secure: false
}
...
I suspect the problem is in wsclient.py with the way I'm passing the ssl info in when creating the connection, but the websocket-client package documentation/examples are a little thin so I haven't been able to figure out where I'm going wrong.