http-party/node-http-proxy

How to detect which client is receiving the package?

legionir opened this issue · 0 comments

Hello,

I am creating a WebSocket proxy. And I need to know which client is receiving the package. Basically I should store data from incoming traffic to use them in outgoing traffic and each client has different data. I searched and tried different ways but none of them worked. Can anyone suggest a working solution?

var proxyWebsocket, serverWebsocket, clientData = {};
const startWSProxy = (data) => {
	proxyWebsocket = new httpProxy.createProxyServer({
		target: {
			protocol: 'ws',
			host: 'localhost',
			port: localPacketPort
		},
		ws: true,
		xfwd: true,
		toProxy: true
	});

	proxyWebsocket.on('open', function (proxySocket) {

		proxySocket.on('data', (res) => {
			// access the identifier here
			// For example:
			// proxySocket._uuid
			// console.log(clientData[proxySocket._uuid]);
		});
	});

	serverWebsocket = http.createServer();
	serverWebsocket.on('upgrade', function (req, socket, head) {
   proxyWebsocket.ws(req, socket, head);
		// Set an identifier here
		// For example:
		// socket._uuid = uuid_generate();
		socket.on('data', (res) => {
			// clientData[socket._uuid][] = res
		});
	});

	serverWebsocket.listen(remotePacketPort);
};