1
0

x-ui.sh 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. #!/bin/bash
  2. red='\033[0;31m'
  3. green='\033[0;32m'
  4. yellow='\033[0;33m'
  5. plain='\033[0m'
  6. #Add some basic function here
  7. function LOGD() {
  8. echo -e "${yellow}[DEG] $* ${plain}"
  9. }
  10. function LOGE() {
  11. echo -e "${red}[ERR] $* ${plain}"
  12. }
  13. function LOGI() {
  14. echo -e "${green}[INF] $* ${plain}"
  15. }
  16. # check root
  17. [[ $EUID -ne 0 ]] && LOGE "ERROR: You must be root to run this script! \n" && exit 1
  18. # Check OS and set release variable
  19. if [[ -f /etc/os-release ]]; then
  20. source /etc/os-release
  21. release=$ID
  22. elif [[ -f /usr/lib/os-release ]]; then
  23. source /usr/lib/os-release
  24. release=$ID
  25. else
  26. echo "Failed to check the system OS, please contact the author!" >&2
  27. exit 1
  28. fi
  29. echo "The OS release is: $release"
  30. os_version=""
  31. os_version=$(grep -i version_id /etc/os-release | cut -d \" -f2 | cut -d . -f1)
  32. if [[ "${release}" == "centos" ]]; then
  33. if [[ ${os_version} -lt 8 ]]; then
  34. echo -e "${red} Please use CentOS 8 or higher ${plain}\n" && exit 1
  35. fi
  36. elif [[ "${release}" == "ubuntu" ]]; then
  37. if [[ ${os_version} -lt 20 ]]; then
  38. echo -e "${red}please use Ubuntu 20 or higher version! ${plain}\n" && exit 1
  39. fi
  40. elif [[ "${release}" == "fedora" ]]; then
  41. if [[ ${os_version} -lt 36 ]]; then
  42. echo -e "${red}please use Fedora 36 or higher version! ${plain}\n" && exit 1
  43. fi
  44. elif [[ "${release}" == "debian" ]]; then
  45. if [[ ${os_version} -lt 10 ]]; then
  46. echo -e "${red} Please use Debian 10 or higher ${plain}\n" && exit 1
  47. fi
  48. elif [[ "${release}" == "arch" ]]; then
  49. echo "OS is ArchLinux"
  50. fi
  51. # Declare Variables
  52. log_folder="${XUI_LOG_FOLDER:=/var/log}"
  53. iplimit_log_path="${log_folder}/3xipl.log"
  54. iplimit_banned_log_path="${log_folder}/3xipl-banned.log"
  55. confirm() {
  56. if [[ $# > 1 ]]; then
  57. echo && read -p "$1 [Default $2]: " temp
  58. if [[ "${temp}" == "" ]]; then
  59. temp=$2
  60. fi
  61. else
  62. read -p "$1 [y/n]: " temp
  63. fi
  64. if [[ "${temp}" == "y" || "${temp}" == "Y" ]]; then
  65. return 0
  66. else
  67. return 1
  68. fi
  69. }
  70. confirm_restart() {
  71. confirm "Restart the panel, Attention: Restarting the panel will also restart xray" "y"
  72. if [[ $? == 0 ]]; then
  73. restart
  74. else
  75. show_menu
  76. fi
  77. }
  78. before_show_menu() {
  79. echo && echo -n -e "${yellow}Press enter to return to the main menu: ${plain}" && read temp
  80. show_menu
  81. }
  82. install() {
  83. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  84. if [[ $? == 0 ]]; then
  85. if [[ $# == 0 ]]; then
  86. start
  87. else
  88. start 0
  89. fi
  90. fi
  91. }
  92. update() {
  93. confirm "This function will forcefully reinstall the latest version, and the data will not be lost. Do you want to continue?" "n"
  94. if [[ $? != 0 ]]; then
  95. LOGE "Cancelled"
  96. if [[ $# == 0 ]]; then
  97. before_show_menu
  98. fi
  99. return 0
  100. fi
  101. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  102. if [[ $? == 0 ]]; then
  103. LOGI "Update is complete, Panel has automatically restarted "
  104. exit 0
  105. fi
  106. }
  107. uninstall() {
  108. confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
  109. if [[ $? != 0 ]]; then
  110. if [[ $# == 0 ]]; then
  111. show_menu
  112. fi
  113. return 0
  114. fi
  115. systemctl stop x-ui
  116. systemctl disable x-ui
  117. rm /etc/systemd/system/x-ui.service -f
  118. systemctl daemon-reload
  119. systemctl reset-failed
  120. rm /etc/x-ui/ -rf
  121. rm /usr/local/x-ui/ -rf
  122. echo ""
  123. echo -e "Uninstalled Successfully, If you want to remove this script, then after exiting the script run ${green}rm /usr/bin/x-ui -f${plain} to delete it."
  124. echo ""
  125. if [[ $# == 0 ]]; then
  126. before_show_menu
  127. fi
  128. }
  129. reset_user() {
  130. confirm "Are you sure to reset the username and password of the panel?" "n"
  131. if [[ $? != 0 ]]; then
  132. if [[ $# == 0 ]]; then
  133. show_menu
  134. fi
  135. return 0
  136. fi
  137. read -rp "Please set the login username [default is a random username]: " config_account
  138. [[ -z $config_account ]] && config_account=$(date +%s%N | md5sum | cut -c 1-8)
  139. read -rp "Please set the login password [default is a random password]: " config_password
  140. [[ -z $config_password ]] && config_password=$(date +%s%N | md5sum | cut -c 1-8)
  141. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password} >/dev/null 2>&1
  142. /usr/local/x-ui/x-ui setting -remove_secret >/dev/null 2>&1
  143. echo -e "Panel login username has been reset to: ${green} ${config_account} ${plain}"
  144. echo -e "Panel login password has been reset to: ${green} ${config_password} ${plain}"
  145. echo -e "${yellow} Panel login secret token disabled ${plain}"
  146. echo -e "${green} Please use the new login username and password to access the X-UI panel. Also remember them! ${plain}"
  147. confirm_restart
  148. }
  149. reset_config() {
  150. confirm "Are you sure you want to reset all panel settings, Account data will not be lost, Username and password will not change" "n"
  151. if [[ $? != 0 ]]; then
  152. if [[ $# == 0 ]]; then
  153. show_menu
  154. fi
  155. return 0
  156. fi
  157. /usr/local/x-ui/x-ui setting -reset
  158. echo -e "All panel settings have been reset to default, Please restart the panel now, and use the default ${green}2053${plain} Port to Access the web Panel"
  159. confirm_restart
  160. }
  161. check_config() {
  162. info=$(/usr/local/x-ui/x-ui setting -show true)
  163. if [[ $? != 0 ]]; then
  164. LOGE "get current settings error, please check logs"
  165. show_menu
  166. fi
  167. LOGI "${info}"
  168. }
  169. set_port() {
  170. echo && echo -n -e "Enter port number[1-65535]: " && read port
  171. if [[ -z "${port}" ]]; then
  172. LOGD "Cancelled"
  173. before_show_menu
  174. else
  175. /usr/local/x-ui/x-ui setting -port ${port}
  176. echo -e "The port is set, Please restart the panel now, and use the new port ${green}${port}${plain} to access web panel"
  177. confirm_restart
  178. fi
  179. }
  180. start() {
  181. check_status
  182. if [[ $? == 0 ]]; then
  183. echo ""
  184. LOGI "Panel is running, No need to start again, If you need to restart, please select restart"
  185. else
  186. systemctl start x-ui
  187. sleep 2
  188. check_status
  189. if [[ $? == 0 ]]; then
  190. LOGI "x-ui Started Successfully"
  191. else
  192. LOGE "panel Failed to start, Probably because it takes longer than two seconds to start, Please check the log information later"
  193. fi
  194. fi
  195. if [[ $# == 0 ]]; then
  196. before_show_menu
  197. fi
  198. }
  199. stop() {
  200. check_status
  201. if [[ $? == 1 ]]; then
  202. echo ""
  203. LOGI "Panel stopped, No need to stop again!"
  204. else
  205. systemctl stop x-ui
  206. sleep 2
  207. check_status
  208. if [[ $? == 1 ]]; then
  209. LOGI "x-ui and xray stopped successfully"
  210. else
  211. LOGE "Panel stop failed, Probably because the stop time exceeds two seconds, Please check the log information later"
  212. fi
  213. fi
  214. if [[ $# == 0 ]]; then
  215. before_show_menu
  216. fi
  217. }
  218. restart() {
  219. systemctl restart x-ui
  220. sleep 2
  221. check_status
  222. if [[ $? == 0 ]]; then
  223. LOGI "x-ui and xray Restarted successfully"
  224. else
  225. LOGE "Panel restart failed, Probably because it takes longer than two seconds to start, Please check the log information later"
  226. fi
  227. if [[ $# == 0 ]]; then
  228. before_show_menu
  229. fi
  230. }
  231. status() {
  232. systemctl status x-ui -l
  233. if [[ $# == 0 ]]; then
  234. before_show_menu
  235. fi
  236. }
  237. enable() {
  238. systemctl enable x-ui
  239. if [[ $? == 0 ]]; then
  240. LOGI "x-ui Set to boot automatically on startup successfully"
  241. else
  242. LOGE "x-ui Failed to set Autostart"
  243. fi
  244. if [[ $# == 0 ]]; then
  245. before_show_menu
  246. fi
  247. }
  248. disable() {
  249. systemctl disable x-ui
  250. if [[ $? == 0 ]]; then
  251. LOGI "x-ui Autostart Cancelled successfully"
  252. else
  253. LOGE "x-ui Failed to cancel autostart"
  254. fi
  255. if [[ $# == 0 ]]; then
  256. before_show_menu
  257. fi
  258. }
  259. show_log() {
  260. journalctl -u x-ui.service -e --no-pager -f
  261. if [[ $# == 0 ]]; then
  262. before_show_menu
  263. fi
  264. }
  265. show_banlog() {
  266. if test -f "${iplimit_banned_log_path}"; then
  267. if [[ -s "${iplimit_banned_log_path}" ]]; then
  268. cat ${iplimit_banned_log_path}
  269. else
  270. echo -e "${red}Log file is empty.${plain}\n"
  271. fi
  272. else
  273. echo -e "${red}Log file not found. Please Install Fail2ban and IP Limit first.${plain}\n"
  274. fi
  275. }
  276. enable_bbr() {
  277. if grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf && grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  278. echo -e "${green}BBR is already enabled!${plain}"
  279. exit 0
  280. fi
  281. # Check the OS and install necessary packages
  282. case "${release}" in
  283. ubuntu|debian)
  284. apt-get update && apt-get install -yqq --no-install-recommends ca-certificates
  285. ;;
  286. centos)
  287. yum -y update && yum -y install ca-certificates
  288. ;;
  289. fedora)
  290. dnf -y update && dnf -y install ca-certificates
  291. ;;
  292. *)
  293. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  294. exit 1
  295. ;;
  296. esac
  297. # Enable BBR
  298. echo "net.core.default_qdisc=fq" | tee -a /etc/sysctl.conf
  299. echo "net.ipv4.tcp_congestion_control=bbr" | tee -a /etc/sysctl.conf
  300. # Apply changes
  301. sysctl -p
  302. # Verify that BBR is enabled
  303. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "bbr" ]]; then
  304. echo -e "${green}BBR has been enabled successfully.${plain}"
  305. else
  306. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  307. fi
  308. }
  309. update_shell() {
  310. wget -O /usr/bin/x-ui -N --no-check-certificate https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  311. if [[ $? != 0 ]]; then
  312. echo ""
  313. LOGE "Failed to download script, Please check whether the machine can connect Github"
  314. before_show_menu
  315. else
  316. chmod +x /usr/bin/x-ui
  317. LOGI "Upgrade script succeeded, Please rerun the script" && exit 0
  318. fi
  319. }
  320. # 0: running, 1: not running, 2: not installed
  321. check_status() {
  322. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  323. return 2
  324. fi
  325. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  326. if [[ "${temp}" == "running" ]]; then
  327. return 0
  328. else
  329. return 1
  330. fi
  331. }
  332. check_enabled() {
  333. temp=$(systemctl is-enabled x-ui)
  334. if [[ "${temp}" == "enabled" ]]; then
  335. return 0
  336. else
  337. return 1
  338. fi
  339. }
  340. check_uninstall() {
  341. check_status
  342. if [[ $? != 2 ]]; then
  343. echo ""
  344. LOGE "Panel installed, Please do not reinstall"
  345. if [[ $# == 0 ]]; then
  346. before_show_menu
  347. fi
  348. return 1
  349. else
  350. return 0
  351. fi
  352. }
  353. check_install() {
  354. check_status
  355. if [[ $? == 2 ]]; then
  356. echo ""
  357. LOGE "Please install the panel first"
  358. if [[ $# == 0 ]]; then
  359. before_show_menu
  360. fi
  361. return 1
  362. else
  363. return 0
  364. fi
  365. }
  366. show_status() {
  367. check_status
  368. case $? in
  369. 0)
  370. echo -e "Panel state: ${green}Running${plain}"
  371. show_enable_status
  372. ;;
  373. 1)
  374. echo -e "Panel state: ${yellow}Not Running${plain}"
  375. show_enable_status
  376. ;;
  377. 2)
  378. echo -e "Panel state: ${red}Not Installed${plain}"
  379. ;;
  380. esac
  381. show_xray_status
  382. }
  383. show_enable_status() {
  384. check_enabled
  385. if [[ $? == 0 ]]; then
  386. echo -e "Start automatically: ${green}Yes${plain}"
  387. else
  388. echo -e "Start automatically: ${red}No${plain}"
  389. fi
  390. }
  391. check_xray_status() {
  392. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  393. if [[ count -ne 0 ]]; then
  394. return 0
  395. else
  396. return 1
  397. fi
  398. }
  399. show_xray_status() {
  400. check_xray_status
  401. if [[ $? == 0 ]]; then
  402. echo -e "xray state: ${green}Running${plain}"
  403. else
  404. echo -e "xray state: ${red}Not Running${plain}"
  405. fi
  406. }
  407. open_ports() {
  408. if ! command -v ufw &>/dev/null; then
  409. echo "ufw firewall is not installed. Installing now..."
  410. apt-get update
  411. apt-get install -y ufw
  412. else
  413. echo "ufw firewall is already installed"
  414. fi
  415. # Check if the firewall is inactive
  416. if ufw status | grep -q "Status: active"; then
  417. echo "firewall is already active"
  418. else
  419. # Open the necessary ports
  420. ufw allow ssh
  421. ufw allow http
  422. ufw allow https
  423. ufw allow 2053/tcp
  424. # Enable the firewall
  425. ufw --force enable
  426. fi
  427. # Prompt the user to enter a list of ports
  428. read -p "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  429. # Check if the input is valid
  430. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  431. echo "Error: Invalid input. Please enter a comma-separated list of ports or a range of ports (e.g. 80,443,2053 or 400-500)." >&2
  432. exit 1
  433. fi
  434. # Open the specified ports using ufw
  435. IFS=',' read -ra PORT_LIST <<<"$ports"
  436. for port in "${PORT_LIST[@]}"; do
  437. if [[ $port == *-* ]]; then
  438. # Split the range into start and end ports
  439. start_port=$(echo $port | cut -d'-' -f1)
  440. end_port=$(echo $port | cut -d'-' -f2)
  441. # Loop through the range and open each port
  442. for ((i = start_port; i <= end_port; i++)); do
  443. ufw allow $i
  444. done
  445. else
  446. ufw allow "$port"
  447. fi
  448. done
  449. # Confirm that the ports are open
  450. ufw status | grep $ports
  451. }
  452. update_geo() {
  453. local defaultBinFolder="/usr/local/x-ui/bin"
  454. read -p "Please enter x-ui bin folder path. Leave blank for default. (Default: '${defaultBinFolder}')" binFolder
  455. binFolder=${binFolder:-${defaultBinFolder}}
  456. if [[ ! -d ${binFolder} ]]; then
  457. LOGE "Folder ${binFolder} not exists!"
  458. LOGI "making bin folder: ${binFolder}..."
  459. mkdir -p ${binFolder}
  460. fi
  461. systemctl stop x-ui
  462. cd ${binFolder}
  463. rm -f geoip.dat geosite.dat iran.dat
  464. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  465. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  466. wget -N https://github.com/MasterKia/iran-hosted-domains/releases/latest/download/iran.dat
  467. systemctl start x-ui
  468. echo -e "${green}Geosite.dat + Geoip.dat + Iran.dat have been updated successfully in bin folder '${binfolder}'!${plain}"
  469. before_show_menu
  470. }
  471. install_acme() {
  472. cd ~
  473. LOGI "install acme..."
  474. curl https://get.acme.sh | sh
  475. if [ $? -ne 0 ]; then
  476. LOGE "install acme failed"
  477. return 1
  478. else
  479. LOGI "install acme succeed"
  480. fi
  481. return 0
  482. }
  483. ssl_cert_issue_main() {
  484. echo -e "${green}\t1.${plain} Get SSL"
  485. echo -e "${green}\t2.${plain} Revoke"
  486. echo -e "${green}\t3.${plain} Force Renew"
  487. echo -e "${green}\t0.${plain} Back to Main Menu"
  488. read -p "Choose an option: " choice
  489. case "$choice" in
  490. 0)
  491. show_menu ;;
  492. 1)
  493. ssl_cert_issue ;;
  494. 2)
  495. local domain=""
  496. read -p "Please enter your domain name to revoke the certificate: " domain
  497. ~/.acme.sh/acme.sh --revoke -d ${domain}
  498. LOGI "Certificate revoked"
  499. ;;
  500. 3)
  501. local domain=""
  502. read -p "Please enter your domain name to forcefully renew an SSL certificate: " domain
  503. ~/.acme.sh/acme.sh --renew -d ${domain} --force ;;
  504. *) echo "Invalid choice" ;;
  505. esac
  506. }
  507. ssl_cert_issue() {
  508. # check for acme.sh first
  509. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  510. echo "acme.sh could not be found. we will install it"
  511. install_acme
  512. if [ $? -ne 0 ]; then
  513. LOGE "install acme failed, please check logs"
  514. exit 1
  515. fi
  516. fi
  517. # install socat second
  518. case "${release}" in
  519. ubuntu|debian)
  520. apt update && apt install socat -y ;;
  521. centos)
  522. yum -y update && yum -y install socat ;;
  523. fedora)
  524. dnf -y update && dnf -y install socat ;;
  525. *)
  526. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  527. exit 1 ;;
  528. esac
  529. if [ $? -ne 0 ]; then
  530. LOGE "install socat failed, please check logs"
  531. exit 1
  532. else
  533. LOGI "install socat succeed..."
  534. fi
  535. # get the domain here,and we need verify it
  536. local domain=""
  537. read -p "Please enter your domain name:" domain
  538. LOGD "your domain is:${domain},check it..."
  539. # here we need to judge whether there exists cert already
  540. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  541. if [ ${currentCert} == ${domain} ]; then
  542. local certInfo=$(~/.acme.sh/acme.sh --list)
  543. LOGE "system already has certs here,can not issue again,current certs details:"
  544. LOGI "$certInfo"
  545. exit 1
  546. else
  547. LOGI "your domain is ready for issuing cert now..."
  548. fi
  549. # create a directory for install cert
  550. certPath="/root/cert/${domain}"
  551. if [ ! -d "$certPath" ]; then
  552. mkdir -p "$certPath"
  553. else
  554. rm -rf "$certPath"
  555. mkdir -p "$certPath"
  556. fi
  557. # get needed port here
  558. local WebPort=80
  559. read -p "please choose which port do you use,default will be 80 port:" WebPort
  560. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  561. LOGE "your input ${WebPort} is invalid,will use default port"
  562. fi
  563. LOGI "will use port:${WebPort} to issue certs,please make sure this port is open..."
  564. # NOTE:This should be handled by user
  565. # open the port and kill the occupied progress
  566. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  567. ~/.acme.sh/acme.sh --issue -d ${domain} --standalone --httpport ${WebPort}
  568. if [ $? -ne 0 ]; then
  569. LOGE "issue certs failed,please check logs"
  570. rm -rf ~/.acme.sh/${domain}
  571. exit 1
  572. else
  573. LOGE "issue certs succeed,installing certs..."
  574. fi
  575. # install cert
  576. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  577. --key-file /root/cert/${domain}/privkey.pem \
  578. --fullchain-file /root/cert/${domain}/fullchain.pem
  579. if [ $? -ne 0 ]; then
  580. LOGE "install certs failed,exit"
  581. rm -rf ~/.acme.sh/${domain}
  582. exit 1
  583. else
  584. LOGI "install certs succeed,enable auto renew..."
  585. fi
  586. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  587. if [ $? -ne 0 ]; then
  588. LOGE "auto renew failed, certs details:"
  589. ls -lah cert/*
  590. chmod 755 $certPath/*
  591. exit 1
  592. else
  593. LOGI "auto renew succeed, certs details:"
  594. ls -lah cert/*
  595. chmod 755 $certPath/*
  596. fi
  597. }
  598. ssl_cert_issue_CF() {
  599. echo -E ""
  600. LOGD "******Instructions for use******"
  601. LOGI "This Acme script requires the following data:"
  602. LOGI "1.Cloudflare Registered e-mail"
  603. LOGI "2.Cloudflare Global API Key"
  604. LOGI "3.The domain name that has been resolved dns to the current server by Cloudflare"
  605. LOGI "4.The script applies for a certificate. The default installation path is /root/cert "
  606. confirm "Confirmed?[y/n]" "y"
  607. if [ $? -eq 0 ]; then
  608. # check for acme.sh first
  609. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  610. echo "acme.sh could not be found. we will install it"
  611. install_acme
  612. if [ $? -ne 0 ]; then
  613. LOGE "install acme failed, please check logs"
  614. exit 1
  615. fi
  616. fi
  617. CF_Domain=""
  618. CF_GlobalKey=""
  619. CF_AccountEmail=""
  620. certPath=/root/cert
  621. if [ ! -d "$certPath" ]; then
  622. mkdir $certPath
  623. else
  624. rm -rf $certPath
  625. mkdir $certPath
  626. fi
  627. LOGD "Please set a domain name:"
  628. read -p "Input your domain here:" CF_Domain
  629. LOGD "Your domain name is set to:${CF_Domain}"
  630. LOGD "Please set the API key:"
  631. read -p "Input your key here:" CF_GlobalKey
  632. LOGD "Your API key is:${CF_GlobalKey}"
  633. LOGD "Please set up registered email:"
  634. read -p "Input your email here:" CF_AccountEmail
  635. LOGD "Your registered email address is:${CF_AccountEmail}"
  636. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  637. if [ $? -ne 0 ]; then
  638. LOGE "Default CA, Lets'Encrypt fail, script exiting..."
  639. exit 1
  640. fi
  641. export CF_Key="${CF_GlobalKey}"
  642. export CF_Email=${CF_AccountEmail}
  643. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  644. if [ $? -ne 0 ]; then
  645. LOGE "Certificate issuance failed, script exiting..."
  646. exit 1
  647. else
  648. LOGI "Certificate issued Successfully, Installing..."
  649. fi
  650. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
  651. --cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
  652. --fullchain-file /root/cert/fullchain.cer
  653. if [ $? -ne 0 ]; then
  654. LOGE "Certificate installation failed, script exiting..."
  655. exit 1
  656. else
  657. LOGI "Certificate installed Successfully,Turning on automatic updates..."
  658. fi
  659. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  660. if [ $? -ne 0 ]; then
  661. LOGE "Auto update setup Failed, script exiting..."
  662. ls -lah cert
  663. chmod 755 $certPath
  664. exit 1
  665. else
  666. LOGI "The certificate is installed and auto-renewal is turned on, Specific information is as follows"
  667. ls -lah cert
  668. chmod 755 $certPath
  669. fi
  670. else
  671. show_menu
  672. fi
  673. }
  674. warp_cloudflare() {
  675. echo -e "${green}\t1.${plain} Install WARP socks5 proxy"
  676. echo -e "${green}\t2.${plain} Account Type (free, plus, team)"
  677. echo -e "${green}\t3.${plain} Turn on/off WireProxy"
  678. echo -e "${green}\t4.${plain} Uninstall WARP"
  679. echo -e "${green}\t0.${plain} Back to Main Menu"
  680. read -p "Choose an option: " choice
  681. case "$choice" in
  682. 0)
  683. show_menu ;;
  684. 1)
  685. bash <(curl -sSL https://raw.githubusercontent.com/hamid-gh98/x-ui-scripts/main/install_warp_proxy.sh)
  686. ;;
  687. 2)
  688. warp a
  689. ;;
  690. 3)
  691. warp y
  692. ;;
  693. 4)
  694. warp u
  695. ;;
  696. *) echo "Invalid choice" ;;
  697. esac
  698. }
  699. run_speedtest() {
  700. # Check if Speedtest is already installed
  701. if ! command -v speedtest &> /dev/null; then
  702. # If not installed, install it
  703. local pkg_manager=""
  704. local speedtest_install_script=""
  705. if command -v dnf &> /dev/null; then
  706. pkg_manager="dnf"
  707. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  708. elif command -v yum &> /dev/null; then
  709. pkg_manager="yum"
  710. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  711. elif command -v apt-get &> /dev/null; then
  712. pkg_manager="apt-get"
  713. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  714. elif command -v apt &> /dev/null; then
  715. pkg_manager="apt"
  716. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  717. fi
  718. if [[ -z $pkg_manager ]]; then
  719. echo "Error: Package manager not found. You may need to install Speedtest manually."
  720. return 1
  721. else
  722. curl -s $speedtest_install_script | bash
  723. $pkg_manager install -y speedtest
  724. fi
  725. fi
  726. # Run Speedtest
  727. speedtest
  728. }
  729. create_iplimit_jails() {
  730. # Use default bantime if not passed => 5 minutes
  731. local bantime="${1:-5}"
  732. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  733. [3x-ipl]
  734. enabled=true
  735. filter=3x-ipl
  736. action=3x-ipl
  737. logpath=${iplimit_log_path}
  738. maxretry=4
  739. findtime=60
  740. bantime=${bantime}m
  741. EOF
  742. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  743. [Definition]
  744. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  745. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  746. ignoreregex =
  747. EOF
  748. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  749. [INCLUDES]
  750. before = iptables-common.conf
  751. [Definition]
  752. actionstart = <iptables> -N f2b-<name>
  753. <iptables> -A f2b-<name> -j <returntype>
  754. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  755. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  756. <actionflush>
  757. <iptables> -X f2b-<name>
  758. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  759. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  760. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  761. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  762. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  763. [Init]
  764. EOF
  765. echo -e "${green}Created Ip Limit jail files with a bantime of ${bantime} minutes.${plain}"
  766. }
  767. iplimit_remove_conflicts() {
  768. local jail_files=(
  769. /etc/fail2ban/jail.conf
  770. /etc/fail2ban/jail.local
  771. )
  772. for file in "${jail_files[@]}"; do
  773. # Check for [3x-ipl] config in jail file then remove it
  774. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  775. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  776. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  777. fi
  778. done
  779. }
  780. iplimit_main() {
  781. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  782. echo -e "${green}\t2.${plain} Change Ban Duration"
  783. echo -e "${green}\t3.${plain} Unban Everyone"
  784. echo -e "${green}\t4.${plain} Check Logs"
  785. echo -e "${green}\t5.${plain} fail2ban status"
  786. echo -e "${green}\t6.${plain} Uninstall IP Limit"
  787. echo -e "${green}\t0.${plain} Back to Main Menu"
  788. read -p "Choose an option: " choice
  789. case "$choice" in
  790. 0)
  791. show_menu ;;
  792. 1)
  793. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  794. if [[ $? == 0 ]]; then
  795. install_iplimit
  796. else
  797. iplimit_main
  798. fi ;;
  799. 2)
  800. read -rp "Please enter new Ban Duration in Minutes [default 5]: " NUM
  801. if [[ $NUM =~ ^[0-9]+$ ]]; then
  802. create_iplimit_jails ${NUM}
  803. systemctl restart fail2ban
  804. else
  805. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  806. fi
  807. iplimit_main ;;
  808. 3)
  809. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  810. if [[ $? == 0 ]]; then
  811. fail2ban-client reload --restart --unban 3x-ipl
  812. echo -e "${green}All users Unbanned successfully.${plain}"
  813. iplimit_main
  814. else
  815. echo -e "${yellow}Cancelled.${plain}"
  816. fi
  817. iplimit_main ;;
  818. 4)
  819. show_banlog
  820. ;;
  821. 5)
  822. service fail2ban status
  823. ;;
  824. 6)
  825. remove_iplimit ;;
  826. *) echo "Invalid choice" ;;
  827. esac
  828. }
  829. install_iplimit() {
  830. if ! command -v fail2ban-client &>/dev/null; then
  831. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  832. # Check the OS and install necessary packages
  833. case "${release}" in
  834. ubuntu|debian)
  835. apt update && apt install fail2ban -y ;;
  836. centos)
  837. yum -y update && yum -y install fail2ban ;;
  838. fedora)
  839. dnf -y update && dnf -y install fail2ban ;;
  840. *)
  841. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  842. exit 1 ;;
  843. esac
  844. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  845. else
  846. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  847. fi
  848. echo -e "${green}Configuring IP Limit...${plain}\n"
  849. # make sure there's no conflict for jail files
  850. iplimit_remove_conflicts
  851. # Check if log file exists
  852. if ! test -f "${iplimit_banned_log_path}"; then
  853. touch ${iplimit_banned_log_path}
  854. fi
  855. # Check if service log file exists so fail2ban won't return error
  856. if ! test -f "${iplimit_log_path}"; then
  857. touch ${iplimit_log_path}
  858. fi
  859. # Create the iplimit jail files
  860. # we didn't pass the bantime here to use the default value
  861. create_iplimit_jails
  862. # Launching fail2ban
  863. if ! systemctl is-active --quiet fail2ban; then
  864. systemctl start fail2ban
  865. else
  866. systemctl restart fail2ban
  867. fi
  868. systemctl enable fail2ban
  869. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  870. before_show_menu
  871. }
  872. remove_iplimit(){
  873. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  874. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  875. echo -e "${green}\t0.${plain} Abort"
  876. read -p "Choose an option: " num
  877. case "$num" in
  878. 1)
  879. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  880. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  881. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  882. systemctl restart fail2ban
  883. echo -e "${green}IP Limit removed successfully!${plain}\n"
  884. before_show_menu ;;
  885. 2)
  886. rm -rf /etc/fail2ban
  887. systemctl stop fail2ban
  888. case "${release}" in
  889. ubuntu|debian)
  890. apt-get purge fail2ban -y;;
  891. centos)
  892. yum remove fail2ban -y;;
  893. fedora)
  894. dnf remove fail2ban -y;;
  895. *)
  896. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  897. exit 1 ;;
  898. esac
  899. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  900. before_show_menu ;;
  901. 0)
  902. echo -e "${yellow}Cancelled.${plain}\n"
  903. iplimit_main ;;
  904. *)
  905. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  906. remove_iplimit ;;
  907. esac
  908. }
  909. show_usage() {
  910. echo "x-ui control menu usages: "
  911. echo "------------------------------------------"
  912. echo -e "x-ui - Enter control menu"
  913. echo -e "x-ui start - Start x-ui "
  914. echo -e "x-ui stop - Stop x-ui "
  915. echo -e "x-ui restart - Restart x-ui "
  916. echo -e "x-ui status - Show x-ui status"
  917. echo -e "x-ui enable - Enable x-ui on system startup"
  918. echo -e "x-ui disable - Disable x-ui on system startup"
  919. echo -e "x-ui log - Check x-ui logs"
  920. echo -e "x-ui banlog - Check Fail2ban ban logs"
  921. echo -e "x-ui update - Update x-ui "
  922. echo -e "x-ui install - Install x-ui "
  923. echo -e "x-ui uninstall - Uninstall x-ui "
  924. echo "------------------------------------------"
  925. }
  926. show_menu() {
  927. echo -e "
  928. ${green}3X-ui Panel Management Script${plain}
  929. ${green}0.${plain} Exit Script
  930. ————————————————
  931. ${green}1.${plain} Install x-ui
  932. ${green}2.${plain} Update x-ui
  933. ${green}3.${plain} Uninstall x-ui
  934. ————————————————
  935. ${green}4.${plain} Reset Username & Password & Secret Token
  936. ${green}5.${plain} Reset Panel Settings
  937. ${green}6.${plain} Change Panel Port
  938. ${green}7.${plain} View Current Panel Settings
  939. ————————————————
  940. ${green}8.${plain} Start x-ui
  941. ${green}9.${plain} Stop x-ui
  942. ${green}10.${plain} Restart x-ui
  943. ${green}11.${plain} Check x-ui Status
  944. ${green}12.${plain} Check x-ui Logs
  945. ————————————————
  946. ${green}13.${plain} Enable x-ui On System Startup
  947. ${green}14.${plain} Disable x-ui On System Startup
  948. ————————————————
  949. ${green}15.${plain} SSL Certificate Management
  950. ${green}16.${plain} Cloudflare SSL Certificate
  951. ${green}17.${plain} IP Limit Management
  952. ${green}18.${plain} WARP Management
  953. ————————————————
  954. ${green}19.${plain} Enable BBR
  955. ${green}20.${plain} Update Geo Files
  956. ${green}21.${plain} Active Firewall and open ports
  957. ${green}22.${plain} Speedtest by Ookla
  958. "
  959. show_status
  960. echo && read -p "Please enter your selection [0-22]: " num
  961. case "${num}" in
  962. 0)
  963. exit 0
  964. ;;
  965. 1)
  966. check_uninstall && install
  967. ;;
  968. 2)
  969. check_install && update
  970. ;;
  971. 3)
  972. check_install && uninstall
  973. ;;
  974. 4)
  975. check_install && reset_user
  976. ;;
  977. 5)
  978. check_install && reset_config
  979. ;;
  980. 6)
  981. check_install && set_port
  982. ;;
  983. 7)
  984. check_install && check_config
  985. ;;
  986. 8)
  987. check_install && start
  988. ;;
  989. 9)
  990. check_install && stop
  991. ;;
  992. 10)
  993. check_install && restart
  994. ;;
  995. 11)
  996. check_install && status
  997. ;;
  998. 12)
  999. check_install && show_log
  1000. ;;
  1001. 13)
  1002. check_install && enable
  1003. ;;
  1004. 14)
  1005. check_install && disable
  1006. ;;
  1007. 15)
  1008. ssl_cert_issue_main
  1009. ;;
  1010. 16)
  1011. ssl_cert_issue_CF
  1012. ;;
  1013. 17)
  1014. iplimit_main
  1015. ;;
  1016. 18)
  1017. warp_cloudflare
  1018. ;;
  1019. 19)
  1020. enable_bbr
  1021. ;;
  1022. 20)
  1023. update_geo
  1024. ;;
  1025. 21)
  1026. open_ports
  1027. ;;
  1028. 22)
  1029. run_speedtest
  1030. ;;
  1031. *)
  1032. LOGE "Please enter the correct number [0-22]"
  1033. ;;
  1034. esac
  1035. }
  1036. if [[ $# > 0 ]]; then
  1037. case $1 in
  1038. "start")
  1039. check_install 0 && start 0
  1040. ;;
  1041. "stop")
  1042. check_install 0 && stop 0
  1043. ;;
  1044. "restart")
  1045. check_install 0 && restart 0
  1046. ;;
  1047. "status")
  1048. check_install 0 && status 0
  1049. ;;
  1050. "enable")
  1051. check_install 0 && enable 0
  1052. ;;
  1053. "disable")
  1054. check_install 0 && disable 0
  1055. ;;
  1056. "log")
  1057. check_install 0 && show_log 0
  1058. ;;
  1059. "banlog")
  1060. check_install 0 && show_banlog 0
  1061. ;;
  1062. "update")
  1063. check_install 0 && update 0
  1064. ;;
  1065. "install")
  1066. check_uninstall 0 && install 0
  1067. ;;
  1068. "uninstall")
  1069. check_install 0 && uninstall 0
  1070. ;;
  1071. *) show_usage ;;
  1072. esac
  1073. else
  1074. show_menu
  1075. fi