#!/bin/bash # lib/settings.sh - Panel settings management # Include guard [[ -n "${__X_UI_SETTINGS_INCLUDED:-}" ]] && return 0 __X_UI_SETTINGS_INCLUDED=1 # Source dependencies source "${LIB_DIR}/common.sh" source "${LIB_DIR}/service.sh" source "${LIB_DIR}/ssl.sh" reset_user() { confirm "Are you sure to reset the username and password of the panel?" "n" if [[ $? != 0 ]]; then if [[ $# == 0 ]]; then show_menu fi return 0 fi read -rp "Please set the login username [default is a random username]: " config_account [[ -z $config_account ]] && config_account=$(gen_random_string 10) read -rp "Please set the login password [default is a random password]: " config_password [[ -z $config_password ]] && config_password=$(gen_random_string 18) read -rp "Do you want to disable currently configured two-factor authentication? (y/n): " twoFactorConfirm if [[ $twoFactorConfirm != "y" && $twoFactorConfirm != "Y" ]]; then ${xui_folder}/x-ui setting -username ${config_account} -password ${config_password} -resetTwoFactor false >/dev/null 2>&1 else ${xui_folder}/x-ui setting -username ${config_account} -password ${config_password} -resetTwoFactor true >/dev/null 2>&1 echo -e "Two factor authentication has been disabled." fi echo -e "Panel login username has been reset to: ${green} ${config_account} ${plain}" echo -e "Panel login password has been reset to: ${green} ${config_password} ${plain}" echo -e "${green} Please use the new login username and password to access the X-UI panel. Also remember them! ${plain}" confirm_restart } reset_webbasepath() { echo -e "${yellow}Resetting Web Base Path${plain}" read -rp "Are you sure you want to reset the web base path? (y/n): " confirm if [[ $confirm != "y" && $confirm != "Y" ]]; then echo -e "${yellow}Operation canceled.${plain}" return fi config_webBasePath=$(gen_random_string 18) # Apply the new web base path setting ${xui_folder}/x-ui setting -webBasePath "${config_webBasePath}" >/dev/null 2>&1 echo -e "Web base path has been reset to: ${green}${config_webBasePath}${plain}" echo -e "${green}Please use the new web base path to access the panel.${plain}" restart } reset_config() { confirm "Are you sure you want to reset all panel settings, Account data will not be lost, Username and password will not change" "n" if [[ $? != 0 ]]; then if [[ $# == 0 ]]; then show_menu fi return 0 fi ${xui_folder}/x-ui setting -reset echo -e "All panel settings have been reset to default." restart } check_config() { local info=$(${xui_folder}/x-ui setting -show true) if [[ $? != 0 ]]; then LOGE "get current settings error, please check logs" show_menu return fi LOGI "${info}" local existing_webBasePath=$(echo "$info" | grep -Eo 'webBasePath: .+' | 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 server_ip=$(curl -s --max-time 3 https://api.ipify.org) if [ -z "$server_ip" ]; then server_ip=$(curl -s --max-time 3 https://4.ident.me) fi if [[ -n "$existing_cert" ]]; then local domain=$(basename "$(dirname "$existing_cert")") if [[ "$domain" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then echo -e "${green}Access URL: https://${domain}:${existing_port}${existing_webBasePath}${plain}" else echo -e "${green}Access URL: https://${server_ip}:${existing_port}${existing_webBasePath}${plain}" fi else echo -e "${red}⚠ WARNING: No SSL certificate configured!${plain}" echo -e "${yellow}You can get a Let's Encrypt certificate for your IP address (valid ~6 days, auto-renews).${plain}" read -rp "Generate SSL certificate for IP now? [y/N]: " gen_ssl if [[ "$gen_ssl" == "y" || "$gen_ssl" == "Y" ]]; then stop >/dev/null 2>&1 ssl_cert_issue_for_ip if [[ $? -eq 0 ]]; then echo -e "${green}Access URL: https://${server_ip}:${existing_port}${existing_webBasePath}${plain}" # ssl_cert_issue_for_ip already restarts the panel, but ensure it's running start >/dev/null 2>&1 else LOGE "IP certificate setup failed." echo -e "${yellow}You can try again via option 18 (SSL Certificate Management).${plain}" start >/dev/null 2>&1 fi else echo -e "${yellow}Access URL: http://${server_ip}:${existing_port}${existing_webBasePath}${plain}" echo -e "${yellow}For security, please configure SSL certificate using option 18 (SSL Certificate Management)${plain}" fi fi } set_port() { echo -n "Enter port number[1-65535]: " read -r port if [[ -z "${port}" ]]; then LOGD "Cancelled" before_show_menu else ${xui_folder}/x-ui setting -port ${port} echo -e "The port is set, Please restart the panel now, and use the new port ${green}${port}${plain} to access web panel" confirm_restart fi }