From 178c324bf1cc6c0d2c07dcb875c1c8772d0adfe3 Mon Sep 17 00:00:00 2001 From: 6C656C65 <73671374+6C656C65@users.noreply.github.com> Date: Sun, 8 Jun 2025 22:49:59 +0200 Subject: [PATCH 1/8] fix: client.py wrap recv with try/except --- pyproxy/handlers/client.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pyproxy/handlers/client.py b/pyproxy/handlers/client.py index bb3bac7..da33d62 100644 --- a/pyproxy/handlers/client.py +++ b/pyproxy/handlers/client.py @@ -90,7 +90,13 @@ def handle_client(self, client_socket): Args: client_socket (socket): The socket object for the client connection. """ - request = client_socket.recv(4096) + try: + request = client_socket.recv(4096) + except ConnectionResetError: + self.console_logger.debug("Connection reset by peer during recv, closing socket.") + client_socket.close() + self.active_connections.pop(threading.get_ident(), None) + return if not request: self.console_logger.debug("No request received, closing connection.") From 288b23718122439449340ed465c0e03fde85e0da Mon Sep 17 00:00:00 2001 From: 6C656C65 <73671374+6C656C65@users.noreply.github.com> Date: Sun, 8 Jun 2025 23:16:05 +0200 Subject: [PATCH 2/8] fix: self.active_connections update dict --- pyproxy/handlers/client.py | 42 +++++++++++++++++--------------------- pyproxy/handlers/http.py | 4 ++-- pyproxy/handlers/https.py | 4 ++-- pyproxy/server.py | 1 + 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/pyproxy/handlers/client.py b/pyproxy/handlers/client.py index da33d62..4f621e8 100644 --- a/pyproxy/handlers/client.py +++ b/pyproxy/handlers/client.py @@ -91,29 +91,25 @@ def handle_client(self, client_socket): client_socket (socket): The socket object for the client connection. """ try: + client_socket.settimeout(10) request = client_socket.recv(4096) - except ConnectionResetError: - self.console_logger.debug("Connection reset by peer during recv, closing socket.") - client_socket.close() - self.active_connections.pop(threading.get_ident(), None) - return - - if not request: - self.console_logger.debug("No request received, closing connection.") - client_socket.close() - self.active_connections.pop(threading.get_ident(), None) - return - first_line = request.decode(errors="ignore").split("\n")[0] + if not request: + return - if first_line.startswith("CONNECT"): - https_handler = self._create_handler( - HttpsHandler, - ssl_config=self.ssl_config, - cancel_inspect_queue=self.cancel_inspect_queue, - cancel_inspect_result_queue=self.cancel_inspect_result_queue, - ) - https_handler.handle_https_connection(client_socket, first_line) - else: - http_handler = self._create_handler(HttpHandler) - http_handler.handle_http_request(client_socket, request) + first_line = request.decode(errors="ignore").split("\n")[0] + print("debug server.py :", threading.get_ident()) + if first_line.startswith("CONNECT"): + https_handler = self._create_handler( + HttpsHandler, + ssl_config=self.ssl_config, + cancel_inspect_queue=self.cancel_inspect_queue, + cancel_inspect_result_queue=self.cancel_inspect_result_queue, + ) + https_handler.handle_https_connection(client_socket, first_line) + else: + http_handler = self._create_handler(HttpHandler) + http_handler.handle_http_request(client_socket, request) + finally: + client_socket.close() + self.active_connections.pop(threading.get_ident(), None) \ No newline at end of file diff --git a/pyproxy/handlers/http.py b/pyproxy/handlers/http.py index 9451f43..33bef10 100644 --- a/pyproxy/handlers/http.py +++ b/pyproxy/handlers/http.py @@ -205,12 +205,12 @@ def forward_request_to_server(self, client_socket, request, url, first_line): thread_id = threading.get_ident() if thread_id in self.active_connections: - self.active_connections[thread_id] = { + self.active_connections[thread_id].update({ "target_ip": server_host, "target_port": server_port, "bytes_sent": 0, "bytes_received": 0, - } + }) try: server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) diff --git a/pyproxy/handlers/https.py b/pyproxy/handlers/https.py index e2b3deb..aaffd19 100644 --- a/pyproxy/handlers/https.py +++ b/pyproxy/handlers/https.py @@ -231,10 +231,10 @@ def handle_https_connection(self, client_socket, first_line): not_inspect = self._should_skip_inspection(server_host) thread_id = threading.get_ident() - self.active_connections[thread_id] = { + self.active_connections[thread_id].update({ "bytes_sent": 0, "bytes_received": 0, - } + }) if self.ssl_config.ssl_inspect and not not_inspect: try: diff --git a/pyproxy/server.py b/pyproxy/server.py index d3360e7..a10dcc7 100644 --- a/pyproxy/server.py +++ b/pyproxy/server.py @@ -362,5 +362,6 @@ def start(self): "bytes_received": 0, "thread_name": client_handler.name, } + print("debug server.py :", client_handler.ident) except KeyboardInterrupt: self.console_logger.info("Proxy interrupted, shutting down.") From be1c1da5b164020448f74bcebdab2681b4fe838f Mon Sep 17 00:00:00 2001 From: 6C656C65 <73671374+6C656C65@users.noreply.github.com> Date: Sun, 8 Jun 2025 23:29:40 +0200 Subject: [PATCH 3/8] remove print & monitoring: add target_ip & target_domain --- pyproxy/handlers/client.py | 1 - pyproxy/handlers/http.py | 9 ++++++++- pyproxy/handlers/https.py | 1 + pyproxy/monitoring/templates/index.html | 2 +- pyproxy/server.py | 1 - 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pyproxy/handlers/client.py b/pyproxy/handlers/client.py index 4f621e8..9ea816d 100644 --- a/pyproxy/handlers/client.py +++ b/pyproxy/handlers/client.py @@ -98,7 +98,6 @@ def handle_client(self, client_socket): return first_line = request.decode(errors="ignore").split("\n")[0] - print("debug server.py :", threading.get_ident()) if first_line.startswith("CONNECT"): https_handler = self._create_handler( HttpsHandler, diff --git a/pyproxy/handlers/http.py b/pyproxy/handlers/http.py index 33bef10..a6cb97b 100644 --- a/pyproxy/handlers/http.py +++ b/pyproxy/handlers/http.py @@ -202,11 +202,18 @@ def forward_request_to_server(self, client_socket, request, url, first_line): server_port = parsed_url.port or ( 443 if parsed_url.scheme == "https" else 80 ) + + try: + ip_address = socket.gethostbyname(server_host) + except socket.gaierror: + ip_address = server_host + thread_id = threading.get_ident() if thread_id in self.active_connections: self.active_connections[thread_id].update({ - "target_ip": server_host, + "target_ip": ip_address, + "target_domain": server_host, "target_port": server_port, "bytes_sent": 0, "bytes_received": 0, diff --git a/pyproxy/handlers/https.py b/pyproxy/handlers/https.py index aaffd19..eaafdca 100644 --- a/pyproxy/handlers/https.py +++ b/pyproxy/handlers/https.py @@ -232,6 +232,7 @@ def handle_https_connection(self, client_socket, first_line): thread_id = threading.get_ident() self.active_connections[thread_id].update({ + "target_domain": server_host, "bytes_sent": 0, "bytes_received": 0, }) diff --git a/pyproxy/monitoring/templates/index.html b/pyproxy/monitoring/templates/index.html index 7cc65ae..bbe9175 100644 --- a/pyproxy/monitoring/templates/index.html +++ b/pyproxy/monitoring/templates/index.html @@ -58,7 +58,7 @@
Client: ${conn.client_ip}:${conn.client_port}
-Target: ${conn.target_ip}:${conn.target_port}
+Target: ${conn.target_domain} (${conn.target_ip}:${conn.target_port})
Sent: ${conn.bytes_sent} bytes
Received: ${formatBytes(conn.bytes_received)}
Name: ${data.name}
+PID: ${data.pid}
+Status: ${data.status}
+Start Time: ${data.start_time}
+ `; + + document.getElementById('subprocesses-section').innerHTML = ` +PID: ${proc.pid}
+Status: ${proc.status}
+No active connections.
' + : data.active_connections.map(conn => ` +Client: ${conn.client_ip}:${conn.client_port}
+Target: ${conn.target_domain} (${conn.target_ip}:${conn.target_port})
+Sent: ${conn.bytes_sent} bytes
+Received: ${formatBytes(conn.bytes_received)}
+Port: ${config.port ? `${config.port}` : '✗'}
+Flask Port: ${config.flask_port ? `${config.flask_port}` : '✗'}
+HTML 403: ${config.html_403 ? `${config.html_403}` : '✗'}
+Blocked Sites File: ${config.filter_config.blocked_sites ? `${config.filter_config.blocked_sites}` : '✗'}
+Blocked URL File: ${config.filter_config.blocked_url ? `${config.filter_config.blocked_url}` : '✗'}
+Filter Mode: ${config.filter_config.filter_mode}
+Access Log: ${config.logger_config.no_logging_access ? '✗' : '✓'} ${config.logger_config.access_log ? `${config.logger_config.access_log}` : '✗'}
+Block Log: ${config.logger_config.no_logging_block ? '✗' : '✓'} ${config.logger_config.block_log ? `${config.logger_config.block_log}` : '✗'}
+Inspect CA Cert: ${config.ssl_config.inspect_ca_cert ? `${config.ssl_config.inspect_ca_cert}` : '✗'}
+Inspect CA Key: ${config.ssl_config.inspect_ca_key ? `${config.ssl_config.inspect_ca_key}` : '✗'}
+Inspect certs folder: ${config.ssl_config.inspect_certs_folder ? `${config.ssl_config.inspect_certs_folder}` : '✗'}
+Cancel inspect: ${config.ssl_config.cancel_inspect ? `${config.ssl_config.cancel_inspect}` : '✗'}
+ `; + } catch (err) { + console.error('Error loading config data:', err); + } +} + +function formatBytes(bytes) { + if (bytes === 0) return '0 B'; + const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; + const i = Math.floor(Math.log(bytes) / Math.log(1024)); + const value = bytes / Math.pow(1024, i); + return value.toFixed(2) + ' ' + sizes[i]; +} + +// Initial fetches and intervals +fetchMonitoringData(); +fetchConfigData(); +setInterval(fetchMonitoringData, 5000); +setInterval(fetchConfigData, 5000); diff --git a/pyproxy/monitoring/templates/index.html b/pyproxy/monitoring/templates/index.html index bbe9175..12d6b1b 100644 --- a/pyproxy/monitoring/templates/index.html +++ b/pyproxy/monitoring/templates/index.html @@ -24,91 +24,6 @@