Browse Source

fix(scripts): make x-ui.sh and update.sh PostgreSQL-aware

update.sh ran setting -show and migrate without sourcing the env file, so
PostgreSQL users had migrations applied to the SQLite default and settings
introspection read the wrong DB. Sourcing the per-distro env file at the
start of update_x-ui exports XUI_DB_TYPE/XUI_DB_DSN to all binary calls.

x-ui.sh now shows the active backend in View Current Settings (password
masked) and removes the env file on uninstall so a later reinstall doesn't
inherit a stale DSN.
MHSanaei 1 day ago
parent
commit
6951198aae
2 changed files with 54 additions and 0 deletions
  1. 27 0
      update.sh
  2. 27 0
      x-ui.sh

+ 27 - 0
update.sh

@@ -105,6 +105,31 @@ gen_random_string() {
         | head -c "$length"
         | head -c "$length"
 }
 }
 
 
+xui_env_file_path() {
+    case "${release}" in
+        ubuntu | debian | armbian)
+            echo "/etc/default/x-ui"
+            ;;
+        arch | manjaro | parch | alpine)
+            echo "/etc/conf.d/x-ui"
+            ;;
+        *)
+            echo "/etc/sysconfig/x-ui"
+            ;;
+    esac
+}
+
+load_xui_env() {
+    local env_file
+    env_file="$(xui_env_file_path)"
+    if [[ -r "$env_file" ]]; then
+        set -a
+        # shellcheck disable=SC1090
+        source "$env_file"
+        set +a
+    fi
+}
+
 install_base() {
 install_base() {
     echo -e "${green}Updating and install dependency packages...${plain}"
     echo -e "${green}Updating and install dependency packages...${plain}"
     case "${release}" in
     case "${release}" in
@@ -775,6 +800,8 @@ config_after_update() {
 update_x-ui() {
 update_x-ui() {
     cd ${xui_folder%/x-ui}/
     cd ${xui_folder%/x-ui}/
 
 
+    load_xui_env
+
     if [ -f "${xui_folder}/x-ui" ]; then
     if [ -f "${xui_folder}/x-ui" ]; then
         current_xui_version=$(${xui_folder}/x-ui -v)
         current_xui_version=$(${xui_folder}/x-ui -v)
         echo -e "${green}Current x-ui version: ${current_xui_version}${plain}"
         echo -e "${green}Current x-ui version: ${current_xui_version}${plain}"

+ 27 - 0
x-ui.sh

@@ -179,6 +179,20 @@ delete_script() {
     exit 1
     exit 1
 }
 }
 
 
+xui_env_file_path() {
+    case "${release}" in
+        ubuntu | debian | armbian)
+            echo "/etc/default/x-ui"
+            ;;
+        arch | manjaro | parch | alpine)
+            echo "/etc/conf.d/x-ui"
+            ;;
+        *)
+            echo "/etc/sysconfig/x-ui"
+            ;;
+    esac
+}
+
 uninstall() {
 uninstall() {
     confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
     confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
     if [[ $? != 0 ]]; then
     if [[ $? != 0 ]]; then
@@ -202,6 +216,7 @@ uninstall() {
 
 
     rm /etc/x-ui/ -rf
     rm /etc/x-ui/ -rf
     rm ${xui_folder}/ -rf
     rm ${xui_folder}/ -rf
+    rm -f "$(xui_env_file_path)"
 
 
     echo ""
     echo ""
     echo -e "Uninstalled Successfully.\n"
     echo -e "Uninstalled Successfully.\n"
@@ -289,6 +304,18 @@ check_config() {
     fi
     fi
     LOGI "${info}"
     LOGI "${info}"
 
 
+    local db_env_file
+    db_env_file="$(xui_env_file_path)"
+    if [[ -r "$db_env_file" ]] && grep -q '^XUI_DB_TYPE=postgres' "$db_env_file"; then
+        local dsn
+        dsn="$(grep -E '^XUI_DB_DSN=' "$db_env_file" | head -1 | cut -d= -f2-)"
+        local dsn_safe
+        dsn_safe="$(echo "$dsn" | sed -E 's|(://[^:/@]+:)[^@]+@|\1****@|')"
+        echo -e "${green}Database: PostgreSQL — ${dsn_safe}${plain}"
+    else
+        echo -e "${green}Database: SQLite (/etc/x-ui/x-ui.db)${plain}"
+    fi
+
     local existing_webBasePath=$(echo "$info" | grep -Eo 'webBasePath: .+' | awk '{print $2}')
     local existing_webBasePath=$(echo "$info" | grep -Eo 'webBasePath: .+' | awk '{print $2}')
     local existing_port=$(echo "$info" | grep -Eo 'port: .+' | awk '{print $2}')
     local existing_port=$(echo "$info" | grep -Eo 'port: .+' | awk '{print $2}')
     local existing_cert=$(${xui_folder}/x-ui setting -getCert true | grep 'cert:' | awk -F': ' '{print $2}' | tr -d '[:space:]')
     local existing_cert=$(${xui_folder}/x-ui setting -getCert true | grep 'cert:' | awk -F': ' '{print $2}' | tr -d '[:space:]')