Spaces:
Running
Running
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("""<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Fluxbox+TigerVNC Space</title> | |
<meta http-equiv="refresh" content="0; url=./vnc.html"> | |
</head> | |
<body> | |
<p>重定向到 VNC 界面...</p> | |
<p><a href="./vnc.html">如果没有自动跳转,请点击这里</a></p> | |
</body> | |
</html> | |
""") | |
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://<your-space-url>:7860/vnc.html") | |
# 启动 ssh_client.py | |
ssh_client_proc = start_ssh_client() | |
if ssh_client_proc: | |
print("ssh_client.py 已启动") | |
# 等待所有进程完成(实际上它们会一直运行) | |
try: | |
while True: | |
time.sleep(600) # 每10分钟检查一次 | |
# 检查各个进程是否还在运行,如果不在则重启 | |
if xvfb_proc.poll() is not None: | |
print("Xvfb 已终止,正在重启...") | |
xvfb_proc = start_xvfb() | |
if fluxbox_proc.poll() is not None: | |
print("Fluxbox 已终止,正在重启...") | |
fluxbox_proc = start_fluxbox(fluxbox_path) | |
if vnc_proc.poll() is not None: | |
print("TigerVNC 已终止,正在重启...") | |
vnc_proc = start_vncserver(vncserver_path) | |
if novnc_proc.poll() is not None: | |
print("noVNC 已终止,正在重启...") | |
novnc_proc = start_novnc(novnc_path) | |
if ssh_client_proc and ssh_client_proc.poll() is not None: | |
print("ssh_client.py 已终止,正在重启...") | |
ssh_client_proc = start_ssh_client() | |
except KeyboardInterrupt: | |
print("正在关闭所有进程...") | |
# 清理进程 | |
for proc in [xvfb_proc, fluxbox_proc, vnc_proc, novnc_proc]: | |
if proc: | |
proc.terminate() | |
if ssh_client_proc: | |
ssh_client_proc.terminate() | |
if __name__ == "__main__": | |
print("启动桌面环境:Fluxbox + TigerVNC + noVNC") | |
setup_and_run() | |