Refactor: Implement Hysteria2 uninstallation in Python

This commit is contained in:
Whispering Wind
2025-05-02 14:37:47 +03:30
committed by GitHub
parent 1e4f6439d3
commit beb8748f4b
3 changed files with 96 additions and 58 deletions

View File

@ -17,7 +17,7 @@ WEBPANEL_ENV_FILE = '/etc/hysteria/core/scripts/webpanel/.env'
class Command(Enum):
'''Contains path to command's script'''
INSTALL_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'install.sh')
UNINSTALL_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'uninstall.sh')
UNINSTALL_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'uninstall.py')
UPDATE_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'update.py')
RESTART_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'restart.py')
CHANGE_PORT_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'change_port.py')
@ -132,7 +132,7 @@ def install_hysteria2(port: int, sni: str):
def uninstall_hysteria2():
'''Uninstalls Hysteria2.'''
run_cmd(['bash', Command.UNINSTALL_HYSTERIA2.value])
run_cmd(['python3', Command.UNINSTALL_HYSTERIA2.value])
def update_hysteria2():

View File

@ -0,0 +1,94 @@
#!/usr/bin/env python3
import os
import subprocess
import sys
SERVICES = [
"hysteria-server.service",
"hysteria-webpanel.service",
"hysteria-caddy.service",
"hysteria-telegram-bot.service",
"hysteria-normal-sub.service",
"hysteria-singbox.service",
"hysteria-ip-limit.service",
]
def run_command(command, error_message):
"""Runs a command and prints an error message if it fails."""
try:
subprocess.run(command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return 0
except subprocess.CalledProcessError:
print(error_message)
return 1
except FileNotFoundError:
print(f"Error: Command not found: {command[0]}")
return 1
def uninstall_hysteria():
"""Uninstalls Hysteria2."""
print("Uninstalling Hysteria2...")
print("Running uninstallation script...")
run_command(["bash", "-c", "curl -fsSL https://get.hy2.sh/ | bash -- --remove"], "Error running the official uninstallation script.")
print("Removing WARP")
cli_path = "/etc/hysteria/core/cli.py"
if os.path.exists(cli_path):
run_command([sys.executable, cli_path, "uninstall-warp"], "Error during WARP removal.")
else:
print("Skipping WARP removal (CLI path not found)")
print("Removing Hysteria folder...")
run_command(["rm", "-rf", "/etc/hysteria"], "Error removing the Hysteria folder.")
print("Deleting hysteria user...")
run_command(["userdel", "-r", "hysteria"], "Error deleting the hysteria user.")
print("Stop/Disabling Hysteria Services...")
for service in SERVICES + ["hysteria-server@*.service"]:
print(f"Stopping and disabling {service}...")
run_command(["systemctl", "stop", service], f"Error stopping {service}.")
run_command(["systemctl", "disable", service], f"Error disabling {service}.")
print("Removing systemd service files...")
for service in SERVICES + ["hysteria-server@*.service"]:
print(f"Removing service file: {service}")
run_command(["rm", "-f", f"/etc/systemd/system/{service}", f"/etc/systemd/system/multi-user.target.wants/{service}"], f"Error removing service files for {service}.")
print("Reloading systemd daemon...")
run_command(["systemctl", "daemon-reload"], "Error reloading systemd daemon.")
print("Removing cron jobs...")
try:
crontab_list = subprocess.run(["crontab", "-l"], capture_output=True, text=True, check=False)
if "hysteria" in crontab_list.stdout:
new_crontab = "\n".join(line for line in crontab_list.stdout.splitlines() if "hysteria" not in line)
process = subprocess.run(["crontab", "-"], input=new_crontab.encode(), check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except FileNotFoundError:
print("Warning: crontab command not found.")
except subprocess.CalledProcessError:
print("Warning: Could not access crontab.")
print("Removing alias 'hys2' from .bashrc...")
bashrc_path = os.path.expanduser("~/.bashrc")
if os.path.exists(bashrc_path):
try:
with open(bashrc_path, 'r') as f:
lines = f.readlines()
with open(bashrc_path, 'w') as f:
for line in lines:
if 'alias hys2=' not in line:
f.write(line)
except IOError:
print(f"Warning: Could not access or modify {bashrc_path}.")
else:
print(f"Warning: {bashrc_path} not found.")
print("Hysteria2 uninstalled!")
print("Rebooting server...")
run_command(["reboot"], "Error initiating reboot.")
if __name__ == "__main__":
uninstall_hysteria()

View File

@ -1,56 +0,0 @@
source /etc/hysteria/core/scripts/path.sh || true
echo "Uninstalling Hysteria2..."
SERVICES=(
"hysteria-server.service"
"hysteria-webpanel.service"
"hysteria-caddy.service"
"hysteria-telegram-bot.service"
"hysteria-normal-sub.service"
"hysteria-singbox.service"
"hysteria-ip-limit.service"
)
echo "Running uninstallation script..."
bash <(curl -fsSL https://get.hy2.sh/) --remove >/dev/null 2>&1
echo "Removing WARP"
if command -v python3 &> /dev/null && [ -f "$CLI_PATH" ]; then
python3 "$CLI_PATH" uninstall-warp || true
else
echo "Skipping WARP removal (python3 or CLI_PATH not found)"
fi
echo "Removing Hysteria folder..."
rm -rf /etc/hysteria >/dev/null 2>&1
echo "Deleting hysteria user..."
userdel -r hysteria >/dev/null 2>&1 || true
echo "Stop/Disabling Hysteria Services..."
for service in "${SERVICES[@]}" "hysteria-server@*.service"; do
echo "Stopping and disabling $service..."
systemctl stop "$service" > /dev/null 2>&1 || true
systemctl disable "$service" > /dev/null 2>&1 || true
done
echo "Removing systemd service files..."
for service in "${SERVICES[@]}" "hysteria-server@*.service"; do
echo "Removing service file: $service"
rm -f "/etc/systemd/system/$service" "/etc/systemd/system/multi-user.target.wants/$service" >/dev/null 2>&1
done
echo "Reloading systemd daemon..."
systemctl daemon-reload >/dev/null 2>&1
echo "Removing cron jobs..."
if crontab -l 2>/dev/null | grep -q "hysteria"; then
(crontab -l | grep -v "hysteria" | crontab -) >/dev/null 2>&1
fi
echo "Removing alias 'hys2' from .bashrc..."
sed -i '/alias hys2=.*\/etc\/hysteria\/menu.sh/d' ~/.bashrc 2>/dev/null || true
echo "Hysteria2 uninstalled!"
echo ""