"""Per-service outbound routes, without urllib's process-wide proxy settings.""" from __future__ import annotations import asyncio import http.client import ipaddress import json import socket import urllib.parse import urllib.request from functools import partial from typing import Any, Literal, NamedTuple class ProxyError(OSError): pass Scheme = Literal["socks5", "socks5h", "http"] class Route(NamedTuple): scheme: Scheme host: str port: int @classmethod def parse(cls, value: Any) -> Route | None: """The route the daemon encoded: null for direct, else {"url": ...}.""" if value is None: return None try: url = urllib.parse.urlsplit(value["url"]) host, port = url.hostname or "", url.port or 0 except (TypeError, KeyError, ValueError): raise ValueError("invalid outbound proxy route") from None match url.scheme: case "socks5" | "socks5h" | "http" as scheme if host and port: return cls(scheme, host, port) raise ValueError("invalid outbound proxy route") def json(self) -> str: host = f"[{self.host}]" if ":" in self.host else self.host return json.dumps({"url": f"{self.scheme}://{host}:{self.port}"}) def from_env(text: str | None) -> Route | None: return Route.parse(json.loads(text or "null")) def _read(sock: socket.socket, size: int) -> bytes: data = b"" while len(data) < size: chunk = sock.recv(size - len(data)) if not chunk: raise ProxyError("outbound proxy closed the connection") data += chunk return data def _socks(sock: socket.socket, route: Route, host: str, port: int) -> None: sock.sendall(b"\x05\x01\x00") if _read(sock, 2) != b"\x05\x00": raise ProxyError("SOCKS5 proxy refused the connection") if route.scheme == "socks5": host = str(socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)[0][4][0]) try: address = ipaddress.ip_address(host) target = bytes((1 if address.version == 4 else 4,)) + address.packed except ValueError: name = host.encode("idna") target = bytes((3, len(name))) + name sock.sendall(b"\x05\x01\x00" + target + port.to_bytes(2, "big")) reply = _read(sock, 4) if reply[:2] != b"\x05\x00": raise ProxyError("SOCKS5 proxy refused the destination") size = {1: 4, 4: 16}.get(reply[3]) or _read(sock, 1)[0] _read(sock, size + 2) def _connect(sock: socket.socket, host: str, port: int) -> None: authority = f"[{host}]:{port}" if ":" in host else f"{host}:{port}" sock.sendall(f"CONNECT {authority} HTTP/1.1\r\nHost: {authority}\r\n\r\n".encode()) head = b"" while not head.endswith(b"\r\n\r\n") and len(head) < 65536: head += _read(sock, 1) status = head.split(b" ", 2) if not head.startswith(b"HTTP/") or len(status) < 2 or status[1][:1] != b"2": raise ProxyError("HTTP proxy refused the CONNECT") def open_socket( host: str, port: int, route: Route | None, timeout: float | None ) -> socket.socket: """A connected socket to host:port; socks5h and CONNECT leave DNS to the proxy.""" if route is None: return socket.create_connection((host, port), timeout) sock = socket.create_connection((route.host, route.port), timeout) try: if route.scheme == "http": _connect(sock, host.encode("idna").decode(), port) else: _socks(sock, route, host, port) except ValueError: sock.close() raise ProxyError("invalid outbound destination") from None except BaseException: sock.close() raise return sock async def open_connection( host: str, port: int, route: Route | None, *, tls: bool = False, timeout: float = 15, limit: int = 65536, ) -> tuple[asyncio.StreamReader, asyncio.StreamWriter]: async with asyncio.timeout(timeout): if route is None: return await asyncio.open_connection( host, port, ssl=tls or None, limit=limit ) sock = await asyncio.to_thread(open_socket, host, port, route, timeout) sock.setblocking(False) return await asyncio.open_connection( sock=sock, ssl=tls or None, server_hostname=host if tls else None, limit=limit, ) class HTTPConnection(http.client.HTTPConnection): def __init__(self, *args: Any, route: Route | None, **kwargs: Any) -> None: super().__init__(*args, **kwargs) self.route = route def connect(self) -> None: self.sock = open_socket(self.host, self.port, self.route, self.timeout) class HTTPSConnection(HTTPConnection, http.client.HTTPSConnection): _context: Any def connect(self) -> None: super().connect() self.sock = self._context.wrap_socket(self.sock, server_hostname=self.host) class _Handler(urllib.request.HTTPHandler, urllib.request.HTTPSHandler): def __init__(self, route: Route | None) -> None: super().__init__() self.route = route def http_open(self, req: urllib.request.Request) -> Any: return self.do_open(partial(HTTPConnection, route=self.route), req) def https_open(self, req: urllib.request.Request) -> Any: return self.do_open(partial(HTTPSConnection, route=self.route), req) def urlopen( request: urllib.request.Request, route: Route | None, timeout: float ) -> Any: """urlopen through route, redirects included, ignoring *_proxy variables.""" opener = urllib.request.build_opener( urllib.request.ProxyHandler({}), _Handler(route) ) return opener.open(request, timeout=timeout)