anyproxy
anyproxy copied to clipboard
feat: support beforeSendRequest for WebSocket
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);
});
});