Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • N node-http-proxy
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 482
    • Issues 482
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 102
    • Merge requests 102
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • http ... PARTY!
  • node-http-proxy
  • Issues
  • #1624
Closed
Open
Issue created Jan 25, 2023 by Areej Fatima@Areej-Fatima

Intercept websocket request before proxying it to server via http-proxy

Here is my code for proxying the websocket request to the targer server. What I want to achieve is that like in case of REST requests in http-proxy, we use proxyReq event to modify the request if we want by accessing headers and req.body, I want to do the same for weboscket request as I want to access the authentication data auth that is passed from the socket client. I don't want to log or intercept the traffic sent or received after the weboscket connection, instead I want to intercept (not modify though) the actual request that contains data to authenticate the client on proxyReqws event or on upgrade event of server. Any help & guidance would be appreciated.

var app = express();
const fs = require('fs')
var https = require('https')
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)

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' } });
});

On the basis of information I get from auth, the request will be routed to the specific instance.

Assignee
Assign to
Time tracking