update.sh 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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])*\.)+(xn--[a-z0-9]{2,}|[A-Za-z]{2,})$ ]] && return 0 || return 1
  71. }
  72. # Port helpers
  73. is_port_in_use() {
  74. local port="$1"
  75. if command -v ss > /dev/null 2>&1; then
  76. ss -ltn 2> /dev/null | awk -v p=":${port}$" '$4 ~ p {exit 0} END {exit 1}'
  77. return
  78. fi
  79. if command -v netstat > /dev/null 2>&1; then
  80. netstat -lnt 2> /dev/null | awk -v p=":${port} " '$4 ~ p {exit 0} END {exit 1}'
  81. return
  82. fi
  83. if command -v lsof > /dev/null 2>&1; then
  84. lsof -nP -iTCP:${port} -sTCP:LISTEN > /dev/null 2>&1 && return 0
  85. fi
  86. return 1
  87. }
  88. gen_random_string() {
  89. local length="$1"
  90. openssl rand -base64 $((length * 2)) \
  91. | tr -dc 'a-zA-Z0-9' \
  92. | head -c "$length"
  93. }
  94. xui_env_file_path() {
  95. case "${release}" in
  96. ubuntu | debian | armbian)
  97. echo "/etc/default/x-ui"
  98. ;;
  99. arch | manjaro | parch | alpine)
  100. echo "/etc/conf.d/x-ui"
  101. ;;
  102. *)
  103. echo "/etc/sysconfig/x-ui"
  104. ;;
  105. esac
  106. }
  107. load_xui_env() {
  108. local env_file
  109. env_file="$(xui_env_file_path)"
  110. if [[ -r "$env_file" ]]; then
  111. set -a
  112. # shellcheck disable=SC1090
  113. source "$env_file"
  114. set +a
  115. fi
  116. }
  117. install_base() {
  118. echo -e "${green}Updating and install dependency packages...${plain}"
  119. case "${release}" in
  120. ubuntu | debian | armbian)
  121. apt-get update > /dev/null 2>&1 && apt-get install -y -q cron curl tar tzdata socat openssl > /dev/null 2>&1
  122. ;;
  123. fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol)
  124. dnf -y update > /dev/null 2>&1 && dnf install -y -q cronie curl tar tzdata socat openssl > /dev/null 2>&1
  125. ;;
  126. centos)
  127. if [[ "${VERSION_ID}" =~ ^7 ]]; then
  128. yum -y update > /dev/null 2>&1 && yum install -y -q cronie curl tar tzdata socat openssl > /dev/null 2>&1
  129. else
  130. dnf -y update > /dev/null 2>&1 && dnf install -y -q cronie curl tar tzdata socat openssl > /dev/null 2>&1
  131. fi
  132. ;;
  133. arch | manjaro | parch)
  134. pacman -Syu > /dev/null 2>&1 && pacman -Syu --noconfirm cronie curl tar tzdata socat openssl > /dev/null 2>&1
  135. ;;
  136. opensuse-tumbleweed | opensuse-leap)
  137. zypper refresh > /dev/null 2>&1 && zypper -q install -y cron curl tar timezone socat openssl > /dev/null 2>&1
  138. ;;
  139. alpine)
  140. apk update > /dev/null 2>&1 && apk add dcron curl tar tzdata socat openssl > /dev/null 2>&1
  141. ;;
  142. *)
  143. apt-get update > /dev/null 2>&1 && apt install -y -q cron curl tar tzdata socat openssl > /dev/null 2>&1
  144. ;;
  145. esac
  146. }
  147. install_acme() {
  148. echo -e "${green}Installing acme.sh for SSL certificate management...${plain}"
  149. cd ~ || return 1
  150. curl -s https://get.acme.sh | sh > /dev/null 2>&1
  151. if [ $? -ne 0 ]; then
  152. echo -e "${red}Failed to install acme.sh${plain}"
  153. return 1
  154. else
  155. echo -e "${green}acme.sh installed successfully${plain}"
  156. fi
  157. return 0
  158. }
  159. setup_ssl_certificate() {
  160. local domain="$1"
  161. local server_ip="$2"
  162. local existing_port="$3"
  163. local existing_webBasePath="$4"
  164. echo -e "${green}Setting up SSL certificate...${plain}"
  165. # Check if acme.sh is installed
  166. if ! command -v ~/.acme.sh/acme.sh &> /dev/null; then
  167. install_acme
  168. if [ $? -ne 0 ]; then
  169. echo -e "${yellow}Failed to install acme.sh, skipping SSL setup${plain}"
  170. return 1
  171. fi
  172. fi
  173. # Create certificate directory
  174. local certPath="/root/cert/${domain}"
  175. mkdir -p "$certPath"
  176. # Issue certificate
  177. echo -e "${green}Issuing SSL certificate for ${domain}...${plain}"
  178. echo -e "${yellow}Note: Port 80 must be open and accessible from the internet${plain}"
  179. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt --force > /dev/null 2>&1
  180. ~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport 80 --force
  181. if [ $? -ne 0 ]; then
  182. echo -e "${yellow}Failed to issue certificate for ${domain}${plain}"
  183. echo -e "${yellow}Please ensure port 80 is open and try again later with: x-ui${plain}"
  184. rm -rf ~/.acme.sh/${domain} 2> /dev/null
  185. rm -rf "$certPath" 2> /dev/null
  186. return 1
  187. fi
  188. # Install certificate
  189. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  190. --key-file /root/cert/${domain}/privkey.pem \
  191. --fullchain-file /root/cert/${domain}/fullchain.pem \
  192. --reloadcmd "systemctl restart x-ui" > /dev/null 2>&1
  193. if [ $? -ne 0 ]; then
  194. echo -e "${yellow}Failed to install certificate${plain}"
  195. return 1
  196. fi
  197. # Enable auto-renew
  198. ~/.acme.sh/acme.sh --upgrade --auto-upgrade > /dev/null 2>&1
  199. chmod 600 $certPath/privkey.pem 2> /dev/null
  200. chmod 644 $certPath/fullchain.pem 2> /dev/null
  201. # Set certificate for panel
  202. local webCertFile="/root/cert/${domain}/fullchain.pem"
  203. local webKeyFile="/root/cert/${domain}/privkey.pem"
  204. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  205. ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile" > /dev/null 2>&1
  206. echo -e "${green}SSL certificate installed and configured successfully!${plain}"
  207. return 0
  208. else
  209. echo -e "${yellow}Certificate files not found${plain}"
  210. return 1
  211. fi
  212. }
  213. # Issue Let's Encrypt IP certificate with shortlived profile (~6 days validity)
  214. # Requires acme.sh and port 80 open for HTTP-01 challenge
  215. setup_ip_certificate() {
  216. local ipv4="$1"
  217. local ipv6="$2" # optional
  218. echo -e "${green}Setting up Let's Encrypt IP certificate (shortlived profile)...${plain}"
  219. echo -e "${yellow}Note: IP certificates are valid for ~6 days and will auto-renew.${plain}"
  220. echo -e "${yellow}Default listener is port 80. If you choose another port, ensure external port 80 forwards to it.${plain}"
  221. # Check for acme.sh
  222. if ! command -v ~/.acme.sh/acme.sh &> /dev/null; then
  223. install_acme
  224. if [ $? -ne 0 ]; then
  225. echo -e "${red}Failed to install acme.sh${plain}"
  226. return 1
  227. fi
  228. fi
  229. # Validate IP address
  230. if [[ -z "$ipv4" ]]; then
  231. echo -e "${red}IPv4 address is required${plain}"
  232. return 1
  233. fi
  234. if ! is_ipv4 "$ipv4"; then
  235. echo -e "${red}Invalid IPv4 address: $ipv4${plain}"
  236. return 1
  237. fi
  238. # Create certificate directory
  239. local certDir="/root/cert/ip"
  240. mkdir -p "$certDir"
  241. # Build domain arguments
  242. local domain_args="-d ${ipv4}"
  243. if [[ -n "$ipv6" ]] && is_ipv6 "$ipv6"; then
  244. domain_args="${domain_args} -d ${ipv6}"
  245. echo -e "${green}Including IPv6 address: ${ipv6}${plain}"
  246. fi
  247. # Set reload command for auto-renewal (add || true so it doesn't fail if service stopped)
  248. local reloadCmd="systemctl restart x-ui 2>/dev/null || rc-service x-ui restart 2>/dev/null || true"
  249. # Choose port for HTTP-01 listener (default 80, prompt override)
  250. local WebPort=""
  251. read -rp "Port to use for ACME HTTP-01 listener (default 80): " WebPort
  252. WebPort="${WebPort:-80}"
  253. if ! [[ "${WebPort}" =~ ^[0-9]+$ ]] || ((WebPort < 1 || WebPort > 65535)); then
  254. echo -e "${red}Invalid port provided. Falling back to 80.${plain}"
  255. WebPort=80
  256. fi
  257. echo -e "${green}Using port ${WebPort} for standalone validation.${plain}"
  258. if [[ "${WebPort}" -ne 80 ]]; then
  259. echo -e "${yellow}Reminder: Let's Encrypt still connects on port 80; forward external port 80 to ${WebPort}.${plain}"
  260. fi
  261. # Ensure chosen port is available
  262. while true; do
  263. if is_port_in_use "${WebPort}"; then
  264. echo -e "${yellow}Port ${WebPort} is currently in use.${plain}"
  265. local alt_port=""
  266. read -rp "Enter another port for acme.sh standalone listener (leave empty to abort): " alt_port
  267. alt_port="${alt_port// /}"
  268. if [[ -z "${alt_port}" ]]; then
  269. echo -e "${red}Port ${WebPort} is busy; cannot proceed.${plain}"
  270. return 1
  271. fi
  272. if ! [[ "${alt_port}" =~ ^[0-9]+$ ]] || ((alt_port < 1 || alt_port > 65535)); then
  273. echo -e "${red}Invalid port provided.${plain}"
  274. return 1
  275. fi
  276. WebPort="${alt_port}"
  277. continue
  278. else
  279. echo -e "${green}Port ${WebPort} is free and ready for standalone validation.${plain}"
  280. break
  281. fi
  282. done
  283. # Issue certificate with shortlived profile
  284. echo -e "${green}Issuing IP certificate for ${ipv4}...${plain}"
  285. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt --force > /dev/null 2>&1
  286. ~/.acme.sh/acme.sh --issue \
  287. ${domain_args} \
  288. --standalone \
  289. --server letsencrypt \
  290. --certificate-profile shortlived \
  291. --days 6 \
  292. --httpport ${WebPort} \
  293. --force
  294. if [ $? -ne 0 ]; then
  295. echo -e "${red}Failed to issue IP certificate${plain}"
  296. echo -e "${yellow}Please ensure port ${WebPort} is reachable (or forwarded from external port 80)${plain}"
  297. # Cleanup acme.sh data for both IPv4 and IPv6 if specified
  298. rm -rf ~/.acme.sh/${ipv4} 2> /dev/null
  299. [[ -n "$ipv6" ]] && rm -rf ~/.acme.sh/${ipv6} 2> /dev/null
  300. rm -rf ${certDir} 2> /dev/null
  301. return 1
  302. fi
  303. echo -e "${green}Certificate issued successfully, installing...${plain}"
  304. # Install certificate
  305. # Note: acme.sh may report "Reload error" and exit non-zero if reloadcmd fails,
  306. # but the cert files are still installed. We check for files instead of exit code.
  307. ~/.acme.sh/acme.sh --installcert -d ${ipv4} \
  308. --key-file "${certDir}/privkey.pem" \
  309. --fullchain-file "${certDir}/fullchain.pem" \
  310. --reloadcmd "${reloadCmd}" 2>&1 || true
  311. # Verify certificate files exist (don't rely on exit code - reloadcmd failure causes non-zero)
  312. if [[ ! -f "${certDir}/fullchain.pem" || ! -f "${certDir}/privkey.pem" ]]; then
  313. echo -e "${red}Certificate files not found after installation${plain}"
  314. # Cleanup acme.sh data for both IPv4 and IPv6 if specified
  315. rm -rf ~/.acme.sh/${ipv4} 2> /dev/null
  316. [[ -n "$ipv6" ]] && rm -rf ~/.acme.sh/${ipv6} 2> /dev/null
  317. rm -rf ${certDir} 2> /dev/null
  318. return 1
  319. fi
  320. echo -e "${green}Certificate files installed successfully${plain}"
  321. # Enable auto-upgrade for acme.sh (ensures cron job runs)
  322. ~/.acme.sh/acme.sh --upgrade --auto-upgrade > /dev/null 2>&1
  323. chmod 600 ${certDir}/privkey.pem 2> /dev/null
  324. chmod 644 ${certDir}/fullchain.pem 2> /dev/null
  325. # Configure panel to use the certificate
  326. echo -e "${green}Setting certificate paths for the panel...${plain}"
  327. ${xui_folder}/x-ui cert -webCert "${certDir}/fullchain.pem" -webCertKey "${certDir}/privkey.pem"
  328. if [ $? -ne 0 ]; then
  329. echo -e "${yellow}Warning: Could not set certificate paths automatically.${plain}"
  330. echo -e "${yellow}You may need to set them manually in the panel settings.${plain}"
  331. echo -e "${yellow}Cert path: ${certDir}/fullchain.pem${plain}"
  332. echo -e "${yellow}Key path: ${certDir}/privkey.pem${plain}"
  333. else
  334. echo -e "${green}Certificate paths set successfully!${plain}"
  335. fi
  336. echo -e "${green}IP certificate installed and configured successfully!${plain}"
  337. echo -e "${green}Certificate valid for ~6 days, auto-renews via acme.sh cron job.${plain}"
  338. echo -e "${yellow}Panel will automatically restart after each renewal.${plain}"
  339. return 0
  340. }
  341. # Comprehensive manual SSL certificate issuance via acme.sh
  342. ssl_cert_issue() {
  343. local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep 'webBasePath:' | awk -F': ' '{print $2}' | tr -d '[:space:]' | sed 's#^/##')
  344. local existing_port=$(${xui_folder}/x-ui setting -show true | grep 'port:' | awk -F': ' '{print $2}' | tr -d '[:space:]')
  345. # check for acme.sh first
  346. if ! command -v ~/.acme.sh/acme.sh &> /dev/null; then
  347. echo "acme.sh could not be found. Installing now..."
  348. cd ~ || return 1
  349. curl -s https://get.acme.sh | sh
  350. if [ $? -ne 0 ]; then
  351. echo -e "${red}Failed to install acme.sh${plain}"
  352. return 1
  353. else
  354. echo -e "${green}acme.sh installed successfully${plain}"
  355. fi
  356. fi
  357. # get the domain here, and we need to verify it
  358. local domain=""
  359. while true; do
  360. read -rp "Please enter your domain name: " domain
  361. domain="${domain// /}" # Trim whitespace
  362. if [[ -z "$domain" ]]; then
  363. echo -e "${red}Domain name cannot be empty. Please try again.${plain}"
  364. continue
  365. fi
  366. if ! is_domain "$domain"; then
  367. echo -e "${red}Invalid domain format: ${domain}. Please enter a valid domain name.${plain}"
  368. continue
  369. fi
  370. break
  371. done
  372. echo -e "${green}Your domain is: ${domain}, checking it...${plain}"
  373. SSL_ISSUED_DOMAIN="${domain}"
  374. # detect existing certificate and reuse it if present
  375. local cert_exists=0
  376. if ~/.acme.sh/acme.sh --list 2> /dev/null | awk '{print $1}' | grep -Fxq "${domain}"; then
  377. cert_exists=1
  378. local certInfo=$(~/.acme.sh/acme.sh --list 2> /dev/null | grep -F "${domain}")
  379. echo -e "${yellow}Existing certificate found for ${domain}, will reuse it.${plain}"
  380. [[ -n "${certInfo}" ]] && echo "$certInfo"
  381. else
  382. echo -e "${green}Your domain is ready for issuing certificates now...${plain}"
  383. fi
  384. # create a directory for the certificate
  385. certPath="/root/cert/${domain}"
  386. if [ ! -d "$certPath" ]; then
  387. mkdir -p "$certPath"
  388. else
  389. rm -rf "$certPath"
  390. mkdir -p "$certPath"
  391. fi
  392. # get the port number for the standalone server
  393. local WebPort=80
  394. read -rp "Please choose which port to use (default is 80): " WebPort
  395. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  396. echo -e "${yellow}Your input ${WebPort} is invalid, will use default port 80.${plain}"
  397. WebPort=80
  398. fi
  399. echo -e "${green}Will use port: ${WebPort} to issue certificates. Please make sure this port is open.${plain}"
  400. # Stop panel temporarily
  401. echo -e "${yellow}Stopping panel temporarily...${plain}"
  402. systemctl stop x-ui 2> /dev/null || rc-service x-ui stop 2> /dev/null
  403. if [[ ${cert_exists} -eq 0 ]]; then
  404. # issue the certificate
  405. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt --force
  406. ~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport ${WebPort} --force
  407. if [ $? -ne 0 ]; then
  408. echo -e "${red}Issuing certificate failed, please check logs.${plain}"
  409. rm -rf ~/.acme.sh/${domain}
  410. systemctl start x-ui 2> /dev/null || rc-service x-ui start 2> /dev/null
  411. return 1
  412. else
  413. echo -e "${green}Issuing certificate succeeded, installing certificates...${plain}"
  414. fi
  415. else
  416. echo -e "${green}Using existing certificate, installing certificates...${plain}"
  417. fi
  418. # Setup reload command
  419. reloadCmd="systemctl restart x-ui || rc-service x-ui restart"
  420. echo -e "${green}Default --reloadcmd for ACME is: ${yellow}systemctl restart x-ui || rc-service x-ui restart${plain}"
  421. echo -e "${green}This command will run on every certificate issue and renew.${plain}"
  422. read -rp "Would you like to modify --reloadcmd for ACME? (y/n): " setReloadcmd
  423. if [[ "$setReloadcmd" == "y" || "$setReloadcmd" == "Y" ]]; then
  424. echo -e "\n${green}\t1.${plain} Preset: systemctl reload nginx ; systemctl restart x-ui"
  425. echo -e "${green}\t2.${plain} Input your own command"
  426. echo -e "${green}\t0.${plain} Keep default reloadcmd"
  427. read -rp "Choose an option: " choice
  428. case "$choice" in
  429. 1)
  430. echo -e "${green}Reloadcmd is: systemctl reload nginx ; systemctl restart x-ui${plain}"
  431. reloadCmd="systemctl reload nginx ; systemctl restart x-ui"
  432. ;;
  433. 2)
  434. echo -e "${yellow}It's recommended to put x-ui restart at the end${plain}"
  435. read -rp "Please enter your custom reloadcmd: " reloadCmd
  436. echo -e "${green}Reloadcmd is: ${reloadCmd}${plain}"
  437. ;;
  438. *)
  439. echo -e "${green}Keeping default reloadcmd${plain}"
  440. ;;
  441. esac
  442. fi
  443. # install the certificate
  444. local installOutput=""
  445. installOutput=$(~/.acme.sh/acme.sh --installcert -d ${domain} \
  446. --key-file /root/cert/${domain}/privkey.pem \
  447. --fullchain-file /root/cert/${domain}/fullchain.pem --reloadcmd "${reloadCmd}" 2>&1)
  448. local installRc=$?
  449. echo "${installOutput}"
  450. local installWroteFiles=0
  451. if echo "${installOutput}" | grep -q "Installing key to:" && echo "${installOutput}" | grep -q "Installing full chain to:"; then
  452. installWroteFiles=1
  453. fi
  454. if [[ -f "/root/cert/${domain}/privkey.pem" && -f "/root/cert/${domain}/fullchain.pem" && (${installRc} -eq 0 || ${installWroteFiles} -eq 1) ]]; then
  455. echo -e "${green}Installing certificate succeeded, enabling auto renew...${plain}"
  456. else
  457. echo -e "${red}Installing certificate failed, exiting.${plain}"
  458. if [[ ${cert_exists} -eq 0 ]]; then
  459. rm -rf ~/.acme.sh/${domain}
  460. fi
  461. systemctl start x-ui 2> /dev/null || rc-service x-ui start 2> /dev/null
  462. return 1
  463. fi
  464. # enable auto-renew
  465. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  466. if [ $? -ne 0 ]; then
  467. echo -e "${yellow}Auto renew setup had issues, certificate details:${plain}"
  468. ls -lah /root/cert/${domain}/
  469. chmod 600 $certPath/privkey.pem
  470. chmod 644 $certPath/fullchain.pem
  471. else
  472. echo -e "${green}Auto renew succeeded, certificate details:${plain}"
  473. ls -lah /root/cert/${domain}/
  474. chmod 600 $certPath/privkey.pem
  475. chmod 644 $certPath/fullchain.pem
  476. fi
  477. # Restart panel
  478. systemctl start x-ui 2> /dev/null || rc-service x-ui start 2> /dev/null
  479. # Prompt user to set panel paths after successful certificate installation
  480. read -rp "Would you like to set this certificate for the panel? (y/n): " setPanel
  481. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  482. local webCertFile="/root/cert/${domain}/fullchain.pem"
  483. local webKeyFile="/root/cert/${domain}/privkey.pem"
  484. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  485. ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  486. echo -e "${green}Certificate paths set for the panel${plain}"
  487. echo -e "${green}Certificate File: $webCertFile${plain}"
  488. echo -e "${green}Private Key File: $webKeyFile${plain}"
  489. echo ""
  490. echo -e "${green}Access URL: https://${domain}:${existing_port}/${existing_webBasePath}${plain}"
  491. echo -e "${yellow}Panel will restart to apply SSL certificate...${plain}"
  492. systemctl restart x-ui 2> /dev/null || rc-service x-ui restart 2> /dev/null
  493. else
  494. echo -e "${red}Error: Certificate or private key file not found for domain: $domain.${plain}"
  495. fi
  496. else
  497. echo -e "${yellow}Skipping panel path setting.${plain}"
  498. fi
  499. return 0
  500. }
  501. # Unified interactive SSL setup (domain or IP)
  502. # Sets global `SSL_HOST` to the chosen domain/IP
  503. prompt_and_setup_ssl() {
  504. local panel_port="$1"
  505. local web_base_path="$2" # expected without leading slash
  506. local server_ip="$3"
  507. local ssl_choice=""
  508. echo -e "${yellow}Choose SSL certificate setup method:${plain}"
  509. echo -e "${green}1.${plain} Let's Encrypt for Domain (90-day validity, auto-renews)"
  510. echo -e "${green}2.${plain} Let's Encrypt for IP Address (6-day validity, auto-renews)"
  511. echo -e "${green}3.${plain} Custom SSL Certificate (Path to existing files)"
  512. echo -e "${green}4.${plain} Skip SSL (advanced — behind reverse proxy / SSH tunnel only)"
  513. echo -e "${blue}Note:${plain} Options 1 & 2 require port 80 open. Option 3 requires manual paths."
  514. echo -e "${blue}Note:${plain} Option 4 serves the panel over plain HTTP — only safe behind nginx/Caddy or an SSH tunnel."
  515. read -rp "Choose an option (default 2 for IP): " ssl_choice
  516. ssl_choice="${ssl_choice// /}" # Trim whitespace
  517. # Default to 2 (IP cert) if input is empty or invalid (not 1, 3 or 4)
  518. if [[ "$ssl_choice" != "1" && "$ssl_choice" != "3" && "$ssl_choice" != "4" ]]; then
  519. ssl_choice="2"
  520. fi
  521. case "$ssl_choice" in
  522. 1)
  523. # User chose Let's Encrypt domain option
  524. echo -e "${green}Using Let's Encrypt for domain certificate...${plain}"
  525. if ssl_cert_issue; then
  526. local cert_domain="${SSL_ISSUED_DOMAIN}"
  527. if [[ -z "${cert_domain}" ]]; then
  528. cert_domain=$(~/.acme.sh/acme.sh --list 2> /dev/null | tail -1 | awk '{print $1}')
  529. fi
  530. if [[ -n "${cert_domain}" ]]; then
  531. SSL_HOST="${cert_domain}"
  532. echo -e "${green}✓ SSL certificate configured successfully with domain: ${cert_domain}${plain}"
  533. else
  534. echo -e "${yellow}SSL setup may have completed, but domain extraction failed${plain}"
  535. SSL_HOST="${server_ip}"
  536. fi
  537. else
  538. echo -e "${red}SSL certificate setup failed for domain mode.${plain}"
  539. SSL_HOST="${server_ip}"
  540. fi
  541. ;;
  542. 2)
  543. # User chose Let's Encrypt IP certificate option
  544. echo -e "${green}Using Let's Encrypt for IP certificate (shortlived profile)...${plain}"
  545. # Ask for optional IPv6
  546. local ipv6_addr=""
  547. read -rp "Do you have an IPv6 address to include? (leave empty to skip): " ipv6_addr
  548. ipv6_addr="${ipv6_addr// /}" # Trim whitespace
  549. # Stop panel if running (port 80 needed)
  550. if [[ $release == "alpine" ]]; then
  551. rc-service x-ui stop > /dev/null 2>&1
  552. else
  553. systemctl stop x-ui > /dev/null 2>&1
  554. fi
  555. setup_ip_certificate "${server_ip}" "${ipv6_addr}"
  556. if [ $? -eq 0 ]; then
  557. SSL_HOST="${server_ip}"
  558. echo -e "${green}✓ Let's Encrypt IP certificate configured successfully${plain}"
  559. else
  560. echo -e "${red}✗ IP certificate setup failed. Please check port 80 is open.${plain}"
  561. SSL_HOST="${server_ip}"
  562. fi
  563. # Restart panel after SSL is configured (restart applies new cert settings)
  564. if [[ $release == "alpine" ]]; then
  565. rc-service x-ui restart > /dev/null 2>&1
  566. else
  567. systemctl restart x-ui > /dev/null 2>&1
  568. fi
  569. ;;
  570. 3)
  571. # User chose Custom Paths (User Provided) option
  572. echo -e "${green}Using custom existing certificate...${plain}"
  573. local custom_cert=""
  574. local custom_key=""
  575. local custom_domain=""
  576. # 3.1 Request Domain to compose Panel URL later
  577. read -rp "Please enter domain name certificate issued for: " custom_domain
  578. custom_domain="${custom_domain// /}" # Remove spaces
  579. # 3.2 Loop for Certificate Path
  580. while true; do
  581. read -rp "Input certificate path (keywords: .crt / fullchain): " custom_cert
  582. # Strip quotes if present
  583. custom_cert=$(echo "$custom_cert" | tr -d '"' | tr -d "'")
  584. if [[ -f "$custom_cert" && -r "$custom_cert" && -s "$custom_cert" ]]; then
  585. break
  586. elif [[ ! -f "$custom_cert" ]]; then
  587. echo -e "${red}Error: File does not exist! Try again.${plain}"
  588. elif [[ ! -r "$custom_cert" ]]; then
  589. echo -e "${red}Error: File exists but is not readable (check permissions)!${plain}"
  590. else
  591. echo -e "${red}Error: File is empty!${plain}"
  592. fi
  593. done
  594. # 3.3 Loop for Private Key Path
  595. while true; do
  596. read -rp "Input private key path (keywords: .key / privatekey): " custom_key
  597. # Strip quotes if present
  598. custom_key=$(echo "$custom_key" | tr -d '"' | tr -d "'")
  599. if [[ -f "$custom_key" && -r "$custom_key" && -s "$custom_key" ]]; then
  600. break
  601. elif [[ ! -f "$custom_key" ]]; then
  602. echo -e "${red}Error: File does not exist! Try again.${plain}"
  603. elif [[ ! -r "$custom_key" ]]; then
  604. echo -e "${red}Error: File exists but is not readable (check permissions)!${plain}"
  605. else
  606. echo -e "${red}Error: File is empty!${plain}"
  607. fi
  608. done
  609. # 3.4 Apply Settings via x-ui binary
  610. ${xui_folder}/x-ui cert -webCert "$custom_cert" -webCertKey "$custom_key" > /dev/null 2>&1
  611. # Set SSL_HOST for composing Panel URL
  612. if [[ -n "$custom_domain" ]]; then
  613. SSL_HOST="$custom_domain"
  614. else
  615. SSL_HOST="${server_ip}"
  616. fi
  617. echo -e "${green}✓ Custom certificate paths applied.${plain}"
  618. echo -e "${yellow}Note: You are responsible for renewing these files externally.${plain}"
  619. systemctl restart x-ui > /dev/null 2>&1 || rc-service x-ui restart > /dev/null 2>&1
  620. ;;
  621. 4)
  622. echo ""
  623. echo -e "${red}⚠ Panel will be installed WITHOUT SSL/TLS.${plain}"
  624. echo -e "${yellow}Login credentials and cookies will travel as plain HTTP.${plain}"
  625. echo -e "${yellow}Only safe when:${plain}"
  626. echo -e "${yellow} • A reverse proxy (nginx, Caddy, Traefik) terminates TLS for you, or${plain}"
  627. echo -e "${yellow} • You access the panel exclusively via SSH tunnel${plain}"
  628. echo ""
  629. SSL_SCHEME="http"
  630. SSL_HOST="${server_ip}"
  631. local bind_local=""
  632. read -rp "Bind the panel to 127.0.0.1 only? (recommended — forces SSH tunnel / reverse-proxy access) [y/N]: " bind_local
  633. if [[ "$bind_local" == "y" || "$bind_local" == "Y" ]]; then
  634. ${xui_folder}/x-ui setting -listenIP "127.0.0.1" > /dev/null 2>&1
  635. SSL_HOST="127.0.0.1"
  636. echo -e "${green}✓ Panel bound to 127.0.0.1 only. It is now unreachable from the public internet.${plain}"
  637. echo ""
  638. echo -e "${green}SSH Port Forwarding — open the panel from your local machine via:${plain}"
  639. echo -e " Standard SSH command:"
  640. echo -e " ${yellow}ssh -L 2222:127.0.0.1:${panel_port} root@${server_ip}${plain}"
  641. echo -e " If using an SSH key:"
  642. echo -e " ${yellow}ssh -i <sshkeypath> -L 2222:127.0.0.1:${panel_port} root@${server_ip}${plain}"
  643. echo -e " Then open in your browser:"
  644. echo -e " ${yellow}http://localhost:2222/${web_base_path}${plain}"
  645. echo ""
  646. echo -e "${yellow}Alternative: point a reverse proxy (nginx/Caddy) at 127.0.0.1:${panel_port} and let it terminate TLS.${plain}"
  647. else
  648. echo -e "${yellow}Panel will listen on all interfaces over plain HTTP. Make sure something else is terminating TLS in front of it.${plain}"
  649. fi
  650. systemctl restart x-ui > /dev/null 2>&1 || rc-service x-ui restart > /dev/null 2>&1
  651. echo -e "${green}✓ SSL setup skipped.${plain}"
  652. ;;
  653. *)
  654. echo -e "${red}Invalid option. Skipping SSL setup.${plain}"
  655. SSL_HOST="${server_ip}"
  656. ;;
  657. esac
  658. }
  659. config_after_update() {
  660. echo -e "${yellow}x-ui settings:${plain}"
  661. ${xui_folder}/x-ui setting -show true
  662. ${xui_folder}/x-ui migrate
  663. # Properly detect empty cert by checking if cert: line exists and has content after it
  664. local existing_cert=$(${xui_folder}/x-ui setting -getCert true 2> /dev/null | grep 'cert:' | awk -F': ' '{print $2}' | tr -d '[:space:]')
  665. local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  666. local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}' | sed 's#^/##')
  667. # Get server IP
  668. local URL_lists=(
  669. "https://api4.ipify.org"
  670. "https://ipv4.icanhazip.com"
  671. "https://v4.api.ipinfo.io/ip"
  672. "https://ipv4.myexternalip.com/raw"
  673. "https://4.ident.me"
  674. "https://check-host.net/ip"
  675. )
  676. local server_ip=""
  677. for ip_address in "${URL_lists[@]}"; do
  678. local response=$(curl -s -w "\n%{http_code}" --max-time 3 "${ip_address}" 2> /dev/null)
  679. local http_code=$(echo "$response" | tail -n1)
  680. local ip_result=$(echo "$response" | head -n-1 | tr -d '[:space:]"')
  681. if [[ "${http_code}" == "200" && "${ip_result}" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  682. server_ip="${ip_result}"
  683. break
  684. fi
  685. done
  686. if [[ -z "$server_ip" ]]; then
  687. echo -e "${yellow}Could not auto-detect server IP from any provider.${plain}"
  688. while [[ -z "$server_ip" ]]; do
  689. read -rp "Please enter your server's public IPv4 address: " server_ip
  690. server_ip="${server_ip// /}"
  691. if [[ ! "$server_ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  692. echo -e "${red}Invalid IPv4 address. Please try again.${plain}"
  693. server_ip=""
  694. fi
  695. done
  696. fi
  697. # Handle missing/short webBasePath
  698. if [[ ${#existing_webBasePath} -lt 4 ]]; then
  699. echo -e "${yellow}WebBasePath is missing or too short. Generating a new one...${plain}"
  700. local config_webBasePath=$(gen_random_string 18)
  701. ${xui_folder}/x-ui setting -webBasePath "${config_webBasePath}"
  702. existing_webBasePath="${config_webBasePath}"
  703. echo -e "${green}New WebBasePath: ${config_webBasePath}${plain}"
  704. fi
  705. # Check and prompt for SSL if missing
  706. if [[ -z "$existing_cert" ]]; then
  707. echo ""
  708. echo -e "${red}═══════════════════════════════════════════${plain}"
  709. echo -e "${red} ⚠ NO SSL CERTIFICATE DETECTED ⚠ ${plain}"
  710. echo -e "${red}═══════════════════════════════════════════${plain}"
  711. echo -e "${yellow}For security, SSL certificate is MANDATORY for all panels.${plain}"
  712. echo -e "${yellow}Let's Encrypt now supports both domains and IP addresses!${plain}"
  713. echo ""
  714. # Prompt and setup SSL (domain or IP)
  715. prompt_and_setup_ssl "${existing_port}" "${existing_webBasePath}" "${server_ip}"
  716. echo ""
  717. echo -e "${green}═══════════════════════════════════════════${plain}"
  718. echo -e "${green} Panel Access Information ${plain}"
  719. echo -e "${green}═══════════════════════════════════════════${plain}"
  720. echo -e "${green}Access URL: https://${SSL_HOST}:${existing_port}/${existing_webBasePath}${plain}"
  721. echo -e "${green}═══════════════════════════════════════════${plain}"
  722. echo -e "${yellow}⚠ SSL Certificate: Enabled and configured${plain}"
  723. else
  724. echo -e "${green}SSL certificate is already configured${plain}"
  725. # Show access URL with existing certificate
  726. local cert_domain=$(basename "$(dirname "$existing_cert")")
  727. echo ""
  728. echo -e "${green}═══════════════════════════════════════════${plain}"
  729. echo -e "${green} Panel Access Information ${plain}"
  730. echo -e "${green}═══════════════════════════════════════════${plain}"
  731. echo -e "${green}Access URL: https://${cert_domain}:${existing_port}/${existing_webBasePath}${plain}"
  732. echo -e "${green}═══════════════════════════════════════════${plain}"
  733. fi
  734. }
  735. update_x-ui() {
  736. cd ${xui_folder%/x-ui}/
  737. load_xui_env
  738. if [ -f "${xui_folder}/x-ui" ]; then
  739. current_xui_version=$(${xui_folder}/x-ui -v)
  740. echo -e "${green}Current x-ui version: ${current_xui_version}${plain}"
  741. else
  742. _fail "ERROR: Current x-ui version: unknown"
  743. fi
  744. echo -e "${green}Downloading new x-ui version...${plain}"
  745. 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/')
  746. if [[ ! -n "$tag_version" ]]; then
  747. echo -e "${yellow}Trying to fetch version with IPv4...${plain}"
  748. tag_version=$(${curl_bin} -4 -Ls "https://api.github.com/repos/MHSanaei/3x-ui/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
  749. if [[ ! -n "$tag_version" ]]; then
  750. _fail "ERROR: Failed to fetch x-ui version, it may be due to GitHub API restrictions, please try it later"
  751. fi
  752. fi
  753. echo -e "Got x-ui latest version: ${tag_version}, beginning the installation..."
  754. ${curl_bin} -fLRo ${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
  755. if [[ $? -ne 0 ]]; then
  756. echo -e "${yellow}Trying to fetch version with IPv4...${plain}"
  757. ${curl_bin} -4fLRo ${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
  758. if [[ $? -ne 0 ]]; then
  759. _fail "ERROR: Failed to download x-ui, please be sure that your server can access GitHub"
  760. fi
  761. fi
  762. if [[ -e ${xui_folder}/ ]]; then
  763. echo -e "${green}Stopping x-ui...${plain}"
  764. if [[ $release == "alpine" ]]; then
  765. if [ -f "/etc/init.d/x-ui" ]; then
  766. rc-service x-ui stop > /dev/null 2>&1
  767. rc-update del x-ui > /dev/null 2>&1
  768. echo -e "${green}Removing old service unit version...${plain}"
  769. rm -f /etc/init.d/x-ui > /dev/null 2>&1
  770. else
  771. rm x-ui-linux-$(arch).tar.gz -f > /dev/null 2>&1
  772. _fail "ERROR: x-ui service unit not installed."
  773. fi
  774. else
  775. if [ -f "${xui_service}/x-ui.service" ]; then
  776. systemctl stop x-ui > /dev/null 2>&1
  777. systemctl disable x-ui > /dev/null 2>&1
  778. echo -e "${green}Removing old systemd unit version...${plain}"
  779. rm ${xui_service}/x-ui.service -f > /dev/null 2>&1
  780. systemctl daemon-reload > /dev/null 2>&1
  781. else
  782. rm x-ui-linux-$(arch).tar.gz -f > /dev/null 2>&1
  783. _fail "ERROR: x-ui systemd unit not installed."
  784. fi
  785. fi
  786. echo -e "${green}Removing old x-ui version...${plain}"
  787. rm ${xui_folder} -f > /dev/null 2>&1
  788. rm ${xui_folder}/x-ui.service -f > /dev/null 2>&1
  789. rm ${xui_folder}/x-ui.service.debian -f > /dev/null 2>&1
  790. rm ${xui_folder}/x-ui.service.arch -f > /dev/null 2>&1
  791. rm ${xui_folder}/x-ui.service.rhel -f > /dev/null 2>&1
  792. rm ${xui_folder}/x-ui -f > /dev/null 2>&1
  793. rm ${xui_folder}/x-ui.sh -f > /dev/null 2>&1
  794. echo -e "${green}Removing old xray version...${plain}"
  795. rm ${xui_folder}/bin/xray-linux-amd64 -f > /dev/null 2>&1
  796. echo -e "${green}Removing old README and LICENSE file...${plain}"
  797. rm ${xui_folder}/bin/README.md -f > /dev/null 2>&1
  798. rm ${xui_folder}/bin/LICENSE -f > /dev/null 2>&1
  799. else
  800. rm x-ui-linux-$(arch).tar.gz -f > /dev/null 2>&1
  801. _fail "ERROR: x-ui not installed."
  802. fi
  803. echo -e "${green}Installing new x-ui version...${plain}"
  804. tar zxvf x-ui-linux-$(arch).tar.gz > /dev/null 2>&1
  805. rm x-ui-linux-$(arch).tar.gz -f > /dev/null 2>&1
  806. cd x-ui > /dev/null 2>&1
  807. chmod +x x-ui > /dev/null 2>&1
  808. # Check the system's architecture and rename the file accordingly
  809. if [[ $(arch) == "armv5" || $(arch) == "armv6" || $(arch) == "armv7" ]]; then
  810. mv bin/xray-linux-$(arch) bin/xray-linux-arm > /dev/null 2>&1
  811. chmod +x bin/xray-linux-arm > /dev/null 2>&1
  812. fi
  813. chmod +x x-ui bin/xray-linux-$(arch) > /dev/null 2>&1
  814. echo -e "${green}Downloading and installing x-ui.sh script...${plain}"
  815. ${curl_bin} -fLRo /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh > /dev/null 2>&1
  816. if [[ $? -ne 0 ]]; then
  817. echo -e "${yellow}Trying to fetch x-ui with IPv4...${plain}"
  818. ${curl_bin} -4fLRo /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh > /dev/null 2>&1
  819. if [[ $? -ne 0 ]]; then
  820. _fail "ERROR: Failed to download x-ui.sh script, please be sure that your server can access GitHub"
  821. fi
  822. fi
  823. chmod +x ${xui_folder}/x-ui.sh > /dev/null 2>&1
  824. chmod +x /usr/bin/x-ui > /dev/null 2>&1
  825. mkdir -p /var/log/x-ui > /dev/null 2>&1
  826. echo -e "${green}Changing owner...${plain}"
  827. chown -R root:root ${xui_folder} > /dev/null 2>&1
  828. if [ -f "${xui_folder}/bin/config.json" ]; then
  829. echo -e "${green}Changing on config file permissions...${plain}"
  830. chmod 640 ${xui_folder}/bin/config.json > /dev/null 2>&1
  831. fi
  832. if [[ $release == "alpine" ]]; then
  833. echo -e "${green}Downloading and installing startup unit x-ui.rc...${plain}"
  834. ${curl_bin} -fLRo /etc/init.d/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.rc > /dev/null 2>&1
  835. if [[ $? -ne 0 ]]; then
  836. ${curl_bin} -4fLRo /etc/init.d/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.rc > /dev/null 2>&1
  837. if [[ $? -ne 0 ]]; then
  838. _fail "ERROR: Failed to download startup unit x-ui.rc, please be sure that your server can access GitHub"
  839. fi
  840. fi
  841. chmod +x /etc/init.d/x-ui > /dev/null 2>&1
  842. chown root:root /etc/init.d/x-ui > /dev/null 2>&1
  843. rc-update add x-ui > /dev/null 2>&1
  844. rc-service x-ui start > /dev/null 2>&1
  845. else
  846. if [ -f "x-ui.service" ]; then
  847. echo -e "${green}Installing systemd unit...${plain}"
  848. cp -f x-ui.service ${xui_service}/ > /dev/null 2>&1
  849. if [[ $? -ne 0 ]]; then
  850. echo -e "${red}Failed to copy x-ui.service${plain}"
  851. exit 1
  852. fi
  853. else
  854. service_installed=false
  855. case "${release}" in
  856. ubuntu | debian | armbian)
  857. if [ -f "x-ui.service.debian" ]; then
  858. echo -e "${green}Installing debian-like systemd unit...${plain}"
  859. cp -f x-ui.service.debian ${xui_service}/x-ui.service > /dev/null 2>&1
  860. if [[ $? -eq 0 ]]; then
  861. service_installed=true
  862. fi
  863. fi
  864. ;;
  865. arch | manjaro | parch)
  866. if [ -f "x-ui.service.arch" ]; then
  867. echo -e "${green}Installing arch-like systemd unit...${plain}"
  868. cp -f x-ui.service.arch ${xui_service}/x-ui.service > /dev/null 2>&1
  869. if [[ $? -eq 0 ]]; then
  870. service_installed=true
  871. fi
  872. fi
  873. ;;
  874. *)
  875. if [ -f "x-ui.service.rhel" ]; then
  876. echo -e "${green}Installing rhel-like systemd unit...${plain}"
  877. cp -f x-ui.service.rhel ${xui_service}/x-ui.service > /dev/null 2>&1
  878. if [[ $? -eq 0 ]]; then
  879. service_installed=true
  880. fi
  881. fi
  882. ;;
  883. esac
  884. # If service file not found in tar.gz, download from GitHub
  885. if [ "$service_installed" = false ]; then
  886. echo -e "${yellow}Service files not found in tar.gz, downloading from GitHub...${plain}"
  887. case "${release}" in
  888. ubuntu | debian | armbian)
  889. ${curl_bin} -4fLRo ${xui_service}/x-ui.service https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.service.debian > /dev/null 2>&1
  890. ;;
  891. arch | manjaro | parch)
  892. ${curl_bin} -4fLRo ${xui_service}/x-ui.service https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.service.arch > /dev/null 2>&1
  893. ;;
  894. *)
  895. ${curl_bin} -4fLRo ${xui_service}/x-ui.service https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.service.rhel > /dev/null 2>&1
  896. ;;
  897. esac
  898. if [[ $? -ne 0 ]]; then
  899. echo -e "${red}Failed to install x-ui.service from GitHub${plain}"
  900. exit 1
  901. fi
  902. fi
  903. fi
  904. chown root:root ${xui_service}/x-ui.service > /dev/null 2>&1
  905. chmod 644 ${xui_service}/x-ui.service > /dev/null 2>&1
  906. systemctl daemon-reload > /dev/null 2>&1
  907. systemctl enable x-ui > /dev/null 2>&1
  908. systemctl start x-ui > /dev/null 2>&1
  909. fi
  910. config_after_update
  911. echo -e "${green}x-ui ${tag_version}${plain} updating finished, it is running now..."
  912. echo -e ""
  913. echo -e "┌───────────────────────────────────────────────────────┐
  914. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  915. │ │
  916. │ ${blue}x-ui${plain} - Admin Management Script │
  917. │ ${blue}x-ui start${plain} - Start │
  918. │ ${blue}x-ui stop${plain} - Stop │
  919. │ ${blue}x-ui restart${plain} - Restart │
  920. │ ${blue}x-ui status${plain} - Current Status │
  921. │ ${blue}x-ui settings${plain} - Current Settings │
  922. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  923. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  924. │ ${blue}x-ui log${plain} - Check logs │
  925. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  926. │ ${blue}x-ui update${plain} - Update │
  927. │ ${blue}x-ui legacy${plain} - Legacy version │
  928. │ ${blue}x-ui install${plain} - Install │
  929. │ ${blue}x-ui uninstall${plain} - Uninstall │
  930. └───────────────────────────────────────────────────────┘"
  931. }
  932. echo -e "${green}Running...${plain}"
  933. install_base
  934. update_x-ui $1