Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support hooks on websocket #486

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

guox191
Copy link
Contributor

@guox191 guox191 commented Mar 28, 2019

add two rules:

  • beforeSendWsMessageToClient
  • beforeSendWsMessageToServer

@CLAassistant
Copy link

CLAassistant commented Apr 8, 2019

CLA assistant check
All committers have signed the CLA.

Copy link

@azu azu left a comment

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

* get request info from the ws client
* @param @required wsClient the ws client of WebSocket
*/
function getWsReqInfo(wsReq) {
Copy link

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

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

@hreflee
Copy link

hreflee commented Jun 22, 2020

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

@wujohns
Copy link

wujohns commented Aug 31, 2022

感觉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.

None yet

6 participants