x-ui.sh 43 KB

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