install.sh 29 KB

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