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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import time
|
| 4 |
+
import glob
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
# ==========================================
|
| 8 |
+
# CONFIGURATION VARIABLES
|
| 9 |
+
# ==========================================
|
| 10 |
+
username = "user" # Replace with your desired username
|
| 11 |
+
password = "root" # Replace with your desired password
|
| 12 |
+
|
| 13 |
+
# Enter a Pin (must be 6 or more digits)
|
| 14 |
+
Pin = 123456
|
| 15 |
+
|
| 16 |
+
# Autostart Browser in RDP
|
| 17 |
+
Autostart = False
|
| 18 |
+
# ==========================================
|
| 19 |
+
|
| 20 |
+
# Prompt the user for the CRP command interactively
|
| 21 |
+
print("===================================================================")
|
| 22 |
+
print("Visit: https://remotedesktop.google.com/headless")
|
| 23 |
+
print("Authenticate, click 'Begin', 'Next', and 'Authorize'.")
|
| 24 |
+
print("Copy the command for 'Debian Linux' and paste it below.")
|
| 25 |
+
print("===================================================================")
|
| 26 |
+
CRP = input("Paste your CRD command here: ").strip()
|
| 27 |
+
|
| 28 |
+
print(f"\nCreating User and Setting it up...")
|
| 29 |
+
|
| 30 |
+
# Creation of user
|
| 31 |
+
os.system(f"sudo useradd -m {username}")
|
| 32 |
+
os.system(f"sudo adduser {username} sudo")
|
| 33 |
+
os.system(f"echo '{username}:{password}' | sudo chpasswd")
|
| 34 |
+
os.system("sudo sed -i 's/\/bin\/sh/\/bin\/bash/g' /etc/passwd")
|
| 35 |
+
|
| 36 |
+
print(f"User created and configured having username `{username}` and password `{password}`\n")
|
| 37 |
+
|
| 38 |
+
class CRD:
|
| 39 |
+
def __init__(self, user):
|
| 40 |
+
os.system("sudo apt update")
|
| 41 |
+
self.installCRD()
|
| 42 |
+
self.installDesktopEnvironment()
|
| 43 |
+
self.installGoogleChrome()
|
| 44 |
+
self.finish(user)
|
| 45 |
+
self.verify_running(user)
|
| 46 |
+
self.tail_logs(user)
|
| 47 |
+
|
| 48 |
+
@staticmethod
|
| 49 |
+
def installCRD():
|
| 50 |
+
print("\nInstalling Chrome Remote Desktop...")
|
| 51 |
+
subprocess.run(['wget', '-O', 'chrome-remote-desktop_current_amd64.deb', 'https://dl.google.com/linux/direct/chrome-remote-desktop_current_amd64.deb'])
|
| 52 |
+
try:
|
| 53 |
+
subprocess.run(['sudo', 'dpkg', '--install', 'chrome-remote-desktop_current_amd64.deb'], check=True)
|
| 54 |
+
subprocess.run(['sudo', 'apt', 'install', '--assume-yes', '--fix-broken'], check=True)
|
| 55 |
+
except subprocess.CalledProcessError:
|
| 56 |
+
print("\n[!] CRITICAL ERROR: Package installation failed.")
|
| 57 |
+
print("[!] Wait for the setup progress bar to disappear, then run this script again.")
|
| 58 |
+
sys.exit(1)
|
| 59 |
+
|
| 60 |
+
@staticmethod
|
| 61 |
+
def installDesktopEnvironment():
|
| 62 |
+
print("\nInstalling Desktop Environment (XFCE4)...")
|
| 63 |
+
os.system("sudo DEBIAN_FRONTEND=noninteractive apt install --assume-yes xvfb xfce4 desktop-base xfce4-terminal")
|
| 64 |
+
os.system("sudo bash -c 'echo \"exec /etc/X11/Xsession /usr/bin/xfce4-session\" > /etc/chrome-remote-desktop-session'")
|
| 65 |
+
os.system("sudo chmod +x /etc/chrome-remote-desktop-session")
|
| 66 |
+
os.system("sudo apt remove --assume-yes gnome-terminal")
|
| 67 |
+
os.system("sudo apt install --assume-yes xscreensaver")
|
| 68 |
+
os.system("sudo systemctl disable lightdm.service")
|
| 69 |
+
|
| 70 |
+
@staticmethod
|
| 71 |
+
def installGoogleChrome():
|
| 72 |
+
print("\nInstalling Google Chrome...")
|
| 73 |
+
subprocess.run(["wget", "-O", "google-chrome-stable_current_amd64.deb", "https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb"])
|
| 74 |
+
try:
|
| 75 |
+
subprocess.run(["sudo", "dpkg", "--install", "google-chrome-stable_current_amd64.deb"], check=True)
|
| 76 |
+
subprocess.run(['sudo', 'apt', 'install', '--assume-yes', '--fix-broken'], check=True)
|
| 77 |
+
except subprocess.CalledProcessError:
|
| 78 |
+
print("\n[!] CRITICAL ERROR: Google Chrome installation failed.")
|
| 79 |
+
sys.exit(1)
|
| 80 |
+
|
| 81 |
+
@staticmethod
|
| 82 |
+
def finish(user):
|
| 83 |
+
print("\nFinalizing setup...")
|
| 84 |
+
if Autostart:
|
| 85 |
+
os.system(f"sudo -u {user} mkdir -p /home/{user}/.config/autostart")
|
| 86 |
+
link = "https://lightning.ai/"
|
| 87 |
+
browser_autostart = f"""[Desktop Entry]
|
| 88 |
+
Type=Application
|
| 89 |
+
Name=Browser
|
| 90 |
+
Exec=sh -c "sensible-browser {link}"
|
| 91 |
+
Icon=
|
| 92 |
+
Comment=Open a predefined page at session signin.
|
| 93 |
+
X-GNOME-Autostart-enabled=true"""
|
| 94 |
+
os.system(f"sudo bash -c 'cat > /home/{user}/.config/autostart/browser.desktop <<EOF\n{browser_autostart}\nEOF'")
|
| 95 |
+
os.system(f"sudo chmod +x /home/{user}/.config/autostart/browser.desktop")
|
| 96 |
+
|
| 97 |
+
os.system("sudo groupadd chrome-remote-desktop || true")
|
| 98 |
+
os.system(f"sudo usermod -aG chrome-remote-desktop {user}")
|
| 99 |
+
|
| 100 |
+
os.system(f"sudo chown -R {user}:{user} /home/{user}")
|
| 101 |
+
os.system("sudo rm -rf /tmp/.X*")
|
| 102 |
+
|
| 103 |
+
# ==========================================
|
| 104 |
+
# CRITICAL FIX: keep the host alive after this script exits
|
| 105 |
+
# ==========================================
|
| 106 |
+
# In containerized / cloud-VM environments (like Lightning.ai), systemd-logind
|
| 107 |
+
# kills a user's processes as soon as their last login session ends. Since the
|
| 108 |
+
# CRD host is started inside a temporary `su - user` session, it gets killed a
|
| 109 |
+
# few seconds after the script finishes unless lingering is enabled. This is
|
| 110 |
+
# what causes the host to look "stuck" on Starting... forever in the web UI.
|
| 111 |
+
os.system(f"sudo loginctl enable-linger {user}")
|
| 112 |
+
|
| 113 |
+
# Clear the failed state of the CRD daemon triggered during package installation,
|
| 114 |
+
# and stop it so the CRP command initializes it cleanly.
|
| 115 |
+
os.system("sudo systemctl reset-failed chrome-remote-desktop.service || true")
|
| 116 |
+
os.system("sudo systemctl stop chrome-remote-desktop.service || true")
|
| 117 |
+
|
| 118 |
+
command = f"{CRP} --pin={Pin}"
|
| 119 |
+
os.system(f"sudo su - {user} -c '{command}'")
|
| 120 |
+
|
| 121 |
+
os.system("sudo service chrome-remote-desktop restart")
|
| 122 |
+
print("Finished Configuration Successfully.")
|
| 123 |
+
|
| 124 |
+
@staticmethod
|
| 125 |
+
def verify_running(user):
|
| 126 |
+
print("\nVerifying the host survives after this session ends...")
|
| 127 |
+
time.sleep(5)
|
| 128 |
+
result = subprocess.run(
|
| 129 |
+
["pgrep", "-fu", user, "chrome-remote-desktop"],
|
| 130 |
+
capture_output=True, text=True
|
| 131 |
+
)
|
| 132 |
+
if result.stdout.strip():
|
| 133 |
+
print("[OK] Host process is running and should stay up (lingering enabled).")
|
| 134 |
+
else:
|
| 135 |
+
print("[!] Host process not found. Lingering may not have taken effect yet.")
|
| 136 |
+
print(f" Try manually: sudo -u {user} /opt/google/chrome-remote-desktop/chrome-remote-desktop --start")
|
| 137 |
+
|
| 138 |
+
@staticmethod
|
| 139 |
+
def tail_logs(user):
|
| 140 |
+
print("\n===================================================================")
|
| 141 |
+
print("RDP created successfully! Go to: https://remotedesktop.google.com/access")
|
| 142 |
+
print("Now streaming live activity logs. (Press Ctrl+C to stop viewing)")
|
| 143 |
+
print("===================================================================\n")
|
| 144 |
+
|
| 145 |
+
xsession_log = f"/home/{user}/.xsession-errors"
|
| 146 |
+
os.system(f"sudo touch {xsession_log}")
|
| 147 |
+
os.system(f"sudo chown {user}:{user} {xsession_log}")
|
| 148 |
+
time.sleep(3)
|
| 149 |
+
|
| 150 |
+
crd_logs = glob.glob("/tmp/chrome_remote_desktop_*")
|
| 151 |
+
crd_log_cmd = ""
|
| 152 |
+
if crd_logs:
|
| 153 |
+
latest_crd_log = max(crd_logs, key=os.path.getctime)
|
| 154 |
+
crd_log_cmd = f" {latest_crd_log}"
|
| 155 |
+
|
| 156 |
+
try:
|
| 157 |
+
os.system(f"sudo tail -F {xsession_log}{crd_log_cmd}")
|
| 158 |
+
except KeyboardInterrupt:
|
| 159 |
+
print("\nStopped watching logs. RDP is still running in the background.")
|
| 160 |
+
|
| 161 |
+
# Entry Point Validation
|
| 162 |
+
try:
|
| 163 |
+
if not CRP or "PASTE_YOUR_CRD_COMMAND_HERE" in CRP:
|
| 164 |
+
print("Error: You did not enter a valid CRD authcode.")
|
| 165 |
+
elif len(str(Pin)) < 6:
|
| 166 |
+
print("Error: Pin must be 6 or more digits.")
|
| 167 |
+
else:
|
| 168 |
+
CRD(username)
|
| 169 |
+
except NameError as e:
|
| 170 |
+
print("'username' variable not found, check your configuration variables.")
|