feat: add HTTP/HTTPS proxy configuration via CLI args and settings

Add --http-proxy, --https-proxy, and --no-proxy CLI arguments that set
the standard HTTP_PROXY/HTTPS_PROXY/NO_PROXY environment variables early
in startup, before any heavy imports. This ensures all outbound traffic
(aiohttp, requests, urllib, subprocesses) is routed through the proxy.

Also reads proxy settings from comfy.settings.json as fallback when CLI
args are not provided, enabling the frontend settings UI to configure
proxy without requiring command-line usage.

Ref: Comfy-Org/desktop#1105
This commit is contained in:
snomiao 2026-03-27 08:51:00 +09:00
parent 2a1f402601
commit 70313b7f00
2 changed files with 54 additions and 0 deletions

View File

@ -231,6 +231,13 @@ parser.add_argument(
help="Set the base URL for the ComfyUI API. (default: https://api.comfy.org)",
)
parser.add_argument("--http-proxy", type=str, default=None, metavar="URL",
help="HTTP/HTTPS proxy URL (e.g. http://127.0.0.1:7890). Sets HTTP_PROXY and HTTPS_PROXY environment variables so all outbound traffic is routed through the proxy.")
parser.add_argument("--https-proxy", type=str, default=None, metavar="URL",
help="HTTPS proxy URL. If not set, --http-proxy is used for both HTTP and HTTPS traffic.")
parser.add_argument("--no-proxy", type=str, default=None, metavar="HOSTS",
help="Comma-separated list of hosts that should bypass the proxy (e.g. localhost,127.0.0.1,*.local).")
database_default_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "user", "comfyui.db")
)

47
main.py
View File

@ -22,11 +22,58 @@ from comfy_execution.utils import get_executing_context
from comfy_api import feature_flags
from app.database.db import init_db, dependencies_available
def _apply_proxy_env_vars():
"""Set proxy environment variables early so all HTTP libraries (aiohttp, requests,
urllib) and subprocesses (git, pip) automatically route through the proxy.
Priority: CLI args > settings file > existing environment variables.
"""
import json as _json
http_proxy = args.http_proxy
https_proxy = args.https_proxy
no_proxy = args.no_proxy
# Fall back to settings file if CLI args are not provided.
if not http_proxy and not https_proxy:
user_dir = args.user_directory or os.path.join(
args.base_directory or os.path.dirname(os.path.realpath(__file__)), "user"
)
settings_path = os.path.join(user_dir, "default", "comfy.settings.json")
if os.path.isfile(settings_path):
try:
with open(settings_path, "r") as f:
settings = _json.load(f)
http_proxy = http_proxy or settings.get("Comfy.Network.Proxy.HttpUrl") or ""
https_proxy = https_proxy or settings.get("Comfy.Network.Proxy.HttpsUrl") or ""
no_proxy = no_proxy or settings.get("Comfy.Network.Proxy.NoProxy") or ""
except Exception:
pass
if http_proxy:
os.environ.setdefault('HTTP_PROXY', http_proxy)
os.environ.setdefault('http_proxy', http_proxy)
# Use http_proxy for HTTPS too unless https_proxy is explicitly set
if not https_proxy:
os.environ.setdefault('HTTPS_PROXY', http_proxy)
os.environ.setdefault('https_proxy', http_proxy)
logging.info("HTTP proxy configured: %s", http_proxy)
if https_proxy:
os.environ.setdefault('HTTPS_PROXY', https_proxy)
os.environ.setdefault('https_proxy', https_proxy)
logging.info("HTTPS proxy configured: %s", https_proxy)
if no_proxy:
os.environ.setdefault('NO_PROXY', no_proxy)
os.environ.setdefault('no_proxy', no_proxy)
if __name__ == "__main__":
#NOTE: These do not do anything on core ComfyUI, they are for custom nodes.
os.environ['HF_HUB_DISABLE_TELEMETRY'] = '1'
os.environ['DO_NOT_TRACK'] = '1'
_apply_proxy_env_vars()
setup_logger(log_level=args.verbose, use_stdout=args.log_stdout)
faulthandler.enable(file=sys.stderr, all_threads=False)