Subscribe to more error events to prevent the process from crashing

This commit is contained in:
Sander Declerck 2025-10-31 09:39:16 +01:00
parent c284ad7ba9
commit 65c9ca62de
No known key found for this signature in database
3 changed files with 44 additions and 12 deletions

View file

@ -37,13 +37,19 @@ function createHttpsServer(hostname, isAllowed) {
forwardRequest(req, hostname, res); forwardRequest(req, hostname, res);
} }
return https.createServer( const server = https.createServer(
{ {
key: cert.privateKey, key: cert.privateKey,
cert: cert.certificate, cert: cert.certificate,
}, },
handleRequest handleRequest
); );
server.on("error", (err) => {
ui.writeError(`Safe-chain: HTTPS server error: ${err.message}`);
});
return server;
} }
function getRequestPathAndQuery(url) { function getRequestPathAndQuery(url) {
@ -62,6 +68,11 @@ function forwardRequest(req, hostname, res) {
res.end("Bad Gateway"); res.end("Bad Gateway");
}); });
req.on("error", (err) => {
ui.writeError(`Safe-chain: Error reading client request: ${err.message}`);
proxyReq.destroy();
});
req.on("data", (chunk) => { req.on("data", (chunk) => {
proxyReq.write(chunk); proxyReq.write(chunk);
}); });
@ -88,6 +99,16 @@ function createProxyRequest(hostname, req, res) {
} }
const proxyReq = https.request(options, (proxyRes) => { const proxyReq = https.request(options, (proxyRes) => {
proxyRes.on("error", (err) => {
ui.writeError(
`Safe-chain: Error reading upstream response: ${err.message}`
);
if (!res.headersSent) {
res.writeHead(502);
res.end("Bad Gateway");
}
});
res.writeHead(proxyRes.statusCode, proxyRes.headers); res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res); proxyRes.pipe(res);
}); });

View file

@ -43,8 +43,13 @@ export function handleHttpProxyRequest(req, res) {
} }
) )
.on("error", (err) => { .on("error", (err) => {
if (!res.headersSent) {
res.writeHead(502); res.writeHead(502);
res.end(`Bad Gateway: ${err.message}`); res.end(`Bad Gateway: ${err.message}`);
} else {
// Headers already sent, just destroy the response
res.destroy();
}
}); });
req.on("error", () => { req.on("error", () => {

View file

@ -24,12 +24,6 @@ export function tunnelRequest(req, clientSocket, head) {
function tunnelRequestToDestination(req, clientSocket, head) { function tunnelRequestToDestination(req, clientSocket, head) {
const { port, hostname } = new URL(`http://${req.url}`); const { port, hostname } = new URL(`http://${req.url}`);
clientSocket.on("error", () => {
// NO-OP
// This can happen if the client TCP socket sends RST instead of FIN.
// Not subscribing to 'close' event will cause node to throw and crash.
});
const serverSocket = net.connect(port || 443, hostname, () => { const serverSocket = net.connect(port || 443, hostname, () => {
clientSocket.write("HTTP/1.1 200 Connection Established\r\n\r\n"); clientSocket.write("HTTP/1.1 200 Connection Established\r\n\r\n");
serverSocket.write(head); serverSocket.write(head);
@ -37,6 +31,14 @@ function tunnelRequestToDestination(req, clientSocket, head) {
clientSocket.pipe(serverSocket); clientSocket.pipe(serverSocket);
}); });
clientSocket.on("error", () => {
// This can happen if the client TCP socket sends RST instead of FIN.
// Not subscribing to 'close' event will cause node to throw and crash.
if (serverSocket.writable) {
serverSocket.end();
}
});
serverSocket.on("error", (err) => { serverSocket.on("error", (err) => {
ui.writeError( ui.writeError(
`Safe-chain: error connecting to ${hostname}:${port} - ${err.message}` `Safe-chain: error connecting to ${hostname}:${port} - ${err.message}`
@ -100,10 +102,14 @@ function tunnelRequestViaProxy(req, clientSocket, head, proxyUrl) {
proxy.port || 8080 proxy.port || 8080
} - ${err.message}` } - ${err.message}`
); );
} else {
ui.writeError(
`Safe-chain: proxy socket error after connection - ${err.message}`
);
}
if (clientSocket.writable) { if (clientSocket.writable) {
clientSocket.end("HTTP/1.1 502 Bad Gateway\r\n\r\n"); clientSocket.end("HTTP/1.1 502 Bad Gateway\r\n\r\n");
} }
}
}); });
clientSocket.on("error", () => { clientSocket.on("error", () => {