x-ui.sh 43 KB

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