1
0

x-ui.sh 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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}" == "arch" ]]; then
  33. echo "Your OS is Arch Linux"
  34. elif [[ "${release}" == "parch" ]]; then
  35. echo "Your OS is Parch linux"
  36. elif [[ "${release}" == "manjaro" ]]; then
  37. echo "Your OS is Manjaro"
  38. elif [[ "${release}" == "armbian" ]]; then
  39. echo "Your OS is Armbian"
  40. elif [[ "${release}" == "opensuse-tumbleweed" ]]; then
  41. echo "Your OS is OpenSUSE Tumbleweed"
  42. elif [[ "${release}" == "centos" ]]; then
  43. if [[ ${os_version} -lt 8 ]]; then
  44. echo -e "${red} Please use CentOS 8 or higher ${plain}\n" && exit 1
  45. fi
  46. elif [[ "${release}" == "ubuntu" ]]; then
  47. if [[ ${os_version} -lt 20 ]]; then
  48. echo -e "${red} Please use Ubuntu 20 or higher version!${plain}\n" && exit 1
  49. fi
  50. elif [[ "${release}" == "fedora" ]]; then
  51. if [[ ${os_version} -lt 36 ]]; then
  52. echo -e "${red} Please use Fedora 36 or higher version!${plain}\n" && exit 1
  53. fi
  54. elif [[ "${release}" == "debian" ]]; then
  55. if [[ ${os_version} -lt 11 ]]; then
  56. echo -e "${red} Please use Debian 11 or higher ${plain}\n" && exit 1
  57. fi
  58. elif [[ "${release}" == "almalinux" ]]; then
  59. if [[ ${os_version} -lt 9 ]]; then
  60. echo -e "${red} Please use AlmaLinux 9 or higher ${plain}\n" && exit 1
  61. fi
  62. elif [[ "${release}" == "rocky" ]]; then
  63. if [[ ${os_version} -lt 9 ]]; then
  64. echo -e "${red} Please use Rocky Linux 9 or higher ${plain}\n" && exit 1
  65. fi
  66. elif [[ "${release}" == "oracle" ]]; then
  67. if [[ ${os_version} -lt 8 ]]; then
  68. echo -e "${red} Please use Oracle Linux 8 or higher ${plain}\n" && exit 1
  69. fi
  70. else
  71. echo -e "${red}Your operating system is not supported by this script.${plain}\n"
  72. echo "Please ensure you are using one of the following supported operating systems:"
  73. echo "- Ubuntu 20.04+"
  74. echo "- Debian 11+"
  75. echo "- CentOS 8+"
  76. echo "- Fedora 36+"
  77. echo "- Arch Linux"
  78. echo "- Parch Linux"
  79. echo "- Manjaro"
  80. echo "- Armbian"
  81. echo "- AlmaLinux 9+"
  82. echo "- Rocky Linux 9+"
  83. echo "- Oracle Linux 8+"
  84. echo "- OpenSUSE Tumbleweed"
  85. exit 1
  86. fi
  87. # Declare Variables
  88. log_folder="${XUI_LOG_FOLDER:=/var/log}"
  89. iplimit_log_path="${log_folder}/3xipl.log"
  90. iplimit_banned_log_path="${log_folder}/3xipl-banned.log"
  91. confirm() {
  92. if [[ $# > 1 ]]; then
  93. echo && read -p "$1 [Default $2]: " temp
  94. if [[ "${temp}" == "" ]]; then
  95. temp=$2
  96. fi
  97. else
  98. read -p "$1 [y/n]: " temp
  99. fi
  100. if [[ "${temp}" == "y" || "${temp}" == "Y" ]]; then
  101. return 0
  102. else
  103. return 1
  104. fi
  105. }
  106. confirm_restart() {
  107. confirm "Restart the panel, Attention: Restarting the panel will also restart xray" "y"
  108. if [[ $? == 0 ]]; then
  109. restart
  110. else
  111. show_menu
  112. fi
  113. }
  114. before_show_menu() {
  115. echo && echo -n -e "${yellow}Press enter to return to the main menu: ${plain}" && read temp
  116. show_menu
  117. }
  118. install() {
  119. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  120. if [[ $? == 0 ]]; then
  121. if [[ $# == 0 ]]; then
  122. start
  123. else
  124. start 0
  125. fi
  126. fi
  127. }
  128. update() {
  129. confirm "This function will forcefully reinstall the latest version, and the data will not be lost. Do you want to continue?" "y"
  130. if [[ $? != 0 ]]; then
  131. LOGE "Cancelled"
  132. if [[ $# == 0 ]]; then
  133. before_show_menu
  134. fi
  135. return 0
  136. fi
  137. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  138. if [[ $? == 0 ]]; then
  139. LOGI "Update is complete, Panel has automatically restarted "
  140. exit 0
  141. fi
  142. }
  143. update_menu() {
  144. echo -e "${yellow}Updating Menu${plain}"
  145. confirm "This function will update the menu to the latest changes." "y"
  146. if [[ $? != 0 ]]; then
  147. LOGE "Cancelled"
  148. if [[ $# == 0 ]]; then
  149. before_show_menu
  150. fi
  151. return 0
  152. fi
  153. wget --no-check-certificate -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh
  154. chmod +x /usr/local/x-ui/x-ui.sh
  155. chmod +x /usr/bin/x-ui
  156. if [[ $? == 0 ]]; then
  157. echo -e "${green}Update successful. The panel has automatically restarted.${plain}"
  158. exit 0
  159. else
  160. echo -e "${red}Failed to update the menu.${plain}"
  161. return 1
  162. fi
  163. }
  164. custom_version() {
  165. echo "Enter the panel version (like 2.0.0):"
  166. read panel_version
  167. if [ -z "$panel_version" ]; then
  168. echo "Panel version cannot be empty. Exiting."
  169. exit 1
  170. fi
  171. download_link="https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh"
  172. # Use the entered panel version in the download link
  173. install_command="bash <(curl -Ls $download_link) v$panel_version"
  174. echo "Downloading and installing panel version $panel_version..."
  175. eval $install_command
  176. }
  177. # Function to handle the deletion of the script file
  178. delete_script() {
  179. rm "$0" # Remove the script file itself
  180. exit 1
  181. }
  182. uninstall() {
  183. confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
  184. if [[ $? != 0 ]]; then
  185. if [[ $# == 0 ]]; then
  186. show_menu
  187. fi
  188. return 0
  189. fi
  190. systemctl stop x-ui
  191. systemctl disable x-ui
  192. rm /etc/systemd/system/x-ui.service -f
  193. systemctl daemon-reload
  194. systemctl reset-failed
  195. rm /etc/x-ui/ -rf
  196. rm /usr/local/x-ui/ -rf
  197. echo ""
  198. echo -e "Uninstalled Successfully.\n"
  199. echo "If you need to install this panel again, you can use below command:"
  200. echo -e "${green}bash <(curl -Ls https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh)${plain}"
  201. echo ""
  202. # Trap the SIGTERM signal
  203. trap delete_script SIGTERM
  204. delete_script
  205. }
  206. reset_user() {
  207. confirm "Are you sure to reset the username and password of the panel?" "n"
  208. if [[ $? != 0 ]]; then
  209. if [[ $# == 0 ]]; then
  210. show_menu
  211. fi
  212. return 0
  213. fi
  214. read -rp "Please set the login username [default is a random username]: " config_account
  215. [[ -z $config_account ]] && config_account=$(date +%s%N | md5sum | cut -c 1-8)
  216. read -rp "Please set the login password [default is a random password]: " config_password
  217. [[ -z $config_password ]] && config_password=$(date +%s%N | md5sum | cut -c 1-8)
  218. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password} >/dev/null 2>&1
  219. /usr/local/x-ui/x-ui setting -remove_secret >/dev/null 2>&1
  220. echo -e "Panel login username has been reset to: ${green} ${config_account} ${plain}"
  221. echo -e "Panel login password has been reset to: ${green} ${config_password} ${plain}"
  222. echo -e "${yellow} Panel login secret token disabled ${plain}"
  223. echo -e "${green} Please use the new login username and password to access the X-UI panel. Also remember them! ${plain}"
  224. confirm_restart
  225. }
  226. gen_random_string() {
  227. local length="$1"
  228. local random_string=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w "$length" | head -n 1)
  229. echo "$random_string"
  230. }
  231. reset_webbasepath() {
  232. echo -e "${yellow}Resetting Web Base Path${plain}"
  233. # Prompt user to set a new web base path
  234. read -rp "Please set the new web base path [press 'y' for a random path]: " config_webBasePath
  235. if [[ $config_webBasePath == "y" ]]; then
  236. config_webBasePath=$(gen_random_string 10)
  237. fi
  238. # Apply the new web base path setting
  239. /usr/local/x-ui/x-ui setting -webBasePath "${config_webBasePath}" >/dev/null 2>&1
  240. systemctl restart x-ui
  241. # Display confirmation message
  242. echo -e "Web base path has been reset to: ${green}${config_webBasePath}${plain}"
  243. echo -e "${green}Please use the new web base path to access the panel.${plain}"
  244. }
  245. reset_config() {
  246. confirm "Are you sure you want to reset all panel settings, Account data will not be lost, Username and password will not change" "n"
  247. if [[ $? != 0 ]]; then
  248. if [[ $# == 0 ]]; then
  249. show_menu
  250. fi
  251. return 0
  252. fi
  253. /usr/local/x-ui/x-ui setting -reset
  254. 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"
  255. confirm_restart
  256. }
  257. check_config() {
  258. info=$(/usr/local/x-ui/x-ui setting -show true)
  259. if [[ $? != 0 ]]; then
  260. LOGE "get current settings error, please check logs"
  261. show_menu
  262. fi
  263. LOGI "${info}"
  264. }
  265. set_port() {
  266. echo && echo -n -e "Enter port number[1-65535]: " && read port
  267. if [[ -z "${port}" ]]; then
  268. LOGD "Cancelled"
  269. before_show_menu
  270. else
  271. /usr/local/x-ui/x-ui setting -port ${port}
  272. echo -e "The port is set, Please restart the panel now, and use the new port ${green}${port}${plain} to access web panel"
  273. confirm_restart
  274. fi
  275. }
  276. start() {
  277. check_status
  278. if [[ $? == 0 ]]; then
  279. echo ""
  280. LOGI "Panel is running, No need to start again, If you need to restart, please select restart"
  281. else
  282. systemctl start x-ui
  283. sleep 2
  284. check_status
  285. if [[ $? == 0 ]]; then
  286. LOGI "x-ui Started Successfully"
  287. else
  288. LOGE "panel Failed to start, Probably because it takes longer than two seconds to start, Please check the log information later"
  289. fi
  290. fi
  291. if [[ $# == 0 ]]; then
  292. before_show_menu
  293. fi
  294. }
  295. stop() {
  296. check_status
  297. if [[ $? == 1 ]]; then
  298. echo ""
  299. LOGI "Panel stopped, No need to stop again!"
  300. else
  301. systemctl stop x-ui
  302. sleep 2
  303. check_status
  304. if [[ $? == 1 ]]; then
  305. LOGI "x-ui and xray stopped successfully"
  306. else
  307. LOGE "Panel stop failed, Probably because the stop time exceeds two seconds, Please check the log information later"
  308. fi
  309. fi
  310. if [[ $# == 0 ]]; then
  311. before_show_menu
  312. fi
  313. }
  314. restart() {
  315. systemctl restart x-ui
  316. sleep 2
  317. check_status
  318. if [[ $? == 0 ]]; then
  319. LOGI "x-ui and xray Restarted successfully"
  320. else
  321. LOGE "Panel restart failed, Probably because it takes longer than two seconds to start, Please check the log information later"
  322. fi
  323. if [[ $# == 0 ]]; then
  324. before_show_menu
  325. fi
  326. }
  327. status() {
  328. systemctl status x-ui -l
  329. if [[ $# == 0 ]]; then
  330. before_show_menu
  331. fi
  332. }
  333. enable() {
  334. systemctl enable x-ui
  335. if [[ $? == 0 ]]; then
  336. LOGI "x-ui Set to boot automatically on startup successfully"
  337. else
  338. LOGE "x-ui Failed to set Autostart"
  339. fi
  340. if [[ $# == 0 ]]; then
  341. before_show_menu
  342. fi
  343. }
  344. disable() {
  345. systemctl disable x-ui
  346. if [[ $? == 0 ]]; then
  347. LOGI "x-ui Autostart Cancelled successfully"
  348. else
  349. LOGE "x-ui Failed to cancel autostart"
  350. fi
  351. if [[ $# == 0 ]]; then
  352. before_show_menu
  353. fi
  354. }
  355. show_log() {
  356. journalctl -u x-ui.service -e --no-pager -f
  357. if [[ $# == 0 ]]; then
  358. before_show_menu
  359. fi
  360. }
  361. show_banlog() {
  362. if test -f "${iplimit_banned_log_path}"; then
  363. if [[ -s "${iplimit_banned_log_path}" ]]; then
  364. cat ${iplimit_banned_log_path}
  365. else
  366. echo -e "${red}Log file is empty.${plain}\n"
  367. fi
  368. else
  369. echo -e "${red}Log file not found. Please Install Fail2ban and IP Limit first.${plain}\n"
  370. fi
  371. }
  372. bbr_menu() {
  373. echo -e "${green}\t1.${plain} Enable BBR"
  374. echo -e "${green}\t2.${plain} Disable BBR"
  375. echo -e "${green}\t0.${plain} Back to Main Menu"
  376. read -p "Choose an option: " choice
  377. case "$choice" in
  378. 0)
  379. show_menu
  380. ;;
  381. 1)
  382. enable_bbr
  383. ;;
  384. 2)
  385. disable_bbr
  386. ;;
  387. *) echo "Invalid choice" ;;
  388. esac
  389. }
  390. disable_bbr() {
  391. if ! grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf || ! grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  392. echo -e "${yellow}BBR is not currently enabled.${plain}"
  393. exit 0
  394. fi
  395. # Replace BBR with CUBIC configurations
  396. sed -i 's/net.core.default_qdisc=fq/net.core.default_qdisc=pfifo_fast/' /etc/sysctl.conf
  397. sed -i 's/net.ipv4.tcp_congestion_control=bbr/net.ipv4.tcp_congestion_control=cubic/' /etc/sysctl.conf
  398. # Apply changes
  399. sysctl -p
  400. # Verify that BBR is replaced with CUBIC
  401. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "cubic" ]]; then
  402. echo -e "${green}BBR has been replaced with CUBIC successfully.${plain}"
  403. else
  404. echo -e "${red}Failed to replace BBR with CUBIC. Please check your system configuration.${plain}"
  405. fi
  406. }
  407. enable_bbr() {
  408. if grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf && grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  409. echo -e "${green}BBR is already enabled!${plain}"
  410. exit 0
  411. fi
  412. # Check the OS and install necessary packages
  413. case "${release}" in
  414. ubuntu | debian | armbian)
  415. apt-get update && apt-get install -yqq --no-install-recommends ca-certificates
  416. ;;
  417. centos | almalinux | rocky | oracle)
  418. yum -y update && yum -y install ca-certificates
  419. ;;
  420. fedora)
  421. dnf -y update && dnf -y install ca-certificates
  422. ;;
  423. arch | manjaro | parch)
  424. pacman -Sy --noconfirm ca-certificates
  425. ;;
  426. *)
  427. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  428. exit 1
  429. ;;
  430. esac
  431. # Enable BBR
  432. echo "net.core.default_qdisc=fq" | tee -a /etc/sysctl.conf
  433. echo "net.ipv4.tcp_congestion_control=bbr" | tee -a /etc/sysctl.conf
  434. # Apply changes
  435. sysctl -p
  436. # Verify that BBR is enabled
  437. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "bbr" ]]; then
  438. echo -e "${green}BBR has been enabled successfully.${plain}"
  439. else
  440. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  441. fi
  442. }
  443. update_shell() {
  444. wget -O /usr/bin/x-ui -N --no-check-certificate https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  445. if [[ $? != 0 ]]; then
  446. echo ""
  447. LOGE "Failed to download script, Please check whether the machine can connect Github"
  448. before_show_menu
  449. else
  450. chmod +x /usr/bin/x-ui
  451. LOGI "Upgrade script succeeded, Please rerun the script" && exit 0
  452. fi
  453. }
  454. # 0: running, 1: not running, 2: not installed
  455. check_status() {
  456. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  457. return 2
  458. fi
  459. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  460. if [[ "${temp}" == "running" ]]; then
  461. return 0
  462. else
  463. return 1
  464. fi
  465. }
  466. check_enabled() {
  467. temp=$(systemctl is-enabled x-ui)
  468. if [[ "${temp}" == "enabled" ]]; then
  469. return 0
  470. else
  471. return 1
  472. fi
  473. }
  474. check_uninstall() {
  475. check_status
  476. if [[ $? != 2 ]]; then
  477. echo ""
  478. LOGE "Panel installed, Please do not reinstall"
  479. if [[ $# == 0 ]]; then
  480. before_show_menu
  481. fi
  482. return 1
  483. else
  484. return 0
  485. fi
  486. }
  487. check_install() {
  488. check_status
  489. if [[ $? == 2 ]]; then
  490. echo ""
  491. LOGE "Please install the panel first"
  492. if [[ $# == 0 ]]; then
  493. before_show_menu
  494. fi
  495. return 1
  496. else
  497. return 0
  498. fi
  499. }
  500. show_status() {
  501. check_status
  502. case $? in
  503. 0)
  504. echo -e "Panel state: ${green}Running${plain}"
  505. show_enable_status
  506. ;;
  507. 1)
  508. echo -e "Panel state: ${yellow}Not Running${plain}"
  509. show_enable_status
  510. ;;
  511. 2)
  512. echo -e "Panel state: ${red}Not Installed${plain}"
  513. ;;
  514. esac
  515. show_xray_status
  516. }
  517. show_enable_status() {
  518. check_enabled
  519. if [[ $? == 0 ]]; then
  520. echo -e "Start automatically: ${green}Yes${plain}"
  521. else
  522. echo -e "Start automatically: ${red}No${plain}"
  523. fi
  524. }
  525. check_xray_status() {
  526. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  527. if [[ count -ne 0 ]]; then
  528. return 0
  529. else
  530. return 1
  531. fi
  532. }
  533. show_xray_status() {
  534. check_xray_status
  535. if [[ $? == 0 ]]; then
  536. echo -e "xray state: ${green}Running${plain}"
  537. else
  538. echo -e "xray state: ${red}Not Running${plain}"
  539. fi
  540. }
  541. firewall_menu() {
  542. echo -e "${green}\t1.${plain} Install Firewall & open ports"
  543. echo -e "${green}\t2.${plain} Allowed List"
  544. echo -e "${green}\t3.${plain} Delete Ports from List"
  545. echo -e "${green}\t4.${plain} Disable Firewall"
  546. echo -e "${green}\t0.${plain} Back to Main Menu"
  547. read -p "Choose an option: " choice
  548. case "$choice" in
  549. 0)
  550. show_menu
  551. ;;
  552. 1)
  553. open_ports
  554. ;;
  555. 2)
  556. sudo ufw status
  557. ;;
  558. 3)
  559. delete_ports
  560. ;;
  561. 4)
  562. sudo ufw disable
  563. ;;
  564. *) echo "Invalid choice" ;;
  565. esac
  566. }
  567. open_ports() {
  568. if ! command -v ufw &>/dev/null; then
  569. echo "ufw firewall is not installed. Installing now..."
  570. apt-get update
  571. apt-get install -y ufw
  572. else
  573. echo "ufw firewall is already installed"
  574. fi
  575. # Check if the firewall is inactive
  576. if ufw status | grep -q "Status: active"; then
  577. echo "Firewall is already active"
  578. else
  579. echo "Activating firewall..."
  580. # Open the necessary ports
  581. ufw allow ssh
  582. ufw allow http
  583. ufw allow https
  584. ufw allow 2053/tcp
  585. # Enable the firewall
  586. ufw --force enable
  587. fi
  588. # Prompt the user to enter a list of ports
  589. read -p "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  590. # Check if the input is valid
  591. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  592. 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
  593. exit 1
  594. fi
  595. # Open the specified ports using ufw
  596. IFS=',' read -ra PORT_LIST <<<"$ports"
  597. for port in "${PORT_LIST[@]}"; do
  598. if [[ $port == *-* ]]; then
  599. # Split the range into start and end ports
  600. start_port=$(echo $port | cut -d'-' -f1)
  601. end_port=$(echo $port | cut -d'-' -f2)
  602. ufw allow $start_port:$end_port/tcp
  603. ufw allow $start_port:$end_port/udp
  604. else
  605. ufw allow "$port"
  606. fi
  607. done
  608. # Confirm that the ports are open
  609. echo "The following ports are now open:"
  610. ufw status | grep "ALLOW" | grep -Eo "[0-9]+(/[a-z]+)?"
  611. echo "Firewall status:"
  612. ufw status verbose
  613. }
  614. delete_ports() {
  615. # Prompt the user to enter the ports they want to delete
  616. read -p "Enter the ports you want to delete (e.g. 80,443,2053 or range 400-500): " ports
  617. # Check if the input is valid
  618. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  619. 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
  620. exit 1
  621. fi
  622. # Delete the specified ports using ufw
  623. IFS=',' read -ra PORT_LIST <<<"$ports"
  624. for port in "${PORT_LIST[@]}"; do
  625. if [[ $port == *-* ]]; then
  626. # Split the range into start and end ports
  627. start_port=$(echo $port | cut -d'-' -f1)
  628. end_port=$(echo $port | cut -d'-' -f2)
  629. # Delete the port range
  630. ufw delete allow $start_port:$end_port/tcp
  631. ufw delete allow $start_port:$end_port/udp
  632. else
  633. ufw delete allow "$port"
  634. fi
  635. done
  636. # Confirm that the ports are deleted
  637. echo "Deleted the specified ports:"
  638. for port in "${PORT_LIST[@]}"; do
  639. if [[ $port == *-* ]]; then
  640. start_port=$(echo $port | cut -d'-' -f1)
  641. end_port=$(echo $port | cut -d'-' -f2)
  642. # Check if the port range has been successfully deleted
  643. (ufw status | grep -q "$start_port:$end_port") || echo "$start_port-$end_port"
  644. else
  645. # Check if the individual port has been successfully deleted
  646. (ufw status | grep -q "$port") || echo "$port"
  647. fi
  648. done
  649. }
  650. update_geo() {
  651. local defaultBinFolder="/usr/local/x-ui/bin"
  652. read -p "Please enter x-ui bin folder path. Leave blank for default. (Default: '${defaultBinFolder}')" binFolder
  653. binFolder=${binFolder:-${defaultBinFolder}}
  654. if [[ ! -d ${binFolder} ]]; then
  655. LOGE "Folder ${binFolder} not exists!"
  656. LOGI "making bin folder: ${binFolder}..."
  657. mkdir -p ${binFolder}
  658. fi
  659. systemctl stop x-ui
  660. cd ${binFolder}
  661. rm -f geoip.dat geosite.dat geoip_IR.dat geosite_IR.dat geoip_VN.dat geosite_VN.dat
  662. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  663. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  664. wget -O geoip_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat
  665. wget -O geosite_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat
  666. wget -O geoip_VN.dat https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geoip.dat
  667. wget -O geosite_VN.dat https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geosite.dat
  668. systemctl start x-ui
  669. echo -e "${green}Geosite.dat + Geoip.dat + geoip_IR.dat + geosite_IR.dat have been updated successfully in bin folder '${binfolder}'!${plain}"
  670. before_show_menu
  671. }
  672. install_acme() {
  673. cd ~
  674. LOGI "install acme..."
  675. curl https://get.acme.sh | sh
  676. if [ $? -ne 0 ]; then
  677. LOGE "install acme failed"
  678. return 1
  679. else
  680. LOGI "install acme succeed"
  681. fi
  682. return 0
  683. }
  684. ssl_cert_issue_main() {
  685. echo -e "${green}\t1.${plain} Get SSL"
  686. echo -e "${green}\t2.${plain} Revoke"
  687. echo -e "${green}\t3.${plain} Force Renew"
  688. echo -e "${green}\t0.${plain} Back to Main Menu"
  689. read -p "Choose an option: " choice
  690. case "$choice" in
  691. 0)
  692. show_menu
  693. ;;
  694. 1)
  695. ssl_cert_issue
  696. ;;
  697. 2)
  698. local domain=""
  699. read -p "Please enter your domain name to revoke the certificate: " domain
  700. ~/.acme.sh/acme.sh --revoke -d ${domain}
  701. LOGI "Certificate revoked"
  702. ;;
  703. 3)
  704. local domain=""
  705. read -p "Please enter your domain name to forcefully renew an SSL certificate: " domain
  706. ~/.acme.sh/acme.sh --renew -d ${domain} --force
  707. ;;
  708. *) echo "Invalid choice" ;;
  709. esac
  710. }
  711. ssl_cert_issue() {
  712. # check for acme.sh first
  713. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  714. echo "acme.sh could not be found. we will install it"
  715. install_acme
  716. if [ $? -ne 0 ]; then
  717. LOGE "install acme failed, please check logs"
  718. exit 1
  719. fi
  720. fi
  721. # install socat second
  722. case "${release}" in
  723. ubuntu | debian | armbian)
  724. apt update && apt install socat -y
  725. ;;
  726. centos | almalinux | rocky | oracle)
  727. yum -y update && yum -y install socat
  728. ;;
  729. fedora)
  730. dnf -y update && dnf -y install socat
  731. ;;
  732. arch | manjaro | parch)
  733. pacman -Sy --noconfirm socat
  734. ;;
  735. *)
  736. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  737. exit 1
  738. ;;
  739. esac
  740. if [ $? -ne 0 ]; then
  741. LOGE "install socat failed, please check logs"
  742. exit 1
  743. else
  744. LOGI "install socat succeed..."
  745. fi
  746. # get the domain here,and we need verify it
  747. local domain=""
  748. read -p "Please enter your domain name:" domain
  749. LOGD "your domain is:${domain},check it..."
  750. # here we need to judge whether there exists cert already
  751. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  752. if [ ${currentCert} == ${domain} ]; then
  753. local certInfo=$(~/.acme.sh/acme.sh --list)
  754. LOGE "system already has certs here,can not issue again,current certs details:"
  755. LOGI "$certInfo"
  756. exit 1
  757. else
  758. LOGI "your domain is ready for issuing cert now..."
  759. fi
  760. # create a directory for install cert
  761. certPath="/root/cert/${domain}"
  762. if [ ! -d "$certPath" ]; then
  763. mkdir -p "$certPath"
  764. else
  765. rm -rf "$certPath"
  766. mkdir -p "$certPath"
  767. fi
  768. # get needed port here
  769. local WebPort=80
  770. read -p "please choose which port do you use,default will be 80 port:" WebPort
  771. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  772. LOGE "your input ${WebPort} is invalid,will use default port"
  773. fi
  774. LOGI "will use port:${WebPort} to issue certs,please make sure this port is open..."
  775. # NOTE:This should be handled by user
  776. # open the port and kill the occupied progress
  777. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  778. ~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport ${WebPort}
  779. if [ $? -ne 0 ]; then
  780. LOGE "issue certs failed,please check logs"
  781. rm -rf ~/.acme.sh/${domain}
  782. exit 1
  783. else
  784. LOGE "issue certs succeed,installing certs..."
  785. fi
  786. # install cert
  787. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  788. --key-file /root/cert/${domain}/privkey.pem \
  789. --fullchain-file /root/cert/${domain}/fullchain.pem
  790. if [ $? -ne 0 ]; then
  791. LOGE "install certs failed,exit"
  792. rm -rf ~/.acme.sh/${domain}
  793. exit 1
  794. else
  795. LOGI "install certs succeed,enable auto renew..."
  796. fi
  797. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  798. if [ $? -ne 0 ]; then
  799. LOGE "auto renew failed, certs details:"
  800. ls -lah cert/*
  801. chmod 755 $certPath/*
  802. exit 1
  803. else
  804. LOGI "auto renew succeed, certs details:"
  805. ls -lah cert/*
  806. chmod 755 $certPath/*
  807. fi
  808. }
  809. ssl_cert_issue_CF() {
  810. echo -E ""
  811. LOGD "******Instructions for use******"
  812. LOGI "This Acme script requires the following data:"
  813. LOGI "1.Cloudflare Registered e-mail"
  814. LOGI "2.Cloudflare Global API Key"
  815. LOGI "3.The domain name that has been resolved dns to the current server by Cloudflare"
  816. LOGI "4.The script applies for a certificate. The default installation path is /root/cert "
  817. confirm "Confirmed?[y/n]" "y"
  818. if [ $? -eq 0 ]; then
  819. # check for acme.sh first
  820. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  821. echo "acme.sh could not be found. we will install it"
  822. install_acme
  823. if [ $? -ne 0 ]; then
  824. LOGE "install acme failed, please check logs"
  825. exit 1
  826. fi
  827. fi
  828. CF_Domain=""
  829. CF_GlobalKey=""
  830. CF_AccountEmail=""
  831. certPath=/root/cert
  832. if [ ! -d "$certPath" ]; then
  833. mkdir $certPath
  834. else
  835. rm -rf $certPath
  836. mkdir $certPath
  837. fi
  838. LOGD "Please set a domain name:"
  839. read -p "Input your domain here:" CF_Domain
  840. LOGD "Your domain name is set to:${CF_Domain}"
  841. LOGD "Please set the API key:"
  842. read -p "Input your key here:" CF_GlobalKey
  843. LOGD "Your API key is:${CF_GlobalKey}"
  844. LOGD "Please set up registered email:"
  845. read -p "Input your email here:" CF_AccountEmail
  846. LOGD "Your registered email address is:${CF_AccountEmail}"
  847. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  848. if [ $? -ne 0 ]; then
  849. LOGE "Default CA, Lets'Encrypt fail, script exiting..."
  850. exit 1
  851. fi
  852. export CF_Key="${CF_GlobalKey}"
  853. export CF_Email=${CF_AccountEmail}
  854. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  855. if [ $? -ne 0 ]; then
  856. LOGE "Certificate issuance failed, script exiting..."
  857. exit 1
  858. else
  859. LOGI "Certificate issued Successfully, Installing..."
  860. fi
  861. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
  862. --cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
  863. --fullchain-file /root/cert/fullchain.cer
  864. if [ $? -ne 0 ]; then
  865. LOGE "Certificate installation failed, script exiting..."
  866. exit 1
  867. else
  868. LOGI "Certificate installed Successfully,Turning on automatic updates..."
  869. fi
  870. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  871. if [ $? -ne 0 ]; then
  872. LOGE "Auto update setup Failed, script exiting..."
  873. ls -lah cert
  874. chmod 755 $certPath
  875. exit 1
  876. else
  877. LOGI "The certificate is installed and auto-renewal is turned on, Specific information is as follows"
  878. ls -lah cert
  879. chmod 755 $certPath
  880. fi
  881. else
  882. show_menu
  883. fi
  884. }
  885. run_speedtest() {
  886. # Check if Speedtest is already installed
  887. if ! command -v speedtest &>/dev/null; then
  888. # If not installed, install it
  889. local pkg_manager=""
  890. local speedtest_install_script=""
  891. if command -v dnf &>/dev/null; then
  892. pkg_manager="dnf"
  893. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  894. elif command -v yum &>/dev/null; then
  895. pkg_manager="yum"
  896. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  897. elif command -v apt-get &>/dev/null; then
  898. pkg_manager="apt-get"
  899. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  900. elif command -v apt &>/dev/null; then
  901. pkg_manager="apt"
  902. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  903. fi
  904. if [[ -z $pkg_manager ]]; then
  905. echo "Error: Package manager not found. You may need to install Speedtest manually."
  906. return 1
  907. else
  908. curl -s $speedtest_install_script | bash
  909. $pkg_manager install -y speedtest
  910. fi
  911. fi
  912. # Run Speedtest
  913. speedtest
  914. }
  915. create_iplimit_jails() {
  916. # Use default bantime if not passed => 15 minutes
  917. local bantime="${1:-15}"
  918. # Uncomment 'allowipv6 = auto' in fail2ban.conf
  919. sed -i 's/#allowipv6 = auto/allowipv6 = auto/g' /etc/fail2ban/fail2ban.conf
  920. #On Debian 12+ fail2ban's default backend should be changed to systemd
  921. if [[ "${release}" == "debian" && ${os_version} -ge 12 ]]; then
  922. sed -i '0,/action =/s/backend = auto/backend = systemd/' /etc/fail2ban/jail.conf
  923. fi
  924. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  925. [3x-ipl]
  926. enabled=true
  927. backend=auto
  928. filter=3x-ipl
  929. action=3x-ipl
  930. logpath=${iplimit_log_path}
  931. maxretry=2
  932. findtime=32
  933. bantime=${bantime}m
  934. EOF
  935. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  936. [Definition]
  937. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  938. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  939. ignoreregex =
  940. EOF
  941. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  942. [INCLUDES]
  943. before = iptables-allports.conf
  944. [Definition]
  945. actionstart = <iptables> -N f2b-<name>
  946. <iptables> -A f2b-<name> -j <returntype>
  947. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  948. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  949. <actionflush>
  950. <iptables> -X f2b-<name>
  951. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  952. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  953. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  954. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  955. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  956. [Init]
  957. EOF
  958. echo -e "${green}Ip Limit jail files created with a bantime of ${bantime} minutes.${plain}"
  959. }
  960. iplimit_remove_conflicts() {
  961. local jail_files=(
  962. /etc/fail2ban/jail.conf
  963. /etc/fail2ban/jail.local
  964. )
  965. for file in "${jail_files[@]}"; do
  966. # Check for [3x-ipl] config in jail file then remove it
  967. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  968. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  969. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  970. fi
  971. done
  972. }
  973. iplimit_main() {
  974. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  975. echo -e "${green}\t2.${plain} Change Ban Duration"
  976. echo -e "${green}\t3.${plain} Unban Everyone"
  977. echo -e "${green}\t4.${plain} Check Logs"
  978. echo -e "${green}\t5.${plain} Fail2ban Status"
  979. echo -e "${green}\t6.${plain} Restart Fail2ban"
  980. echo -e "${green}\t7.${plain} Uninstall Fail2ban"
  981. echo -e "${green}\t0.${plain} Back to Main Menu"
  982. read -p "Choose an option: " choice
  983. case "$choice" in
  984. 0)
  985. show_menu
  986. ;;
  987. 1)
  988. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  989. if [[ $? == 0 ]]; then
  990. install_iplimit
  991. else
  992. iplimit_main
  993. fi
  994. ;;
  995. 2)
  996. read -rp "Please enter new Ban Duration in Minutes [default 30]: " NUM
  997. if [[ $NUM =~ ^[0-9]+$ ]]; then
  998. create_iplimit_jails ${NUM}
  999. systemctl restart fail2ban
  1000. else
  1001. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  1002. fi
  1003. iplimit_main
  1004. ;;
  1005. 3)
  1006. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  1007. if [[ $? == 0 ]]; then
  1008. fail2ban-client reload --restart --unban 3x-ipl
  1009. truncate -s 0 "${iplimit_banned_log_path}"
  1010. echo -e "${green}All users Unbanned successfully.${plain}"
  1011. iplimit_main
  1012. else
  1013. echo -e "${yellow}Cancelled.${plain}"
  1014. fi
  1015. iplimit_main
  1016. ;;
  1017. 4)
  1018. show_banlog
  1019. ;;
  1020. 5)
  1021. service fail2ban status
  1022. ;;
  1023. 6)
  1024. systemctl restart fail2ban
  1025. ;;
  1026. 7)
  1027. remove_iplimit
  1028. ;;
  1029. *) echo "Invalid choice" ;;
  1030. esac
  1031. }
  1032. install_iplimit() {
  1033. if ! command -v fail2ban-client &>/dev/null; then
  1034. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  1035. # Check the OS and install necessary packages
  1036. case "${release}" in
  1037. ubuntu)
  1038. if [[ "${os_version}" -ge 24 ]]; then
  1039. apt update && apt install python3-pip -y
  1040. python3 -m pip install pyasynchat --break-system-packages
  1041. fi
  1042. apt update && apt install fail2ban -y
  1043. ;;
  1044. debian | armbian)
  1045. apt update && apt install fail2ban -y
  1046. ;;
  1047. centos | almalinux | rocky | oracle)
  1048. yum update -y && yum install epel-release -y
  1049. yum -y install fail2ban
  1050. ;;
  1051. fedora)
  1052. dnf -y update && dnf -y install fail2ban
  1053. ;;
  1054. arch | manjaro | parch)
  1055. pacman -Syu --noconfirm fail2ban
  1056. ;;
  1057. *)
  1058. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  1059. exit 1
  1060. ;;
  1061. esac
  1062. if ! command -v fail2ban-client &>/dev/null; then
  1063. echo -e "${red}Fail2ban installation failed.${plain}\n"
  1064. exit 1
  1065. fi
  1066. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  1067. else
  1068. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  1069. fi
  1070. echo -e "${green}Configuring IP Limit...${plain}\n"
  1071. # make sure there's no conflict for jail files
  1072. iplimit_remove_conflicts
  1073. # Check if log file exists
  1074. if ! test -f "${iplimit_banned_log_path}"; then
  1075. touch ${iplimit_banned_log_path}
  1076. fi
  1077. # Check if service log file exists so fail2ban won't return error
  1078. if ! test -f "${iplimit_log_path}"; then
  1079. touch ${iplimit_log_path}
  1080. fi
  1081. # Create the iplimit jail files
  1082. # we didn't pass the bantime here to use the default value
  1083. create_iplimit_jails
  1084. # Launching fail2ban
  1085. if ! systemctl is-active --quiet fail2ban; then
  1086. systemctl start fail2ban
  1087. systemctl enable fail2ban
  1088. else
  1089. systemctl restart fail2ban
  1090. fi
  1091. systemctl enable fail2ban
  1092. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  1093. before_show_menu
  1094. }
  1095. remove_iplimit() {
  1096. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  1097. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  1098. echo -e "${green}\t0.${plain} Abort"
  1099. read -p "Choose an option: " num
  1100. case "$num" in
  1101. 1)
  1102. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  1103. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  1104. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  1105. systemctl restart fail2ban
  1106. echo -e "${green}IP Limit removed successfully!${plain}\n"
  1107. before_show_menu
  1108. ;;
  1109. 2)
  1110. rm -rf /etc/fail2ban
  1111. systemctl stop fail2ban
  1112. case "${release}" in
  1113. ubuntu | debian | armbian)
  1114. apt-get remove -y fail2ban
  1115. apt-get purge -y fail2ban -y
  1116. apt-get autoremove -y
  1117. ;;
  1118. centos | almalinux | rocky | oracle)
  1119. yum remove fail2ban -y
  1120. yum autoremove -y
  1121. ;;
  1122. fedora)
  1123. dnf remove fail2ban -y
  1124. dnf autoremove -y
  1125. ;;
  1126. arch | manjaro | parch)
  1127. pacman -Rns --noconfirm fail2ban
  1128. ;;
  1129. *)
  1130. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  1131. exit 1
  1132. ;;
  1133. esac
  1134. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  1135. before_show_menu
  1136. ;;
  1137. 0)
  1138. echo -e "${yellow}Cancelled.${plain}\n"
  1139. iplimit_main
  1140. ;;
  1141. *)
  1142. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1143. remove_iplimit
  1144. ;;
  1145. esac
  1146. }
  1147. show_usage() {
  1148. echo "x-ui control menu usages: "
  1149. echo "------------------------------------------"
  1150. echo -e "SUBCOMMANDS:"
  1151. echo -e "x-ui - Admin Management Script"
  1152. echo -e "x-ui start - Start"
  1153. echo -e "x-ui stop - Stop"
  1154. echo -e "x-ui restart - Restart"
  1155. echo -e "x-ui status - Current Status"
  1156. echo -e "x-ui settings - Current Settings"
  1157. echo -e "x-ui enable - Enable Autostart on OS Startup"
  1158. echo -e "x-ui disable - Disable Autostart on OS Startup"
  1159. echo -e "x-ui log - Check logs"
  1160. echo -e "x-ui banlog - Check Fail2ban ban logs"
  1161. echo -e "x-ui update - Update"
  1162. echo -e "x-ui custom - custom version"
  1163. echo -e "x-ui install - Install"
  1164. echo -e "x-ui uninstall - Uninstall"
  1165. echo "------------------------------------------"
  1166. }
  1167. show_menu() {
  1168. echo -e "
  1169. ${green}3X-UI Panel Management Script${plain}
  1170. ${green}0.${plain} Exit Script
  1171. ————————————————
  1172. ${green}1.${plain} Install
  1173. ${green}2.${plain} Update
  1174. ${green}3.${plain} Update Menu
  1175. ${green}4.${plain} Custom Version
  1176. ${green}5.${plain} Uninstall
  1177. ————————————————
  1178. ${green}6.${plain} Reset Username & Password & Secret Token
  1179. ${green}7.${plain} Reset Web Base Path
  1180. ${green}8.${plain} Reset Settings
  1181. ${green}9.${plain} Change Port
  1182. ${green}10.${plain} View Current Settings
  1183. ————————————————
  1184. ${green}11.${plain} Start
  1185. ${green}12.${plain} Stop
  1186. ${green}13.${plain} Restart
  1187. ${green}14.${plain} Check Status
  1188. ${green}15.${plain} Check Logs
  1189. ————————————————
  1190. ${green}16.${plain} Enable Autostart
  1191. ${green}17.${plain} Disable Autostart
  1192. ————————————————
  1193. ${green}18.${plain} SSL Certificate Management
  1194. ${green}19.${plain} Cloudflare SSL Certificate
  1195. ${green}20.${plain} IP Limit Management
  1196. ${green}21.${plain} Firewall Management
  1197. ————————————————
  1198. ${green}22.${plain} Enable BBR
  1199. ${green}23.${plain} Update Geo Files
  1200. ${green}24.${plain} Speedtest by Ookla
  1201. "
  1202. show_status
  1203. echo && read -p "Please enter your selection [0-24]: " num
  1204. case "${num}" in
  1205. 0)
  1206. exit 0
  1207. ;;
  1208. 1)
  1209. check_uninstall && install
  1210. ;;
  1211. 2)
  1212. check_install && update
  1213. ;;
  1214. 3)
  1215. check_install && update_menu
  1216. ;;
  1217. 4)
  1218. check_install && custom_version
  1219. ;;
  1220. 5)
  1221. check_install && uninstall
  1222. ;;
  1223. 6)
  1224. check_install && reset_user
  1225. ;;
  1226. 7)
  1227. check_install && reset_webbasepath
  1228. ;;
  1229. 8)
  1230. check_install && reset_config
  1231. ;;
  1232. 9)
  1233. check_install && set_port
  1234. ;;
  1235. 10)
  1236. check_install && check_config
  1237. ;;
  1238. 11)
  1239. check_install && start
  1240. ;;
  1241. 12)
  1242. check_install && stop
  1243. ;;
  1244. 13)
  1245. check_install && restart
  1246. ;;
  1247. 14)
  1248. check_install && status
  1249. ;;
  1250. 15)
  1251. check_install && show_log
  1252. ;;
  1253. 16)
  1254. check_install && enable
  1255. ;;
  1256. 17)
  1257. check_install && disable
  1258. ;;
  1259. 18)
  1260. ssl_cert_issue_main
  1261. ;;
  1262. 19)
  1263. ssl_cert_issue_CF
  1264. ;;
  1265. 20)
  1266. iplimit_main
  1267. ;;
  1268. 21)
  1269. firewall_menu
  1270. ;;
  1271. 22)
  1272. bbr_menu
  1273. ;;
  1274. 23)
  1275. update_geo
  1276. ;;
  1277. 24)
  1278. run_speedtest
  1279. ;;
  1280. *)
  1281. LOGE "Please enter the correct number [0-24]"
  1282. ;;
  1283. esac
  1284. }
  1285. if [[ $# > 0 ]]; then
  1286. case $1 in
  1287. "start")
  1288. check_install 0 && start 0
  1289. ;;
  1290. "stop")
  1291. check_install 0 && stop 0
  1292. ;;
  1293. "restart")
  1294. check_install 0 && restart 0
  1295. ;;
  1296. "status")
  1297. check_install 0 && status 0
  1298. ;;
  1299. "settings")
  1300. check_install 0 && check_config 0
  1301. ;;
  1302. "enable")
  1303. check_install 0 && enable 0
  1304. ;;
  1305. "disable")
  1306. check_install 0 && disable 0
  1307. ;;
  1308. "log")
  1309. check_install 0 && show_log 0
  1310. ;;
  1311. "banlog")
  1312. check_install 0 && show_banlog 0
  1313. ;;
  1314. "update")
  1315. check_install 0 && update 0
  1316. ;;
  1317. "custom")
  1318. check_install 0 && custom_version 0
  1319. ;;
  1320. "install")
  1321. check_uninstall 0 && install 0
  1322. ;;
  1323. "uninstall")
  1324. check_install 0 && uninstall 0
  1325. ;;
  1326. *) show_usage ;;
  1327. esac
  1328. else
  1329. show_menu
  1330. fi