Fix debug traffic for websockets proxy

This commit is contained in:
coletdjnz 2024-05-19 10:17:28 +12:00
parent 6dcc99ee4a
commit 976cec10c9
No known key found for this signature in database
GPG Key ID: 91984263BB39894A
2 changed files with 23 additions and 1 deletions

View File

@ -307,6 +307,7 @@ def create_http_connect_connection(
source_address=None,
username=None,
password=None,
debug=False,
):
proxy_headers = dict()
@ -316,6 +317,8 @@ def create_http_connect_connection(
f'{username or ""}:{password or ""}'.encode('utf-8')).decode('utf-8')
conn = HTTPConnection(proxy_host, port=proxy_port, timeout=timeout)
conn.set_debuglevel(int(debug))
conn.response_class = NoCloseHTTPResponse
if hasattr(conn, '_create_connection'):

View File

@ -4,7 +4,6 @@
import io
import logging
import ssl
import sys
import urllib.parse
from ._helper import (
@ -70,6 +69,25 @@
websockets.sync.connection.Connection.recv_events_exc = None
class WebsocketsLoggingHandler(logging.Handler):
"""Redirect websocket logs to our logger"""
def __init__(self, logger, *args, **kwargs):
super().__init__(*args, **kwargs)
self._logger = logger
def emit(self, record):
try:
msg = self.format(record)
if record.levelno >= logging.ERROR:
self._logger.error(msg)
else:
self._logger.stdout(msg)
except Exception:
self.handleError(record)
class WebsocketsResponseAdapter(WebSocketResponse):
def __init__(self, ws: websockets.sync.client.ClientConnection, url):
@ -181,6 +199,7 @@ def _make_sock(self, proxy, url, timeout):
source_address=self.source_address,
username=parsed_proxy_url.username,
password=parsed_proxy_url.password,
debug=self.verbose,
)
return create_connection(
address=(parsed_url.host, parsed_url.port),