1
0

update.sh 29 KB

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