import os import subprocess import threading import time import urllib.request import tarfile import zipfile import http.server import socketserver import signal # 下载并解压工具函数 def download_and_extract(url, extract_to, is_zip=False): filename = url.split("/")[-1] if not os.path.exists(extract_to): os.makedirs(extract_to) filepath = os.path.join(extract_to, filename) if not os.path.exists(filepath): print(f"Downloading {filename} ...") urllib.request.urlretrieve(url, filepath) if is_zip: with zipfile.ZipFile(filepath, "r") as zip_ref: zip_ref.extractall(extract_to) else: with tarfile.open(filepath, "r:*") as tar: tar.extractall(path=extract_to) # 下载并解压 fluxbox def setup_fluxbox(): url = "https://github.com/void-linux/void-packages/files/12496441/fluxbox-1.3.7-x86_64.tar.gz" extract_to = "./fluxbox" download_and_extract(url, extract_to) return os.path.join(extract_to, "fluxbox-1.3.7-x86_64", "bin", "fluxbox") # 下载并解压 TigerVNC def setup_tigervnc(): url = "https://github.com/TigerVNC/tigervnc/releases/download/v1.13.1/tigervnc-1.13.1.x86_64.tar.gz" extract_to = "./tigervnc" download_and_extract(url, extract_to) return os.path.join(extract_to, "tigervnc-1.13.1.x86_64", "vncserver") # 下载并解压 noVNC def setup_novnc(): url = "https://github.com/novnc/noVNC/archive/refs/heads/master.zip" extract_to = "./novnc" download_and_extract(url, extract_to, is_zip=True) return os.path.join(extract_to, "noVNC-master") # 启动 Xvfb def start_xvfb(): return subprocess.Popen(["Xvfb", ":1", "-screen", "0", "1024x768x16"], env={**os.environ, "DISPLAY": ":1"}) # 启动 fluxbox def start_fluxbox(fluxbox_path): return subprocess.Popen([fluxbox_path], env={**os.environ, "DISPLAY": ":1"}) # 启动 VNC server def start_vncserver(vncserver_path): return subprocess.Popen([vncserver_path, ":1", "-geometry", "1024x768", "-SecurityTypes", "None"], env={**os.environ, "DISPLAY": ":1"}) # 启动 noVNC def start_novnc(novnc_path): websockify = os.path.join(novnc_path, "utils", "websockify", "run") return subprocess.Popen(["python", websockify, "7860", "localhost:5901", "--web", novnc_path], env=os.environ) # 启动 ssh_client.py def start_ssh_client(): if os.path.exists("ssh_client.py"): return subprocess.Popen(["python", "ssh_client.py"]) return None # 创建简单的重定向页面到 noVNC def create_redirect_html(): with open("index.html", "w") as f: f.write("""
重定向到 VNC 界面...
""") def setup_and_run(): print("正在设置和启动桌面环境...") fluxbox_path = setup_fluxbox() vncserver_path = setup_tigervnc() novnc_path = setup_novnc() # 创建重定向页面 create_redirect_html() # 启动 Xvfb xvfb_proc = start_xvfb() print("Xvfb 已启动") time.sleep(2) # 启动 fluxbox fluxbox_proc = start_fluxbox(fluxbox_path) print("Fluxbox 已启动") time.sleep(2) # 启动 VNC server vnc_proc = start_vncserver(vncserver_path) print("TigerVNC 已启动") time.sleep(2) # 启动 noVNC novnc_proc = start_novnc(novnc_path) print("noVNC 已启动在端口 7860,请访问 http://