update.sh 32 KB

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