Harden local updater and install changed dependencies
This commit is contained in:
+60
-1
@@ -1,8 +1,12 @@
|
||||
"""Safely fast-forward a Git installation of this node package."""
|
||||
|
||||
import ipaddress
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
|
||||
PLUGIN_DIR = Path(__file__).resolve().parent.parent
|
||||
@@ -12,6 +16,35 @@ class UpdateError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def is_local_management_request(request):
|
||||
"""Allow software management only from the local ComfyUI browser."""
|
||||
peer = request.transport.get_extra_info("peername") if request.transport else None
|
||||
if not peer:
|
||||
return False
|
||||
try:
|
||||
if not ipaddress.ip_address(peer[0]).is_loopback:
|
||||
return False
|
||||
host = request.headers.get("Host", "")
|
||||
host_url = urlsplit(f"http://{host}")
|
||||
hostname = host_url.hostname
|
||||
if host_url.username or host_url.password or host_url.path or host_url.query or host_url.fragment:
|
||||
return False
|
||||
if hostname != "localhost" and not ipaddress.ip_address(hostname).is_loopback:
|
||||
return False
|
||||
origin = request.headers.get("Origin", "")
|
||||
parsed_origin = urlsplit(origin)
|
||||
except (ValueError, TypeError):
|
||||
return False
|
||||
|
||||
return (
|
||||
parsed_origin.scheme == request.scheme
|
||||
and parsed_origin.netloc.lower() == host.lower()
|
||||
and not parsed_origin.path
|
||||
and not parsed_origin.query
|
||||
and not parsed_origin.fragment
|
||||
)
|
||||
|
||||
|
||||
def _git(*args, timeout=60, check=True):
|
||||
env = os.environ.copy()
|
||||
env["GIT_TERMINAL_PROMPT"] = "0"
|
||||
@@ -37,6 +70,29 @@ def _git(*args, timeout=60, check=True):
|
||||
return result
|
||||
|
||||
|
||||
def _install_requirements(requirements):
|
||||
fd, filename = tempfile.mkstemp(prefix=".o1key-requirements-", suffix=".txt", dir=PLUGIN_DIR)
|
||||
try:
|
||||
with os.fdopen(fd, "w", encoding="utf-8") as req_file:
|
||||
req_file.write(requirements)
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-m", "pip", "install", "-r", filename],
|
||||
cwd=PLUGIN_DIR,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
timeout=600,
|
||||
)
|
||||
except subprocess.TimeoutExpired as exc:
|
||||
raise UpdateError("依赖安装超时,节点代码尚未更新,请检查网络后重试。") from exc
|
||||
if result.returncode:
|
||||
raise UpdateError("依赖安装失败,节点代码尚未更新,请检查 ComfyUI 的 Python 环境后重试。")
|
||||
finally:
|
||||
Path(filename).unlink(missing_ok=True)
|
||||
|
||||
|
||||
def update_package():
|
||||
"""Update origin/main without discarding local changes or switching branches."""
|
||||
if not (PLUGIN_DIR / ".git").exists():
|
||||
@@ -59,8 +115,11 @@ def update_package():
|
||||
if _git("merge-base", "--is-ancestor", "HEAD", "FETCH_HEAD", check=False).returncode:
|
||||
raise UpdateError("本地与 origin/main 已分叉,无法安全快进。请手动处理。")
|
||||
|
||||
new_requirements = _git("show", "FETCH_HEAD:requirements.txt", check=False).stdout
|
||||
requirements_changed = old_requirements != new_requirements
|
||||
if requirements_changed and new_requirements.strip():
|
||||
_install_requirements(new_requirements)
|
||||
_git("merge", "--ff-only", "FETCH_HEAD")
|
||||
requirements_changed = old_requirements != (PLUGIN_DIR / "requirements.txt").read_text(encoding="utf-8")
|
||||
return {
|
||||
"updated": True,
|
||||
"version": new_commit[:7],
|
||||
|
||||
Reference in New Issue
Block a user