1
0

update.sh 46 KB

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