install.sh 46 KB

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