Reputation: 2585
I'm trying to set up an SSH tunnel to a local Node.js instance running in debugging mode, which listens on port 9229. The goal is to forward traffic from a remote machine to the debugging port on my local machine, as per the Node.js remote debugging documentation.
Here's how I set up the debugger, running node debugger.js
(Node v18.x):
// debugger.js
import inspector from 'inspector';
inspector.open();
inspector.waitForDebugger();
inspector.close();
Here’s the WebSocket client:
// websocket-client.js
import WebSocket from 'ws';
const URL = 'ws://127.0.0.1:9229/6925164a-c269-4d0c-98d6-570e05d1a821';
const ws = new WebSocket(URL);
ws.on('error', console.error);
ws.on('open', () => { console.log("I'm connected!"); });
This client works fine with local urls (127.0.0.1:9229
). However, when I attempt to connect to the Node.js debugger from a public URL tunneling to localhost
, it stops working and I receive the following error:
WebSocket error: Error: Unexpected server response: 400
at ClientRequest.<anonymous> (C:\Users\***\js_\node_modules\ws\lib\websocket.js:913:7)
at ClientRequest.emit (node:events:513:28)
at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:695:27)
at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17)
at TLSSocket.socketOnData (node:_http_client:536:22)
at TLSSocket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at TLSWrap.onStreamRead (node:internal/stream_base_commons:19)
Here’s the SSH reverse tunnel command I used to connect the debugging port (via localhost.run
):
ssh -R 80:localhost:9229 [email protected]
And if I paste the output URL (with the rest of the debugger path) into websocket-client.js
, I'm not able to connect anymore neither using ws://
nor wss://
.
const URL = 'ws://xxxxxxx.lhr.life/6925164a-c269-4d0c-98d6-570e05d1a821';
What’s odd is that I was able to establish a WebSocket connection to another local WebSocket server if I don't specify a path. Any ideas on how to establish a remote connection to a Node.js debugger?
Summary: I tried setting up a reverse SSH tunnel to my computer and expose it to the Internet. I was expecting to forward Node.js WebSockets requests to my machine through the tunnel properly.
Upvotes: 0
Views: 53