Skip to content

feat: support hooks on websocket#486

Open
guox191 wants to merge 2 commits into
alibaba:masterfrom
guox191:master
Open

feat: support hooks on websocket#486
guox191 wants to merge 2 commits into
alibaba:masterfrom
guox191:master

Conversation

@guox191

@guox191 guox191 commented Mar 28, 2019

Copy link
Copy Markdown
Contributor

add two rules:

  • beforeSendWsMessageToClient
  • beforeSendWsMessageToServer

@CLAassistant

CLAassistant commented Apr 8, 2019

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@azu azu left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this pull request ❤️
I have tested this patch on https://www.websocket.org/echo.html with following script.
It work!

module.exports = {
    summary: 'WebSocket Rule',
    * beforeDealHttpsRequest(requestDetail) {
        return requestDetail.host === "echo.websocket.org:443";
    },
    * beforeSendWsMessageToClient(requestDetail) {
        return {
          data: 'replaced by beforeSendWsMessageToClient'
        }
    }
};

Comment thread lib/handlers/wsHandler.js
* get request info from the ws client
* @param @required wsClient the ws client of WebSocket
*/
function getWsReqInfo(wsReq) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that some weboscet url has not :{port}.
So, you need to normalize port before building url.

/**
 * get request info from the ws client
 * @param @required wsClient the ws client of WebSocket
 */
function getWsReqInfo(wsReq) {
  const headers = wsReq.headers || {};
  const host = headers.host;
  const hostname = host.split(':')[0];
  const port = host.split(':')[1];
  // https://github.com/alibaba/anyproxy/pull/450
  const portPart = port ? `:${port}` : '';
  // TODO 如果是windows机器,url是不是全路径?需要对其过滤,取出
  const path = wsReq.url || '/';
  const isEncript = wsReq.connection && wsReq.connection.encrypted;

  return {
    url: `${isEncript ? 'wss' : 'ws'}://${hostname}${portPart}${path}`,
    headers: headers, // the full headers of origin ws connection
    noWsHeaders: getNoWsHeaders(headers),
    secure: Boolean(isEncript),
    hostname: hostname,
    port: port,
    path: path
  };
}

ref #450

This changes is patched and this ws proxy work on https://www.websocket.org/echo.html.

@wangxiaoji01

Copy link
Copy Markdown

Exc me , can I ask that how to set the websocket port?

@hreflee

hreflee commented Jun 22, 2020

Copy link
Copy Markdown

Will this PR be further processed? I am also looking forward to websocket hook functions! 🙏

@wujohns

wujohns commented Aug 31, 2022

Copy link
Copy Markdown

感觉anyproxy官方已经放弃维护了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants