diff --git a/install.sh b/install.sh index fd730bca..57d7f654 100644 --- a/install.sh +++ b/install.sh @@ -8,6 +8,9 @@ plain='\033[0m' cur_dir=$(pwd) +xui_folder="${XUI_MAIN_FOLDER:=/usr/local/x-ui}" +xui_service="${XUI_SERVICE:=/etc/systemd/system}" + # check root [[ $EUID -ne 0 ]] && echo -e "${red}Fatal error: ${plain} Please run this script with root privilege \n " && exit 1 @@ -76,9 +79,9 @@ gen_random_string() { } config_after_install() { - local existing_hasDefaultCredential=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'hasDefaultCredential: .+' | awk '{print $2}') - local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}') - local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}') + local existing_hasDefaultCredential=$(${xui_folder}/x-ui setting -show true | grep -Eo 'hasDefaultCredential: .+' | awk '{print $2}') + local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}') + local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}') local URL_lists=( "https://api4.ipify.org" "https://ipv4.icanhazip.com" @@ -110,7 +113,7 @@ config_after_install() { echo -e "${yellow}Generated random port: ${config_port}${plain}" fi - /usr/local/x-ui/x-ui setting -username "${config_username}" -password "${config_password}" -port "${config_port}" -webBasePath "${config_webBasePath}" + ${xui_folder}/x-ui setting -username "${config_username}" -password "${config_password}" -port "${config_port}" -webBasePath "${config_webBasePath}" echo -e "This is a fresh installation, generating random login info for security concerns:" echo -e "###############################################" echo -e "${green}Username: ${config_username}${plain}" @@ -122,7 +125,7 @@ config_after_install() { else local config_webBasePath=$(gen_random_string 18) echo -e "${yellow}WebBasePath is missing or too short. Generating a new one...${plain}" - /usr/local/x-ui/x-ui setting -webBasePath "${config_webBasePath}" + ${xui_folder}/x-ui setting -webBasePath "${config_webBasePath}" echo -e "${green}New WebBasePath: ${config_webBasePath}${plain}" echo -e "${green}Access URL: http://${server_ip}:${existing_port}/${config_webBasePath}${plain}" fi @@ -132,7 +135,7 @@ config_after_install() { local config_password=$(gen_random_string 10) echo -e "${yellow}Default credentials detected. Security update required...${plain}" - /usr/local/x-ui/x-ui setting -username "${config_username}" -password "${config_password}" + ${xui_folder}/x-ui setting -username "${config_username}" -password "${config_password}" echo -e "Generated new random login credentials:" echo -e "###############################################" echo -e "${green}Username: ${config_username}${plain}" @@ -143,11 +146,11 @@ config_after_install() { fi fi - /usr/local/x-ui/x-ui migrate + ${xui_folder}/x-ui migrate } install_x-ui() { - cd /usr/local/ + cd ${xui_folder%/x-ui}/ # Download resources if [ $# == 0 ]; then @@ -161,7 +164,7 @@ install_x-ui() { fi fi echo -e "Got x-ui latest version: ${tag_version}, beginning the installation..." - wget --inet4-only -N -O /usr/local/x-ui-linux-$(arch).tar.gz https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz + wget --inet4-only -N -O ${xui_folder}-linux-$(arch).tar.gz https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz if [[ $? -ne 0 ]]; then echo -e "${red}Downloading x-ui failed, please be sure that your server can access GitHub ${plain}" exit 1 @@ -178,7 +181,7 @@ install_x-ui() { url="https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz" echo -e "Beginning to install x-ui $1" - wget --inet4-only -N -O /usr/local/x-ui-linux-$(arch).tar.gz ${url} + wget --inet4-only -N -O ${xui_folder}-linux-$(arch).tar.gz ${url} if [[ $? -ne 0 ]]; then echo -e "${red}Download x-ui $1 failed, please check if the version exists ${plain}" exit 1 @@ -191,13 +194,13 @@ install_x-ui() { fi # Stop x-ui service and remove old resources - if [[ -e /usr/local/x-ui/ ]]; then + if [[ -e ${xui_folder}/ ]]; then if [[ $release == "alpine" ]]; then rc-service x-ui stop else systemctl stop x-ui fi - rm /usr/local/x-ui/ -rf + rm ${xui_folder}/ -rf fi # Extract resources and set permissions @@ -230,7 +233,7 @@ install_x-ui() { rc-update add x-ui rc-service x-ui start else - cp -f x-ui.service /etc/systemd/system/ + cp -f x-ui.service ${xui_service}/ systemctl daemon-reload systemctl enable x-ui systemctl start x-ui diff --git a/update.sh b/update.sh index 66c0566c..47fdab10 100755 --- a/update.sh +++ b/update.sh @@ -6,6 +6,9 @@ blue='\033[0;34m' yellow='\033[0;33m' plain='\033[0m' +xui_folder="${XUI_MAIN_FOLDER:=/usr/local/x-ui}" +xui_service="${XUI_SERVICE:=/etc/systemd/system}" + # Don't edit this config b_source="${BASH_SOURCE[0]}" while [ -h "$b_source" ]; do @@ -103,15 +106,15 @@ install_base() { config_after_update() { echo -e "${yellow}x-ui settings:${plain}" - /usr/local/x-ui/x-ui setting -show true - /usr/local/x-ui/x-ui migrate + ${xui_folder}/x-ui setting -show true + ${xui_folder}/x-ui migrate } update_x-ui() { - cd /usr/local/ + cd ${xui_folder%/x-ui}/ - if [ -f "/usr/local/x-ui/x-ui" ]; then - current_xui_version=$(/usr/local/x-ui/x-ui -v) + if [ -f "${xui_folder}/x-ui" ]; then + current_xui_version=$(${xui_folder}/x-ui -v) echo -e "${green}Current x-ui version: ${current_xui_version}${plain}" else _fail "ERROR: Current x-ui version: unknown" @@ -128,16 +131,16 @@ update_x-ui() { fi fi echo -e "Got x-ui latest version: ${tag_version}, beginning the installation..." - ${wget_bin} -N -O /usr/local/x-ui-linux-$(arch).tar.gz https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz 2>/dev/null + ${wget_bin} -N -O ${xui_folder}-linux-$(arch).tar.gz https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz 2>/dev/null if [[ $? -ne 0 ]]; then echo -e "${yellow}Trying to fetch version with IPv4...${plain}" - ${wget_bin} --inet4-only -N -O /usr/local/x-ui-linux-$(arch).tar.gz https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz 2>/dev/null + ${wget_bin} --inet4-only -N -O ${xui_folder}-linux-$(arch).tar.gz https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz 2>/dev/null if [[ $? -ne 0 ]]; then _fail "ERROR: Failed to download x-ui, please be sure that your server can access GitHub" fi fi - if [[ -e /usr/local/x-ui/ ]]; then + if [[ -e ${xui_folder}/ ]]; then echo -e "${green}Stopping x-ui...${plain}" if [[ $release == "alpine" ]]; then if [ -f "/etc/init.d/x-ui" ]; then @@ -150,11 +153,11 @@ update_x-ui() { _fail "ERROR: x-ui service unit not installed." fi else - if [ -f "/etc/systemd/system/x-ui.service" ]; then + if [ -f "${xui_service}/x-ui.service" ]; then systemctl stop x-ui >/dev/null 2>&1 systemctl disable x-ui >/dev/null 2>&1 echo -e "${green}Removing old systemd unit version...${plain}" - rm /etc/systemd/system/x-ui.service -f >/dev/null 2>&1 + rm ${xui_service}/x-ui.service -f >/dev/null 2>&1 systemctl daemon-reload >/dev/null 2>&1 else rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1 @@ -163,14 +166,14 @@ update_x-ui() { fi echo -e "${green}Removing old x-ui version...${plain}" rm /usr/bin/x-ui -f >/dev/null 2>&1 - rm /usr/local/x-ui/x-ui.service -f >/dev/null 2>&1 - rm /usr/local/x-ui/x-ui -f >/dev/null 2>&1 - rm /usr/local/x-ui/x-ui.sh -f >/dev/null 2>&1 + rm ${xui_folder}/x-ui.service -f >/dev/null 2>&1 + rm ${xui_folder}/x-ui -f >/dev/null 2>&1 + rm ${xui_folder}/x-ui.sh -f >/dev/null 2>&1 echo -e "${green}Removing old xray version...${plain}" - rm /usr/local/x-ui/bin/xray-linux-amd64 -f >/dev/null 2>&1 + rm ${xui_folder}/bin/xray-linux-amd64 -f >/dev/null 2>&1 echo -e "${green}Removing old README and LICENSE file...${plain}" - rm /usr/local/x-ui/bin/README.md -f >/dev/null 2>&1 - rm /usr/local/x-ui/bin/LICENSE -f >/dev/null 2>&1 + rm ${xui_folder}/bin/README.md -f >/dev/null 2>&1 + rm ${xui_folder}/bin/LICENSE -f >/dev/null 2>&1 else rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1 _fail "ERROR: x-ui not installed." @@ -200,15 +203,15 @@ update_x-ui() { fi fi - chmod +x /usr/local/x-ui/x-ui.sh >/dev/null 2>&1 + chmod +x ${xui_folder}/x-ui.sh >/dev/null 2>&1 chmod +x /usr/bin/x-ui >/dev/null 2>&1 echo -e "${green}Changing owner...${plain}" - chown -R root:root /usr/local/x-ui >/dev/null 2>&1 + chown -R root:root ${xui_folder} >/dev/null 2>&1 - if [ -f "/usr/local/x-ui/bin/config.json" ]; then + if [ -f "${xui_folder}/bin/config.json" ]; then echo -e "${green}Changing on config file permissions...${plain}" - chmod 640 /usr/local/x-ui/bin/config.json >/dev/null 2>&1 + chmod 640 ${xui_folder}/bin/config.json >/dev/null 2>&1 fi if [[ $release == "alpine" ]]; then @@ -226,8 +229,8 @@ update_x-ui() { rc-service x-ui start >/dev/null 2>&1 else echo -e "${green}Installing systemd unit...${plain}" - cp -f x-ui.service /etc/systemd/system/ >/dev/null 2>&1 - chown root:root /etc/systemd/system/x-ui.service >/dev/null 2>&1 + cp -f x-ui.service ${xui_service}/ >/dev/null 2>&1 + chown root:root ${xui_service}/x-ui.service >/dev/null 2>&1 systemctl daemon-reload >/dev/null 2>&1 systemctl enable x-ui >/dev/null 2>&1 systemctl start x-ui >/dev/null 2>&1 diff --git a/x-ui.sh b/x-ui.sh index f8ead9d6..050d4cd0 100644 --- a/x-ui.sh +++ b/x-ui.sh @@ -39,6 +39,8 @@ os_version="" os_version=$(grep "^VERSION_ID" /etc/os-release | cut -d '=' -f2 | tr -d '"' | tr -d '.') # Declare Variables +xui_folder="${XUI_MAIN_FOLDER:=/usr/local/x-ui}" +xui_service="${XUI_SERVICE:=/etc/systemd/system}" log_folder="${XUI_LOG_FOLDER:=/var/log}" iplimit_log_path="${log_folder}/3xipl.log" iplimit_banned_log_path="${log_folder}/3xipl-banned.log" @@ -112,7 +114,7 @@ update_menu() { fi wget -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh - chmod +x /usr/local/x-ui/x-ui.sh + chmod +x ${xui_folder}/x-ui.sh chmod +x /usr/bin/x-ui if [[ $? == 0 ]]; then @@ -161,13 +163,13 @@ uninstall() { else systemctl stop x-ui systemctl disable x-ui - rm /etc/systemd/system/x-ui.service -f + rm ${xui_service}/x-ui.service -f systemctl daemon-reload systemctl reset-failed fi rm /etc/x-ui/ -rf - rm /usr/local/x-ui/ -rf + rm ${xui_folder}/ -rf echo "" echo -e "Uninstalled Successfully.\n" @@ -195,9 +197,9 @@ reset_user() { read -rp "Do you want to disable currently configured two-factor authentication? (y/n): " twoFactorConfirm if [[ $twoFactorConfirm != "y" && $twoFactorConfirm != "Y" ]]; then - /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password} -resetTwoFactor false >/dev/null 2>&1 + ${xui_folder}/x-ui setting -username ${config_account} -password ${config_password} -resetTwoFactor false >/dev/null 2>&1 else - /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password} -resetTwoFactor true >/dev/null 2>&1 + ${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 @@ -225,7 +227,7 @@ reset_webbasepath() { config_webBasePath=$(gen_random_string 18) # Apply the new web base path setting - /usr/local/x-ui/x-ui setting -webBasePath "${config_webBasePath}" >/dev/null 2>&1 + ${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}" @@ -240,13 +242,13 @@ reset_config() { fi return 0 fi - /usr/local/x-ui/x-ui setting -reset + ${xui_folder}/x-ui setting -reset echo -e "All panel settings have been reset to default." restart } check_config() { - local info=$(/usr/local/x-ui/x-ui setting -show true) + local info=$(${xui_folder}/x-ui setting -show true) if [[ $? != 0 ]]; then LOGE "get current settings error, please check logs" show_menu @@ -256,7 +258,7 @@ check_config() { 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=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}') + local existing_cert=$(${xui_folder}/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}') 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) @@ -282,7 +284,7 @@ set_port() { LOGD "Cancelled" before_show_menu else - /usr/local/x-ui/x-ui setting -port ${port} + ${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 @@ -574,7 +576,7 @@ check_status() { return 1 fi else - if [[ ! -f /etc/systemd/system/x-ui.service ]]; then + 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) @@ -896,7 +898,7 @@ update_geo() { echo -e "${green}\t0.${plain} Back to Main Menu" read -rp "Choose an option: " choice - cd /usr/local/x-ui/bin + cd ${xui_folder}/bin case "$choice" in 0) @@ -1037,7 +1039,7 @@ ssl_cert_issue_main() { local webKeyFile="/root/cert/${domain}/privkey.pem" if [[ -f "${webCertFile}" && -f "${webKeyFile}" ]]; then - /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile" + ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile" echo "Panel paths set for domain: $domain" echo " - Certificate File: $webCertFile" echo " - Private Key File: $webKeyFile" @@ -1060,8 +1062,8 @@ ssl_cert_issue_main() { } ssl_cert_issue() { - local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}') - local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}') + local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}') + local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}') # check for acme.sh first if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then echo "acme.sh could not be found. we will install it" @@ -1212,7 +1214,7 @@ ssl_cert_issue() { local webKeyFile="/root/cert/${domain}/privkey.pem" if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then - /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile" + ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile" LOGI "Panel paths set for domain: $domain" LOGI " - Certificate File: $webCertFile" LOGI " - Private Key File: $webKeyFile" @@ -1227,8 +1229,8 @@ ssl_cert_issue() { } ssl_cert_issue_CF() { - local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}') - local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}') + local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}') + local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}') LOGI "****** Instructions for Use ******" LOGI "Follow the steps below to complete the process:" LOGI "1. Cloudflare Registered E-mail." @@ -1352,7 +1354,7 @@ ssl_cert_issue_CF() { local webKeyFile="${certPath}/privkey.pem" if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then - /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile" + ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile" LOGI "Panel paths set for domain: $CF_Domain" LOGI " - Certificate File: $webCertFile" LOGI " - Private Key File: $webKeyFile" @@ -1816,11 +1818,11 @@ SSH_port_forwarding() { break fi done - local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}') - local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}') - local existing_listenIP=$(/usr/local/x-ui/x-ui setting -getListen true | grep -Eo 'listenIP: .+' | awk '{print $2}') - local existing_cert=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}') - local existing_key=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'key: .+' | awk '{print $2}') + local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}') + local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}') + local existing_listenIP=$(${xui_folder}/x-ui setting -getListen true | grep -Eo 'listenIP: .+' | awk '{print $2}') + local existing_cert=$(${xui_folder}/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}') + local existing_key=$(${xui_folder}/x-ui setting -getCert true | grep -Eo 'key: .+' | awk '{print $2}') local config_listenIP="" local listen_choice="" @@ -1861,7 +1863,7 @@ SSH_port_forwarding() { config_listenIP="127.0.0.1" [[ "$listen_choice" == "2" ]] && read -rp "Enter custom IP to listen on: " config_listenIP - /usr/local/x-ui/x-ui setting -listenIP "${config_listenIP}" >/dev/null 2>&1 + ${xui_folder}/x-ui setting -listenIP "${config_listenIP}" >/dev/null 2>&1 echo -e "${green}listen IP has been set to ${config_listenIP}.${plain}" echo -e "\n${green}SSH Port Forwarding Configuration:${plain}" echo -e "Standard SSH command:" @@ -1877,7 +1879,7 @@ SSH_port_forwarding() { fi ;; 2) - /usr/local/x-ui/x-ui setting -listenIP 0.0.0.0 >/dev/null 2>&1 + ${xui_folder}/x-ui setting -listenIP 0.0.0.0 >/dev/null 2>&1 echo -e "${green}Listen IP has been cleared.${plain}" restart ;;