x-ui.sh 47 KB

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