From 1781ca446d505980e4c8b082b4386cc8ae8f5cce Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Thu, 29 May 2025 23:21:56 +0330 Subject: [PATCH] feat: improve edit_subpath function with better error handling and efficient Caddy reload - Add service status validation before editing subpath - Use systemctl reload for Caddy config changes - Add fallback to restart if reload fails - Enhance user feedback with old/new subpath comparison - Improve error handling and status verification --- .vscode/settings.json | 5 +++++ core/scripts/normalsub/normalsub.sh | 26 ++++++++++++++++++++------ menu.sh | 5 ++--- 3 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a490b61 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "githubPullRequests.ignoredPullRequestBranches": [ + "main" + ] +} \ No newline at end of file diff --git a/core/scripts/normalsub/normalsub.sh b/core/scripts/normalsub/normalsub.sh index b21f89b..acb276d 100644 --- a/core/scripts/normalsub/normalsub.sh +++ b/core/scripts/normalsub/normalsub.sh @@ -193,25 +193,39 @@ edit_subpath() { exit 1 fi + if ! systemctl is-active --quiet hysteria-normal-sub.service || ! systemctl is-active --quiet hysteria-caddy-normalsub.service; then + echo -e "${red}Error: One or more services are not running. Please start the services first.${NC}" + exit 1 + fi + source "$NORMALSUB_ENV_FILE" local old_subpath="$SUBPATH" sed -i "s|^SUBPATH=.*|SUBPATH=$new_path|" "$NORMALSUB_ENV_FILE" - echo -e "${green}SUBPATH updated to $new_path in $NORMALSUB_ENV_FILE.${NC}" + echo -e "${green}SUBPATH updated from '$old_subpath' to '$new_path' in $NORMALSUB_ENV_FILE.${NC}" update_caddy_file_normalsub "$HYSTERIA_DOMAIN" "$HYSTERIA_PORT" "$new_path" "$AIOHTTP_LISTEN_ADDRESS" "$AIOHTTP_LISTEN_PORT" - echo -e "${green}Caddyfile for Normalsub updated.${NC}" + echo -e "${green}Caddyfile for Normalsub updated with new subpath.${NC}" - echo -e "${yellow}Restarting hysteria-normal-sub service...${NC}" + echo -e "${yellow}Restarting hysteria-normal-sub service to reload environment...${NC}" systemctl restart hysteria-normal-sub.service - echo -e "${yellow}Reloading hysteria-caddy-normalsub service...${NC}" - systemctl restart hysteria-caddy-normalsub.service + + echo -e "${yellow}Reloading Caddy configuration...${NC}" + if systemctl reload hysteria-caddy-normalsub.service 2>/dev/null; then + echo -e "${green}Caddy configuration reloaded successfully.${NC}" + else + echo -e "${yellow}Reload failed, restarting Caddy service...${NC}" + systemctl restart hysteria-caddy-normalsub.service + fi if systemctl is-active --quiet hysteria-normal-sub.service && systemctl is-active --quiet hysteria-caddy-normalsub.service; then - echo -e "${green}Services restarted/reloaded successfully.${NC}" + echo -e "${green}Services updated successfully.${NC}" echo -e "${green}New access base URL: https://$HYSTERIA_DOMAIN:$HYSTERIA_PORT/$new_path/sub/normal/{username}${NC}" + echo -e "${cyan}Old subpath '$old_subpath' is no longer accessible.${NC}" else echo -e "${red}Error: One or more services failed to restart/reload. Please check logs.${NC}" + systemctl status hysteria-normal-sub.service --no-pager + systemctl status hysteria-caddy-normalsub.service --no-pager fi } diff --git a/menu.sh b/menu.sh index edeee18..9c13d39 100644 --- a/menu.sh +++ b/menu.sh @@ -619,9 +619,8 @@ normalsub_handler() { elif ! [[ "$subpath" =~ [A-Z] ]] || ! [[ "$subpath" =~ [a-z] ]] || ! [[ "$subpath" =~ [0-9] ]]; then echo "Error: SUBPATH must include at least one uppercase letter, one lowercase letter, and one number." else - sed -i "s|^SUBPATH=.*|SUBPATH=${subpath}|" "$NORMALSUB_ENV" - systemctl restart hysteria-normal-sub.service - echo "SUBPATH updated successfully!" + python3 $CLI_PATH normal-sub -a edit_subpath -sp "$subpath" + # echo "SUBPATH updated successfully!" break fi done