|
13 | 13 | import os |
14 | 14 | import random |
15 | 15 | import ssl |
| 16 | +import subprocess |
16 | 17 | import sys |
17 | 18 | import textwrap |
18 | 19 | import time |
@@ -1111,47 +1112,79 @@ class LinuxAutostartManager(IAutostartManager): |
1111 | 1112 |
|
1112 | 1113 | @staticmethod |
1113 | 1114 | def manage_autostart(action: str = "install") -> None: |
1114 | | - """Manages Linux autostart""" |
| 1115 | + """Manages Linux autostart using systemd user services""" |
1115 | 1116 |
|
1116 | 1117 | app_name = "NoDPIProxy" |
1117 | 1118 | exec_path = sys.executable |
| 1119 | + service_name = f"{app_name.lower()}.service" |
| 1120 | + |
| 1121 | + user_service_dir = Path.home() / ".config" / "systemd" / "user" |
| 1122 | + service_file = user_service_dir / service_name |
| 1123 | + |
| 1124 | + blacklist_path = f"{os.path.dirname(exec_path)}/blacklist.txt" |
1118 | 1125 |
|
1119 | 1126 | if action == "install": |
1120 | 1127 | try: |
1121 | | - autostart_dir = Path.home() / ".config" / "autostart" |
1122 | | - autostart_dir.mkdir(parents=True, exist_ok=True) |
| 1128 | + user_service_dir.mkdir(parents=True, exist_ok=True) |
| 1129 | + |
| 1130 | + service_content = f"""[Unit] |
| 1131 | +Description=NoDPIProxy Service |
| 1132 | +After=network.target graphical-session.target |
| 1133 | +Wants=network.target |
| 1134 | +
|
| 1135 | +[Service] |
| 1136 | +Type=simple |
| 1137 | +ExecStart={exec_path} --blacklist "{blacklist_path}" --quiet |
| 1138 | +Restart=on-failure |
| 1139 | +RestartSec=5 |
| 1140 | +Environment=DISPLAY=:0 |
| 1141 | +Environment=XAUTHORITY=%h/.Xauthority |
| 1142 | +
|
| 1143 | +[Install] |
| 1144 | +WantedBy=default.target |
| 1145 | +""" |
1123 | 1146 |
|
1124 | | - desktop_file = autostart_dir / f"{app_name}.desktop" |
| 1147 | + with open(service_file, "w", encoding="utf-8") as f: |
| 1148 | + f.write(service_content) |
1125 | 1149 |
|
1126 | | - desktop_content = ("[Desktop Entry]" |
1127 | | - "\nType=Application" |
1128 | | - f"\nName={app_name}" |
1129 | | - f"\nExec={exec_path} --blacklist '{os.path.dirname(exec_path)}/blacklist.txt'" |
1130 | | - "\nHidden=false" |
1131 | | - "\nNoDisplay=false" |
1132 | | - "\nX-GNOME-Autostart-enabled=true") |
| 1150 | + subprocess.run( |
| 1151 | + ["systemctl", "--user", "daemon-reload"], check=True) |
1133 | 1152 |
|
1134 | | - with open(desktop_file, "w", encoding="utf-8") as f: |
1135 | | - f.write(desktop_content) |
| 1153 | + subprocess.run( |
| 1154 | + ["systemctl", "--user", "enable", service_name], check=True) |
| 1155 | + subprocess.run(["systemctl", "--user", "start", |
| 1156 | + service_name], check=True) |
1136 | 1157 |
|
1137 | 1158 | print( |
1138 | | - f"\033[92m[INFO]:\033[97m Added to autostart: {exec_path}") |
| 1159 | + f"\033[92m[INFO]:\033[97m Service installed and started: {service_name}") |
| 1160 | + print("\033[93m[NOTE]:\033[97m Service will auto-start on login") |
1139 | 1161 |
|
| 1162 | + except subprocess.CalledProcessError as e: |
| 1163 | + print(f"\033[91m[ERROR]: Systemd command failed: {e}\033[0m") |
1140 | 1164 | except Exception as e: |
1141 | 1165 | print( |
1142 | 1166 | f"\033[91m[ERROR]: Autostart operation failed: {e}\033[0m") |
1143 | 1167 |
|
1144 | 1168 | elif action == "uninstall": |
1145 | | - autostart_dir = Path.home() / ".config" / "autostart" |
1146 | | - desktop_file = autostart_dir / f"{app_name}.desktop" |
| 1169 | + try: |
| 1170 | + subprocess.run(["systemctl", "--user", "stop", service_name], |
| 1171 | + capture_output=True, check=True) |
| 1172 | + subprocess.run(["systemctl", "--user", "disable", service_name], |
| 1173 | + capture_output=True, check=True) |
1147 | 1174 |
|
1148 | | - if desktop_file.exists(): |
1149 | | - try: |
1150 | | - desktop_file.unlink() |
1151 | | - print("\033[92m[INFO]:\033[97m Removed from autostart") |
1152 | | - except Exception as e: |
1153 | | - print( |
1154 | | - f"\033[91m[ERROR]: Autostart operation failed: {e}\033[0m") |
| 1175 | + if service_file.exists(): |
| 1176 | + service_file.unlink() |
| 1177 | + |
| 1178 | + subprocess.run( |
| 1179 | + ["systemctl", "--user", "daemon-reload"], check=True) |
| 1180 | + |
| 1181 | + print("\033[92m[INFO]:\033[97m Service removed from autostart") |
| 1182 | + |
| 1183 | + except subprocess.CalledProcessError as e: |
| 1184 | + print(f"\033[91m[ERROR]: Systemd command failed: {e}\033[0m") |
| 1185 | + except Exception as e: |
| 1186 | + print( |
| 1187 | + f"\033[91m[ERROR]: Autostart operation failed: {e}\033[0m") |
1155 | 1188 |
|
1156 | 1189 |
|
1157 | 1190 | class ProxyApplication: |
|
0 commit comments