Skip to content

Instantly share code, notes, and snippets.

@lortonx
Created May 19, 2023 19:27
Show Gist options
  • Save lortonx/acd2d489346010578b7155120c251def to your computer and use it in GitHub Desktop.
Save lortonx/acd2d489346010578b7155120c251def to your computer and use it in GitHub Desktop.
Reverse proxy for nodejs debugger, for uWebSockets
import {default as uWS} from 'uWebSockets.js'
import WebSocket from 'ws';
import inspector from 'inspector';
const listeningPort = 3000
const secretToken = '83bc34ab-ef29-443e-92c8-56295d928449'
// connection link
// devtools://devtools/bundled/js_app.html?experiments=true&v8only=true&ws=yourhost.com/yourSecretToken
function injectDebugger() {
if(!process.debugPort) return
console.warn('Debugger is exposed')
/**
* @typedef {uWS.WebSocket & {connection: WebSocket}} uws_debugger_context
*/
const nope = () => false
const decoder = new TextDecoder()
listenerSocket.ws('/' + secretToken,{
idleTimeout: 36000,
maxBackpressure: 51200,
maxPayloadLength: 51200,
compression: uWS.DEDICATED_COMPRESSOR_4KB,
upgrade: async (res, req, context) => {
const connection = new WebSocket(inspector.url())
const upgradeData = [
{connection},
req.getHeader('sec-websocket-key'),
req.getHeader('sec-websocket-protocol'),
req.getHeader('sec-websocket-extensions'),
context
]
connection.onopen = () => {
res.upgrade.apply(res, upgradeData);
}
connection.onerror = (error) => {}
res.onAborted(() => {
connection.close()
});
},
/**
* @param {uws_debugger_context} uws
*/
open: (uws) => {
uws.connection.onmessage = (event) => {
// @ts-ignore
uws.send(event.data, false, true)
}
uws.connection.onclose = () => {
if(uws.send !== nope) uws.close()
}
},
/**
* @param {uws_debugger_context} uws
* @param {ArrayBuffer} message
* */
message: (uws, message, isBinary) => {
uws.connection.send(decoder.decode(message))
},
/** @param {uws_debugger_context} uws */
close: (uws, code, message) => {
uws.connection.close(1001)
uws.send = nope
}
})
}
const listenerSocket = uWS./*SSL*/App({})
injectDebugger()
listenerSocket.listen("0.0.0.0", listeningPort, (listenSocket) => {
console.log(`WS port opened: ${listeningPort}`)
resolve()
})
@lortonx
Copy link
Author

lortonx commented May 19, 2023

How to connect to a debugger for a containerized nodejs application that uses uWebSockets.js ?
How to inspect applications in a Docker container?
How to inspect nodejs heroku or coolify.io apps?

This little snippet will answer your request.
This snippet is not recommended for use in production, the author of the script is not responsible for possible risks.
See your hosting provider for debugging instructions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment