proxy_bypass.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """NO_PROXY matching shared by the LLM transport (``agent/process_bootstrap.py``) and the
  2. gateway platform adapters (``gateway/platforms/base.py``).
  3. One matcher so "is this host in NO_PROXY" has one answer everywhere: exact hosts, domain
  4. suffixes (``example.com``, ``.example.com``, ``*.example.com``), IP literals, CIDR ranges,
  5. optional ``host:port`` entries and ``*``. The stdlib ``proxy_bypass_environment`` understands
  6. none of the CIDR / ``*.`` forms, which is why the LLM path used to route ``10.x`` endpoints
  7. through the corporate proxy while Telegram/Discord bypassed it.
  8. Leaf module: stdlib only, importable during early boot.
  9. """
  10. from __future__ import annotations
  11. import ipaddress
  12. import os
  13. import re
  14. from urllib.parse import urlsplit
  15. PROXY_ENV_KEYS = ("HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY", "https_proxy", "http_proxy", "all_proxy")
  16. def first_proxy_env_value() -> str:
  17. """First non-empty HTTPS_PROXY / HTTP_PROXY / ALL_PROXY value (any case), or ''."""
  18. return next((v for k in PROXY_ENV_KEYS if (v := (os.environ.get(k) or "").strip())), "")
  19. def split_host_port(value: str) -> tuple[str, int | None]:
  20. """``(host, port)`` from a URL (scheme optional: ``//host/path``), ``[v6]:port``,
  21. ``host:port`` or bare host; host lowercased. A malformed URL port (``host:abc``,
  22. ``host:99999``) yields ``(host, None)`` rather than raising."""
  23. raw = str(value or "").strip()
  24. if not raw:
  25. return "", None
  26. if "://" in raw or raw.startswith("//"):
  27. parsed = urlsplit(raw)
  28. host = parsed.hostname or ""
  29. try:
  30. port = parsed.port
  31. except ValueError: # ``host:abc`` / ``host:99999``: keep the host, drop the port
  32. port = None
  33. elif raw.startswith("[") and "]" in raw:
  34. host, _, rest = raw[1:].partition("]")
  35. port = int(rest[1:]) if rest.startswith(":") and rest[1:].isdigit() else None
  36. elif raw.count(":") == 1 and raw.rpartition(":")[2].isdigit():
  37. host, _, port_s = raw.rpartition(":")
  38. port = int(port_s)
  39. else:
  40. host, port = raw.strip("[]"), None
  41. return host.lower().rstrip("."), port
  42. def no_proxy_entries(no_proxy_value: str | None = None) -> list[str]:
  43. """Comma/whitespace-separated NO_PROXY entries; from the environment (both casings) when
  44. ``no_proxy_value`` is None."""
  45. if no_proxy_value is None:
  46. no_proxy_value = ",".join(os.environ.get(key, "") for key in ("NO_PROXY", "no_proxy"))
  47. return [part for part in re.split(r"[\s,]+", no_proxy_value.strip()) if part]
  48. # Loopback must never be dialed through a proxy. ``websockets>=14`` connects with
  49. # ``proxy=True`` and resolves it via ``urllib.request.getproxies()`` — on macOS that reads the
  50. # *system* proxy (``_scproxy``) even with no ``*_proxy`` env vars — so a local CDP endpoint
  51. # (``ws://127.0.0.1:<port>/devtools/...``) is dialed through the proxy and the handshake dies
  52. # with "did not receive a valid HTTP response" (#110565). ``urllib``'s bypass check honours
  53. # NO_PROXY in both casings, so children get the entries appended; in-process dials pass
  54. # ``proxy=None`` when the host is loopback.
  55. LOOPBACK_HOSTS = ("127.0.0.1", "localhost", "::1")
  56. def is_loopback_host(host: str | None) -> bool:
  57. """True for a host that must always bypass a proxy: ``localhost`` or any loopback IP literal
  58. (``127.x.x.x``, ``::1``, ``::ffff:127.0.0.1``)."""
  59. host = str(host or "").strip().lower().strip("[]")
  60. ip = _ip_or_none(host)
  61. return host == "localhost" or (ip is not None and ip.is_loopback)
  62. def loopback_connect_kwargs(url: str) -> dict:
  63. """``websockets.connect`` kwargs for an in-process dial: ``{"proxy": None}`` when ``url``
  64. targets loopback (skip the library's system-proxy auto-detection), else ``{}`` so remote
  65. endpoints keep the default proxy behaviour."""
  66. return {"proxy": None} if is_loopback_host(split_host_port(url)[0]) else {}
  67. def loopback_request_kwargs(url: str) -> dict:
  68. """``requests.get`` kwargs for an in-process HTTP dial (CDP ``/json/version`` discovery /
  69. readiness): ``{"proxies": {"http": None, "https": None}}`` when ``url`` targets loopback so
  70. ``requests`` skips ``getproxies()`` (env and macOS system proxy), else ``{}``."""
  71. return {"proxies": {"http": None, "https": None}} if is_loopback_host(split_host_port(url)[0]) else {}
  72. def add_loopback_no_proxy(env: dict) -> dict:
  73. """Append the loopback hosts to ``NO_PROXY`` / ``no_proxy`` in ``env`` (both casings),
  74. keeping every operator-provided entry; returns ``env``. An operator ``*`` (bypass everything)
  75. already covers loopback and would stop being the wildcard once anything is appended to it."""
  76. if any("*" in no_proxy_entries(env.get(key) or "") for key in ("NO_PROXY", "no_proxy")):
  77. return env # both casings: requests/urllib read ``no_proxy`` first, so a loopback-only one would win
  78. for key in ("NO_PROXY", "no_proxy"):
  79. entries = no_proxy_entries(env.get(key) or "")
  80. missing = [host for host in LOOPBACK_HOSTS if host not in entries]
  81. if missing:
  82. env[key] = ",".join(entries + missing)
  83. return env
  84. def _ip_or_none(value: str, parse=ipaddress.ip_address):
  85. """``parse(value)`` or None on ``ValueError`` (``parse`` is ip_address / ip_network)."""
  86. try:
  87. return parse(value)
  88. except ValueError:
  89. return None
  90. def no_proxy_entry_matches(entry: str, host: str, port: int | None = None) -> bool:
  91. token = str(entry or "").strip().lower()
  92. if not token:
  93. return False
  94. if token == "*":
  95. return True
  96. token_host, token_port = split_host_port(token)
  97. if not token_host or (token_port is not None and (port is None or token_port != port)):
  98. return False
  99. host_ip = _ip_or_none(host)
  100. network = _ip_or_none(token_host, lambda v: ipaddress.ip_network(v, strict=False))
  101. if network is not None: # CIDR or bare IP literal (a /32 / /128 network)
  102. return host_ip is not None and host_ip in network
  103. # ``*.example.com`` and ``.example.com`` both mean apex + subdomains (curl/requests
  104. # convention, and what is_host_excluded_by_no_proxy promised the Slack adapter).
  105. suffix = token_host.removeprefix("*").removeprefix(".")
  106. return host == suffix or host.endswith(f".{suffix}")
  107. def should_bypass_proxy(
  108. target_hosts: str | list[str] | tuple[str, ...] | set[str] | None, *, no_proxy_value: str | None = None,
  109. ) -> bool:
  110. """True when NO_PROXY (the environment, or ``no_proxy_value``) matches at least one target
  111. host (a URL, ``host:port`` or bare host)."""
  112. entries = no_proxy_entries(no_proxy_value)
  113. if not entries or not target_hosts:
  114. return False
  115. candidates = [target_hosts] if isinstance(target_hosts, str) else list(target_hosts)
  116. return any(
  117. host and any(no_proxy_entry_matches(entry, host, port) for entry in entries)
  118. for host, port in map(split_host_port, map(str, candidates)))