1
0

x-ui.sh 43 KB

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