From d6dda73fb983b5f8aae30992c7c64925eddb0756 Mon Sep 17 00:00:00 2001 From: Sander Declerck Date: Fri, 24 Oct 2025 16:21:14 +0200 Subject: [PATCH] WIP --- .../src/environment/userInteraction.js | 5 +++++ .../src/registryProxy/mitmRequestHandler.js | 18 ++++++++++++++++-- .../src/registryProxy/registryProxy.js | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/safe-chain/src/environment/userInteraction.js b/packages/safe-chain/src/environment/userInteraction.js index 829afa1..1b4eae8 100644 --- a/packages/safe-chain/src/environment/userInteraction.js +++ b/packages/safe-chain/src/environment/userInteraction.js @@ -26,6 +26,10 @@ function writeError(message, ...optionalParams) { console.error(message, ...optionalParams); } +function writeVerboseInformation(message, ...optionalParams) { + writeInformation(message, ...optionalParams); +} + function startProcess(message) { if (isCi()) { return { @@ -89,6 +93,7 @@ async function confirm(config) { export const ui = { writeInformation, + writeVerboseInformation, writeWarning, writeError, emptyLine, diff --git a/packages/safe-chain/src/registryProxy/mitmRequestHandler.js b/packages/safe-chain/src/registryProxy/mitmRequestHandler.js index 63a8168..b0c0af7 100644 --- a/packages/safe-chain/src/registryProxy/mitmRequestHandler.js +++ b/packages/safe-chain/src/registryProxy/mitmRequestHandler.js @@ -1,11 +1,16 @@ import https from "https"; import { generateCertForHost } from "./certUtils.js"; import { HttpsProxyAgent } from "https-proxy-agent"; +import { ui } from "../environment/userInteraction.js"; export function mitmConnect(req, clientSocket, isAllowed) { + ui.writeVerboseInformation(`Safe-chain: Set up MITM tunnel for ${req.url}`); const { hostname } = new URL(`http://${req.url}`); - clientSocket.on("error", () => { + clientSocket.on("error", (err) => { + ui.writeVerboseInformation( + `Safe-chain: Client socket error for ${req.url}: ${err.message}` + ); // 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. @@ -28,6 +33,9 @@ function createHttpsServer(hostname, isAllowed) { const targetUrl = `https://${hostname}${pathAndQuery}`; if (!(await isAllowed(targetUrl))) { + ui.writeVerboseInformation( + `Safe-chain: Blocking request to ${targetUrl}` + ); res.writeHead(403, "Forbidden - blocked by safe-chain"); res.end("Blocked by safe-chain"); return; @@ -57,7 +65,10 @@ function getRequestPathAndQuery(url) { function forwardRequest(req, hostname, res) { const proxyReq = createProxyRequest(hostname, req, res); - proxyReq.on("error", () => { + proxyReq.on("error", (err) => { + ui.writeVerboseInformation( + `Safe-chain: Error occurred while proxying request: ${err.message}` + ); res.writeHead(502); res.end("Bad Gateway"); }); @@ -67,6 +78,9 @@ function forwardRequest(req, hostname, res) { }); req.on("end", () => { + ui.writeVerboseInformation( + `Safe-chain: Finished proxying request to ${req.url} for ${hostname}` + ); proxyReq.end(); }); } diff --git a/packages/safe-chain/src/registryProxy/registryProxy.js b/packages/safe-chain/src/registryProxy/registryProxy.js index b0e8dd1..b5227b4 100644 --- a/packages/safe-chain/src/registryProxy/registryProxy.js +++ b/packages/safe-chain/src/registryProxy/registryProxy.js @@ -109,6 +109,7 @@ function handleConnect(req, clientSocket, head) { mitmConnect(req, clientSocket, isAllowedUrl); } else { // For other hosts, just tunnel the request to the destination tcp socket + ui.writeVerboseInformation(`Safe-chain: Tunneling request to ${req.url}`); tunnelRequest(req, clientSocket, head); } }