2

I created a proxy server (node-http-proxy) to redirect websocket connections to another websocket server like:

websocket client <-----> proxy <-----> websocket target server

In order to control the messages that sent from client to the sever, I would like to know what messages are sent to the server and do some filtering. I found the following codes helping me to get the sent messages but I can't filter the unwanted messages (such as 'hello'). Is there other method (or package) I can use to add some logic before the messages are sent to target server ?

proxy.on('proxyReqWs', function(proxyReq, req, socket, res, options) {
    var parser = new WsParser(0, false);
    socket.pipe(parser);
    parser.on('frame', function (frame) {
        // handle the frame
        console.log('Up:',frame);
        console.log('Up data:'+frame.data);
    });
});
Conrad
  • 53
  • 2
  • Why you can't filter messages at finish WS server, onMessage event? – Oleksiy Sep 20 '17 at 15:06
  • It is not our server thus I have no control on it. It provides us only a set of credentials for all the resources, and I would like to do some resources control on my side. – Conrad Sep 20 '17 at 15:47

1 Answers1

0

Best but not nice solution I found is to read messages like this https://github.com/http-party/node-http-proxy/issues/1632

and then check the method name to call .end() like:

proxy.on('proxyReqWs', (proxyReq, _req, clientSocket, _res, _options) => {

clientSocket.on('error', (error) => {
    console.error(`Client socket error:`, error)
})

clientSocket.on('close', (hadError) => {
    console.log(`Client socket closed${hadError ? ' with error' : ''}.`)
})

const perMessageDeflate = new PerMessageDeflate({ serverMaxWindowBits: 10, clientMaxWindowBits: 10 }, false)
perMessageDeflate.accept([{}])
const receiver = new Receiver({ isServer: true, extensions: { 'permessage-deflate': perMessageDeflate } })
clientSocket.pipe(receiver)

receiver.on('message', (message) => {
    console.log(`Client wrote >>>> ${message.toString()}`)
    let parsedMsg = JSON.parse(message);
    let blockMethodNames = ['method_name_to_block'];
    console.log(`Client called >>>> ${parsedMsg.method}`)
    if (blockMethodNames.indexOf(parsedMsg.method) > -1) {
        clientSocket.end();
    }
})
})
Matjaz Hirsman
  • 326
  • 3
  • 9