gpt4 book ai didi

node.js - 如何通过 Node http-proxy记录websocket流量数据

转载 作者:太空宇宙 更新时间:2023-11-04 03:28:18 27 4
gpt4 key购买 nike

我想通过 http-proxy 代理 websockets,我可以记录接收数据,但我无法从代理获取发送数据。我该怎么办?

#!/usr/bin/env node

var wstarget = 'ws://localhost:8080/?r='+(Date.now() / 1000 | 0);

var httpProxy = require('http-proxy');
var http = require('http');

var proxy = httpProxy.createProxyServer({});

proxy.on('open', function (proxySocket) {
proxySocket.on('data', function (data) {
//console.log('\n'+data);
console.log('',data); // Just log the receive data! Can't get the Send data?
});
});

var server = http.createServer();

server.on('upgrade', function (req, socket, head) {
console.log("Proxying websocket connection to "+wstarget);
proxy.ws(req, socket, head, {
target: wstarget,
changeOrigin: true,
ws: true});
});

server.listen(8881);

最佳答案

我找到了答案!

 ...

var WsParser = require('simples/lib/parsers/ws'); // npm install simples

proxy.on('open', function (proxySocket) {
proxySocket.on('data', function (data) {
console.log('Down:'+data);
console.log('',data);
});

});

proxy.on('proxyReqWs', function(proxyReq, req, socket, options, head) {

var parser = new WsParser(0, false);

socket.pipe(parser);

parser.on('frame', function (frame) {
// handle the frame
console.log('Up:',frame);
console.log('Up data:'+frame.data);
/*
The structure of a frame:
{
data: buffer,
fin: boolean,
length: int,
masked: boolean,
opcode: int
}
*/
});

});

...

关于node.js - 如何通过 Node http-proxy记录websocket流量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41610152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com