Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server crashed with thousands of [HPM] Upgrading to WebSocket attempts #733

Closed
2 tasks done
hossein-zare opened this issue Mar 21, 2022 · 7 comments
Closed
2 tasks done

Comments

@hossein-zare
Copy link

hossein-zare commented Mar 21, 2022

Checks

Describe the bug (be clear and concise)

I want to proxy socket.mydomain.com:4000 to a websocket server running at mydomain.com:4001.

// client
const socket = io('ws://socket.mydomain.com:4000');

it's working fine but If i change socket.domain.com to app.domain.com the server tries to upgrade to websocket then craches.

// client
const socket = io('ws://app.mydomain.com:4000');

Log

[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
[HPM] Upgrading to WebSocket
...

[HPM] Error occurred while proxying request app.mydomain.com:4000/socket.io/?EIO=4&transport=websocket to undefined [HPE_INVALID_CONSTANT] (https://nodejs.org/api/errors.html#errors_common_system_errors)

Error: read ECONNRESET
    at TCP.onStreamRead (node:internal/stream_base_commons:220:20) {
  errno: -4077,
  code: 'ECONNRESET',
  syscall: 'read'
}

Step-by-step reproduction instructions

1. ...
2. ...

Expected behavior (be clear and concise)

The server must not crash if the client tries to connect to another subdomain.

How is http-proxy-middleware used in your project?

-- http-proxy-middleware@2.0.4


const conf = {
  target: `http://mydomain.com:4000`,
  changeOrigin: true,
  ws: true,
  router: {
    'socket.mydomain.com:4000': `http://mydomain.com:4001`,
  }
};

const wsProxy = createProxyMiddleware(conf);

app.use(wsProxy);

server.on('upgrade', wsProxy.upgrade);

What http-proxy-middleware configuration are you using?

const conf = {
  target: `http://mydomain.com:4000`,
  changeOrigin: true,
  ws: true,
  router: {
    'socket.mydomain.com:4000': `http://mydomain.com:4001`,
  }
}

What OS/version and node/version are you seeing the problem?

Windows 10
Node: 16.13.0

Additional context (optional)

No response

@Vladyslav-Murashchenko
Copy link

@hossein-zare I am getting the same issue. Do you know how to fix this?

@hossein-zare
Copy link
Author

hossein-zare commented Apr 20, 2022

@VladislavMurashchenko

I used http-proxy:

const require('http-proxy');

const app = express();
const server = http.createServer(app);

const proxy = httpProxy.createProxyServer({
    target: {
        host: 'example.com',
        port: 4001
    }
});

server.on('upgrade', (req, socket, head) => {
    const domain = req.headers.host;
    const host = domain.split(':')[0];

    if (host === 'socket.example.com') {
        proxy.ws(req, socket, head);
    } else {
        socket.destroy();
    }
});

@chimurai
Copy link
Owner

chimurai commented Apr 21, 2022

Can you try with v2.0.6?

It contains a fix for server crashes from websocket errors

@chimurai
Copy link
Owner

Think the filter option is what you were looking for:

// only proxy on this condition:
const filter = (path, req) => {
    const domain = req.headers.host;
    const host = domain.split(':')[0];
    return host === 'socket.example.com'
}

const conf = {
  target: `http://mydomain.com:4000`,
  changeOrigin: true,
  ws: true,
  router: {
    'socket.mydomain.com:4000': `http://mydomain.com:4001`,
  }
};

const wsProxy = createProxyMiddleware(filter, conf);

@whatwewant
Copy link

still broken

@whatwewant
Copy link

how to: just

this.proxy.ws(req, socket, head, activeProxyOptions, this.logError); 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants