anyproxy icon indicating copy to clipboard operation
anyproxy copied to clipboard

feat: support beforeSendRequest for WebSocket

Open roland-reed opened this issue 4 years ago • 1 comments

Add support for forwarding WebSocket connections in beforeSendRequest, but modifying WebSocket message is NOT supported.

e.g.

// rule.js
module.exports = {
  async beforeSendRequest(requestDetail) {
    const {
      protocol,
      requestOptions: { headers },
    } = requestDetail;

    // This example will forward every WebSocket to 127.0.0.1:8800
    // Replace with your own conditions
    if (protocol === 'ws' || protocol === 'wss') {
      return {
        protocol: 'ws',
        requestOptions: {
          hostname: '127.0.0.1',
          port: '8800',
          path: '/',
          headers,
        },
      };
    }

    return requestDetail;
  },
  async beforeDealHttpsRequest(requestDetail) {
    return true;
  },
};
// ws.js
const WebSocket = require('ws');

const server = new WebSocket.Server({
  port: 8800,
});

server.on('connection', ws => {
  ws.on('message', msg => {
    ws.send(msg);
  });
});

roland-reed avatar Aug 17 '20 12:08 roland-reed

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Aug 17 '20 12:08 CLAassistant