#!/bin/bash # lib/service.sh - Service control functions (start, stop, restart, status, enable, disable) # Include guard [[ -n "${__X_UI_SERVICE_INCLUDED:-}" ]] && return 0 __X_UI_SERVICE_INCLUDED=1 # Source dependencies source "${LIB_DIR}/common.sh" # 0: running, 1: not running, 2: not installed check_status() { if [[ $release == "alpine" ]]; then if [[ ! -f /etc/init.d/x-ui ]]; then return 2 fi if [[ $(rc-service x-ui status | grep -F 'status: started' -c) == 1 ]]; then return 0 else return 1 fi else if [[ ! -f ${xui_service}/x-ui.service ]]; then return 2 fi temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1) if [[ "${temp}" == "running" ]]; then return 0 else return 1 fi fi } check_enabled() { if [[ $release == "alpine" ]]; then if [[ $(rc-update show | grep -F 'x-ui' | grep default -c) == 1 ]]; then return 0 else return 1 fi else temp=$(systemctl is-enabled x-ui) if [[ "${temp}" == "enabled" ]]; then return 0 else return 1 fi fi } check_uninstall() { check_status if [[ $? != 2 ]]; then echo "" LOGE "Panel installed, Please do not reinstall" if [[ $# == 0 ]]; then before_show_menu fi return 1 else return 0 fi } check_install() { check_status if [[ $? == 2 ]]; then echo "" LOGE "Please install the panel first" if [[ $# == 0 ]]; then before_show_menu fi return 1 else return 0 fi } show_status() { check_status case $? in 0) echo -e "Panel state: ${green}Running${plain}" show_enable_status ;; 1) echo -e "Panel state: ${yellow}Not Running${plain}" show_enable_status ;; 2) echo -e "Panel state: ${red}Not Installed${plain}" ;; esac show_xray_status } show_enable_status() { check_enabled if [[ $? == 0 ]]; then echo -e "Start automatically: ${green}Yes${plain}" else echo -e "Start automatically: ${red}No${plain}" fi } check_xray_status() { count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l) if [[ count -ne 0 ]]; then return 0 else return 1 fi } show_xray_status() { check_xray_status if [[ $? == 0 ]]; then echo -e "xray state: ${green}Running${plain}" else echo -e "xray state: ${red}Not Running${plain}" fi } start() { check_status if [[ $? == 0 ]]; then echo "" LOGI "Panel is running, No need to start again, If you need to restart, please select restart" else if [[ $release == "alpine" ]]; then rc-service x-ui start else systemctl start x-ui fi sleep 2 check_status if [[ $? == 0 ]]; then LOGI "x-ui Started Successfully" else LOGE "panel Failed to start, Probably because it takes longer than two seconds to start, Please check the log information later" fi fi if [[ $# == 0 ]]; then before_show_menu fi } stop() { check_status if [[ $? == 1 ]]; then echo "" LOGI "Panel stopped, No need to stop again!" else if [[ $release == "alpine" ]]; then rc-service x-ui stop else systemctl stop x-ui fi sleep 2 check_status if [[ $? == 1 ]]; then LOGI "x-ui and xray stopped successfully" else LOGE "Panel stop failed, Probably because the stop time exceeds two seconds, Please check the log information later" fi fi if [[ $# == 0 ]]; then before_show_menu fi } restart() { if [[ $release == "alpine" ]]; then rc-service x-ui restart else systemctl restart x-ui fi sleep 2 check_status if [[ $? == 0 ]]; then LOGI "x-ui and xray Restarted successfully" else LOGE "Panel restart failed, Probably because it takes longer than two seconds to start, Please check the log information later" fi if [[ $# == 0 ]]; then before_show_menu fi } status() { if [[ $release == "alpine" ]]; then rc-service x-ui status else systemctl status x-ui -l fi if [[ $# == 0 ]]; then before_show_menu fi } enable() { if [[ $release == "alpine" ]]; then rc-update add x-ui else systemctl enable x-ui fi if [[ $? == 0 ]]; then LOGI "x-ui Set to boot automatically on startup successfully" else LOGE "x-ui Failed to set Autostart" fi if [[ $# == 0 ]]; then before_show_menu fi } disable() { if [[ $release == "alpine" ]]; then rc-update del x-ui else systemctl disable x-ui fi if [[ $? == 0 ]]; then LOGI "x-ui Autostart Cancelled successfully" else LOGE "x-ui Failed to cancel autostart" fi if [[ $# == 0 ]]; then before_show_menu fi } show_log() { if [[ $release == "alpine" ]]; then echo -e "${green}\t1.${plain} Debug Log" echo -e "${green}\t0.${plain} Back to Main Menu" read -rp "Choose an option: " choice case "$choice" in 0) show_menu ;; 1) grep -F 'x-ui[' /var/log/messages if [[ $# == 0 ]]; then before_show_menu fi ;; *) echo -e "${red}Invalid option. Please select a valid number.${plain}\n" show_log ;; esac else echo -e "${green}\t1.${plain} Debug Log" echo -e "${green}\t2.${plain} Clear All logs" echo -e "${green}\t0.${plain} Back to Main Menu" read -rp "Choose an option: " choice case "$choice" in 0) show_menu ;; 1) journalctl -u x-ui -e --no-pager -f -p debug if [[ $# == 0 ]]; then before_show_menu fi ;; 2) sudo journalctl --rotate sudo journalctl --vacuum-time=1s echo "All Logs cleared." restart ;; *) echo -e "${red}Invalid option. Please select a valid number.${plain}\n" show_log ;; esac fi }