import os import subprocess import threading import time import socket import platform def install_all(): os.system("apt-get update && apt-get install -y xvfb xfce4 xfce4-goodies x11vnc dbus-x11 wget gnupg2") # 安装谷歌浏览器 os.system("wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -") os.system("echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' > /etc/apt/sources.list.d/google-chrome.list") os.system("apt-get update && apt-get install -y google-chrome-stable") print("[INFO] 已安装Xvfb、桌面环境、x11vnc和谷歌浏览器") def start_xvfb(): subprocess.Popen(["Xvfb", ":1", "-screen", "0", "1280x800x24"]) print("[INFO] Xvfb虚拟X服务器已启动,DISPLAY=:1") def start_xfce(): env = os.environ.copy() env["DISPLAY"] = ":1" subprocess.Popen(["startxfce4"], env=env) print("[INFO] Xfce桌面已启动 (DISPLAY=:1)") def start_vnc(): vnc_port = 5900 print(f"[INFO] VNC服务即将启动,端口: {vnc_port}") env = os.environ.copy() env["DISPLAY"] = ":1" subprocess.Popen(["x11vnc", "-display", ":1", "-forever", "-nopw", "-listen", "0.0.0.0", "-xkb"], env=env) print(f"[INFO] x11vnc已启动,端口: {vnc_port} (DISPLAY=:1)") def is_vnc_running(): try: output = subprocess.check_output(["ps", "aux"]).decode() return any("x11vnc" in line for line in output.splitlines()) except Exception as e: print(f"[WARN] 检查x11vnc进程时出错: {e}") return False def monitor_vnc(): while True: running = is_vnc_running() print(f"[MONITOR] x11vnc运行状态: {'运行中' if running else '已退出'}") if not running: print("[ERROR] x11vnc服务已退出!") time.sleep(30) def get_ip(): try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) ip = s.getsockname()[0] s.close() print(f"[INFO] 当前主机IP: {ip}") return ip except Exception as e: print(f"[WARN] 获取IP地址失败: {e}") return None def run_main_py(): py_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "main.py") subprocess.Popen(["python3", py_path]) print("[INFO] main.py 已启动") def is_main_py_running(): try: output = subprocess.check_output(["ps", "aux"]).decode() return any("python3" in line and "main.py" in line for line in output.splitlines()) except Exception as e: print(f"[WARN] 检查main.py进程时出错: {e}") return False def monitor_main_py(): while True: running = is_main_py_running() print(f"[MONITOR] main.py运行状态: {'运行中' if running else '已退出'}") if not running: print("[ERROR] main.py服务已退出!") time.sleep(30) if __name__ == "__main__": print(f"[INFO] 当前平台: {platform.platform()}") install_all() get_ip() threading.Thread(target=start_xvfb, daemon=True).start() time.sleep(2) threading.Thread(target=start_xfce, daemon=True).start() time.sleep(5) threading.Thread(target=start_vnc, daemon=True).start() time.sleep(5) print(f"[INFO] x11vnc当前状态: {'运行中' if is_vnc_running() else '未运行'}") threading.Thread(target=monitor_vnc, daemon=True).start() threading.Thread(target=monitor_main_py, daemon=True).start() run_main_py() while True: time.sleep(60)