update.sh 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. #!/bin/bash
  2. red='\033[0;31m'
  3. green='\033[0;32m'
  4. blue='\033[0;34m'
  5. yellow='\033[0;33m'
  6. plain='\033[0m'
  7. xui_folder="${XUI_MAIN_FOLDER:=/usr/local/x-ui}"
  8. xui_service="${XUI_SERVICE:=/etc/systemd/system}"
  9. # Don't edit this config
  10. b_source="${BASH_SOURCE[0]}"
  11. while [ -h "$b_source" ]; do
  12. b_dir="$(cd -P "$(dirname "$b_source")" >/dev/null 2>&1 && pwd || pwd -P)"
  13. b_source="$(readlink "$b_source")"
  14. [[ $b_source != /* ]] && b_source="$b_dir/$b_source"
  15. done
  16. cur_dir="$(cd -P "$(dirname "$b_source")" >/dev/null 2>&1 && pwd || pwd -P)"
  17. script_name=$(basename "$0")
  18. # Check command exist function
  19. _command_exists() {
  20. type "$1" &>/dev/null
  21. }
  22. # Fail, log and exit script function
  23. _fail() {
  24. local msg=${1}
  25. echo -e "${red}${msg}${plain}"
  26. exit 2
  27. }
  28. # check root
  29. [[ $EUID -ne 0 ]] && _fail "FATAL ERROR: Please run this script with root privilege."
  30. if _command_exists wget; then
  31. wget_bin=$(which wget)
  32. else
  33. _fail "ERROR: Command 'wget' not found."
  34. fi
  35. if _command_exists curl; then
  36. curl_bin=$(which curl)
  37. else
  38. _fail "ERROR: Command 'curl' not found."
  39. fi
  40. # Check OS and set release variable
  41. if [[ -f /etc/os-release ]]; then
  42. source /etc/os-release
  43. release=$ID
  44. elif [[ -f /usr/lib/os-release ]]; then
  45. source /usr/lib/os-release
  46. release=$ID
  47. else
  48. _fail "Failed to check the system OS, please contact the author!"
  49. fi
  50. echo "The OS release is: $release"
  51. arch() {
  52. case "$(uname -m)" in
  53. x86_64 | x64 | amd64) echo 'amd64' ;;
  54. i*86 | x86) echo '386' ;;
  55. armv8* | armv8 | arm64 | aarch64) echo 'arm64' ;;
  56. armv7* | armv7 | arm) echo 'armv7' ;;
  57. armv6* | armv6) echo 'armv6' ;;
  58. armv5* | armv5) echo 'armv5' ;;
  59. s390x) echo 's390x' ;;
  60. *) echo -e "${red}Unsupported CPU architecture!${plain}" && rm -f "${cur_dir}/${script_name}" >/dev/null 2>&1 && exit 2;;
  61. esac
  62. }
  63. echo "Arch: $(arch)"
  64. # Simple helpers
  65. is_ipv4() {
  66. [[ "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] && return 0 || return 1
  67. }
  68. is_ipv6() {
  69. [[ "$1" =~ : ]] && return 0 || return 1
  70. }
  71. is_ip() {
  72. is_ipv4 "$1" || is_ipv6 "$1"
  73. }
  74. is_domain() {
  75. [[ "$1" =~ ^([A-Za-z0-9](-*[A-Za-z0-9])*\.)+[A-Za-z]{2,}$ ]] && return 0 || return 1
  76. }
  77. gen_random_string() {
  78. local length="$1"
  79. local random_string=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w "$length" | head -n 1)
  80. echo "$random_string"
  81. }
  82. install_base() {
  83. echo -e "${green}Updating and install dependency packages...${plain}"
  84. case "${release}" in
  85. ubuntu | debian | armbian)
  86. apt-get update >/dev/null 2>&1 && apt-get install -y -q wget curl tar tzdata openssl socat >/dev/null 2>&1
  87. ;;
  88. fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol)
  89. dnf -y update >/dev/null 2>&1 && dnf install -y -q wget curl tar tzdata openssl socat >/dev/null 2>&1
  90. ;;
  91. centos)
  92. if [[ "${VERSION_ID}" =~ ^7 ]]; then
  93. yum -y update >/dev/null 2>&1 && yum install -y -q wget curl tar tzdata openssl socat >/dev/null 2>&1
  94. else
  95. dnf -y update >/dev/null 2>&1 && dnf install -y -q wget curl tar tzdata openssl socat >/dev/null 2>&1
  96. fi
  97. ;;
  98. arch | manjaro | parch)
  99. pacman -Syu >/dev/null 2>&1 && pacman -Syu --noconfirm wget curl tar tzdata openssl socat >/dev/null 2>&1
  100. ;;
  101. opensuse-tumbleweed | opensuse-leap)
  102. zypper refresh >/dev/null 2>&1 && zypper -q install -y wget curl tar timezone openssl socat >/dev/null 2>&1
  103. ;;
  104. alpine)
  105. apk update >/dev/null 2>&1 && apk add wget curl tar tzdata openssl socat >/dev/null 2>&1
  106. ;;
  107. *)
  108. apt-get update >/dev/null 2>&1 && apt install -y -q wget curl tar tzdata openssl socat >/dev/null 2>&1
  109. ;;
  110. esac
  111. }
  112. install_acme() {
  113. echo -e "${green}Installing acme.sh for SSL certificate management...${plain}"
  114. cd ~ || return 1
  115. curl -s https://get.acme.sh | sh >/dev/null 2>&1
  116. if [ $? -ne 0 ]; then
  117. echo -e "${red}Failed to install acme.sh${plain}"
  118. return 1
  119. else
  120. echo -e "${green}acme.sh installed successfully${plain}"
  121. fi
  122. return 0
  123. }
  124. setup_ssl_certificate() {
  125. local domain="$1"
  126. local server_ip="$2"
  127. local existing_port="$3"
  128. local existing_webBasePath="$4"
  129. echo -e "${green}Setting up SSL certificate...${plain}"
  130. # Check if acme.sh is installed
  131. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  132. install_acme
  133. if [ $? -ne 0 ]; then
  134. echo -e "${yellow}Failed to install acme.sh, skipping SSL setup${plain}"
  135. return 1
  136. fi
  137. fi
  138. # Create certificate directory
  139. local certPath="/root/cert/${domain}"
  140. mkdir -p "$certPath"
  141. # Issue certificate
  142. echo -e "${green}Issuing SSL certificate for ${domain}...${plain}"
  143. echo -e "${yellow}Note: Port 80 must be open and accessible from the internet${plain}"
  144. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt >/dev/null 2>&1
  145. ~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport 80 --force
  146. if [ $? -ne 0 ]; then
  147. echo -e "${yellow}Failed to issue certificate for ${domain}${plain}"
  148. echo -e "${yellow}Please ensure port 80 is open and try again later with: x-ui${plain}"
  149. rm -rf ~/.acme.sh/${domain} 2>/dev/null
  150. rm -rf "$certPath" 2>/dev/null
  151. return 1
  152. fi
  153. # Install certificate
  154. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  155. --key-file /root/cert/${domain}/privkey.pem \
  156. --fullchain-file /root/cert/${domain}/fullchain.pem \
  157. --reloadcmd "systemctl restart x-ui" >/dev/null 2>&1
  158. if [ $? -ne 0 ]; then
  159. echo -e "${yellow}Failed to install certificate${plain}"
  160. return 1
  161. fi
  162. # Enable auto-renew
  163. ~/.acme.sh/acme.sh --upgrade --auto-upgrade >/dev/null 2>&1
  164. chmod 755 $certPath/* 2>/dev/null
  165. # Set certificate for panel
  166. local webCertFile="/root/cert/${domain}/fullchain.pem"
  167. local webKeyFile="/root/cert/${domain}/privkey.pem"
  168. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  169. ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile" >/dev/null 2>&1
  170. echo -e "${green}SSL certificate installed and configured successfully!${plain}"
  171. return 0
  172. else
  173. echo -e "${yellow}Certificate files not found${plain}"
  174. return 1
  175. fi
  176. }
  177. # Fallback: generate a self-signed certificate (not publicly trusted)
  178. setup_self_signed_certificate() {
  179. local name="$1" # domain or IP to place in SAN
  180. local certDir="/root/cert/selfsigned"
  181. echo -e "${yellow}Generating a self-signed certificate (not publicly trusted)...${plain}"
  182. mkdir -p "$certDir"
  183. local sanExt=""
  184. if is_ip "$name"; then
  185. sanExt="IP:${name}"
  186. else
  187. sanExt="DNS:${name}"
  188. fi
  189. # Try -addext; fallback to config if not supported
  190. openssl req -x509 -nodes -newkey rsa:2048 -days 365 \
  191. -keyout "${certDir}/privkey.pem" \
  192. -out "${certDir}/fullchain.pem" \
  193. -subj "/CN=${name}" \
  194. -addext "subjectAltName=${sanExt}" >/dev/null 2>&1
  195. if [[ $? -ne 0 ]]; then
  196. local tmpCfg="${certDir}/openssl.cnf"
  197. cat > "$tmpCfg" <<EOF
  198. [req]
  199. distinguished_name=req_distinguished_name
  200. req_extensions=v3_req
  201. [req_distinguished_name]
  202. [v3_req]
  203. subjectAltName=${sanExt}
  204. EOF
  205. openssl req -x509 -nodes -newkey rsa:2048 -days 365 \
  206. -keyout "${certDir}/privkey.pem" \
  207. -out "${certDir}/fullchain.pem" \
  208. -subj "/CN=${name}" \
  209. -config "$tmpCfg" -extensions v3_req >/dev/null 2>&1
  210. rm -f "$tmpCfg"
  211. fi
  212. if [[ ! -f "${certDir}/fullchain.pem" || ! -f "${certDir}/privkey.pem" ]]; then
  213. echo -e "${red}Failed to generate self-signed certificate${plain}"
  214. return 1
  215. fi
  216. chmod 755 ${certDir}/* 2>/dev/null
  217. ${xui_folder}/x-ui cert -webCert "${certDir}/fullchain.pem" -webCertKey "${certDir}/privkey.pem" >/dev/null 2>&1
  218. echo -e "${yellow}Self-signed certificate configured. Browsers will show a warning.${plain}"
  219. return 0
  220. }
  221. # Comprehensive manual SSL certificate issuance via acme.sh
  222. ssl_cert_issue() {
  223. local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep 'webBasePath:' | awk -F': ' '{print $2}' | tr -d '[:space:]' | sed 's#^/##')
  224. local existing_port=$(${xui_folder}/x-ui setting -show true | grep 'port:' | awk -F': ' '{print $2}' | tr -d '[:space:]')
  225. # check for acme.sh first
  226. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  227. echo "acme.sh could not be found. Installing now..."
  228. cd ~ || return 1
  229. curl -s https://get.acme.sh | sh
  230. if [ $? -ne 0 ]; then
  231. echo -e "${red}Failed to install acme.sh${plain}"
  232. return 1
  233. else
  234. echo -e "${green}acme.sh installed successfully${plain}"
  235. fi
  236. fi
  237. # get the domain here, and we need to verify it
  238. local domain=""
  239. while true; do
  240. read -rp "Please enter your domain name: " domain
  241. domain="${domain// /}" # Trim whitespace
  242. if [[ -z "$domain" ]]; then
  243. echo -e "${red}Domain name cannot be empty. Please try again.${plain}"
  244. continue
  245. fi
  246. if ! is_domain "$domain"; then
  247. echo -e "${red}Invalid domain format: ${domain}. Please enter a valid domain name.${plain}"
  248. continue
  249. fi
  250. break
  251. done
  252. echo -e "${green}Your domain is: ${domain}, checking it...${plain}"
  253. # check if there already exists a certificate
  254. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  255. if [ "${currentCert}" == "${domain}" ]; then
  256. local certInfo=$(~/.acme.sh/acme.sh --list)
  257. echo -e "${red}System already has certificates for this domain. Cannot issue again.${plain}"
  258. echo -e "${yellow}Current certificate details:${plain}"
  259. echo "$certInfo"
  260. return 1
  261. else
  262. echo -e "${green}Your domain is ready for issuing certificates now...${plain}"
  263. fi
  264. # create a directory for the certificate
  265. certPath="/root/cert/${domain}"
  266. if [ ! -d "$certPath" ]; then
  267. mkdir -p "$certPath"
  268. else
  269. rm -rf "$certPath"
  270. mkdir -p "$certPath"
  271. fi
  272. # get the port number for the standalone server
  273. local WebPort=80
  274. read -rp "Please choose which port to use (default is 80): " WebPort
  275. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  276. echo -e "${yellow}Your input ${WebPort} is invalid, will use default port 80.${plain}"
  277. WebPort=80
  278. fi
  279. echo -e "${green}Will use port: ${WebPort} to issue certificates. Please make sure this port is open.${plain}"
  280. # Stop panel temporarily
  281. echo -e "${yellow}Stopping panel temporarily...${plain}"
  282. systemctl stop x-ui 2>/dev/null || rc-service x-ui stop 2>/dev/null
  283. # issue the certificate
  284. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  285. ~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport ${WebPort} --force
  286. if [ $? -ne 0 ]; then
  287. echo -e "${red}Issuing certificate failed, please check logs.${plain}"
  288. rm -rf ~/.acme.sh/${domain}
  289. systemctl start x-ui 2>/dev/null || rc-service x-ui start 2>/dev/null
  290. return 1
  291. else
  292. echo -e "${green}Issuing certificate succeeded, installing certificates...${plain}"
  293. fi
  294. # Setup reload command
  295. reloadCmd="systemctl restart x-ui || rc-service x-ui restart"
  296. echo -e "${green}Default --reloadcmd for ACME is: ${yellow}systemctl restart x-ui || rc-service x-ui restart${plain}"
  297. echo -e "${green}This command will run on every certificate issue and renew.${plain}"
  298. read -rp "Would you like to modify --reloadcmd for ACME? (y/n): " setReloadcmd
  299. if [[ "$setReloadcmd" == "y" || "$setReloadcmd" == "Y" ]]; then
  300. echo -e "\n${green}\t1.${plain} Preset: systemctl reload nginx ; systemctl restart x-ui"
  301. echo -e "${green}\t2.${plain} Input your own command"
  302. echo -e "${green}\t0.${plain} Keep default reloadcmd"
  303. read -rp "Choose an option: " choice
  304. case "$choice" in
  305. 1)
  306. echo -e "${green}Reloadcmd is: systemctl reload nginx ; systemctl restart x-ui${plain}"
  307. reloadCmd="systemctl reload nginx ; systemctl restart x-ui"
  308. ;;
  309. 2)
  310. echo -e "${yellow}It's recommended to put x-ui restart at the end${plain}"
  311. read -rp "Please enter your custom reloadcmd: " reloadCmd
  312. echo -e "${green}Reloadcmd is: ${reloadCmd}${plain}"
  313. ;;
  314. *)
  315. echo -e "${green}Keeping default reloadcmd${plain}"
  316. ;;
  317. esac
  318. fi
  319. # install the certificate
  320. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  321. --key-file /root/cert/${domain}/privkey.pem \
  322. --fullchain-file /root/cert/${domain}/fullchain.pem --reloadcmd "${reloadCmd}"
  323. if [ $? -ne 0 ]; then
  324. echo -e "${red}Installing certificate failed, exiting.${plain}"
  325. rm -rf ~/.acme.sh/${domain}
  326. systemctl start x-ui 2>/dev/null || rc-service x-ui start 2>/dev/null
  327. return 1
  328. else
  329. echo -e "${green}Installing certificate succeeded, enabling auto renew...${plain}"
  330. fi
  331. # enable auto-renew
  332. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  333. if [ $? -ne 0 ]; then
  334. echo -e "${yellow}Auto renew setup had issues, certificate details:${plain}"
  335. ls -lah /root/cert/${domain}/
  336. chmod 755 $certPath/*
  337. else
  338. echo -e "${green}Auto renew succeeded, certificate details:${plain}"
  339. ls -lah /root/cert/${domain}/
  340. chmod 755 $certPath/*
  341. fi
  342. # Restart panel
  343. systemctl start x-ui 2>/dev/null || rc-service x-ui start 2>/dev/null
  344. # Prompt user to set panel paths after successful certificate installation
  345. read -rp "Would you like to set this certificate for the panel? (y/n): " setPanel
  346. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  347. local webCertFile="/root/cert/${domain}/fullchain.pem"
  348. local webKeyFile="/root/cert/${domain}/privkey.pem"
  349. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  350. ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  351. echo -e "${green}Certificate paths set for the panel${plain}"
  352. echo -e "${green}Certificate File: $webCertFile${plain}"
  353. echo -e "${green}Private Key File: $webKeyFile${plain}"
  354. echo ""
  355. echo -e "${green}Access URL: https://${domain}:${existing_port}/${existing_webBasePath}${plain}"
  356. echo -e "${yellow}Panel will restart to apply SSL certificate...${plain}"
  357. systemctl restart x-ui 2>/dev/null || rc-service x-ui restart 2>/dev/null
  358. else
  359. echo -e "${red}Error: Certificate or private key file not found for domain: $domain.${plain}"
  360. fi
  361. else
  362. echo -e "${yellow}Skipping panel path setting.${plain}"
  363. fi
  364. return 0
  365. }
  366. # Unified interactive SSL setup (domain or self-signed)
  367. # Sets global `SSL_HOST` to the chosen domain/IP
  368. prompt_and_setup_ssl() {
  369. local panel_port="$1"
  370. local web_base_path="$2" # expected without leading slash
  371. local server_ip="$3"
  372. local ssl_choice=""
  373. echo -e "${yellow}Choose SSL certificate setup method:${plain}"
  374. echo -e "${green}1.${plain} Let's Encrypt (domain required, recommended)"
  375. echo -e "${green}2.${plain} Self-signed certificate (for testing/local use)"
  376. read -rp "Choose an option (default 2): " ssl_choice
  377. ssl_choice="${ssl_choice// /}" # Trim whitespace
  378. # Default to 2 (self-signed) if not 1
  379. if [[ "$ssl_choice" != "1" ]]; then
  380. ssl_choice="2"
  381. fi
  382. case "$ssl_choice" in
  383. 1)
  384. # User chose Let's Encrypt domain option
  385. echo -e "${green}Using ssl_cert_issue() for comprehensive domain setup...${plain}"
  386. ssl_cert_issue
  387. # Extract the domain that was used from the certificate
  388. local cert_domain=$(~/.acme.sh/acme.sh --list 2>/dev/null | tail -1 | awk '{print $1}')
  389. if [[ -n "${cert_domain}" ]]; then
  390. SSL_HOST="${cert_domain}"
  391. echo -e "${green}✓ SSL certificate configured successfully with domain: ${cert_domain}${plain}"
  392. else
  393. echo -e "${yellow}SSL setup may have completed, but domain extraction failed${plain}"
  394. SSL_HOST="${server_ip}"
  395. fi
  396. ;;
  397. 2)
  398. # User chose self-signed option
  399. # Stop panel if running
  400. if [[ $release == "alpine" ]]; then
  401. rc-service x-ui stop >/dev/null 2>&1
  402. else
  403. systemctl stop x-ui >/dev/null 2>&1
  404. fi
  405. echo -e "${yellow}Using server IP for self-signed certificate: ${server_ip}${plain}"
  406. setup_self_signed_certificate "${server_ip}"
  407. if [ $? -eq 0 ]; then
  408. SSL_HOST="${server_ip}"
  409. echo -e "${green}✓ Self-signed SSL configured successfully${plain}"
  410. else
  411. echo -e "${red}✗ Self-signed SSL setup failed${plain}"
  412. SSL_HOST="${server_ip}"
  413. fi
  414. # Start panel after SSL is configured
  415. if [[ $release == "alpine" ]]; then
  416. rc-service x-ui start >/dev/null 2>&1
  417. else
  418. systemctl start x-ui >/dev/null 2>&1
  419. fi
  420. ;;
  421. 0)
  422. echo -e "${yellow}Skipping SSL setup${plain}"
  423. SSL_HOST="${server_ip}"
  424. ;;
  425. *)
  426. echo -e "${red}Invalid option. Skipping SSL setup.${plain}"
  427. SSL_HOST="${server_ip}"
  428. ;;
  429. esac
  430. }
  431. config_after_update() {
  432. echo -e "${yellow}x-ui settings:${plain}"
  433. ${xui_folder}/x-ui setting -show true
  434. ${xui_folder}/x-ui migrate
  435. # Properly detect empty cert by checking if cert: line exists and has content after it
  436. local existing_cert=$(${xui_folder}/x-ui setting -getCert true 2>/dev/null | grep 'cert:' | awk -F': ' '{print $2}' | tr -d '[:space:]')
  437. local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  438. local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}' | sed 's#^/##')
  439. # Get server IP
  440. local URL_lists=(
  441. "https://api4.ipify.org"
  442. "https://ipv4.icanhazip.com"
  443. "https://v4.api.ipinfo.io/ip"
  444. "https://ipv4.myexternalip.com/raw"
  445. "https://4.ident.me"
  446. "https://check-host.net/ip"
  447. )
  448. local server_ip=""
  449. for ip_address in "${URL_lists[@]}"; do
  450. server_ip=$(${curl_bin} -s --max-time 3 "${ip_address}" 2>/dev/null | tr -d '[:space:]')
  451. if [[ -n "${server_ip}" ]]; then
  452. break
  453. fi
  454. done
  455. # Handle missing/short webBasePath
  456. if [[ ${#existing_webBasePath} -lt 4 ]]; then
  457. echo -e "${yellow}WebBasePath is missing or too short. Generating a new one...${plain}"
  458. local config_webBasePath=$(gen_random_string 18)
  459. ${xui_folder}/x-ui setting -webBasePath "${config_webBasePath}"
  460. existing_webBasePath="${config_webBasePath}"
  461. echo -e "${green}New WebBasePath: ${config_webBasePath}${plain}"
  462. fi
  463. # Check and prompt for SSL if missing
  464. if [[ -z "$existing_cert" ]]; then
  465. echo ""
  466. echo -e "${red}═══════════════════════════════════════════${plain}"
  467. echo -e "${red} ⚠ NO SSL CERTIFICATE DETECTED ⚠ ${plain}"
  468. echo -e "${red}═══════════════════════════════════════════${plain}"
  469. echo -e "${yellow}For security, SSL certificate is MANDATORY for all panels.${plain}"
  470. echo -e "${yellow}Let's Encrypt requires a domain name; IP certs are not issued. Use self-signed for IP.${plain}"
  471. echo ""
  472. if [[ -z "${server_ip}" ]]; then
  473. echo -e "${red}Failed to detect server IP${plain}"
  474. echo -e "${yellow}Please configure SSL manually using: x-ui${plain}"
  475. return
  476. fi
  477. # Prompt and setup SSL (domain or self-signed)
  478. prompt_and_setup_ssl "${existing_port}" "${existing_webBasePath}" "${server_ip}"
  479. echo ""
  480. echo -e "${green}═══════════════════════════════════════════${plain}"
  481. echo -e "${green} Panel Access Information ${plain}"
  482. echo -e "${green}═══════════════════════════════════════════${plain}"
  483. echo -e "${green}Access URL: https://${SSL_HOST}:${existing_port}/${existing_webBasePath}${plain}"
  484. echo -e "${green}═══════════════════════════════════════════${plain}"
  485. echo -e "${yellow}⚠ SSL Certificate: Enabled and configured${plain}"
  486. else
  487. echo -e "${green}SSL certificate is already configured${plain}"
  488. # Show access URL with existing certificate
  489. local cert_domain=$(basename "$(dirname "$existing_cert")")
  490. echo ""
  491. echo -e "${green}═══════════════════════════════════════════${plain}"
  492. echo -e "${green} Panel Access Information ${plain}"
  493. echo -e "${green}═══════════════════════════════════════════${plain}"
  494. echo -e "${green}Access URL: https://${cert_domain}:${existing_port}/${existing_webBasePath}${plain}"
  495. echo -e "${green}═══════════════════════════════════════════${plain}"
  496. fi
  497. }
  498. update_x-ui() {
  499. cd ${xui_folder%/x-ui}/
  500. if [ -f "${xui_folder}/x-ui" ]; then
  501. current_xui_version=$(${xui_folder}/x-ui -v)
  502. echo -e "${green}Current x-ui version: ${current_xui_version}${plain}"
  503. else
  504. _fail "ERROR: Current x-ui version: unknown"
  505. fi
  506. echo -e "${green}Downloading new x-ui version...${plain}"
  507. tag_version=$(${curl_bin} -Ls "https://api.github.com/repos/MHSanaei/3x-ui/releases/latest" 2>/dev/null | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  508. if [[ ! -n "$tag_version" ]]; then
  509. echo -e "${yellow}Trying to fetch version with IPv4...${plain}"
  510. tag_version=$(${curl_bin} -4 -Ls "https://api.github.com/repos/MHSanaei/3x-ui/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  511. if [[ ! -n "$tag_version" ]]; then
  512. _fail "ERROR: Failed to fetch x-ui version, it may be due to GitHub API restrictions, please try it later"
  513. fi
  514. fi
  515. echo -e "Got x-ui latest version: ${tag_version}, beginning the installation..."
  516. ${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
  517. if [[ $? -ne 0 ]]; then
  518. echo -e "${yellow}Trying to fetch version with IPv4...${plain}"
  519. ${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
  520. if [[ $? -ne 0 ]]; then
  521. _fail "ERROR: Failed to download x-ui, please be sure that your server can access GitHub"
  522. fi
  523. fi
  524. if [[ -e ${xui_folder}/ ]]; then
  525. echo -e "${green}Stopping x-ui...${plain}"
  526. if [[ $release == "alpine" ]]; then
  527. if [ -f "/etc/init.d/x-ui" ]; then
  528. rc-service x-ui stop >/dev/null 2>&1
  529. rc-update del x-ui >/dev/null 2>&1
  530. echo -e "${green}Removing old service unit version...${plain}"
  531. rm -f /etc/init.d/x-ui >/dev/null 2>&1
  532. else
  533. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  534. _fail "ERROR: x-ui service unit not installed."
  535. fi
  536. else
  537. if [ -f "${xui_service}/x-ui.service" ]; then
  538. systemctl stop x-ui >/dev/null 2>&1
  539. systemctl disable x-ui >/dev/null 2>&1
  540. echo -e "${green}Removing old systemd unit version...${plain}"
  541. rm ${xui_service}/x-ui.service -f >/dev/null 2>&1
  542. systemctl daemon-reload >/dev/null 2>&1
  543. else
  544. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  545. _fail "ERROR: x-ui systemd unit not installed."
  546. fi
  547. fi
  548. echo -e "${green}Removing old x-ui version...${plain}"
  549. rm ${xui_folder} -f >/dev/null 2>&1
  550. rm ${xui_folder}/x-ui.service -f >/dev/null 2>&1
  551. rm ${xui_folder}/x-ui.service.debian -f >/dev/null 2>&1
  552. rm ${xui_folder}/x-ui.service.rhel -f >/dev/null 2>&1
  553. rm ${xui_folder}/x-ui -f >/dev/null 2>&1
  554. rm ${xui_folder}/x-ui.sh -f >/dev/null 2>&1
  555. echo -e "${green}Removing old xray version...${plain}"
  556. rm ${xui_folder}/bin/xray-linux-amd64 -f >/dev/null 2>&1
  557. echo -e "${green}Removing old README and LICENSE file...${plain}"
  558. rm ${xui_folder}/bin/README.md -f >/dev/null 2>&1
  559. rm ${xui_folder}/bin/LICENSE -f >/dev/null 2>&1
  560. else
  561. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  562. _fail "ERROR: x-ui not installed."
  563. fi
  564. echo -e "${green}Installing new x-ui version...${plain}"
  565. tar zxvf x-ui-linux-$(arch).tar.gz >/dev/null 2>&1
  566. rm x-ui-linux-$(arch).tar.gz -f >/dev/null 2>&1
  567. cd x-ui >/dev/null 2>&1
  568. chmod +x x-ui >/dev/null 2>&1
  569. # Check the system's architecture and rename the file accordingly
  570. if [[ $(arch) == "armv5" || $(arch) == "armv6" || $(arch) == "armv7" ]]; then
  571. mv bin/xray-linux-$(arch) bin/xray-linux-arm >/dev/null 2>&1
  572. chmod +x bin/xray-linux-arm >/dev/null 2>&1
  573. fi
  574. chmod +x x-ui bin/xray-linux-$(arch) >/dev/null 2>&1
  575. echo -e "${green}Downloading and installing x-ui.sh script...${plain}"
  576. ${wget_bin} -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh >/dev/null 2>&1
  577. if [[ $? -ne 0 ]]; then
  578. echo -e "${yellow}Trying to fetch x-ui with IPv4...${plain}"
  579. ${wget_bin} --inet4-only -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh >/dev/null 2>&1
  580. if [[ $? -ne 0 ]]; then
  581. _fail "ERROR: Failed to download x-ui.sh script, please be sure that your server can access GitHub"
  582. fi
  583. fi
  584. chmod +x ${xui_folder}/x-ui.sh >/dev/null 2>&1
  585. chmod +x /usr/bin/x-ui >/dev/null 2>&1
  586. mkdir -p /var/log/x-ui >/dev/null 2>&1
  587. echo -e "${green}Changing owner...${plain}"
  588. chown -R root:root ${xui_folder} >/dev/null 2>&1
  589. if [ -f "${xui_folder}/bin/config.json" ]; then
  590. echo -e "${green}Changing on config file permissions...${plain}"
  591. chmod 640 ${xui_folder}/bin/config.json >/dev/null 2>&1
  592. fi
  593. if [[ $release == "alpine" ]]; then
  594. echo -e "${green}Downloading and installing startup unit x-ui.rc...${plain}"
  595. ${wget_bin} -O /etc/init.d/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.rc >/dev/null 2>&1
  596. if [[ $? -ne 0 ]]; then
  597. ${wget_bin} --inet4-only -O /etc/init.d/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.rc >/dev/null 2>&1
  598. if [[ $? -ne 0 ]]; then
  599. _fail "ERROR: Failed to download startup unit x-ui.rc, please be sure that your server can access GitHub"
  600. fi
  601. fi
  602. chmod +x /etc/init.d/x-ui >/dev/null 2>&1
  603. chown root:root /etc/init.d/x-ui >/dev/null 2>&1
  604. rc-update add x-ui >/dev/null 2>&1
  605. rc-service x-ui start >/dev/null 2>&1
  606. else
  607. if [ -f "x-ui.service" ]; then
  608. echo -e "${green}Installing systemd unit...${plain}"
  609. cp -f x-ui.service ${xui_service}/ >/dev/null 2>&1
  610. else
  611. case "${release}" in
  612. ubuntu | debian | armbian)
  613. echo -e "${green}Installing debian-like systemd unit...${plain}"
  614. cp -f x-ui.service.debian ${xui_service}/x-ui.service >/dev/null 2>&1
  615. ;;
  616. *)
  617. echo -e "${green}Installing rhel-like systemd unit...${plain}"
  618. cp -f x-ui.service.rhel ${xui_service}/x-ui.service >/dev/null 2>&1
  619. ;;
  620. esac
  621. fi
  622. chown root:root ${xui_service}/x-ui.service >/dev/null 2>&1
  623. systemctl daemon-reload >/dev/null 2>&1
  624. systemctl enable x-ui >/dev/null 2>&1
  625. systemctl start x-ui >/dev/null 2>&1
  626. fi
  627. config_after_update
  628. echo -e "${green}x-ui ${tag_version}${plain} updating finished, it is running now..."
  629. echo -e ""
  630. echo -e "┌───────────────────────────────────────────────────────┐
  631. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  632. │ │
  633. │ ${blue}x-ui${plain} - Admin Management Script │
  634. │ ${blue}x-ui start${plain} - Start │
  635. │ ${blue}x-ui stop${plain} - Stop │
  636. │ ${blue}x-ui restart${plain} - Restart │
  637. │ ${blue}x-ui status${plain} - Current Status │
  638. │ ${blue}x-ui settings${plain} - Current Settings │
  639. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  640. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  641. │ ${blue}x-ui log${plain} - Check logs │
  642. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  643. │ ${blue}x-ui update${plain} - Update │
  644. │ ${blue}x-ui legacy${plain} - Legacy version │
  645. │ ${blue}x-ui install${plain} - Install │
  646. │ ${blue}x-ui uninstall${plain} - Uninstall │
  647. └───────────────────────────────────────────────────────┘"
  648. }
  649. echo -e "${green}Running...${plain}"
  650. install_base
  651. update_x-ui $1