From be438420d59612e2501d8d0f9d6f139002417384 Mon Sep 17 00:00:00 2001 From: agatha Date: Sun, 15 Mar 2026 16:22:42 -0400 Subject: [PATCH] feat: add protocol-prefix parsers for socks5://ip:port format --- .../builtin/parsers/protocol_prefix.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/proxy_pool/plugins/builtin/parsers/protocol_prefix.py diff --git a/src/proxy_pool/plugins/builtin/parsers/protocol_prefix.py b/src/proxy_pool/plugins/builtin/parsers/protocol_prefix.py new file mode 100644 index 0000000..563a293 --- /dev/null +++ b/src/proxy_pool/plugins/builtin/parsers/protocol_prefix.py @@ -0,0 +1,54 @@ +from __future__ import annotations + +import re +from typing import Any + +from proxy_pool.config import Settings +from proxy_pool.plugins.protocols import DiscoveredProxy + +_PROTOCOL_LINE_RE = re.compile( + r"^(https?|socks[45])://(\d{1,3}(?:\.\d{1,3}){3}):(\d{2,5})$" +) + +SUPPORTED_PROTOCOLS = {"http", "https", "socks4", "socks5"} + + +class ProtocolPrefixParser: + name = "protocol_prefix" + + def supports(self, url: str) -> bool: + return False # Must be explicitly configured + + async def parse( + self, + raw: bytes, + source_url: str, + source_id: Any, + default_protocol: str, + ) -> list[DiscoveredProxy]: + results: list[DiscoveredProxy] = [] + text = raw.decode("utf-8", errors="ignore") + + for line in text.splitlines(): + line = line.strip().lower() + match = _PROTOCOL_LINE_RE.match(line) + if match: + protocol = match.group(1) + if protocol in SUPPORTED_PROTOCOLS: + results.append( + DiscoveredProxy( + ip=match.group(2), + port=int(match.group(3)), + protocol=protocol, + source_id=source_id, + ) + ) + + return results + + def default_schedule(self) -> str | None: + return "*/30 * * * *" + + +def create_plugin(settings: Settings) -> ProtocolPrefixParser: + return ProtocolPrefixParser()