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

Is websocket proxying working?

Open bushev opened this issue 3 years ago • 6 comments
trafficstars

I'm trying to run a basic example but getting an error:

TypeError: socket.setNoDelay is not a function
     at Object.common.setupSocket (/home/ubuntu/http-proxy/build/webpack:/proxy-app/node_modules/http-proxy/lib/http-proxy/common.js:127:1)

The code:

const proxy = httpProxy.createProxyServer({
    ws: true
});

proxy.on('upgrade', (req, socket, head) => {

    const { query } = Url.parse(req.url, true);
    const redirectParams = { target: { host: query.csHost as string, port: parseInt(query.csPort as string, 10) } };
    console.log('[WS] Redirecting (upgrade)...', { url: req.url, redirectParams });
    proxy.ws(req, socket, head, redirectParams);
});


const server = http.createServer(async (req, res) => {

      const { query } = Url.parse(req.url, true);
      const redirectParams = { target: { host: query.csHost as string, port: parseInt(query.csPort as string, 10) } };
      console.log('[HTTP] Redirecting...', { url: req.url, redirectParams });
      proxy.ws(req, res, redirectParams);
}

server.listen(80);

bushev avatar Feb 05 '22 22:02 bushev

up

lucasvieceli avatar Jun 08 '22 11:06 lucasvieceli

It does not seem to work for me either!

aeciolevy avatar Sep 06 '22 18:09 aeciolevy

I'm trying to run a basic example but getting an error:

TypeError: socket.setNoDelay is not a function
     at Object.common.setupSocket (/home/ubuntu/http-proxy/build/webpack:/proxy-app/node_modules/http-proxy/lib/http-proxy/common.js:127:1)

The code:

const proxy = httpProxy.createProxyServer({
    ws: true
});

proxy.on('upgrade', (req, socket, head) => {

    const { query } = Url.parse(req.url, true);
    const redirectParams = { target: { host: query.csHost as string, port: parseInt(query.csPort as string, 10) } };
    console.log('[WS] Redirecting (upgrade)...', { url: req.url, redirectParams });
    proxy.ws(req, socket, head, redirectParams);
});


const server = http.createServer(async (req, res) => {

      const { query } = Url.parse(req.url, true);
      const redirectParams = { target: { host: query.csHost as string, port: parseInt(query.csPort as string, 10) } };
      console.log('[HTTP] Redirecting...', { url: req.url, redirectParams });
      proxy.ws(req, res, redirectParams);
}

server.listen(80);

@bushev but I think you need to do server.on('upgrade', ...

aeciolevy avatar Sep 06 '22 19:09 aeciolevy

Add an error handler to the socket seems to help.

proxy.on('upgrade', (req, socket, head) => {

  socket.on('error', (error) => {
           console.error(error);
       });
       
    const { query } = Url.parse(req.url, true);
    const redirectParams = { target: { host: query.csHost as string, port: parseInt(query.csPort as string, 10) } };
    console.log('[WS] Redirecting (upgrade)...', { url: req.url, redirectParams });
    proxy.ws(req, socket, head, redirectParams)
});

yuchuan1 avatar Sep 16 '22 09:09 yuchuan1

When I use websocket config ,I meet the following error.

Invalid frame header

image

const proxyServer = httpProxy.createProxyServer({
  secure: false, ws: true, toProxy: true, logLevel: 'debug', changeOrigin: false, hostRewrite: true
});

server.on('upgrade', function (req, socket, head) {
  if (req.baseUrl === '/tty') {
    const proxyKey = req.originalUrl.split('/')[2];
    console.log('req.url', req.url);
    socket.on('error', err => {
      console.error(err); // ECONNRESET will be caught here
    });
    socket.on('data', data => {
    });
    codeServerProxy.ws(req, socket, head, {
      target: httpAgent.url
    });
  }
});

alanhe421 avatar Dec 08 '22 18:12 alanhe421