1
0

install.sh 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  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. # check root
  10. [[ $EUID -ne 0 ]] && echo -e "${red}Fatal error: ${plain} Please run this script with root privilege \n " && exit 1
  11. # Check OS and set release variable
  12. if [[ -f /etc/os-release ]]; then
  13. source /etc/os-release
  14. release=$ID
  15. elif [[ -f /usr/lib/os-release ]]; then
  16. source /usr/lib/os-release
  17. release=$ID
  18. else
  19. echo "Failed to check the system OS, please contact the author!" >&2
  20. exit 1
  21. fi
  22. echo "The OS release is: $release"
  23. arch() {
  24. case "$(uname -m)" in
  25. x86_64 | x64 | amd64) echo 'amd64' ;;
  26. i*86 | x86) echo '386' ;;
  27. armv8* | armv8 | arm64 | aarch64) echo 'arm64' ;;
  28. armv7* | armv7 | arm) echo 'armv7' ;;
  29. armv6* | armv6) echo 'armv6' ;;
  30. armv5* | armv5) echo 'armv5' ;;
  31. s390x) echo 's390x' ;;
  32. *) echo -e "${green}Unsupported CPU architecture! ${plain}" && rm -f "$(realpath "$0")" && exit 1 ;;
  33. esac
  34. }
  35. echo "Arch: $(arch)"
  36. # Non-interactive mode: triggered explicitly via XUI_NONINTERACTIVE=1, or
  37. # implicitly when stdin is not a TTY (e.g. `curl ... | bash`, cloud-init).
  38. # In this mode every prompt below is replaced by an env var or a sane default.
  39. if [[ "${XUI_NONINTERACTIVE:-0}" == "1" ]] || [[ ! -t 0 ]]; then
  40. NONINTERACTIVE=1
  41. else
  42. NONINTERACTIVE=0
  43. fi
  44. export NONINTERACTIVE
  45. # Simple helpers
  46. is_ipv4() {
  47. [[ "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] && return 0 || return 1
  48. }
  49. is_ipv6() {
  50. [[ "$1" =~ : ]] && return 0 || return 1
  51. }
  52. is_ip() {
  53. is_ipv4 "$1" || is_ipv6 "$1"
  54. }
  55. is_domain() {
  56. [[ "$1" =~ ^([A-Za-z0-9](-*[A-Za-z0-9])*\.)+(xn--[a-z0-9]{2,}|[A-Za-z]{2,})$ ]] && return 0 || return 1
  57. }
  58. # acme.sh's standalone server binds IPv4 by default; --listen-v6 makes it
  59. # v6-only, which breaks HTTP-01 validation when the domain's A record points
  60. # at this host's IPv4 (#4994). Only force IPv6 when the host has no global
  61. # IPv4 address at all.
  62. acme_listen_flag() {
  63. if ip -4 addr show scope global 2> /dev/null | grep -q "inet "; then
  64. echo ""
  65. else
  66. echo "--listen-v6"
  67. fi
  68. }
  69. # Port helpers
  70. is_port_in_use() {
  71. local port="$1"
  72. if command -v ss > /dev/null 2>&1; then
  73. ss -ltn 2> /dev/null | awk -v p=":${port}$" '$4 ~ p {exit 0} END {exit 1}'
  74. return
  75. fi
  76. if command -v netstat > /dev/null 2>&1; then
  77. netstat -lnt 2> /dev/null | awk -v p=":${port} " '$4 ~ p {exit 0} END {exit 1}'
  78. return
  79. fi
  80. if command -v lsof > /dev/null 2>&1; then
  81. lsof -nP -iTCP:${port} -sTCP:LISTEN > /dev/null 2>&1 && return 0
  82. fi
  83. return 1
  84. }
  85. install_base() {
  86. case "${release}" in
  87. ubuntu | debian | armbian)
  88. apt-get update && apt-get install -y -q cron curl tar tzdata socat ca-certificates openssl
  89. ;;
  90. fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol)
  91. dnf makecache -y && dnf install -y -q cronie curl tar tzdata socat ca-certificates openssl
  92. ;;
  93. centos)
  94. if [[ "${VERSION_ID}" =~ ^7 ]]; then
  95. yum makecache -y && yum install -y cronie curl tar tzdata socat ca-certificates openssl
  96. else
  97. dnf makecache -y && dnf install -y -q cronie curl tar tzdata socat ca-certificates openssl
  98. fi
  99. ;;
  100. arch | manjaro | parch)
  101. pacman -Sy --noconfirm cronie curl tar tzdata socat ca-certificates openssl
  102. ;;
  103. opensuse-tumbleweed | opensuse-leap)
  104. zypper refresh && zypper -q install -y cron curl tar timezone socat ca-certificates openssl
  105. ;;
  106. alpine)
  107. apk update && apk add dcron curl tar tzdata socat ca-certificates openssl
  108. ;;
  109. *)
  110. apt-get update && apt-get install -y -q cron curl tar tzdata socat ca-certificates openssl
  111. ;;
  112. esac
  113. }
  114. gen_random_string() {
  115. local length="$1"
  116. openssl rand -base64 $((length * 2)) \
  117. | tr -dc 'a-zA-Z0-9' \
  118. | head -c "$length"
  119. }
  120. # prompt_or_default VARNAME "prompt text" "default" [ENV_NAME]
  121. # Interactive: read into VARNAME. Non-interactive: VARNAME = ${ENV_NAME:-default}.
  122. # ENV_NAME defaults to VARNAME when omitted. Keeps every interactive prompt
  123. # string byte-for-byte identical to the original `read -rp`.
  124. prompt_or_default() {
  125. local __var="$1" __prompt="$2" __default="$3" __env="${4:-$1}"
  126. if [[ "$NONINTERACTIVE" == "1" ]]; then
  127. printf -v "$__var" '%s' "${!__env:-$__default}"
  128. else
  129. # shellcheck disable=SC2229
  130. read -rp "$__prompt" "$__var"
  131. fi
  132. }
  133. # write_install_result <user> <pass> <port> <webpath> <scheme> <host> <token> <dbtype>
  134. # Persists a parseable, root-only credentials file consumed by cloud-init/MOTD.
  135. # Values are written with printf '%q' so a pinned password/username containing
  136. # spaces, quotes, $(...) or backticks is shell-escaped and the file stays safely
  137. # source-able (consumers do '. install-result.env'). For the alphanumeric random
  138. # values gen_random_string emits, %q is a no-op. This is a DIFFERENT file from the
  139. # Postgres env file (/etc/default/x-ui).
  140. write_install_result() {
  141. local u="$1" p="$2" port="$3" wbp="$4" scheme="$5" host="$6" token="$7" dbtype="$8"
  142. local result_file="/etc/x-ui/install-result.env"
  143. local url_host="${host:-SERVER_IP_UNKNOWN}"
  144. install -d -m 700 /etc/x-ui 2> /dev/null
  145. local prev_umask
  146. prev_umask=$(umask)
  147. umask 077
  148. if ! {
  149. printf 'XUI_USERNAME=%q\n' "$u"
  150. printf 'XUI_PASSWORD=%q\n' "$p"
  151. printf 'XUI_PANEL_PORT=%q\n' "$port"
  152. printf 'XUI_WEB_BASE_PATH=%q\n' "$wbp"
  153. printf 'XUI_ACCESS_URL=%q\n' "${scheme}://${url_host}:${port}/${wbp}"
  154. printf 'XUI_API_TOKEN=%q\n' "$token"
  155. printf 'XUI_DB_TYPE=%q\n' "$dbtype"
  156. } > "$result_file"; then
  157. umask "$prev_umask"
  158. echo -e "${yellow}Warning: failed to write ${result_file}.${plain}" >&2
  159. return 1
  160. fi
  161. umask "$prev_umask"
  162. chmod 600 "$result_file" 2> /dev/null
  163. chown root:root "$result_file" 2> /dev/null || true
  164. echo -e "${green}Install result written to ${result_file} (mode 600).${plain}"
  165. }
  166. # RHEL-family initdb writes pg_hba.conf host rules with ident auth, which
  167. # compares the OS username against the Postgres role and always rejects the
  168. # randomly generated panel role over TCP (#5806). Prepend password-auth rules
  169. # scoped to the panel database; first match wins, and md5 also accepts
  170. # scram-sha-256-stored verifiers, so this works on every supported distro.
  171. pg_ensure_hba_password_auth() {
  172. local pg_db="$1"
  173. local hba_file
  174. hba_file=$(sudo -u postgres psql -tAc 'SHOW hba_file' 2> /dev/null | tr -d '[:space:]')
  175. [[ -n "${hba_file}" && -f "${hba_file}" ]] || return 0
  176. grep -Eq "^host[[:space:]]+${pg_db}[[:space:]]" "${hba_file}" && return 0
  177. local tmp
  178. tmp=$(mktemp) || return 1
  179. {
  180. echo "# Added by 3x-ui: allow password logins for the panel database."
  181. echo "host ${pg_db} all 127.0.0.1/32 md5"
  182. echo "host ${pg_db} all ::1/128 md5"
  183. cat "${hba_file}"
  184. } > "${tmp}" || {
  185. rm -f "${tmp}"
  186. return 1
  187. }
  188. cat "${tmp}" > "${hba_file}" || {
  189. rm -f "${tmp}"
  190. return 1
  191. }
  192. rm -f "${tmp}"
  193. sudo -u postgres psql -tAc 'SELECT pg_reload_conf()' > /dev/null 2>&1 || true
  194. }
  195. install_postgres_local() {
  196. local pg_user pg_pass
  197. pg_pass=$(gen_random_string 24)
  198. local pg_db="xui"
  199. local pg_host="127.0.0.1"
  200. local pg_port="5432"
  201. case "${release}" in
  202. ubuntu | debian | armbian)
  203. apt-get update >&2 && apt-get install -y -q postgresql >&2 || return 1
  204. ;;
  205. fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol)
  206. dnf install -y -q postgresql-server postgresql-contrib >&2 || return 1
  207. [[ -d /var/lib/pgsql/data && -f /var/lib/pgsql/data/PG_VERSION ]] || postgresql-setup --initdb >&2 || return 1
  208. ;;
  209. centos)
  210. if [[ "${VERSION_ID}" =~ ^7 ]]; then
  211. yum install -y postgresql-server postgresql-contrib >&2 || return 1
  212. else
  213. dnf install -y -q postgresql-server postgresql-contrib >&2 || return 1
  214. fi
  215. [[ -d /var/lib/pgsql/data && -f /var/lib/pgsql/data/PG_VERSION ]] || postgresql-setup --initdb >&2 || return 1
  216. ;;
  217. arch | manjaro | parch)
  218. pacman -Sy --noconfirm postgresql >&2 || return 1
  219. if [[ ! -f /var/lib/postgres/data/PG_VERSION ]]; then
  220. sudo -u postgres initdb -D /var/lib/postgres/data >&2 || return 1
  221. fi
  222. ;;
  223. opensuse-tumbleweed | opensuse-leap)
  224. zypper -q install -y postgresql-server postgresql-contrib >&2 || return 1
  225. if [[ ! -f /var/lib/pgsql/data/PG_VERSION ]]; then
  226. install -d -o postgres -g postgres -m 700 /var/lib/pgsql/data >&2 || return 1
  227. su - postgres -c "initdb -D /var/lib/pgsql/data" >&2 || return 1
  228. fi
  229. ;;
  230. alpine)
  231. apk add --no-cache postgresql postgresql-contrib >&2 || return 1
  232. if [[ ! -f /var/lib/postgresql/data/PG_VERSION ]]; then
  233. /etc/init.d/postgresql setup >&2 || return 1
  234. fi
  235. rc-update add postgresql default >&2 2> /dev/null || true
  236. rc-service postgresql start >&2 || return 1
  237. ;;
  238. *)
  239. echo -e "${red}Unsupported distro for automatic PostgreSQL install: ${release}${plain}" >&2
  240. return 1
  241. ;;
  242. esac
  243. if [[ "${release}" != "alpine" ]]; then
  244. systemctl enable --now postgresql >&2 || return 1
  245. fi
  246. # Wait briefly for the server to accept connections.
  247. local i
  248. for i in 1 2 3 4 5; do
  249. sudo -u postgres psql -tAc 'SELECT 1' > /dev/null 2>&1 && break
  250. sleep 1
  251. done
  252. local existing_owner=""
  253. existing_owner=$(sudo -u postgres psql -tAc \
  254. "SELECT pg_catalog.pg_get_userbyid(datdba) FROM pg_database WHERE datname='${pg_db}'" 2> /dev/null \
  255. | tr -d '[:space:]')
  256. if [[ -n "${existing_owner}" && "${existing_owner}" != "postgres" ]]; then
  257. pg_user="${existing_owner}"
  258. else
  259. pg_user=$(gen_random_string 8)
  260. fi
  261. # Idempotent role/db creation. Identifiers are double-quoted because a
  262. # random username may start with a digit, which Postgres rejects unquoted.
  263. sudo -u postgres psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='${pg_user}'" 2> /dev/null \
  264. | grep -q 1 \
  265. || sudo -u postgres psql -c "CREATE USER \"${pg_user}\" WITH PASSWORD '${pg_pass}';" >&2 || return 1
  266. sudo -u postgres psql -tAc "SELECT 1 FROM pg_database WHERE datname='${pg_db}'" 2> /dev/null \
  267. | grep -q 1 \
  268. || sudo -u postgres psql -c "CREATE DATABASE \"${pg_db}\" OWNER \"${pg_user}\";" >&2 || return 1
  269. sudo -u postgres psql -c "ALTER USER \"${pg_user}\" WITH PASSWORD '${pg_pass}';" >&2 || return 1
  270. pg_ensure_hba_password_auth "${pg_db}" \
  271. || echo -e "${yellow}Warning: could not update pg_hba.conf; PostgreSQL may reject the panel's TCP login (ident auth).${plain}" >&2
  272. local pg_pass_enc
  273. pg_pass_enc=$(printf '%s' "${pg_pass}" | sed -e 's/%/%25/g' -e 's/:/%3A/g' -e 's/@/%40/g' -e 's|/|%2F|g' -e 's/?/%3F/g' -e 's/#/%23/g')
  274. if [[ -n "${PG_CRED_FILE:-}" ]]; then
  275. local prev_umask
  276. prev_umask=$(umask)
  277. umask 077
  278. if ! cat > "${PG_CRED_FILE}" << EOF; then
  279. PG_USER=${pg_user}
  280. PG_PASS=${pg_pass}
  281. PG_HOST=${pg_host}
  282. PG_PORT=${pg_port}
  283. PG_DB=${pg_db}
  284. EOF
  285. umask "${prev_umask}"
  286. echo -e "${red}Failed to write PostgreSQL credentials to ${PG_CRED_FILE}${plain}" >&2
  287. return 1
  288. fi
  289. umask "${prev_umask}"
  290. fi
  291. echo "postgres://${pg_user}:${pg_pass_enc}@${pg_host}:${pg_port}/${pg_db}?sslmode=disable"
  292. return 0
  293. }
  294. ensure_pg_client() {
  295. if command -v pg_dump > /dev/null 2>&1 && command -v pg_restore > /dev/null 2>&1; then
  296. return 0
  297. fi
  298. echo -e "${yellow}Installing PostgreSQL client tools (pg_dump/pg_restore) for in-panel backup...${plain}" >&2
  299. case "${release}" in
  300. ubuntu | debian | armbian)
  301. apt-get update >&2 && apt-get install -y -q postgresql-client >&2 || return 1
  302. ;;
  303. fedora | amzn | virtuozzo | rhel | almalinux | rocky | ol)
  304. dnf install -y -q postgresql >&2 || return 1
  305. ;;
  306. centos)
  307. if [[ "${VERSION_ID}" =~ ^7 ]]; then
  308. yum install -y postgresql >&2 || return 1
  309. else
  310. dnf install -y -q postgresql >&2 || return 1
  311. fi
  312. ;;
  313. arch | manjaro | parch)
  314. pacman -Sy --noconfirm postgresql >&2 || return 1
  315. ;;
  316. opensuse-tumbleweed | opensuse-leap)
  317. zypper -q install -y postgresql >&2 || return 1
  318. ;;
  319. alpine)
  320. apk add --no-cache postgresql-client >&2 || return 1
  321. ;;
  322. *)
  323. return 1
  324. ;;
  325. esac
  326. command -v pg_dump > /dev/null 2>&1 && command -v pg_restore > /dev/null 2>&1
  327. }
  328. install_acme() {
  329. echo -e "${green}Installing acme.sh for SSL certificate management...${plain}"
  330. cd ~ || return 1
  331. curl -s https://get.acme.sh | sh > /dev/null 2>&1
  332. if [ $? -ne 0 ]; then
  333. echo -e "${red}Failed to install acme.sh${plain}"
  334. return 1
  335. else
  336. echo -e "${green}acme.sh installed successfully${plain}"
  337. fi
  338. return 0
  339. }
  340. setup_ssl_certificate() {
  341. local domain="$1"
  342. local server_ip="$2"
  343. local existing_port="$3"
  344. local existing_webBasePath="$4"
  345. echo -e "${green}Setting up SSL certificate...${plain}"
  346. # Check if acme.sh is installed
  347. if ! command -v ~/.acme.sh/acme.sh &> /dev/null; then
  348. install_acme
  349. if [ $? -ne 0 ]; then
  350. echo -e "${yellow}Failed to install acme.sh, skipping SSL setup${plain}"
  351. return 1
  352. fi
  353. fi
  354. # Create certificate directory
  355. local certPath="/root/cert/${domain}"
  356. mkdir -p "$certPath"
  357. # Issue certificate
  358. echo -e "${green}Issuing SSL certificate for ${domain}...${plain}"
  359. echo -e "${yellow}Note: Port 80 must be open and accessible from the internet${plain}"
  360. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt --force > /dev/null 2>&1
  361. ~/.acme.sh/acme.sh --issue -d ${domain} $(acme_listen_flag) --standalone --httpport 80 --force
  362. if [ $? -ne 0 ]; then
  363. echo -e "${yellow}Failed to issue certificate for ${domain}${plain}"
  364. echo -e "${yellow}Please ensure port 80 is open and try again later with: x-ui${plain}"
  365. rm -rf ~/.acme.sh/${domain} ~/.acme.sh/${domain}_ecc 2> /dev/null
  366. rm -rf "$certPath" 2> /dev/null
  367. return 1
  368. fi
  369. # Install certificate
  370. ~/.acme.sh/acme.sh --installcert --force -d ${domain} \
  371. --key-file /root/cert/${domain}/privkey.pem \
  372. --fullchain-file /root/cert/${domain}/fullchain.pem \
  373. --reloadcmd "systemctl restart x-ui" > /dev/null 2>&1
  374. if [ $? -ne 0 ]; then
  375. echo -e "${yellow}Failed to install certificate${plain}"
  376. return 1
  377. fi
  378. # Enable auto-renew
  379. ~/.acme.sh/acme.sh --upgrade --auto-upgrade > /dev/null 2>&1
  380. # Secure permissions: private key readable only by owner
  381. chmod 600 $certPath/privkey.pem 2> /dev/null
  382. chmod 644 $certPath/fullchain.pem 2> /dev/null
  383. # Set certificate for panel
  384. local webCertFile="/root/cert/${domain}/fullchain.pem"
  385. local webKeyFile="/root/cert/${domain}/privkey.pem"
  386. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  387. ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile" > /dev/null 2>&1
  388. echo -e "${green}SSL certificate installed and configured successfully!${plain}"
  389. return 0
  390. else
  391. echo -e "${yellow}Certificate files not found${plain}"
  392. return 1
  393. fi
  394. }
  395. # Issue Let's Encrypt IP certificate with shortlived profile (~6 days validity)
  396. # Requires acme.sh and port 80 open for HTTP-01 challenge
  397. setup_ip_certificate() {
  398. local ipv4="$1"
  399. local ipv6="$2" # optional
  400. echo -e "${green}Setting up Let's Encrypt IP certificate (shortlived profile)...${plain}"
  401. echo -e "${yellow}Note: IP certificates are valid for ~6 days and will auto-renew.${plain}"
  402. echo -e "${yellow}Default listener is port 80. If you choose another port, ensure external port 80 forwards to it.${plain}"
  403. # Check for acme.sh
  404. if ! command -v ~/.acme.sh/acme.sh &> /dev/null; then
  405. install_acme
  406. if [ $? -ne 0 ]; then
  407. echo -e "${red}Failed to install acme.sh${plain}"
  408. return 1
  409. fi
  410. fi
  411. # Validate IP address
  412. if [[ -z "$ipv4" ]]; then
  413. echo -e "${red}IPv4 address is required${plain}"
  414. return 1
  415. fi
  416. if ! is_ipv4 "$ipv4"; then
  417. echo -e "${red}Invalid IPv4 address: $ipv4${plain}"
  418. return 1
  419. fi
  420. # Create certificate directory
  421. local certDir="/root/cert/ip"
  422. mkdir -p "$certDir"
  423. # Build domain arguments
  424. local domain_args="-d ${ipv4}"
  425. if [[ -n "$ipv6" ]] && is_ipv6 "$ipv6"; then
  426. domain_args="${domain_args} -d ${ipv6}"
  427. echo -e "${green}Including IPv6 address: ${ipv6}${plain}"
  428. fi
  429. # Set reload command for auto-renewal (add || true so it doesn't fail during first install)
  430. local reloadCmd="systemctl restart x-ui 2>/dev/null || rc-service x-ui restart 2>/dev/null || true"
  431. # Choose port for HTTP-01 listener (default 80, prompt override)
  432. local WebPort=""
  433. prompt_or_default WebPort "Port to use for ACME HTTP-01 listener (default 80): " "80" XUI_ACME_HTTP_PORT
  434. WebPort="${WebPort:-80}"
  435. if ! [[ "${WebPort}" =~ ^[0-9]+$ ]] || ((WebPort < 1 || WebPort > 65535)); then
  436. echo -e "${red}Invalid port provided. Falling back to 80.${plain}"
  437. WebPort=80
  438. fi
  439. echo -e "${green}Using port ${WebPort} for standalone validation.${plain}"
  440. if [[ "${WebPort}" -ne 80 ]]; then
  441. echo -e "${yellow}Reminder: Let's Encrypt still connects on port 80; forward external port 80 to ${WebPort}.${plain}"
  442. fi
  443. # Ensure chosen port is available
  444. while true; do
  445. if is_port_in_use "${WebPort}"; then
  446. echo -e "${yellow}Port ${WebPort} is in use.${plain}"
  447. local alt_port=""
  448. if [[ "$NONINTERACTIVE" == "1" ]]; then
  449. echo -e "${red}Port ${WebPort} is busy; cannot proceed in non-interactive mode.${plain}"
  450. return 1
  451. fi
  452. read -rp "Enter another port for acme.sh standalone listener (leave empty to abort): " alt_port
  453. alt_port="${alt_port// /}"
  454. if [[ -z "${alt_port}" ]]; then
  455. echo -e "${red}Port ${WebPort} is busy; cannot proceed.${plain}"
  456. return 1
  457. fi
  458. if ! [[ "${alt_port}" =~ ^[0-9]+$ ]] || ((alt_port < 1 || alt_port > 65535)); then
  459. echo -e "${red}Invalid port provided.${plain}"
  460. return 1
  461. fi
  462. WebPort="${alt_port}"
  463. continue
  464. else
  465. echo -e "${green}Port ${WebPort} is free and ready for standalone validation.${plain}"
  466. break
  467. fi
  468. done
  469. # Issue certificate with shortlived profile
  470. echo -e "${green}Issuing IP certificate for ${ipv4}...${plain}"
  471. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt --force > /dev/null 2>&1
  472. [[ -n "${XUI_ACME_EMAIL:-}" ]] && ~/.acme.sh/acme.sh --register-account -m "${XUI_ACME_EMAIL}" > /dev/null 2>&1
  473. ~/.acme.sh/acme.sh --issue \
  474. ${domain_args} \
  475. --standalone \
  476. --server letsencrypt \
  477. --certificate-profile shortlived \
  478. --days 6 \
  479. --httpport ${WebPort} \
  480. --force
  481. if [ $? -ne 0 ]; then
  482. echo -e "${red}Failed to issue IP certificate${plain}"
  483. echo -e "${yellow}Please ensure port ${WebPort} is reachable (or forwarded from external port 80)${plain}"
  484. # Cleanup acme.sh data for both IPv4 and IPv6 if specified
  485. rm -rf ~/.acme.sh/${ipv4} ~/.acme.sh/${ipv4}_ecc 2> /dev/null
  486. [[ -n "$ipv6" ]] && rm -rf ~/.acme.sh/${ipv6} ~/.acme.sh/${ipv6}_ecc 2> /dev/null
  487. rm -rf ${certDir} 2> /dev/null
  488. return 1
  489. fi
  490. echo -e "${green}Certificate issued successfully, installing...${plain}"
  491. # Install certificate
  492. # Note: acme.sh may report "Reload error" and exit non-zero if reloadcmd fails,
  493. # but the cert files are still installed. We check for files instead of exit code.
  494. ~/.acme.sh/acme.sh --installcert --force -d ${ipv4} \
  495. --key-file "${certDir}/privkey.pem" \
  496. --fullchain-file "${certDir}/fullchain.pem" \
  497. --reloadcmd "${reloadCmd}" 2>&1 || true
  498. # Verify certificate files exist (don't rely on exit code - reloadcmd failure causes non-zero)
  499. if [[ ! -f "${certDir}/fullchain.pem" || ! -f "${certDir}/privkey.pem" ]]; then
  500. echo -e "${red}Certificate files not found after installation${plain}"
  501. # Cleanup acme.sh data for both IPv4 and IPv6 if specified
  502. rm -rf ~/.acme.sh/${ipv4} ~/.acme.sh/${ipv4}_ecc 2> /dev/null
  503. [[ -n "$ipv6" ]] && rm -rf ~/.acme.sh/${ipv6} ~/.acme.sh/${ipv6}_ecc 2> /dev/null
  504. rm -rf ${certDir} 2> /dev/null
  505. return 1
  506. fi
  507. echo -e "${green}Certificate files installed successfully${plain}"
  508. # Enable auto-upgrade for acme.sh (ensures cron job runs)
  509. ~/.acme.sh/acme.sh --upgrade --auto-upgrade > /dev/null 2>&1
  510. # Secure permissions: private key readable only by owner
  511. chmod 600 ${certDir}/privkey.pem 2> /dev/null
  512. chmod 644 ${certDir}/fullchain.pem 2> /dev/null
  513. # Configure panel to use the certificate
  514. echo -e "${green}Setting certificate paths for the panel...${plain}"
  515. ${xui_folder}/x-ui cert -webCert "${certDir}/fullchain.pem" -webCertKey "${certDir}/privkey.pem"
  516. if [ $? -ne 0 ]; then
  517. echo -e "${yellow}Warning: Could not set certificate paths automatically${plain}"
  518. echo -e "${yellow}Certificate files are at:${plain}"
  519. echo -e " Cert: ${certDir}/fullchain.pem"
  520. echo -e " Key: ${certDir}/privkey.pem"
  521. else
  522. echo -e "${green}Certificate paths configured successfully${plain}"
  523. fi
  524. echo -e "${green}IP certificate installed and configured successfully!${plain}"
  525. echo -e "${green}Certificate valid for ~6 days, auto-renews via acme.sh cron job.${plain}"
  526. echo -e "${yellow}acme.sh will automatically renew and reload x-ui before expiry.${plain}"
  527. return 0
  528. }
  529. # Comprehensive manual SSL certificate issuance via acme.sh
  530. ssl_cert_issue() {
  531. local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep 'webBasePath:' | awk -F': ' '{print $2}' | tr -d '[:space:]' | sed 's#^/##')
  532. local existing_port=$(${xui_folder}/x-ui setting -show true | grep 'port:' | awk -F': ' '{print $2}' | tr -d '[:space:]')
  533. # check for acme.sh first
  534. if ! command -v ~/.acme.sh/acme.sh &> /dev/null; then
  535. echo "acme.sh could not be found. Installing now..."
  536. cd ~ || return 1
  537. curl -s https://get.acme.sh | sh
  538. if [ $? -ne 0 ]; then
  539. echo -e "${red}Failed to install acme.sh${plain}"
  540. return 1
  541. else
  542. echo -e "${green}acme.sh installed successfully${plain}"
  543. fi
  544. fi
  545. # get the domain here, and we need to verify it
  546. local domain=""
  547. if [[ "$NONINTERACTIVE" == "1" ]]; then
  548. domain="${XUI_DOMAIN// /}"
  549. if [[ -z "$domain" ]] || ! is_domain "$domain"; then
  550. echo -e "${red}XUI_SSL_MODE=domain requires a valid XUI_DOMAIN (got: '${XUI_DOMAIN:-}').${plain}"
  551. return 1
  552. fi
  553. else
  554. while true; do
  555. read -rp "Please enter your domain name: " domain
  556. domain="${domain// /}" # Trim whitespace
  557. if [[ -z "$domain" ]]; then
  558. echo -e "${red}Domain name cannot be empty. Please try again.${plain}"
  559. continue
  560. fi
  561. if ! is_domain "$domain"; then
  562. echo -e "${red}Invalid domain format: ${domain}. Please enter a valid domain name.${plain}"
  563. continue
  564. fi
  565. break
  566. done
  567. fi
  568. echo -e "${green}Your domain is: ${domain}, checking it...${plain}"
  569. SSL_ISSUED_DOMAIN="${domain}"
  570. # detect existing certificate and reuse it only if its files are actually
  571. # present and non-empty. acme.sh stores ECC certs under ${domain}_ecc and RSA
  572. # certs under ${domain}; a failed issuance can leave a domain entry in --list
  573. # with no usable cert files, which must not be reused (it produces a 0-byte
  574. # fullchain.pem). Broken partial state is cleaned up so issuance can proceed.
  575. local cert_exists=0
  576. if ~/.acme.sh/acme.sh --list 2> /dev/null | awk '{print $1}' | grep -Fxq "${domain}"; then
  577. local acmeCertDir=""
  578. if [[ -s ~/.acme.sh/${domain}_ecc/fullchain.cer && -s ~/.acme.sh/${domain}_ecc/${domain}.key ]]; then
  579. acmeCertDir=~/.acme.sh/${domain}_ecc
  580. elif [[ -s ~/.acme.sh/${domain}/fullchain.cer && -s ~/.acme.sh/${domain}/${domain}.key ]]; then
  581. acmeCertDir=~/.acme.sh/${domain}
  582. fi
  583. if [[ -n "${acmeCertDir}" ]]; then
  584. cert_exists=1
  585. local certInfo=$(~/.acme.sh/acme.sh --list 2> /dev/null | grep -F "${domain}")
  586. echo -e "${yellow}Existing certificate found for ${domain}, will reuse it.${plain}"
  587. [[ -n "${certInfo}" ]] && echo "$certInfo"
  588. else
  589. echo -e "${yellow}Found incomplete acme.sh state for ${domain} (no valid certificate files); cleaning it up and re-issuing.${plain}"
  590. rm -rf ~/.acme.sh/${domain} ~/.acme.sh/${domain}_ecc
  591. fi
  592. fi
  593. if [[ ${cert_exists} -eq 0 ]]; then
  594. echo -e "${green}Your domain is ready for issuing certificates now...${plain}"
  595. fi
  596. # create a directory for the certificate
  597. certPath="/root/cert/${domain}"
  598. if [ ! -d "$certPath" ]; then
  599. mkdir -p "$certPath"
  600. else
  601. rm -rf "$certPath"
  602. mkdir -p "$certPath"
  603. fi
  604. # get the port number for the standalone server
  605. local WebPort=80
  606. prompt_or_default WebPort "Please choose which port to use (default is 80): " "80" XUI_ACME_HTTP_PORT
  607. if [[ -z ${WebPort} ]]; then
  608. WebPort=80
  609. elif [[ ! ${WebPort} =~ ^[1-9][0-9]*$ || ${WebPort} -gt 65535 ]]; then
  610. echo -e "${yellow}Your input ${WebPort} is invalid, will use default port 80.${plain}"
  611. WebPort=80
  612. fi
  613. echo -e "${green}Will use port: ${WebPort} to issue certificates. Please make sure this port is open.${plain}"
  614. # Stop panel temporarily
  615. echo -e "${yellow}Stopping panel temporarily...${plain}"
  616. systemctl stop x-ui 2> /dev/null || rc-service x-ui stop 2> /dev/null
  617. if [[ ${cert_exists} -eq 0 ]]; then
  618. # issue the certificate
  619. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt --force
  620. [[ -n "${XUI_ACME_EMAIL:-}" ]] && ~/.acme.sh/acme.sh --register-account -m "${XUI_ACME_EMAIL}" > /dev/null 2>&1
  621. ~/.acme.sh/acme.sh --issue -d ${domain} $(acme_listen_flag) --standalone --httpport ${WebPort} --force
  622. if [ $? -ne 0 ]; then
  623. echo -e "${red}Issuing certificate failed, please check logs.${plain}"
  624. rm -rf ~/.acme.sh/${domain} ~/.acme.sh/${domain}_ecc
  625. systemctl start x-ui 2> /dev/null || rc-service x-ui start 2> /dev/null
  626. return 1
  627. else
  628. echo -e "${green}Issuing certificate succeeded, installing certificates...${plain}"
  629. fi
  630. else
  631. echo -e "${green}Using existing certificate, installing certificates...${plain}"
  632. fi
  633. # Setup reload command
  634. reloadCmd="systemctl restart x-ui || rc-service x-ui restart"
  635. echo -e "${green}Default --reloadcmd for ACME is: ${yellow}systemctl restart x-ui || rc-service x-ui restart${plain}"
  636. echo -e "${green}This command will run on every certificate issue and renew.${plain}"
  637. if [[ "$NONINTERACTIVE" == "1" ]]; then
  638. setReloadcmd="n"
  639. else
  640. read -rp "Would you like to modify --reloadcmd for ACME? (y/n): " setReloadcmd
  641. fi
  642. if [[ "$setReloadcmd" == "y" || "$setReloadcmd" == "Y" ]]; then
  643. echo -e "\n${green}\t1.${plain} Preset: systemctl reload nginx ; systemctl restart x-ui"
  644. echo -e "${green}\t2.${plain} Input your own command"
  645. echo -e "${green}\t0.${plain} Keep default reloadcmd"
  646. read -rp "Choose an option: " choice
  647. case "$choice" in
  648. 1)
  649. echo -e "${green}Reloadcmd is: systemctl reload nginx ; systemctl restart x-ui${plain}"
  650. reloadCmd="systemctl reload nginx ; systemctl restart x-ui"
  651. ;;
  652. 2)
  653. echo -e "${yellow}It's recommended to put x-ui restart at the end${plain}"
  654. read -rp "Please enter your custom reloadcmd: " reloadCmd
  655. echo -e "${green}Reloadcmd is: ${reloadCmd}${plain}"
  656. ;;
  657. *)
  658. echo -e "${green}Keeping default reloadcmd${plain}"
  659. ;;
  660. esac
  661. fi
  662. # install the certificate
  663. local installOutput=""
  664. installOutput=$(~/.acme.sh/acme.sh --installcert --force -d ${domain} \
  665. --key-file /root/cert/${domain}/privkey.pem \
  666. --fullchain-file /root/cert/${domain}/fullchain.pem --reloadcmd "${reloadCmd}" 2>&1)
  667. local installRc=$?
  668. echo "${installOutput}"
  669. local installWroteFiles=0
  670. if echo "${installOutput}" | grep -q "Installing key to:" && echo "${installOutput}" | grep -q "Installing full chain to:"; then
  671. installWroteFiles=1
  672. fi
  673. if [[ -f "/root/cert/${domain}/privkey.pem" && -f "/root/cert/${domain}/fullchain.pem" && (${installRc} -eq 0 || ${installWroteFiles} -eq 1) ]]; then
  674. echo -e "${green}Installing certificate succeeded, enabling auto renew...${plain}"
  675. else
  676. echo -e "${red}Installing certificate failed, exiting.${plain}"
  677. if [[ ${cert_exists} -eq 0 ]]; then
  678. rm -rf ~/.acme.sh/${domain} ~/.acme.sh/${domain}_ecc
  679. fi
  680. systemctl start x-ui 2> /dev/null || rc-service x-ui start 2> /dev/null
  681. return 1
  682. fi
  683. # enable auto-renew
  684. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  685. if [ $? -ne 0 ]; then
  686. echo -e "${yellow}Auto renew setup had issues, certificate details:${plain}"
  687. ls -lah /root/cert/${domain}/
  688. # Secure permissions: private key readable only by owner
  689. chmod 600 $certPath/privkey.pem 2> /dev/null
  690. chmod 644 $certPath/fullchain.pem 2> /dev/null
  691. else
  692. echo -e "${green}Auto renew succeeded, certificate details:${plain}"
  693. ls -lah /root/cert/${domain}/
  694. # Secure permissions: private key readable only by owner
  695. chmod 600 $certPath/privkey.pem 2> /dev/null
  696. chmod 644 $certPath/fullchain.pem 2> /dev/null
  697. fi
  698. # start panel
  699. systemctl start x-ui 2> /dev/null || rc-service x-ui start 2> /dev/null
  700. # Prompt user to set panel paths after successful certificate installation
  701. if [[ "$NONINTERACTIVE" == "1" ]]; then
  702. setPanel="y"
  703. else
  704. read -rp "Would you like to set this certificate for the panel? (y/n): " setPanel
  705. fi
  706. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  707. local webCertFile="/root/cert/${domain}/fullchain.pem"
  708. local webKeyFile="/root/cert/${domain}/privkey.pem"
  709. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  710. ${xui_folder}/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  711. echo -e "${green}Certificate paths set for the panel${plain}"
  712. echo -e "${green}Certificate File: $webCertFile${plain}"
  713. echo -e "${green}Private Key File: $webKeyFile${plain}"
  714. echo ""
  715. echo -e "${green}Access URL: https://${domain}:${existing_port}/${existing_webBasePath}${plain}"
  716. echo -e "${yellow}Panel will restart to apply SSL certificate...${plain}"
  717. systemctl restart x-ui 2> /dev/null || rc-service x-ui restart 2> /dev/null
  718. else
  719. echo -e "${red}Error: Certificate or private key file not found for domain: $domain.${plain}"
  720. fi
  721. else
  722. echo -e "${yellow}Skipping panel path setting.${plain}"
  723. fi
  724. return 0
  725. }
  726. # Reusable interactive SSL setup (domain or IP)
  727. # Sets global `SSL_HOST` to the chosen domain/IP for Access URL usage
  728. prompt_and_setup_ssl() {
  729. local panel_port="$1"
  730. local web_base_path="$2"
  731. local server_ip="$3"
  732. local ssl_choice=""
  733. SSL_SCHEME="https"
  734. echo -e "${yellow}Choose SSL certificate setup method:${plain}"
  735. echo -e "${green}1.${plain} Let's Encrypt for Domain (90-day validity, auto-renews)"
  736. echo -e "${green}2.${plain} Let's Encrypt for IP Address (6-day validity, auto-renews)"
  737. echo -e "${green}3.${plain} Custom SSL Certificate (Path to existing files)"
  738. echo -e "${green}4.${plain} Skip SSL (advanced — behind reverse proxy / SSH tunnel only)"
  739. echo -e "${blue}Note:${plain} Options 1 & 2 require port 80 open. Option 3 requires manual paths."
  740. echo -e "${blue}Note:${plain} Option 4 serves the panel over plain HTTP — only safe behind nginx/Caddy or an SSH tunnel."
  741. if [[ "$NONINTERACTIVE" == "1" ]]; then
  742. case "${XUI_SSL_MODE:-none}" in
  743. domain) ssl_choice="1" ;;
  744. ip) ssl_choice="2" ;;
  745. none | "") ssl_choice="4" ;;
  746. *)
  747. echo -e "${yellow}Unknown XUI_SSL_MODE='${XUI_SSL_MODE}', defaulting to none (HTTP).${plain}"
  748. ssl_choice="4"
  749. ;;
  750. esac
  751. else
  752. read -rp "Choose an option (default 2 for IP): " ssl_choice
  753. ssl_choice="${ssl_choice// /}" # Trim whitespace
  754. # Default to 2 (IP cert) if input is empty or invalid (not 1, 3 or 4)
  755. if [[ "$ssl_choice" != "1" && "$ssl_choice" != "3" && "$ssl_choice" != "4" ]]; then
  756. ssl_choice="2"
  757. fi
  758. fi
  759. case "$ssl_choice" in
  760. 1)
  761. # User chose Let's Encrypt domain option
  762. echo -e "${green}Using Let's Encrypt for domain certificate...${plain}"
  763. if ssl_cert_issue; then
  764. local cert_domain="${SSL_ISSUED_DOMAIN}"
  765. if [[ -z "${cert_domain}" ]]; then
  766. cert_domain=$(~/.acme.sh/acme.sh --list 2> /dev/null | tail -1 | awk '{print $1}')
  767. fi
  768. if [[ -n "${cert_domain}" ]]; then
  769. SSL_HOST="${cert_domain}"
  770. echo -e "${green}✓ SSL certificate configured successfully with domain: ${cert_domain}${plain}"
  771. else
  772. echo -e "${yellow}SSL setup may have completed, but domain extraction failed${plain}"
  773. SSL_HOST="${server_ip}"
  774. fi
  775. else
  776. echo -e "${red}SSL certificate setup failed for domain mode.${plain}"
  777. SSL_HOST="${server_ip}"
  778. fi
  779. ;;
  780. 2)
  781. # User chose Let's Encrypt IP certificate option
  782. echo -e "${green}Using Let's Encrypt for IP certificate (shortlived profile)...${plain}"
  783. # Confirm the auto-detected IP before issuing for it: with asymmetric
  784. # routing / multi-WAN the echo services can return a transit address.
  785. if [[ "$NONINTERACTIVE" != "1" ]]; then
  786. local ip_confirm=""
  787. read -rp "Is ${server_ip} the correct incoming public IPv4 address for this server? [Default y]: " ip_confirm
  788. if [[ -n "$ip_confirm" && "$ip_confirm" != "y" && "$ip_confirm" != "Y" ]]; then
  789. server_ip=""
  790. while [[ -z "$server_ip" ]]; do
  791. read -rp "Please enter your server's public IPv4 address: " server_ip
  792. server_ip="${server_ip// /}"
  793. if [[ ! "$server_ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  794. echo -e "${red}Invalid IPv4 address. Please try again.${plain}"
  795. server_ip=""
  796. fi
  797. done
  798. fi
  799. fi
  800. # Ask for optional IPv6
  801. local ipv6_addr=""
  802. prompt_or_default ipv6_addr "Do you have an IPv6 address to include? (leave empty to skip): " "" XUI_SSL_IPV6
  803. ipv6_addr="${ipv6_addr// /}" # Trim whitespace
  804. # Stop panel if running (port 80 needed)
  805. if [[ $release == "alpine" ]]; then
  806. rc-service x-ui stop > /dev/null 2>&1
  807. else
  808. systemctl stop x-ui > /dev/null 2>&1
  809. fi
  810. setup_ip_certificate "${server_ip}" "${ipv6_addr}"
  811. if [ $? -eq 0 ]; then
  812. SSL_HOST="${server_ip}"
  813. echo -e "${green}✓ Let's Encrypt IP certificate configured successfully${plain}"
  814. else
  815. echo -e "${red}✗ IP certificate setup failed. Please check port 80 is open.${plain}"
  816. SSL_HOST="${server_ip}"
  817. fi
  818. ;;
  819. 3)
  820. # User chose Custom Paths (User Provided) option
  821. echo -e "${green}Using custom existing certificate...${plain}"
  822. local custom_cert=""
  823. local custom_key=""
  824. local custom_domain=""
  825. # 3.1 Request Domain to compose Panel URL later
  826. read -rp "Please enter domain name certificate issued for: " custom_domain
  827. custom_domain="${custom_domain// /}" # Remove spaces
  828. # 3.2 Loop for Certificate Path
  829. while true; do
  830. read -rp "Input certificate path (keywords: .crt / fullchain): " custom_cert
  831. # Strip quotes if present
  832. custom_cert=$(echo "$custom_cert" | tr -d '"' | tr -d "'")
  833. if [[ -f "$custom_cert" && -r "$custom_cert" && -s "$custom_cert" ]]; then
  834. break
  835. elif [[ ! -f "$custom_cert" ]]; then
  836. echo -e "${red}Error: File does not exist! Try again.${plain}"
  837. elif [[ ! -r "$custom_cert" ]]; then
  838. echo -e "${red}Error: File exists but is not readable (check permissions)!${plain}"
  839. else
  840. echo -e "${red}Error: File is empty!${plain}"
  841. fi
  842. done
  843. # 3.3 Loop for Private Key Path
  844. while true; do
  845. read -rp "Input private key path (keywords: .key / privatekey): " custom_key
  846. # Strip quotes if present
  847. custom_key=$(echo "$custom_key" | tr -d '"' | tr -d "'")
  848. if [[ -f "$custom_key" && -r "$custom_key" && -s "$custom_key" ]]; then
  849. break
  850. elif [[ ! -f "$custom_key" ]]; then
  851. echo -e "${red}Error: File does not exist! Try again.${plain}"
  852. elif [[ ! -r "$custom_key" ]]; then
  853. echo -e "${red}Error: File exists but is not readable (check permissions)!${plain}"
  854. else
  855. echo -e "${red}Error: File is empty!${plain}"
  856. fi
  857. done
  858. # 3.4 Apply Settings via x-ui binary
  859. ${xui_folder}/x-ui cert -webCert "$custom_cert" -webCertKey "$custom_key" > /dev/null 2>&1
  860. # Set SSL_HOST for composing Panel URL
  861. if [[ -n "$custom_domain" ]]; then
  862. SSL_HOST="$custom_domain"
  863. else
  864. SSL_HOST="${server_ip}"
  865. fi
  866. echo -e "${green}✓ Custom certificate paths applied.${plain}"
  867. echo -e "${yellow}Note: You are responsible for renewing these files externally.${plain}"
  868. systemctl restart x-ui > /dev/null 2>&1 || rc-service x-ui restart > /dev/null 2>&1
  869. ;;
  870. 4)
  871. echo ""
  872. echo -e "${red}⚠ Panel will be installed WITHOUT SSL/TLS.${plain}"
  873. echo -e "${yellow}Login credentials and cookies will travel as plain HTTP.${plain}"
  874. echo -e "${yellow}Only safe when:${plain}"
  875. echo -e "${yellow} • A reverse proxy (nginx, Caddy, Traefik) terminates TLS for you, or${plain}"
  876. echo -e "${yellow} • You access the panel exclusively via SSH tunnel${plain}"
  877. echo ""
  878. SSL_SCHEME="http"
  879. SSL_HOST="${server_ip}"
  880. local bind_local=""
  881. if [[ "$NONINTERACTIVE" == "1" ]]; then
  882. # Cloud images must stay reachable on their public interface.
  883. bind_local="n"
  884. else
  885. read -rp "Bind the panel to 127.0.0.1 only? (recommended — forces SSH tunnel / reverse-proxy access) [y/N]: " bind_local
  886. fi
  887. if [[ "$bind_local" == "y" || "$bind_local" == "Y" ]]; then
  888. ${xui_folder}/x-ui setting -listenIP "127.0.0.1" > /dev/null 2>&1
  889. SSL_HOST="127.0.0.1"
  890. echo -e "${green}✓ Panel bound to 127.0.0.1 only. It is now unreachable from the public internet.${plain}"
  891. echo ""
  892. echo -e "${green}SSH Port Forwarding — open the panel from your local machine via:${plain}"
  893. echo -e " Standard SSH command:"
  894. echo -e " ${yellow}ssh -L 2222:127.0.0.1:${panel_port} root@${server_ip}${plain}"
  895. echo -e " If using an SSH key:"
  896. echo -e " ${yellow}ssh -i <sshkeypath> -L 2222:127.0.0.1:${panel_port} root@${server_ip}${plain}"
  897. echo -e " Then open in your browser:"
  898. echo -e " ${yellow}http://localhost:2222/${web_base_path}${plain}"
  899. echo ""
  900. echo -e "${yellow}Alternative: point a reverse proxy (nginx/Caddy) at 127.0.0.1:${panel_port} and let it terminate TLS.${plain}"
  901. else
  902. echo -e "${yellow}Panel will listen on all interfaces over plain HTTP. Make sure something else is terminating TLS in front of it.${plain}"
  903. fi
  904. systemctl restart x-ui > /dev/null 2>&1 || rc-service x-ui restart > /dev/null 2>&1
  905. echo -e "${green}✓ SSL setup skipped.${plain}"
  906. ;;
  907. *)
  908. echo -e "${red}Invalid option. Skipping SSL setup.${plain}"
  909. SSL_HOST="${server_ip}"
  910. ;;
  911. esac
  912. }
  913. config_after_install() {
  914. local existing_hasDefaultCredential=$(${xui_folder}/x-ui setting -show true | grep -Eo 'hasDefaultCredential: .+' | awk '{print $2}')
  915. local existing_webBasePath=$(${xui_folder}/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}' | sed 's#^/##')
  916. local existing_port=$(${xui_folder}/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  917. # Properly detect empty cert by checking if cert: line exists and has content after it
  918. local existing_cert=$(${xui_folder}/x-ui setting -getCert true | grep 'cert:' | awk -F': ' '{print $2}' | tr -d '[:space:]')
  919. local URL_lists=(
  920. "https://api4.ipify.org"
  921. "https://ipv4.icanhazip.com"
  922. "https://v4.api.ipinfo.io/ip"
  923. "https://ipv4.myexternalip.com/raw"
  924. "https://4.ident.me"
  925. "https://check-host.net/ip"
  926. )
  927. local server_ip=""
  928. for ip_address in "${URL_lists[@]}"; do
  929. local response=$(curl -s -w "\n%{http_code}" --max-time 3 "${ip_address}" 2> /dev/null)
  930. local http_code=$(echo "$response" | tail -n1)
  931. local ip_result=$(echo "$response" | head -n-1 | tr -d '[:space:]"')
  932. if [[ "${http_code}" == "200" && "${ip_result}" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  933. server_ip="${ip_result}"
  934. break
  935. fi
  936. done
  937. if [[ -z "$server_ip" ]]; then
  938. if [[ "$NONINTERACTIVE" == "1" ]]; then
  939. # Panel binds 0.0.0.0 regardless; the IP is only used to compose the
  940. # displayed access URL. Fall back to XUI_SERVER_IP or leave blank.
  941. server_ip="${XUI_SERVER_IP:-}"
  942. else
  943. echo -e "${yellow}Could not auto-detect server IP from any provider.${plain}"
  944. while [[ -z "$server_ip" ]]; do
  945. read -rp "Please enter your server's public IPv4 address: " server_ip
  946. server_ip="${server_ip// /}"
  947. if [[ ! "$server_ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  948. echo -e "${red}Invalid IPv4 address. Please try again.${plain}"
  949. server_ip=""
  950. fi
  951. done
  952. fi
  953. fi
  954. if [[ ${#existing_webBasePath} -lt 4 ]]; then
  955. if [[ "$existing_hasDefaultCredential" == "true" ]]; then
  956. local config_webBasePath="${XUI_WEB_BASE_PATH:-$(gen_random_string 18)}"
  957. local config_username="${XUI_USERNAME:-$(gen_random_string 10)}"
  958. local config_password="${XUI_PASSWORD:-$(gen_random_string 10)}"
  959. local config_port=""
  960. local db_label="SQLite (/etc/x-ui/x-ui.db)"
  961. echo ""
  962. echo -e "${green}═══════════════════════════════════════════${plain}"
  963. echo -e "${green} Database Selection ${plain}"
  964. echo -e "${green}═══════════════════════════════════════════${plain}"
  965. echo -e " 1) SQLite (default — recommended for < 500 clients)"
  966. echo -e " 2) PostgreSQL (recommended for high client counts / many nodes)"
  967. if [[ "$NONINTERACTIVE" == "1" ]]; then
  968. if [[ "${XUI_DB_TYPE:-sqlite}" == "postgres" ]]; then
  969. db_choice="2"
  970. else
  971. db_choice="1"
  972. fi
  973. else
  974. read -rp "Choose [1]: " db_choice
  975. db_choice="${db_choice:-1}"
  976. fi
  977. if [[ "$db_choice" == "2" ]]; then
  978. local xui_env_file
  979. case "${release}" in
  980. ubuntu | debian | armbian)
  981. xui_env_file="/etc/default/x-ui"
  982. ;;
  983. arch | manjaro | parch | alpine)
  984. xui_env_file="/etc/conf.d/x-ui"
  985. ;;
  986. *)
  987. xui_env_file="/etc/sysconfig/x-ui"
  988. ;;
  989. esac
  990. local xui_dsn=""
  991. local pg_mode=""
  992. local pg_local_installed=0
  993. while [[ -z "$xui_dsn" ]]; do
  994. if [[ "$NONINTERACTIVE" == "1" ]]; then
  995. if [[ -n "${XUI_DB_DSN:-}" ]]; then
  996. xui_dsn="${XUI_DB_DSN}"
  997. db_label="PostgreSQL (external)"
  998. break
  999. fi
  1000. echo -e "${yellow}Installing PostgreSQL locally (non-interactive)...${plain}"
  1001. local pg_cred_file
  1002. pg_cred_file=$(mktemp 2> /dev/null) || pg_cred_file=$(mktemp -t x-ui-pg-creds.XXXXXXXX)
  1003. if [[ -n "${pg_cred_file}" ]] && xui_dsn=$(PG_CRED_FILE="${pg_cred_file}" install_postgres_local); then
  1004. pg_local_installed=1
  1005. if [[ -r "${pg_cred_file}" ]]; then
  1006. # shellcheck disable=SC1090
  1007. source "${pg_cred_file}"
  1008. fi
  1009. rm -f "${pg_cred_file}"
  1010. db_label="PostgreSQL (${PG_USER}@${PG_HOST}:${PG_PORT}/${PG_DB})"
  1011. break
  1012. fi
  1013. rm -f "${pg_cred_file}"
  1014. echo -e "${red}PostgreSQL installation failed in non-interactive mode; aborting.${plain}"
  1015. echo -e "${yellow}Set XUI_DB_DSN to use an existing server, or XUI_DB_TYPE=sqlite.${plain}"
  1016. exit 1
  1017. fi
  1018. echo ""
  1019. echo -e " 1) Install PostgreSQL locally and create a dedicated user/db (recommended)"
  1020. echo -e " 2) Use an existing PostgreSQL server (enter DSN)"
  1021. read -rp "Choose [1]: " pg_mode
  1022. pg_mode="${pg_mode:-1}"
  1023. if [[ "$pg_mode" == "2" ]]; then
  1024. while [[ -z "$xui_dsn" ]]; do
  1025. read -rp "Enter PostgreSQL DSN (postgres://user:pass@host:port/dbname?sslmode=disable): " xui_dsn
  1026. xui_dsn="${xui_dsn// /}"
  1027. done
  1028. db_label="PostgreSQL (external)"
  1029. else
  1030. echo -e "${yellow}Installing PostgreSQL — this may take a moment...${plain}"
  1031. local pg_cred_file
  1032. pg_cred_file=$(mktemp 2> /dev/null) || pg_cred_file=$(mktemp -t x-ui-pg-creds.XXXXXXXX)
  1033. if [[ -z "${pg_cred_file}" ]]; then
  1034. echo -e "${red}Failed to create temporary credentials file.${plain}"
  1035. xui_dsn=""
  1036. continue
  1037. fi
  1038. if xui_dsn=$(PG_CRED_FILE="${pg_cred_file}" install_postgres_local); then
  1039. pg_local_installed=1
  1040. if [[ -r "${pg_cred_file}" ]]; then
  1041. # shellcheck disable=SC1090
  1042. source "${pg_cred_file}"
  1043. fi
  1044. rm -f "${pg_cred_file}"
  1045. db_label="PostgreSQL (${PG_USER}@${PG_HOST}:${PG_PORT}/${PG_DB})"
  1046. else
  1047. rm -f "${pg_cred_file}"
  1048. echo ""
  1049. echo -e "${red}PostgreSQL installation failed.${plain}"
  1050. echo -e " 1) Retry local install"
  1051. echo -e " 2) Enter an external DSN instead"
  1052. echo -e " 3) Abort install"
  1053. echo -e " 4) Fall back to SQLite"
  1054. read -rp "Choose [1]: " pg_fail
  1055. pg_fail="${pg_fail:-1}"
  1056. case "$pg_fail" in
  1057. 2) pg_mode="2" ;;
  1058. 3)
  1059. echo -e "${red}Install aborted.${plain}"
  1060. exit 1
  1061. ;;
  1062. 4)
  1063. db_choice="1"
  1064. xui_dsn=""
  1065. break
  1066. ;;
  1067. *) xui_dsn="" ;;
  1068. esac
  1069. fi
  1070. fi
  1071. done
  1072. if [[ -n "$xui_dsn" ]]; then
  1073. install -d -m 755 "$(dirname "$xui_env_file")"
  1074. umask 077
  1075. cat > "$xui_env_file" << EOF
  1076. XUI_DB_TYPE=postgres
  1077. XUI_DB_DSN=${xui_dsn}
  1078. EOF
  1079. chmod 600 "$xui_env_file"
  1080. umask 022
  1081. export XUI_DB_TYPE=postgres
  1082. export XUI_DB_DSN="${xui_dsn}"
  1083. ensure_pg_client || echo -e "${yellow}⚠ Could not install pg_dump/pg_restore. In-panel database backup/restore will be unavailable until you install the postgresql-client package.${plain}"
  1084. fi
  1085. fi
  1086. if [[ "$NONINTERACTIVE" == "1" ]]; then
  1087. if [[ -n "${XUI_PANEL_PORT:-}" ]]; then
  1088. config_port="${XUI_PANEL_PORT}"
  1089. echo -e "${yellow}Your Panel Port is: ${config_port}${plain}"
  1090. else
  1091. config_port=$(shuf -i 1024-62000 -n 1)
  1092. echo -e "${yellow}Generated random port: ${config_port}${plain}"
  1093. fi
  1094. else
  1095. read -rp "Would you like to customize the Panel Port settings? (If not, a random port will be applied) [y/n]: " config_confirm
  1096. if [[ "${config_confirm}" == "y" || "${config_confirm}" == "Y" ]]; then
  1097. read -rp "Please set up the panel port: " config_port
  1098. echo -e "${yellow}Your Panel Port is: ${config_port}${plain}"
  1099. else
  1100. config_port=$(shuf -i 1024-62000 -n 1)
  1101. echo -e "${yellow}Generated random port: ${config_port}${plain}"
  1102. fi
  1103. fi
  1104. ${xui_folder}/x-ui setting -username "${config_username}" -password "${config_password}" -port "${config_port}" -webBasePath "${config_webBasePath}"
  1105. echo ""
  1106. echo -e "${green}═══════════════════════════════════════════${plain}"
  1107. echo -e "${green} SSL Certificate Setup (RECOMMENDED) ${plain}"
  1108. echo -e "${green}═══════════════════════════════════════════${plain}"
  1109. echo -e "${yellow}SSL is strongly recommended. Skip only if a reverse proxy${plain}"
  1110. echo -e "${yellow}or SSH tunnel handles TLS for you.${plain}"
  1111. echo -e "${yellow}Let's Encrypt now supports both domains and IP addresses!${plain}"
  1112. echo ""
  1113. prompt_and_setup_ssl "${config_port}" "${config_webBasePath}" "${server_ip}"
  1114. # Retrieve the API token for display
  1115. local config_apiToken=$(${xui_folder}/x-ui setting -getApiToken true | grep -Eo 'apiToken: .+' | awk '{print $2}')
  1116. # Display final credentials and access information
  1117. echo ""
  1118. echo -e "${green}═══════════════════════════════════════════${plain}"
  1119. echo -e "${green} Panel Installation Complete! ${plain}"
  1120. echo -e "${green}═══════════════════════════════════════════${plain}"
  1121. echo -e "${green}Username: ${config_username}${plain}"
  1122. echo -e "${green}Password: ${config_password}${plain}"
  1123. echo -e "${green}Port: ${config_port}${plain}"
  1124. echo -e "${green}WebBasePath: ${config_webBasePath}${plain}"
  1125. echo -e "${green}Database: ${db_label}${plain}"
  1126. echo -e "${green}Access URL: ${SSL_SCHEME}://${SSL_HOST}:${config_port}/${config_webBasePath}${plain}"
  1127. echo -e "${green}API Token: ${config_apiToken}${plain}"
  1128. echo -e "${green}═══════════════════════════════════════════${plain}"
  1129. echo -e "${yellow}⚠ IMPORTANT: Save these credentials securely!${plain}"
  1130. if [[ "$SSL_SCHEME" == "https" ]]; then
  1131. echo -e "${yellow}⚠ SSL Certificate: Enabled and configured${plain}"
  1132. else
  1133. echo -e "${yellow}⚠ SSL Certificate: Skipped — panel is HTTP-only. Use a reverse proxy or SSH tunnel.${plain}"
  1134. fi
  1135. if [[ "$db_choice" == "2" ]]; then
  1136. echo ""
  1137. echo -e "${green}PostgreSQL backup & restore is built into the panel:${plain}"
  1138. echo -e " ${blue}${SSL_SCHEME}://${SSL_HOST}:${config_port}/${config_webBasePath}${plain} → Backup & Restore"
  1139. echo -e "${yellow} Back Up downloads a pg_dump .dump file; Restore reloads it via pg_restore.${plain}"
  1140. fi
  1141. if [[ "$db_choice" == "2" && "$pg_local_installed" == "1" ]]; then
  1142. echo ""
  1143. echo -e "${green}═══════════════════════════════════════════${plain}"
  1144. echo -e "${green} PostgreSQL Credentials ${plain}"
  1145. echo -e "${green}═══════════════════════════════════════════${plain}"
  1146. echo -e "${green}DB Name: ${PG_DB}${plain}"
  1147. echo -e "${green}Username: ${PG_USER}${plain}"
  1148. echo -e "${green}Password: ${PG_PASS}${plain}"
  1149. echo -e "${green}Host: ${PG_HOST}${plain}"
  1150. echo -e "${green}Port: ${PG_PORT}${plain}"
  1151. echo -e "${green}DSN: ${xui_dsn}${plain}"
  1152. echo -e "${green}Env file: ${xui_env_file}${plain}"
  1153. echo -e "${green}-------------------------------------------${plain}"
  1154. echo -e "${green}Connect from this server:${plain}"
  1155. echo -e " ${blue}sudo -u postgres psql -d ${PG_DB}${plain} (as the postgres superuser)"
  1156. echo -e " ${blue}PGPASSWORD='${PG_PASS}' psql -h ${PG_HOST} -p ${PG_PORT} -U ${PG_USER} -d ${PG_DB}${plain}"
  1157. echo -e "${green}═══════════════════════════════════════════${plain}"
  1158. echo -e "${yellow}⚠ The panel reads these credentials from ${xui_env_file}.${plain}"
  1159. echo -e "${yellow}⚠ Save the password — it is not stored anywhere else in plain text.${plain}"
  1160. unset PG_USER PG_PASS PG_HOST PG_PORT PG_DB
  1161. fi
  1162. # Persist a machine-parseable credentials file for cloud-init / MOTD.
  1163. : "${SSL_SCHEME:=https}"
  1164. : "${SSL_HOST:=${server_ip}}"
  1165. local db_type_out="sqlite"
  1166. [[ "$db_choice" == "2" ]] && db_type_out="postgres"
  1167. write_install_result "${config_username}" "${config_password}" "${config_port}" \
  1168. "${config_webBasePath}" "${SSL_SCHEME}" "${SSL_HOST}" "${config_apiToken}" "${db_type_out}"
  1169. else
  1170. local config_webBasePath=$(gen_random_string 18)
  1171. echo -e "${yellow}WebBasePath is missing or too short. Generating a new one...${plain}"
  1172. ${xui_folder}/x-ui setting -webBasePath "${config_webBasePath}"
  1173. echo -e "${green}New WebBasePath: ${config_webBasePath}${plain}"
  1174. # If the panel is already installed but no certificate is configured, prompt for SSL now
  1175. if [[ -z "${existing_cert}" ]]; then
  1176. echo ""
  1177. echo -e "${green}═══════════════════════════════════════════${plain}"
  1178. echo -e "${green} SSL Certificate Setup (RECOMMENDED) ${plain}"
  1179. echo -e "${green}═══════════════════════════════════════════${plain}"
  1180. echo -e "${yellow}Let's Encrypt now supports both domains and IP addresses!${plain}"
  1181. echo ""
  1182. prompt_and_setup_ssl "${existing_port}" "${config_webBasePath}" "${server_ip}"
  1183. echo -e "${green}Access URL: ${SSL_SCHEME}://${SSL_HOST}:${existing_port}/${config_webBasePath}${plain}"
  1184. else
  1185. # If a cert already exists, just show the access URL
  1186. echo -e "${green}Access URL: https://${server_ip}:${existing_port}/${config_webBasePath}${plain}"
  1187. fi
  1188. fi
  1189. else
  1190. if [[ "$existing_hasDefaultCredential" == "true" ]]; then
  1191. local config_username="${XUI_USERNAME:-$(gen_random_string 10)}"
  1192. local config_password="${XUI_PASSWORD:-$(gen_random_string 10)}"
  1193. echo -e "${yellow}Default credentials detected. Security update required...${plain}"
  1194. ${xui_folder}/x-ui setting -username "${config_username}" -password "${config_password}"
  1195. echo -e "Generated new random login credentials:"
  1196. echo -e "###############################################"
  1197. echo -e "${green}Username: ${config_username}${plain}"
  1198. echo -e "${green}Password: ${config_password}${plain}"
  1199. echo -e "###############################################"
  1200. # Persist a machine-parseable credentials file for cloud-init / MOTD.
  1201. local config_apiToken
  1202. config_apiToken=$(${xui_folder}/x-ui setting -getApiToken true | grep -Eo 'apiToken: .+' | awk '{print $2}')
  1203. : "${SSL_SCHEME:=https}"
  1204. : "${SSL_HOST:=${server_ip}}"
  1205. write_install_result "${config_username}" "${config_password}" "${existing_port}" \
  1206. "${existing_webBasePath}" "${SSL_SCHEME}" "${SSL_HOST}" "${config_apiToken}" "${XUI_DB_TYPE:-sqlite}"
  1207. else
  1208. echo -e "${green}Username, Password, and WebBasePath are properly set.${plain}"
  1209. fi
  1210. # Existing install: if no cert configured, prompt user for SSL setup
  1211. # Properly detect empty cert by checking if cert: line exists and has content after it
  1212. existing_cert=$(${xui_folder}/x-ui setting -getCert true | grep 'cert:' | awk -F': ' '{print $2}' | tr -d '[:space:]')
  1213. if [[ -z "$existing_cert" ]]; then
  1214. echo ""
  1215. echo -e "${green}═══════════════════════════════════════════${plain}"
  1216. echo -e "${green} SSL Certificate Setup (RECOMMENDED) ${plain}"
  1217. echo -e "${green}═══════════════════════════════════════════${plain}"
  1218. echo -e "${yellow}Let's Encrypt now supports both domains and IP addresses!${plain}"
  1219. echo ""
  1220. prompt_and_setup_ssl "${existing_port}" "${existing_webBasePath}" "${server_ip}"
  1221. echo -e "${green}Access URL: ${SSL_SCHEME}://${SSL_HOST}:${existing_port}/${existing_webBasePath}${plain}"
  1222. else
  1223. echo -e "${green}SSL certificate already configured. No action needed.${plain}"
  1224. fi
  1225. fi
  1226. ${xui_folder}/x-ui migrate
  1227. }
  1228. # setup_fail2ban auto-installs and configures fail2ban for the IP Limit feature
  1229. # by invoking the freshly installed x-ui CLI. IP Limit is load-bearing on
  1230. # fail2ban (without it the panel disables the limitIp field and zeroes existing
  1231. # limits), so a fresh install should make it work out of the box, just like the
  1232. # Docker image already does. Non-fatal by design: a fail2ban failure must never
  1233. # abort the panel install.
  1234. setup_fail2ban() {
  1235. if [[ -n "${XUI_ENABLE_FAIL2BAN+x}" && "${XUI_ENABLE_FAIL2BAN}" != "true" ]]; then
  1236. echo -e "${yellow}XUI_ENABLE_FAIL2BAN=${XUI_ENABLE_FAIL2BAN}, skipping Fail2ban auto-setup.${plain}"
  1237. return 0
  1238. fi
  1239. if [[ ! -x /usr/bin/x-ui ]]; then
  1240. echo -e "${yellow}x-ui CLI not found; skipping Fail2ban auto-setup.${plain}"
  1241. return 0
  1242. fi
  1243. # Scripts older than v3.4.0 have no setup-fail2ban and exit 0 from the
  1244. # usage banner, which would read as success here.
  1245. if ! grep -q '"setup-fail2ban")' /usr/bin/x-ui; then
  1246. echo -e "${yellow}This x-ui.sh predates 'x-ui setup-fail2ban'; skipping Fail2ban auto-setup.${plain}"
  1247. return 0
  1248. fi
  1249. echo -e "${green}Setting up Fail2ban for the IP Limit feature...${plain}"
  1250. if /usr/bin/x-ui setup-fail2ban; then
  1251. echo -e "${green}Fail2ban setup complete.${plain}"
  1252. else
  1253. echo -e "${yellow}Fail2ban setup did not finish; IP Limit stays disabled until you run 'x-ui' and open the IP Limit menu. Continuing.${plain}"
  1254. fi
  1255. return 0
  1256. }
  1257. # Lands a systemd unit file at ${xui_service}/x-ui.service via a temp file +
  1258. # atomic mv, so a failed cp/curl or an interrupted mv never leaves a
  1259. # truncated unit file at the live path -- systemd would then fail to parse
  1260. # it on the next daemon-reload/start. Same pattern already used for
  1261. # /usr/bin/x-ui elsewhere in this script. source_is_url picks cp (from a
  1262. # file already extracted from the release tarball) vs curl (GitHub fallback).
  1263. _install_xui_service_unit() {
  1264. local source="$1"
  1265. local source_is_url="$2"
  1266. local dest="${xui_service}/x-ui.service"
  1267. local temp_file="${dest}.tmp.$$"
  1268. rm -f "$temp_file"
  1269. if [[ "$source_is_url" == "true" ]]; then
  1270. curl -fLRo "$temp_file" "$source" > /dev/null 2>&1
  1271. else
  1272. cp -f "$source" "$temp_file" > /dev/null 2>&1
  1273. fi
  1274. if [[ $? -ne 0 ]]; then
  1275. rm -f "$temp_file"
  1276. return 1
  1277. fi
  1278. if [[ ! -s "$temp_file" ]]; then
  1279. rm -f "$temp_file"
  1280. return 1
  1281. fi
  1282. mv -f "$temp_file" "$dest"
  1283. if [[ $? -ne 0 ]]; then
  1284. rm -f "$temp_file"
  1285. return 1
  1286. fi
  1287. return 0
  1288. }
  1289. # resolve_latest_tag prints the latest stable release tag. It prefers the web
  1290. # releases/latest redirect, which is not subject to the unauthenticated API's
  1291. # 60 req/h-per-IP limit that trips shared CI/CGNAT addresses (the install then
  1292. # fails with "Failed to fetch x-ui version"), and falls back to the API.
  1293. resolve_latest_tag() {
  1294. local url tag
  1295. url=$(curl -sSLI -o /dev/null -w '%{url_effective}' --retry 5 --retry-delay 3 --connect-timeout 15 --max-time 60 "https://github.com/MHSanaei/3x-ui/releases/latest" 2>/dev/null)
  1296. tag=${url##*/tag/}
  1297. if [[ "$tag" != "$url" && -n "$tag" && "$tag" != "latest" ]]; then
  1298. echo "$tag"
  1299. return 0
  1300. fi
  1301. curl -Ls --retry 5 --retry-delay 3 --connect-timeout 15 --max-time 60 "https://api.github.com/repos/MHSanaei/3x-ui/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'
  1302. }
  1303. # Releases publish <asset>.sha256 next to each archive. A mismatch or a failed
  1304. # sidecar download aborts the install; only a 404 (releases predating the
  1305. # sidecar) is tolerated with a warning.
  1306. verify_release_checksum() {
  1307. local url="$1" file="$2" sums="$2.sha256" code expected actual
  1308. rm -f "${sums}"
  1309. code=$(curl -sL --retry 3 --retry-delay 3 --connect-timeout 15 --max-time 60 -o "${sums}" -w '%{http_code}' "${url}.sha256")
  1310. if [[ "${code}" == "404" ]]; then
  1311. rm -f "${sums}"
  1312. echo -e "${yellow}No checksum published for this release, skipping verification${plain}"
  1313. return 0
  1314. fi
  1315. if [[ "${code}" != "200" ]]; then
  1316. rm -f "${sums}" "${file}"
  1317. echo -e "${red}Failed to download the checksum for $(basename "${file}") (HTTP ${code})${plain}"
  1318. exit 1
  1319. fi
  1320. expected=$(awk 'NR == 1 {print $1}' "${sums}")
  1321. actual=$(sha256sum "${file}" | awk '{print $1}')
  1322. rm -f "${sums}"
  1323. if [[ ! "${expected}" =~ ^[0-9a-f]{64}$ || "${expected}" != "${actual}" ]]; then
  1324. rm -f "${file}"
  1325. echo -e "${red}Checksum mismatch for $(basename "${file}"): expected ${expected:-<none>}, got ${actual}${plain}"
  1326. exit 1
  1327. fi
  1328. echo -e "${green}Checksum verified: ${actual}${plain}"
  1329. }
  1330. # Older tags predate some of these files (x-ui.rc arrived in v2.8.4). Serving
  1331. # main's copy against an old binary is the mismatch this pinning exists to
  1332. # prevent, so probe before anything is stopped or removed and refuse the tag.
  1333. require_repo_files() {
  1334. local ref="$1" name status
  1335. shift
  1336. [[ "${ref}" == "main" ]] && return 0
  1337. for name in "$@"; do
  1338. status=$(curl -sIL --retry 3 --connect-timeout 15 -o /dev/null -w '%{http_code}' "https://raw.githubusercontent.com/MHSanaei/3x-ui/${ref}/${name}")
  1339. if [[ "${status}" != "200" ]]; then
  1340. echo -e "${red}${name} is not available for ${ref} (HTTP ${status})${plain}"
  1341. echo -e "${red}Install a release that ships it, or 'dev' for the rolling build. Your existing installation has not been touched.${plain}"
  1342. exit 1
  1343. fi
  1344. done
  1345. }
  1346. install_x-ui() {
  1347. cd ${xui_folder%/x-ui}/
  1348. # Download resources
  1349. if [ $# == 0 ]; then
  1350. tag_version=$(resolve_latest_tag)
  1351. if [[ ! -n "$tag_version" ]]; then
  1352. echo -e "${red}Failed to fetch x-ui version, it may be due to GitHub API restrictions, please try it later${plain}"
  1353. exit 1
  1354. fi
  1355. echo -e "Got x-ui latest version: ${tag_version}, beginning the installation..."
  1356. curl -fLR --retry 5 --retry-delay 3 --connect-timeout 15 --speed-limit 1 --speed-time 300 -o ${xui_folder}-linux-$(arch).tar.gz https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz
  1357. if [[ $? -ne 0 ]]; then
  1358. echo -e "${red}Downloading x-ui failed, please be sure that your server can access GitHub ${plain}"
  1359. exit 1
  1360. fi
  1361. if [[ ! -s ${xui_folder}-linux-$(arch).tar.gz ]]; then
  1362. rm ${xui_folder}-linux-$(arch).tar.gz -f
  1363. echo -e "${red}Downloaded x-ui release archive is empty${plain}"
  1364. exit 1
  1365. fi
  1366. verify_release_checksum "https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz" "${xui_folder}-linux-$(arch).tar.gz"
  1367. else
  1368. tag_version=$1
  1369. # The rolling dev channel ships under a fixed, non-semver tag that is
  1370. # force-moved to the latest main commit on every push. Accept `dev` as a
  1371. # convenient alias and skip the numeric floor check for it.
  1372. if [[ "$tag_version" == "dev" || "$tag_version" == "dev-latest" ]]; then
  1373. tag_version="dev-latest"
  1374. echo -e "${yellow}Installing the rolling dev build (tag: dev-latest). This is a per-commit pre-release, not a stable version.${plain}"
  1375. else
  1376. tag_version_numeric=${tag_version#v}
  1377. min_version="2.3.5"
  1378. if [[ "$(printf '%s\n' "$min_version" "$tag_version_numeric" | sort -V | head -n1)" != "$min_version" ]]; then
  1379. echo -e "${red}Please use a newer version (at least v2.3.5). Exiting installation.${plain}"
  1380. exit 1
  1381. fi
  1382. fi
  1383. url="https://github.com/MHSanaei/3x-ui/releases/download/${tag_version}/x-ui-linux-$(arch).tar.gz"
  1384. echo -e "Beginning to install x-ui ${tag_version}"
  1385. curl -fLR --retry 5 --retry-delay 3 --connect-timeout 15 --speed-limit 1 --speed-time 300 -o ${xui_folder}-linux-$(arch).tar.gz ${url}
  1386. if [[ $? -ne 0 ]]; then
  1387. echo -e "${red}Download x-ui ${tag_version} failed, please check if the version exists ${plain}"
  1388. exit 1
  1389. fi
  1390. if [[ ! -s ${xui_folder}-linux-$(arch).tar.gz ]]; then
  1391. rm ${xui_folder}-linux-$(arch).tar.gz -f
  1392. echo -e "${red}Downloaded x-ui release archive is empty${plain}"
  1393. exit 1
  1394. fi
  1395. verify_release_checksum "${url}" "${xui_folder}-linux-$(arch).tar.gz"
  1396. fi
  1397. # x-ui.sh, x-ui.rc and the unit files must come from the same release as
  1398. # the binary; only the rolling dev build tracks main.
  1399. local script_ref="${tag_version}"
  1400. if [[ "${tag_version}" == "dev-latest" ]]; then
  1401. script_ref="main"
  1402. fi
  1403. # The unit files are only fetched when the release tarball lacks them, so
  1404. # they are checked at that point instead of here.
  1405. local required_files=("x-ui.sh")
  1406. [[ $release == "alpine" ]] && required_files+=("x-ui.rc")
  1407. require_repo_files "${script_ref}" "${required_files[@]}"
  1408. local xui_script_temp="/usr/bin/x-ui-temp.$$"
  1409. rm -f "${xui_script_temp}"
  1410. curl -fLRo "${xui_script_temp}" "https://raw.githubusercontent.com/MHSanaei/3x-ui/${script_ref}/x-ui.sh"
  1411. if [[ $? -ne 0 ]]; then
  1412. rm -f "${xui_script_temp}"
  1413. echo -e "${red}Failed to download x-ui.sh${plain}"
  1414. exit 1
  1415. fi
  1416. if [[ ! -s "${xui_script_temp}" ]]; then
  1417. rm -f "${xui_script_temp}"
  1418. echo -e "${red}Downloaded x-ui.sh is empty${plain}"
  1419. exit 1
  1420. fi
  1421. # Stop x-ui service and remove old resources
  1422. local custom_bin_backup=""
  1423. if [[ -e ${xui_folder}/ ]]; then
  1424. if [[ $release == "alpine" ]]; then
  1425. rc-service x-ui stop
  1426. else
  1427. systemctl stop x-ui
  1428. fi
  1429. # Kill any leftover mtg (MTProto) sidecars. x-ui runs them outside its own
  1430. # lifecycle, so on Linux a stale one can survive the stop and keep holding
  1431. # an inbound port with an outdated secret, silently breaking new clients.
  1432. # The freshly installed panel respawns a clean mtg per inbound on start.
  1433. pkill -f 'mtg-linux-[^ ]* run ' > /dev/null 2>&1 || true
  1434. # bin/ is about to be wiped wholesale by the tar extraction below. The
  1435. # release only ships known assets (xray/mtg binaries, the bundled
  1436. # geoip*/geosite*.dat sets) -- anything else in bin/ was placed there
  1437. # by the admin (e.g. a hand-added custom geoip/geosite file referenced
  1438. # from a routing rule via "ext:<file>:<code>") and would otherwise be
  1439. # silently deleted on every update, breaking Xray at next start with
  1440. # "failed to open <file>: no such file or directory" for any routing
  1441. # rule that references it. Moved aside rather than copied: a rename
  1442. # on the same filesystem is atomic (no truncated file if disk space
  1443. # runs out mid-copy, unlike `cp`) and keeps the snapshot under
  1444. # /usr/local rather than a separate, possibly small/tmpfs $TMPDIR.
  1445. if [[ -d "${xui_folder}/bin" ]]; then
  1446. custom_bin_backup="${xui_folder%/x-ui}/x-ui-bin-backup.$$"
  1447. rm -rf "${custom_bin_backup}"
  1448. if ! mv "${xui_folder}/bin" "${custom_bin_backup}"; then
  1449. custom_bin_backup=""
  1450. echo -e "${yellow}Could not back up bin/ -- custom files there will not be preserved across this update${plain}"
  1451. fi
  1452. fi
  1453. # Sole cleanup path for the backup from here on -- covers both the
  1454. # two `exit 1`s below (extraction/binary-missing failures) and an
  1455. # interrupted update (Ctrl-C, signal) before the restore runs.
  1456. # Cleared once the restore below finishes normally.
  1457. trap '[[ -n "${custom_bin_backup}" ]] && rm -rf "${custom_bin_backup}"' EXIT INT TERM
  1458. rm ${xui_folder}/ -rf
  1459. fi
  1460. # Extract resources and set permissions
  1461. tar zxvf x-ui-linux-$(arch).tar.gz
  1462. if [[ $? -ne 0 ]]; then
  1463. rm x-ui-linux-$(arch).tar.gz -f
  1464. rm -f "${xui_script_temp}"
  1465. echo -e "${red}Failed to extract the x-ui release archive -- the previous installation has already been removed, so the panel will not start until this is fixed; try running the installer again${plain}"
  1466. exit 1
  1467. fi
  1468. rm x-ui-linux-$(arch).tar.gz -f
  1469. cd x-ui
  1470. if [[ $? -ne 0 || ! -s x-ui ]]; then
  1471. rm -f "${xui_script_temp}"
  1472. echo -e "${red}Extracted x-ui archive is missing the x-ui binary -- the previous installation has already been removed, so the panel will not start until this is fixed; try running the installer again${plain}"
  1473. exit 1
  1474. fi
  1475. chmod +x x-ui
  1476. chmod +x x-ui.sh
  1477. # Check the system's architecture and rename the file accordingly.
  1478. # The panel binary maps GOARCH=arm to "arm32" (internal/xray/process.go),
  1479. # so the Xray binary must be named xray-linux-arm32; mtg keeps plain "arm".
  1480. if [[ $(arch) == "armv5" || $(arch) == "armv6" || $(arch) == "armv7" ]]; then
  1481. mv bin/xray-linux-$(arch) bin/xray-linux-arm32
  1482. chmod +x bin/xray-linux-arm32
  1483. if [[ -f bin/mtg-linux-$(arch) ]]; then
  1484. mv bin/mtg-linux-$(arch) bin/mtg-linux-arm
  1485. chmod +x bin/mtg-linux-arm
  1486. fi
  1487. fi
  1488. chmod +x x-ui bin/xray-linux-$(arch)
  1489. if [[ -f bin/mtg-linux-arm ]]; then
  1490. chmod +x bin/mtg-linux-arm
  1491. elif [[ -f bin/mtg-linux-$(arch) ]]; then
  1492. chmod +x bin/mtg-linux-$(arch)
  1493. fi
  1494. # Restore anything from the old bin/ that the fresh release doesn't ship
  1495. # (custom geoip/geosite files, or anything else an admin hand-placed
  1496. # there) -- never overwrites a same-named file the new release provides,
  1497. # so bundled assets (geoip.dat, geoip_RU.dat, ...) still get the fresh
  1498. # per-release copy. Runs after the arch-rename above so xray-linux-arm32/
  1499. # mtg-linux-arm already exist under their final names there and aren't
  1500. # mistaken for custom files needing a restore. Skips paths the panel
  1501. # itself regenerates at runtime (config.json, mtproto/*.toml -- see
  1502. # internal/xray/process.go, internal/mtproto/manager.go): those aren't
  1503. # admin-placed, and restoring a stale one only resurrects dead state (an
  1504. # orphaned mtg config for a since-deleted inbound) or the wrong
  1505. # directory permissions.
  1506. if [[ -n "${custom_bin_backup}" ]]; then
  1507. local restored_custom_bin=()
  1508. while IFS= read -r -d '' f; do
  1509. local rel="${f#"${custom_bin_backup}"/}"
  1510. case "${rel}" in
  1511. config.json | mtproto | mtproto/*) continue ;;
  1512. esac
  1513. if [[ ! -e "bin/${rel}" ]]; then
  1514. mkdir -p "bin/$(dirname "${rel}")"
  1515. cp -a "${f}" "bin/${rel}"
  1516. restored_custom_bin+=("${rel}")
  1517. fi
  1518. done < <(find "${custom_bin_backup}" \( -type f -o -type l \) -print0)
  1519. rm -rf "${custom_bin_backup}"
  1520. custom_bin_backup=""
  1521. if [[ ${#restored_custom_bin[@]} -gt 0 ]]; then
  1522. echo -e "${green}Restored custom file(s) in bin/ not shipped by this release: ${restored_custom_bin[*]}${plain}"
  1523. fi
  1524. fi
  1525. trap - EXIT INT TERM
  1526. # Update x-ui cli and se set permission
  1527. mv -f "${xui_script_temp}" /usr/bin/x-ui
  1528. if [[ $? -ne 0 ]]; then
  1529. rm -f "${xui_script_temp}"
  1530. echo -e "${red}Failed to install x-ui.sh${plain}"
  1531. exit 1
  1532. fi
  1533. chmod +x /usr/bin/x-ui
  1534. mkdir -p /var/log/x-ui
  1535. config_after_install
  1536. # Etckeeper compatibility
  1537. if [ -d "/etc/.git" ]; then
  1538. if [ -f "/etc/.gitignore" ]; then
  1539. if ! grep -q "x-ui/x-ui.db" "/etc/.gitignore"; then
  1540. echo "" >> "/etc/.gitignore"
  1541. echo "x-ui/x-ui.db" >> "/etc/.gitignore"
  1542. echo -e "${green}Added x-ui.db to /etc/.gitignore for etckeeper${plain}"
  1543. fi
  1544. else
  1545. echo "x-ui/x-ui.db" > "/etc/.gitignore"
  1546. echo -e "${green}Created /etc/.gitignore and added x-ui.db for etckeeper${plain}"
  1547. fi
  1548. fi
  1549. if [[ $release == "alpine" ]]; then
  1550. xui_rc_temp="/etc/init.d/x-ui.tmp.$$"
  1551. rm -f "${xui_rc_temp}"
  1552. curl -fLRo "${xui_rc_temp}" "https://raw.githubusercontent.com/MHSanaei/3x-ui/${script_ref}/x-ui.rc"
  1553. if [[ $? -ne 0 ]]; then
  1554. rm -f "${xui_rc_temp}"
  1555. echo -e "${red}Failed to download x-ui.rc${plain}"
  1556. exit 1
  1557. fi
  1558. if [[ ! -s "${xui_rc_temp}" ]]; then
  1559. rm -f "${xui_rc_temp}"
  1560. echo -e "${red}Downloaded x-ui.rc is empty${plain}"
  1561. exit 1
  1562. fi
  1563. mv -f "${xui_rc_temp}" /etc/init.d/x-ui
  1564. if [[ $? -ne 0 ]]; then
  1565. rm -f "${xui_rc_temp}"
  1566. echo -e "${red}Failed to install x-ui.rc${plain}"
  1567. exit 1
  1568. fi
  1569. chmod +x /etc/init.d/x-ui
  1570. rc-update add x-ui
  1571. rc-service x-ui start
  1572. else
  1573. # Install systemd service file
  1574. service_installed=false
  1575. if [ -f "x-ui.service" ]; then
  1576. echo -e "${green}Found x-ui.service in extracted files, installing...${plain}"
  1577. if _install_xui_service_unit "x-ui.service" "false"; then
  1578. service_installed=true
  1579. fi
  1580. fi
  1581. if [ "$service_installed" = false ]; then
  1582. case "${release}" in
  1583. ubuntu | debian | armbian)
  1584. if [ -f "x-ui.service.debian" ]; then
  1585. echo -e "${green}Found x-ui.service.debian in extracted files, installing...${plain}"
  1586. if _install_xui_service_unit "x-ui.service.debian" "false"; then
  1587. service_installed=true
  1588. fi
  1589. fi
  1590. ;;
  1591. arch | manjaro | parch)
  1592. if [ -f "x-ui.service.arch" ]; then
  1593. echo -e "${green}Found x-ui.service.arch in extracted files, installing...${plain}"
  1594. if _install_xui_service_unit "x-ui.service.arch" "false"; then
  1595. service_installed=true
  1596. fi
  1597. fi
  1598. ;;
  1599. *)
  1600. if [ -f "x-ui.service.rhel" ]; then
  1601. echo -e "${green}Found x-ui.service.rhel in extracted files, installing...${plain}"
  1602. if _install_xui_service_unit "x-ui.service.rhel" "false"; then
  1603. service_installed=true
  1604. fi
  1605. fi
  1606. ;;
  1607. esac
  1608. fi
  1609. # If service file not found in tar.gz, download from GitHub
  1610. if [ "$service_installed" = false ]; then
  1611. echo -e "${yellow}Service files not found in tar.gz, downloading from GitHub...${plain}"
  1612. case "${release}" in
  1613. ubuntu | debian | armbian)
  1614. service_unit_url="https://raw.githubusercontent.com/MHSanaei/3x-ui/${script_ref}/x-ui.service.debian"
  1615. ;;
  1616. arch | manjaro | parch)
  1617. service_unit_url="https://raw.githubusercontent.com/MHSanaei/3x-ui/${script_ref}/x-ui.service.arch"
  1618. ;;
  1619. *)
  1620. service_unit_url="https://raw.githubusercontent.com/MHSanaei/3x-ui/${script_ref}/x-ui.service.rhel"
  1621. ;;
  1622. esac
  1623. if ! _install_xui_service_unit "$service_unit_url" "true"; then
  1624. echo -e "${red}Failed to install x-ui.service from GitHub (${script_ref}) -- the release tarball did not ship one either${plain}"
  1625. exit 1
  1626. fi
  1627. service_installed=true
  1628. fi
  1629. if [ "$service_installed" = true ]; then
  1630. echo -e "${green}Setting up systemd unit...${plain}"
  1631. chown root:root ${xui_service}/x-ui.service > /dev/null 2>&1
  1632. chmod 644 ${xui_service}/x-ui.service > /dev/null 2>&1
  1633. systemctl daemon-reload
  1634. systemctl enable x-ui
  1635. systemctl start x-ui
  1636. else
  1637. echo -e "${red}Failed to install x-ui.service file${plain}"
  1638. exit 1
  1639. fi
  1640. fi
  1641. # IP Limit relies on fail2ban; install + configure it now so the feature
  1642. # works out of the box (no-op when XUI_ENABLE_FAIL2BAN=false). Never fatal.
  1643. setup_fail2ban
  1644. echo -e "${green}x-ui ${tag_version}${plain} installation finished, it is running now..."
  1645. echo -e ""
  1646. echo -e "┌───────────────────────────────────────────────────────┐
  1647. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  1648. │ │
  1649. │ ${blue}x-ui${plain} - Admin Management Script │
  1650. │ ${blue}x-ui start${plain} - Start │
  1651. │ ${blue}x-ui stop${plain} - Stop │
  1652. │ ${blue}x-ui restart${plain} - Restart │
  1653. │ ${blue}x-ui status${plain} - Current Status │
  1654. │ ${blue}x-ui settings${plain} - Current Settings │
  1655. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  1656. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  1657. │ ${blue}x-ui log${plain} - Check logs │
  1658. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  1659. │ ${blue}x-ui update${plain} - Update │
  1660. │ ${blue}x-ui legacy${plain} - Legacy version │
  1661. │ ${blue}x-ui install${plain} - Install │
  1662. │ ${blue}x-ui uninstall${plain} - Uninstall │
  1663. └───────────────────────────────────────────────────────┘"
  1664. }
  1665. echo -e "${green}Running...${plain}"
  1666. install_base
  1667. install_x-ui $1