update_proxy_env.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Undo the system proxy the Desktop handed to an update's children.
  2. The Desktop resolves the OS proxy (PAC included) and gives it to the update's
  3. git, urllib, npm and uv children as HTTPS_PROXY/HTTP_PROXY with an extended
  4. NO_PROXY (apps/desktop/electron/updater/system-proxy-env.ts). ``hermes update``
  5. then restarts gateways and backends that inherit that env; for them it is not
  6. the user's setting, and every host the PAC sends direct (LAN model servers,
  7. internal names) would go through the proxy. ``HERMES_UPDATE_SYSTEM_PROXY`` names
  8. the variables the Desktop added and ``HERMES_UPDATE_USER_<KEY>`` holds a user's
  9. own NO_PROXY it extended; a long-lived process restores its env from them at
  10. start. Same algorithm as restoreUserProxyEnv, posix.sh and windows.ps1, plus one
  11. rule only a Hermes process needs: a value the user's ``.env`` set wins. ``hermes
  12. update`` and the restarted process load ``~/.hermes/.env`` with override before
  13. this runs, so a marked key holding the value a dotenv load published is the
  14. user's own setting (same value as the Desktop's or not) and is kept. The Desktop
  15. and the hand-off scripts never load ``.env``, so their envs never hit that case.
  16. """
  17. from __future__ import annotations
  18. import os
  19. from typing import MutableMapping
  20. MARKER = "HERMES_UPDATE_SYSTEM_PROXY"
  21. SAVED_PREFIX = "HERMES_UPDATE_USER_"
  22. _ADDED_KEYS = ("HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "NO_PROXY", "no_proxy")
  23. _NO_PROXY_KEYS = ("NO_PROXY", "no_proxy")
  24. def restore_user_proxy_env(environ: MutableMapping[str, str] | None = None) -> None:
  25. """Drop the proxy variables the Desktop added and restore the user's own NO_PROXY."""
  26. from hermes_cli.env_loader import dotenv_published_values
  27. env = os.environ if environ is None else environ
  28. def users_dotenv_value(key: str) -> bool:
  29. return key in env and env[key] in dotenv_published_values(key)
  30. for key in env.get(MARKER, "").split(","):
  31. if key in _ADDED_KEYS and not users_dotenv_value(key):
  32. env.pop(key, None)
  33. for key in _NO_PROXY_KEYS:
  34. saved = env.pop(SAVED_PREFIX + key, None)
  35. if saved is not None and not users_dotenv_value(key):
  36. env[key] = saved
  37. env.pop(MARKER, None)
  38. def prefer_dotenv_proxy_env(environ: MutableMapping[str, str] | None = None) -> None:
  39. """In ``hermes update``, after its ``.env`` load: the user's ``.env`` proxy beats the system one.
  40. The Desktop injects every case variant (``HTTPS_PROXY`` and ``https_proxy``) but the ``.env``
  41. load replaces only the spelling the file uses, and urllib and curl prefer the lower-case one, so
  42. the update would still go through the system proxy. A ``.env`` proxy is an explicit setting the
  43. Desktop could not see; had it seen one it would have injected nothing, so everything it added is
  44. undone. A ``.env`` NO_PROXY alone replaces the user's own entries in every injected NO_PROXY
  45. variant, keeping the entries the Desktop added; a ``*`` in it bypasses every proxy.
  46. """
  47. from agent.proxy_bypass import no_proxy_entries
  48. from hermes_cli.env_loader import dotenv_published_values
  49. env = os.environ if environ is None else environ
  50. if MARKER not in env:
  51. return
  52. if any(dotenv_published_values(name) for name in ("https_proxy", "http_proxy", "all_proxy")):
  53. restore_user_proxy_env(env)
  54. return
  55. published = dotenv_published_values("no_proxy")
  56. users = next((env[key] for key in env if key.lower() == "no_proxy" and env[key] in published), None)
  57. if users is None:
  58. return
  59. if "*" in no_proxy_entries(users):
  60. restore_user_proxy_env(env)
  61. return
  62. own = {e for key in _NO_PROXY_KEYS for e in no_proxy_entries(env.get(SAVED_PREFIX + key, ""))}
  63. added = [e for key in _NO_PROXY_KEYS if env.get(key, users) != users
  64. for e in no_proxy_entries(env[key]) if e not in own]
  65. merged = ",".join(dict.fromkeys([*no_proxy_entries(users), *added]))
  66. for key in _NO_PROXY_KEYS:
  67. if key in env:
  68. env[key] = merged