node-http-proxy icon indicating copy to clipboard operation
node-http-proxy copied to clipboard

Modify websocket messages before they are sent to the target

Open DanielStarkey opened this issue 3 years ago • 5 comments

Is it possible to be able to modify the content of the Websocket messages before they are forwarded onto the target? TIA 🙂

DanielStarkey avatar Dec 21 '22 08:12 DanielStarkey

hi find your answer? i want modify response of target response before send end user, But I don't know How do I do it?

iamhassanfaraji avatar Jan 15 '23 09:01 iamhassanfaraji

Hi, I want to access websocket request before actually forwarding it via http-proxy. Can we access websocket request via http-proxy before sending it?

areejfatimah7 avatar Jan 23 '23 06:01 areejfatimah7

I ended up using the ws library to be able to intercept and modify requests and responses. Functionality is not provided out of the box, but it is simple to achieve by maintaining 2 websocket connections - one between client and proxy and then another connection between proxy and target.

DanielStarkey avatar Jan 23 '23 07:01 DanielStarkey

I explored ws module, but I don't know why my authorization param is said to be undefined. The client side sends the authorization params to authenticate the client. Here is the code I have written to achieve this.

var app = express();
const fs = require('fs')
var https = require('https')
const WebSocket = require('ws');
var httpProxy = require('http-proxy');
const credentials = { key: fs.readFileSync("localhost.key"), cert: fs.readFileSync("localhost.cert") };

var proxy = httpProxy.createProxyServer({ ws: true });
var server = https.createServer(credentials, app).listen(8080)
const wss = new WebSocket.Server({ server});

wss.on('connection', (ws, upgradeReq) => {
    const authToken = upgradeReq.headers.authorization;
    console.log(authToken);
  });
proxy.on('proxyReqWs', (proxyReq, req, socket, options, head) => {
    //before the request is actually proxyed to the target server
    //access websocket request & modify it
});

app.use('/socket.io', function (req, res) {
    proxy.on('proxyRes', function (proxyRes, req, res) {
     //on response, do something
    });
    proxy.on('error', function (err, req, res) {
        console.log(err)
    })
    proxy.web(req, res, { target: { host: address.host, port: address.port, path: '/socket.io' } })

})

server.on('upgrade', function (req, socket, head) {
    console.log("Upgraded to websockets")
    proxy.ws(req, socket, head, { target: { host: address.host, port: address.port, path: '/socket.io' } });
});

I don't know which address should I put in WebSocket.Server, http-proxy's address or the actual socket server's? because as you said there will be two websocker connections. So, is my approach correct and what address should I put in there (websocket connection between the client and the proxy)?

areejfatimah7 avatar Jan 24 '23 10:01 areejfatimah7

I explored ws module, but I don't know why my authorization param is said to be undefined. The client side sends the authorization params to authenticate the client. Here is the code I have written to achieve this.

var app = express();
const fs = require('fs')
var https = require('https')
const WebSocket = require('ws');
var httpProxy = require('http-proxy');
const credentials = { key: fs.readFileSync("localhost.key"), cert: fs.readFileSync("localhost.cert") };

var proxy = httpProxy.createProxyServer({ ws: true });
var server = https.createServer(credentials, app).listen(8080)
const wss = new WebSocket.Server({ server});

wss.on('connection', (ws, upgradeReq) => {
    const authToken = upgradeReq.headers.authorization;
    console.log(authToken);
  });
proxy.on('proxyReqWs', (proxyReq, req, socket, options, head) => {
    //before the request is actually proxyed to the target server
    //access websocket request & modify it
});

app.use('/socket.io', function (req, res) {
    proxy.on('proxyRes', function (proxyRes, req, res) {
     //on response, do something
    });
    proxy.on('error', function (err, req, res) {
        console.log(err)
    })
    proxy.web(req, res, { target: { host: address.host, port: address.port, path: '/socket.io' } })

})

server.on('upgrade', function (req, socket, head) {
    console.log("Upgraded to websockets")
    proxy.ws(req, socket, head, { target: { host: address.host, port: address.port, path: '/socket.io' } });
});

I don't know which address should I put in WebSocket.Server, http-proxy's address or the actual socket server's? because as you said there will be two websocker connections. So, is my approach correct and what address should I put in there (websocket connection between the client and the proxy)?

I have put together an example of how this can be achieved here: https://gist.github.com/DanielStarkey/5fa2a591ec5c7a1878e64294cdd89bd8

DanielStarkey avatar Jan 24 '23 11:01 DanielStarkey