From 585fc260b0b34ff79f2aea1ddcdb7755989e2874 Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 14 Mar 2026 15:31:56 -0400 Subject: [PATCH] feat: add TCP connect checker plugin --- .../plugins/builtin/checkers/tcp_connect.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/proxy_pool/plugins/builtin/checkers/tcp_connect.py diff --git a/src/proxy_pool/plugins/builtin/checkers/tcp_connect.py b/src/proxy_pool/plugins/builtin/checkers/tcp_connect.py new file mode 100644 index 0000000..bf827ed --- /dev/null +++ b/src/proxy_pool/plugins/builtin/checkers/tcp_connect.py @@ -0,0 +1,58 @@ +from __future__ import annotations + +import asyncio + +from proxy_pool.config import Settings +from proxy_pool.plugins.protocols import CheckContext, CheckResult + + +class TcpConnectChecker: + name = "tcp_connect" + stage = 1 + priority = 0 + timeout = 5.0 + + def __init__(self, timeout: float = 5.0) -> None: + self.timeout = timeout + + async def check( + self, + proxy_ip: str, + proxy_port: int, + proxy_protocol: str, + context: CheckContext, + ) -> CheckResult: + try: + reader, writer = await asyncio.wait_for( + asyncio.open_connection(proxy_ip, proxy_port), + timeout=self.timeout, + ) + latency = context.elapsed_ms() + context.tcp_latency_ms = latency + writer.close() + await writer.wait_closed() + + return CheckResult( + passed=True, + detail="TCP connect OK", + latency_ms=latency, + ) + except TimeoutError: + return CheckResult( + passed=False, + detail=f"TCP connect timed out after {self.timeout}s", + ) + except OSError as err: + return CheckResult( + passed=False, + detail=f"TCP connect failed: {err}", + ) + + def should_skip(self, proxy_protocol: str) -> bool: + return False + + +def create_plugin(settings: Settings) -> TcpConnectChecker: + return TcpConnectChecker( + timeout=settings.proxy.check_tcp_timeout, + )