Skip to content

Commit 94f4342

Browse files
committed
Autostart in Linux switch to systemd
1 parent d36e3ee commit 94f4342

1 file changed

Lines changed: 56 additions & 23 deletions

File tree

src/main.py

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import os
1414
import random
1515
import ssl
16+
import subprocess
1617
import sys
1718
import textwrap
1819
import time
@@ -1111,47 +1112,79 @@ class LinuxAutostartManager(IAutostartManager):
11111112

11121113
@staticmethod
11131114
def manage_autostart(action: str = "install") -> None:
1114-
"""Manages Linux autostart"""
1115+
"""Manages Linux autostart using systemd user services"""
11151116

11161117
app_name = "NoDPIProxy"
11171118
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"
11181125

11191126
if action == "install":
11201127
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+
"""
11231146

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)
11251149

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)
11331152

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)
11361157

11371158
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")
11391161

1162+
except subprocess.CalledProcessError as e:
1163+
print(f"\033[91m[ERROR]: Systemd command failed: {e}\033[0m")
11401164
except Exception as e:
11411165
print(
11421166
f"\033[91m[ERROR]: Autostart operation failed: {e}\033[0m")
11431167

11441168
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)
11471174

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")
11551188

11561189

11571190
class ProxyApplication:

0 commit comments

Comments
 (0)