node-http-mitm-proxy icon indicating copy to clipboard operation
node-http-mitm-proxy copied to clipboard

Illisible data in websocket

Open AxelROGET opened this issue 2 years ago • 0 comments

I'm attempting to intercept data from WebSocket, but the data appears to be unreadable (it's a buffer). When I attempt to use toString(), I receive something like ���mlL�� and the WebSocket just doesn't work when I use socket.setEncoding().

I've tried to retrieve the socket from proxyReq, socket, server.on("upgrade") but the result remains the same (even is the Buffer are differents).

const express = require("express");
const { createProxyMiddleware } = require("http-proxy-middleware");


const app = express();

// Configuration :
const PORT = 3000;
const HOST = "localhost";
const API_SERVICE_URL = "http://192.168.1.197:8123";

const proxy = createProxyMiddleware({
    target: API_SERVICE_URL,
    changeOrigin: true,
    pathRewrite: {
        [`^/`]: '',
    },
    ws: true,
    logLevel: 'debug',
    onProxyReqWs: (proxyReq, req, socket) => {
        proxyReq.on('upgrade', (req, socket, head) => {
            proxy.upgrade(req, socket, head);

            // socket.setEncoding('utf-8')

            socket.on('data', data => {
                console.log(data);
                const buff = Buffer.from(data);
                console.log(buff.toString())
            })
        })
    }

})

// Proxy endpoints
app.use('/', proxy);

// Start the Proxy
const server = app.listen(PORT, HOST, () => {
    console.log(`Starting Proxy at ${HOST}:${PORT}`);
});

Do you have any advice on what I should do? Thanks!

AxelROGET avatar Nov 16 '23 16:11 AxelROGET