feat: add protocol-prefix parsers for socks5://ip:port format

This commit is contained in:
agatha 2026-03-15 16:22:42 -04:00
parent 51f6cfb4b4
commit be438420d5

View File

@ -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()