diff --git a/core/cli.py b/core/cli.py index 2943c75..82f98ba 100644 --- a/core/cli.py +++ b/core/cli.py @@ -1,9 +1,11 @@ #!/usr/bin/env python3 import os -import subprocess +import io import click +import subprocess from enum import StrEnum +from contextlib import redirect_stdout import traffic @@ -15,6 +17,7 @@ class Command(StrEnum): INSTALL_HYSTERIA2 = os.path.join(SCRIPT_DIR,'hysteria2' ,'install.sh') UNINSTALL_HYSTERIA2 = os.path.join(SCRIPT_DIR,'hysteria2', 'uninstall.sh') UPDATE_HYSTERIA2 = os.path.join(SCRIPT_DIR,'hysteria2', 'update.sh') + RESTART_HYSTERIA2 = os.path.join(SCRIPT_DIR,'hysteria2','restart.sh') CHANGE_PORT_HYSTERIA2 = os.path.join(SCRIPT_DIR,'hysteria2' ,'change_port.sh') ADD_USER = os.path.join(SCRIPT_DIR,'hysteria2' ,'add_user.sh') EDIT_USER = os.path.join(SCRIPT_DIR,'hysteria2' ,'edit_user.sh') @@ -66,6 +69,16 @@ def uninstall_hysteria2(): def update_hysteria2(): run_cmd(['bash', Command.UPDATE_HYSTERIA2]) +@cli.command('restart-hysteria2') +def restart_hysteria2(): + # save traffic status before restarting hysteria2 + # ignore the traffic_status prints + f = io.StringIO() + with redirect_stdout(f): + traffic.traffic_status() + run_cmd(['bash', Command.RESTART_HYSTERIA2]) + + @cli.command('change-hysteria2-port') @click.option('--port','-p', required=True, help='New port for Hysteria2',type=int) def change_hysteria2_port(port:int): diff --git a/core/scripts/hysteria2/restart.sh b/core/scripts/hysteria2/restart.sh new file mode 100644 index 0000000..315a734 --- /dev/null +++ b/core/scripts/hysteria2/restart.sh @@ -0,0 +1 @@ +systemctl restart hysteria-server.service \ No newline at end of file diff --git a/core/scripts/hysteria2/update.sh b/core/scripts/hysteria2/update.sh new file mode 100644 index 0000000..606edc2 --- /dev/null +++ b/core/scripts/hysteria2/update.sh @@ -0,0 +1,42 @@ +echo "Starting the update process for Hysteria2..." +echo "Backing up the current configuration..." +cp /etc/hysteria/config.json /etc/hysteria/config_backup.json +if [ $? -ne 0 ]; then + echo "${red}Error:${NC} Failed to back up configuration. Aborting update." + return 1 +fi + +echo "Downloading and installing the latest version of Hysteria2..." +bash <(curl -fsSL https://get.hy2.sh/) >/dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "${red}Error:${NC} Failed to download or install the latest version. Restoring backup configuration." + mv /etc/hysteria/config_backup.json /etc/hysteria/config.json + restart_hysteria_service >/dev/null 2>&1 + return 1 +fi + +echo "Restoring configuration from backup..." +mv /etc/hysteria/config_backup.json /etc/hysteria/config.json +if [ $? -ne 0 ]; then + echo "${red}Error:${NC} Failed to restore configuration from backup." + return 1 +fi + +echo "Modifying systemd service to use config.json..." +sed -i 's|/etc/hysteria/config.yaml|/etc/hysteria/config.json|' /etc/systemd/system/hysteria-server.service +if [ $? -ne 0 ]; then + echo "${red}Error:${NC} Failed to modify systemd service." + return 1 +fi + +rm /etc/hysteria/config.yaml +systemctl daemon-reload >/dev/null 2>&1 +restart_hysteria_service >/dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "${red}Error:${NC} Failed to restart Hysteria2 service." + return 1 +fi + +echo "Hysteria2 has been successfully updated." +echo "" +return 0 \ No newline at end of file diff --git a/core/scripts/warp/uninstall.sh b/core/scripts/warp/uninstall.sh index e69de29..f8d8cd0 100644 --- a/core/scripts/warp/uninstall.sh +++ b/core/scripts/warp/uninstall.sh @@ -0,0 +1,30 @@ +if systemctl is-active --quiet wg-quick@wgcf.service; then + echo "Uninstalling WARP..." + bash <(curl -fsSL git.io/warp.sh) dwg + + if [ -f "/etc/hysteria/config.json" ]; then + default_config='["reject(geosite:ir)", "reject(geoip:ir)", "reject(geosite:category-ads-all)", "reject(geoip:private)", "reject(geosite:google@ads)"]' + + jq --argjson default_config "$default_config" ' + .acl.inline |= map( + if . == "warps(all)" or . == "warps(geoip:google)" or . == "warps(geosite:google)" or . == "warps(geosite:netflix)" or . == "warps(geosite:spotify)" or . == "warps(geosite:openai)" or . == "warps(geoip:openai)" then + "direct" + elif . == "warps(geosite:ir)" then + "reject(geosite:ir)" + elif . == "warps(geoip:ir)" then + "reject(geoip:ir)" + else + . + end + ) | .acl.inline |= ($default_config + (. - $default_config | map(select(. != "direct")))) + ' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json + jq 'del(.outbounds[] | select(.name == "warps" and .type == "direct" and .direct.mode == 4 and .direct.bindDevice == "wgcf"))' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json + + restart_hysteria_service >/dev/null 2>&1 + echo "WARP uninstalled and configurations reset to default." + else + echo "${red}Error:${NC} Config file /etc/hysteria/config.json not found." + fi +else + echo "WARP is not active. Skipping uninstallation." +fi \ No newline at end of file diff --git a/menu.sh b/menu.sh index dad8118..2618215 100644 --- a/menu.sh +++ b/menu.sh @@ -241,35 +241,6 @@ modify_users() { python3 "$modify_script" } -# TODO: remove -# Function to uninstall Hysteria2 -uninstall_hysteria() { - echo "Uninstalling Hysteria2..." - sleep 1 - echo "Running uninstallation script..." - bash <(curl -fsSL https://get.hy2.sh/) --remove >/dev/null 2>&1 - sleep 1 - echo "Removing Hysteria folder..." - rm -rf /etc/hysteria >/dev/null 2>&1 - sleep 1 - echo "Deleting hysteria user..." - userdel -r hysteria >/dev/null 2>&1 - sleep 1 - echo "Removing systemd service files..." - rm -f /etc/systemd/system/multi-user.target.wants/hysteria-server.service >/dev/null 2>&1 - rm -f /etc/systemd/system/multi-user.target.wants/hysteria-server@*.service >/dev/null 2>&1 - sleep 1 - echo "Reloading systemd daemon..." - systemctl daemon-reload >/dev/null 2>&1 - sleep 1 - echo "Removing cron jobs..." - (crontab -l | grep -v "python3 /etc/hysteria/traffic.py" | crontab -) >/dev/null 2>&1 - (crontab -l | grep -v "/etc/hysteria/users/kick.sh" | crontab -) >/dev/null 2>&1 - sleep 1 - echo "Hysteria2 uninstalled!" - echo "" -} - # Function to install WARP and update config.json install_warp() { # Check if wg-quick@wgcf.service is active @@ -292,39 +263,6 @@ install_warp() { fi } -# Function to uninstall WARP and update config.json -uninstall_warp() { - if systemctl is-active --quiet wg-quick@wgcf.service; then - echo "Uninstalling WARP..." - bash <(curl -fsSL git.io/warp.sh) dwg - - if [ -f "/etc/hysteria/config.json" ]; then - default_config='["reject(geosite:ir)", "reject(geoip:ir)", "reject(geosite:category-ads-all)", "reject(geoip:private)", "reject(geosite:google@ads)"]' - - jq --argjson default_config "$default_config" ' - .acl.inline |= map( - if . == "warps(all)" or . == "warps(geoip:google)" or . == "warps(geosite:google)" or . == "warps(geosite:netflix)" or . == "warps(geosite:spotify)" or . == "warps(geosite:openai)" or . == "warps(geoip:openai)" then - "direct" - elif . == "warps(geosite:ir)" then - "reject(geosite:ir)" - elif . == "warps(geoip:ir)" then - "reject(geoip:ir)" - else - . - end - ) | .acl.inline |= ($default_config + (. - $default_config | map(select(. != "direct")))) - ' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json - jq 'del(.outbounds[] | select(.name == "warps" and .type == "direct" and .direct.mode == 4 and .direct.bindDevice == "wgcf"))' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json - - restart_hysteria_service >/dev/null 2>&1 - echo "WARP uninstalled and configurations reset to default." - else - echo "${red}Error:${NC} Config file /etc/hysteria/config.json not found." - fi - else - echo "WARP is not active. Skipping uninstallation." - fi -} # Function to configure WARP configure_warp() {