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

未关闭
#1,624 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
20/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
停滞

调研方向

先阅读 issue 中所示的 server.on('upgrade')、proxy.ws() 以及 proxyReqWs 事件周围的 websocket 流程,然后将其与 REST-proxyReq 流程进行比较。完成标准是:在对连接进行代理并将其路由到所选实例之前,有一条清晰且有文档记录的路径可以访问 websocket 身份验证数据。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
JavaScript
星标
14.1k
派生
2k
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

http-party/node-http-proxy 的其他 Issue

查看 http-party/node-http-proxy 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。