Asynchronous HTTPS proxy server with SSL inspection capabilities
| 13 | |
| 14 | |
| 15 | class ProxyServer: |
| 16 | """ |
| 17 | Asynchronous HTTPS proxy server with SSL inspection capabilities |
| 18 | """ |
| 19 | |
| 20 | def __init__( |
| 21 | self, |
| 22 | host: str = "0.0.0.0", |
| 23 | port: int = 3120, |
| 24 | intercept_domains: Optional[List[str]] = None, |
| 25 | upstream_proxy: Optional[str] = None, |
| 26 | queue: Optional[Any] = None, |
| 27 | ): |
| 28 | self.host = host |
| 29 | self.port = port |
| 30 | self.intercept_domains = intercept_domains or [] |
| 31 | self.passthrough_domains = [ |
| 32 | "feedback-pa.clients6.google.com", |
| 33 | "play.google.com", |
| 34 | "apis.google.com", |
| 35 | "accounts.google.com", |
| 36 | ] |
| 37 | self.upstream_proxy = upstream_proxy |
| 38 | self.queue = queue |
| 39 | |
| 40 | # Initialize components |
| 41 | self.cert_manager = CertificateManager() |
| 42 | self.proxy_connector = ProxyConnector(upstream_proxy) |
| 43 | |
| 44 | # Create logs directory |
| 45 | log_dir = Path("logs") |
| 46 | log_dir.mkdir(exist_ok=True) |
| 47 | self.interceptor = HttpInterceptor(str(log_dir)) |
| 48 | |
| 49 | # Set up logging |
| 50 | self.logger = logging.getLogger("proxy_server") |
| 51 | |
| 52 | # Keep track of background tasks |
| 53 | self.background_tasks = set() |
| 54 | |
| 55 | def _safe_close(self, writer): |
| 56 | """ |
| 57 | Safely close a writer with robust error handling for SSL shutdown timeouts |
| 58 | """ |
| 59 | if not writer: |
| 60 | return |
| 61 | |
| 62 | try: |
| 63 | sock = writer.get_extra_info("socket") |
| 64 | if sock: |
| 65 | try: |
| 66 | sock.shutdown(socket.SHUT_RDWR) |
| 67 | except (OSError, ssl.SSLError): |
| 68 | pass |
| 69 | except Exception: |
| 70 | pass |
| 71 | finally: |
| 72 | try: |
no outgoing calls