Something went wrong. Try again.
small gleam coding and (not yet) persistent agent daemon with a detachable cli forked from okami.mom/albedo
Something went wrong. Try again.
5.8 kB · 174 lines
Python
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175"""Per-service outbound routes, without urllib's process-wide proxy settings."""
from __future__ import annotations
import asyncioimport http.clientimport ipaddressimport jsonimport socketimport urllib.parseimport urllib.requestfrom functools import partialfrom 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)