x-ui.sh 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  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. echo -e "${green}\t1.${plain} Loyalsoldier (geoip.dat, geosite.dat)"
  668. echo -e "${green}\t2.${plain} chocolate4u (geoip_IR.dat, geosite_IR.dat)"
  669. echo -e "${green}\t3.${plain} vuong2023 (geoip_VN.dat, geosite_VN.dat)"
  670. echo -e "${green}\t0.${plain} Back to Main Menu"
  671. read -p "Choose an option: " choice
  672. systemctl stop x-ui
  673. cd /usr/local/x-ui/bin
  674. case "$choice" in
  675. 0)
  676. show_menu
  677. ;;
  678. 1)
  679. rm -f geoip.dat geosite.dat
  680. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  681. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  682. echo -e "${green}Loyalsoldier datasets have been updated successfully!${plain}"
  683. ;;
  684. 2)
  685. rm -f geoip_IR.dat geosite_IR.dat
  686. wget -O geoip_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat
  687. wget -O geosite_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat
  688. echo -e "${green}chocolate4u datasets have been updated successfully!${plain}"
  689. ;;
  690. 3)
  691. rm -f geoip_VN.dat geosite_VN.dat
  692. wget -O geoip_VN.dat -N https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geoip.dat
  693. wget -O geosite_VN.dat -N https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geosite.dat
  694. echo -e "${green}vuong2023 datasets have been updated successfully!${plain}"
  695. ;;
  696. *)
  697. echo "Invalid option selected! No updates made."
  698. ;;
  699. esac
  700. systemctl start x-ui
  701. before_show_menu
  702. }
  703. install_acme() {
  704. # Check if acme.sh is already installed
  705. if command -v ~/.acme.sh/acme.sh &>/dev/null; then
  706. LOGI "acme.sh is already installed."
  707. return 0
  708. fi
  709. LOGI "Installing acme.sh..."
  710. cd ~ || return 1 # Ensure you can change to the home directory
  711. curl -s https://get.acme.sh | sh
  712. if [ $? -ne 0 ]; then
  713. LOGE "Installation of acme.sh failed."
  714. return 1
  715. else
  716. LOGI "Installation of acme.sh succeeded."
  717. fi
  718. return 0
  719. }
  720. ssl_cert_issue_main() {
  721. echo -e "${green}\t1.${plain} Get SSL"
  722. echo -e "${green}\t2.${plain} Revoke"
  723. echo -e "${green}\t3.${plain} Force Renew"
  724. echo -e "${green}\t4.${plain} Show Existing Domains"
  725. echo -e "${green}\t5.${plain} Set Cert paths for the panel"
  726. echo -e "${green}\t0.${plain} Back to Main Menu"
  727. read -p "Choose an option: " choice
  728. case "$choice" in
  729. 0)
  730. show_menu
  731. ;;
  732. 1)
  733. ssl_cert_issue
  734. ;;
  735. 2)
  736. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  737. if [ -z "$domains" ]; then
  738. echo "No certificates found to revoke."
  739. else
  740. echo "Existing domains:"
  741. echo "$domains"
  742. read -p "Please enter a domain from the list to revoke the certificate: " domain
  743. if echo "$domains" | grep -qw "$domain"; then
  744. ~/.acme.sh/acme.sh --revoke -d ${domain}
  745. LOGI "Certificate revoked for domain: $domain"
  746. else
  747. echo "Invalid domain entered."
  748. fi
  749. fi
  750. ;;
  751. 3)
  752. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  753. if [ -z "$domains" ]; then
  754. echo "No certificates found to renew."
  755. else
  756. echo "Existing domains:"
  757. echo "$domains"
  758. read -p "Please enter a domain from the list to renew the SSL certificate: " domain
  759. if echo "$domains" | grep -qw "$domain"; then
  760. ~/.acme.sh/acme.sh --renew -d ${domain} --force
  761. LOGI "Certificate forcefully renewed for domain: $domain"
  762. else
  763. echo "Invalid domain entered."
  764. fi
  765. fi
  766. ;;
  767. 4)
  768. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  769. if [ -z "$domains" ]; then
  770. echo "No certificates found."
  771. else
  772. echo "Existing domains and their paths:"
  773. for domain in $domains; do
  774. local cert_path="/root/cert/${domain}/fullchain.pem"
  775. local key_path="/root/cert/${domain}/privkey.pem"
  776. if [[ -f "${cert_path}" && -f "${key_path}" ]]; then
  777. echo -e "Domain: ${domain}"
  778. echo -e "\tCertificate Path: ${cert_path}"
  779. echo -e "\tPrivate Key Path: ${key_path}"
  780. else
  781. echo -e "Domain: ${domain} - Certificate or Key missing."
  782. fi
  783. done
  784. fi
  785. ;;
  786. 5)
  787. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  788. if [ -z "$domains" ]; then
  789. echo "No certificates found."
  790. else
  791. echo "Available domains:"
  792. echo "$domains"
  793. read -p "Please choose a domain to set the panel paths: " domain
  794. if echo "$domains" | grep -qw "$domain"; then
  795. local webCertFile="/root/cert/${domain}/fullchain.pem"
  796. local webKeyFile="/root/cert/${domain}/privkey.pem"
  797. if [[ -f "${webCertFile}" && -f "${webKeyFile}" ]]; then
  798. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  799. echo "Panel paths set for domain: $domain"
  800. echo " - Certificate File: $webCertFile"
  801. echo " - Private Key File: $webKeyFile"
  802. else
  803. echo "Certificate or private key not found for domain: $domain."
  804. fi
  805. else
  806. echo "Invalid domain entered."
  807. fi
  808. fi
  809. ;;
  810. *)
  811. echo "Invalid choice"
  812. ;;
  813. esac
  814. }
  815. ssl_cert_issue() {
  816. # check for acme.sh first
  817. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  818. echo "acme.sh could not be found. we will install it"
  819. install_acme
  820. if [ $? -ne 0 ]; then
  821. LOGE "install acme failed, please check logs"
  822. exit 1
  823. fi
  824. fi
  825. # install socat second
  826. case "${release}" in
  827. ubuntu | debian | armbian)
  828. apt update && apt install socat -y
  829. ;;
  830. centos | almalinux | rocky | oracle)
  831. yum -y update && yum -y install socat
  832. ;;
  833. fedora | amzn)
  834. dnf -y update && dnf -y install socat
  835. ;;
  836. arch | manjaro | parch)
  837. pacman -Sy --noconfirm socat
  838. ;;
  839. *)
  840. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  841. exit 1
  842. ;;
  843. esac
  844. if [ $? -ne 0 ]; then
  845. LOGE "install socat failed, please check logs"
  846. exit 1
  847. else
  848. LOGI "install socat succeed..."
  849. fi
  850. # get the domain here, and we need to verify it
  851. local domain=""
  852. read -p "Please enter your domain name: " domain
  853. LOGD "Your domain is: ${domain}, checking it..."
  854. # check if there already exists a certificate
  855. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  856. if [ "${currentCert}" == "${domain}" ]; then
  857. local certInfo=$(~/.acme.sh/acme.sh --list)
  858. LOGE "System already has certificates for this domain. Cannot issue again. Current certificate details:"
  859. LOGI "$certInfo"
  860. exit 1
  861. else
  862. LOGI "Your domain is ready for issuing certificates now..."
  863. fi
  864. # create a directory for the certificate
  865. certPath="/root/cert/${domain}"
  866. if [ ! -d "$certPath" ]; then
  867. mkdir -p "$certPath"
  868. else
  869. rm -rf "$certPath"
  870. mkdir -p "$certPath"
  871. fi
  872. # get the port number for the standalone server
  873. local WebPort=80
  874. read -p "Please choose which port to use (default is 80): " WebPort
  875. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  876. LOGE "Your input ${WebPort} is invalid, will use default port 80."
  877. WebPort=80
  878. fi
  879. LOGI "Will use port: ${WebPort} to issue certificates. Please make sure this port is open."
  880. # issue the certificate
  881. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  882. ~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport ${WebPort}
  883. if [ $? -ne 0 ]; then
  884. LOGE "Issuing certificate failed, please check logs."
  885. rm -rf ~/.acme.sh/${domain}
  886. exit 1
  887. else
  888. LOGE "Issuing certificate succeeded, installing certificates..."
  889. fi
  890. # install the certificate
  891. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  892. --key-file /root/cert/${domain}/privkey.pem \
  893. --fullchain-file /root/cert/${domain}/fullchain.pem
  894. if [ $? -ne 0 ]; then
  895. LOGE "Installing certificate failed, exiting."
  896. rm -rf ~/.acme.sh/${domain}
  897. exit 1
  898. else
  899. LOGI "Installing certificate succeeded, enabling auto renew..."
  900. fi
  901. # enable auto-renew
  902. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  903. if [ $? -ne 0 ]; then
  904. LOGE "Auto renew failed, certificate details:"
  905. ls -lah cert/*
  906. chmod 755 $certPath/*
  907. exit 1
  908. else
  909. LOGI "Auto renew succeeded, certificate details:"
  910. ls -lah cert/*
  911. chmod 755 $certPath/*
  912. fi
  913. # Prompt user to set panel paths after successful certificate installation
  914. read -p "Would you like to set this certificate for the panel? (y/n): " setPanel
  915. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  916. local webCertFile="/root/cert/${domain}/fullchain.pem"
  917. local webKeyFile="/root/cert/${domain}/privkey.pem"
  918. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  919. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  920. LOGI "Panel paths set for domain: $domain"
  921. LOGI " - Certificate File: $webCertFile"
  922. LOGI " - Private Key File: $webKeyFile"
  923. else
  924. LOGE "Error: Certificate or private key file not found for domain: $domain."
  925. fi
  926. else
  927. LOGI "Skipping panel path setting."
  928. fi
  929. }
  930. ssl_cert_issue_CF() {
  931. echo -E ""
  932. LOGD "******Instructions for use******"
  933. LOGI "This Acme script requires the following data:"
  934. LOGI "1.Cloudflare Registered e-mail"
  935. LOGI "2.Cloudflare Global API Key"
  936. LOGI "3.The domain name that has been resolved dns to the current server by Cloudflare"
  937. LOGI "4.The script applies for a certificate. The default installation path is /root/cert "
  938. confirm "Confirmed?[y/n]" "y"
  939. if [ $? -eq 0 ]; then
  940. # check for acme.sh first
  941. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  942. echo "acme.sh could not be found. we will install it"
  943. install_acme
  944. if [ $? -ne 0 ]; then
  945. LOGE "install acme failed, please check logs"
  946. exit 1
  947. fi
  948. fi
  949. CF_Domain=""
  950. CF_GlobalKey=""
  951. CF_AccountEmail=""
  952. certPath=/root/cert
  953. if [ ! -d "$certPath" ]; then
  954. mkdir $certPath
  955. else
  956. rm -rf $certPath
  957. mkdir $certPath
  958. fi
  959. LOGD "Please set a domain name:"
  960. read -p "Input your domain here:" CF_Domain
  961. LOGD "Your domain name is set to:${CF_Domain}"
  962. LOGD "Please set the API key:"
  963. read -p "Input your key here:" CF_GlobalKey
  964. LOGD "Your API key is:${CF_GlobalKey}"
  965. LOGD "Please set up registered email:"
  966. read -p "Input your email here:" CF_AccountEmail
  967. LOGD "Your registered email address is:${CF_AccountEmail}"
  968. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  969. if [ $? -ne 0 ]; then
  970. LOGE "Default CA, Lets'Encrypt fail, script exiting..."
  971. exit 1
  972. fi
  973. export CF_Key="${CF_GlobalKey}"
  974. export CF_Email=${CF_AccountEmail}
  975. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  976. if [ $? -ne 0 ]; then
  977. LOGE "Certificate issuance failed, script exiting..."
  978. exit 1
  979. else
  980. LOGI "Certificate issued Successfully, Installing..."
  981. fi
  982. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
  983. --cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
  984. --fullchain-file /root/cert/fullchain.cer
  985. if [ $? -ne 0 ]; then
  986. LOGE "Certificate installation failed, script exiting..."
  987. exit 1
  988. else
  989. LOGI "Certificate installed Successfully,Turning on automatic updates..."
  990. fi
  991. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  992. if [ $? -ne 0 ]; then
  993. LOGE "Auto update setup Failed, script exiting..."
  994. ls -lah cert
  995. chmod 755 $certPath
  996. exit 1
  997. else
  998. LOGI "The certificate is installed and auto-renewal is turned on, Specific information is as follows"
  999. ls -lah cert
  1000. chmod 755 $certPath
  1001. fi
  1002. else
  1003. show_menu
  1004. fi
  1005. }
  1006. run_speedtest() {
  1007. # Check if Speedtest is already installed
  1008. if ! command -v speedtest &>/dev/null; then
  1009. # If not installed, install it
  1010. local pkg_manager=""
  1011. local speedtest_install_script=""
  1012. if command -v dnf &>/dev/null; then
  1013. pkg_manager="dnf"
  1014. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1015. elif command -v yum &>/dev/null; then
  1016. pkg_manager="yum"
  1017. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1018. elif command -v apt-get &>/dev/null; then
  1019. pkg_manager="apt-get"
  1020. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1021. elif command -v apt &>/dev/null; then
  1022. pkg_manager="apt"
  1023. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1024. fi
  1025. if [[ -z $pkg_manager ]]; then
  1026. echo "Error: Package manager not found. You may need to install Speedtest manually."
  1027. return 1
  1028. else
  1029. curl -s $speedtest_install_script | bash
  1030. $pkg_manager install -y speedtest
  1031. fi
  1032. fi
  1033. # Run Speedtest
  1034. speedtest
  1035. }
  1036. create_iplimit_jails() {
  1037. # Use default bantime if not passed => 15 minutes
  1038. local bantime="${1:-15}"
  1039. # Uncomment 'allowipv6 = auto' in fail2ban.conf
  1040. sed -i 's/#allowipv6 = auto/allowipv6 = auto/g' /etc/fail2ban/fail2ban.conf
  1041. #On Debian 12+ fail2ban's default backend should be changed to systemd
  1042. if [[ "${release}" == "debian" && ${os_version} -ge 12 ]]; then
  1043. sed -i '0,/action =/s/backend = auto/backend = systemd/' /etc/fail2ban/jail.conf
  1044. fi
  1045. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  1046. [3x-ipl]
  1047. enabled=true
  1048. backend=auto
  1049. filter=3x-ipl
  1050. action=3x-ipl
  1051. logpath=${iplimit_log_path}
  1052. maxretry=2
  1053. findtime=32
  1054. bantime=${bantime}m
  1055. EOF
  1056. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  1057. [Definition]
  1058. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  1059. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  1060. ignoreregex =
  1061. EOF
  1062. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  1063. [INCLUDES]
  1064. before = iptables-allports.conf
  1065. [Definition]
  1066. actionstart = <iptables> -N f2b-<name>
  1067. <iptables> -A f2b-<name> -j <returntype>
  1068. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  1069. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  1070. <actionflush>
  1071. <iptables> -X f2b-<name>
  1072. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  1073. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  1074. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  1075. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  1076. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  1077. [Init]
  1078. EOF
  1079. echo -e "${green}Ip Limit jail files created with a bantime of ${bantime} minutes.${plain}"
  1080. }
  1081. iplimit_remove_conflicts() {
  1082. local jail_files=(
  1083. /etc/fail2ban/jail.conf
  1084. /etc/fail2ban/jail.local
  1085. )
  1086. for file in "${jail_files[@]}"; do
  1087. # Check for [3x-ipl] config in jail file then remove it
  1088. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  1089. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  1090. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  1091. fi
  1092. done
  1093. }
  1094. iplimit_main() {
  1095. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  1096. echo -e "${green}\t2.${plain} Change Ban Duration"
  1097. echo -e "${green}\t3.${plain} Unban Everyone"
  1098. echo -e "${green}\t4.${plain} Check Logs"
  1099. echo -e "${green}\t5.${plain} Fail2ban Status"
  1100. echo -e "${green}\t6.${plain} Restart Fail2ban"
  1101. echo -e "${green}\t7.${plain} Uninstall Fail2ban"
  1102. echo -e "${green}\t0.${plain} Back to Main Menu"
  1103. read -p "Choose an option: " choice
  1104. case "$choice" in
  1105. 0)
  1106. show_menu
  1107. ;;
  1108. 1)
  1109. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  1110. if [[ $? == 0 ]]; then
  1111. install_iplimit
  1112. else
  1113. iplimit_main
  1114. fi
  1115. ;;
  1116. 2)
  1117. read -rp "Please enter new Ban Duration in Minutes [default 30]: " NUM
  1118. if [[ $NUM =~ ^[0-9]+$ ]]; then
  1119. create_iplimit_jails ${NUM}
  1120. systemctl restart fail2ban
  1121. else
  1122. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  1123. fi
  1124. iplimit_main
  1125. ;;
  1126. 3)
  1127. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  1128. if [[ $? == 0 ]]; then
  1129. fail2ban-client reload --restart --unban 3x-ipl
  1130. truncate -s 0 "${iplimit_banned_log_path}"
  1131. echo -e "${green}All users Unbanned successfully.${plain}"
  1132. iplimit_main
  1133. else
  1134. echo -e "${yellow}Cancelled.${plain}"
  1135. fi
  1136. iplimit_main
  1137. ;;
  1138. 4)
  1139. show_banlog
  1140. ;;
  1141. 5)
  1142. service fail2ban status
  1143. ;;
  1144. 6)
  1145. systemctl restart fail2ban
  1146. ;;
  1147. 7)
  1148. remove_iplimit
  1149. ;;
  1150. *) echo "Invalid choice" ;;
  1151. esac
  1152. }
  1153. install_iplimit() {
  1154. if ! command -v fail2ban-client &>/dev/null; then
  1155. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  1156. # Check the OS and install necessary packages
  1157. case "${release}" in
  1158. ubuntu)
  1159. if [[ "${os_version}" -ge 24 ]]; then
  1160. apt update && apt install python3-pip -y
  1161. python3 -m pip install pyasynchat --break-system-packages
  1162. fi
  1163. apt update && apt install fail2ban -y
  1164. ;;
  1165. debian | armbian)
  1166. apt update && apt install fail2ban -y
  1167. ;;
  1168. centos | almalinux | rocky | oracle)
  1169. yum update -y && yum install epel-release -y
  1170. yum -y install fail2ban
  1171. ;;
  1172. fedora | amzn)
  1173. dnf -y update && dnf -y install fail2ban
  1174. ;;
  1175. arch | manjaro | parch)
  1176. pacman -Syu --noconfirm fail2ban
  1177. ;;
  1178. *)
  1179. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  1180. exit 1
  1181. ;;
  1182. esac
  1183. if ! command -v fail2ban-client &>/dev/null; then
  1184. echo -e "${red}Fail2ban installation failed.${plain}\n"
  1185. exit 1
  1186. fi
  1187. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  1188. else
  1189. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  1190. fi
  1191. echo -e "${green}Configuring IP Limit...${plain}\n"
  1192. # make sure there's no conflict for jail files
  1193. iplimit_remove_conflicts
  1194. # Check if log file exists
  1195. if ! test -f "${iplimit_banned_log_path}"; then
  1196. touch ${iplimit_banned_log_path}
  1197. fi
  1198. # Check if service log file exists so fail2ban won't return error
  1199. if ! test -f "${iplimit_log_path}"; then
  1200. touch ${iplimit_log_path}
  1201. fi
  1202. # Create the iplimit jail files
  1203. # we didn't pass the bantime here to use the default value
  1204. create_iplimit_jails
  1205. # Launching fail2ban
  1206. if ! systemctl is-active --quiet fail2ban; then
  1207. systemctl start fail2ban
  1208. systemctl enable fail2ban
  1209. else
  1210. systemctl restart fail2ban
  1211. fi
  1212. systemctl enable fail2ban
  1213. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  1214. before_show_menu
  1215. }
  1216. remove_iplimit() {
  1217. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  1218. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  1219. echo -e "${green}\t0.${plain} Abort"
  1220. read -p "Choose an option: " num
  1221. case "$num" in
  1222. 1)
  1223. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  1224. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  1225. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  1226. systemctl restart fail2ban
  1227. echo -e "${green}IP Limit removed successfully!${plain}\n"
  1228. before_show_menu
  1229. ;;
  1230. 2)
  1231. rm -rf /etc/fail2ban
  1232. systemctl stop fail2ban
  1233. case "${release}" in
  1234. ubuntu | debian | armbian)
  1235. apt-get remove -y fail2ban
  1236. apt-get purge -y fail2ban -y
  1237. apt-get autoremove -y
  1238. ;;
  1239. centos | almalinux | rocky | oracle)
  1240. yum remove fail2ban -y
  1241. yum autoremove -y
  1242. ;;
  1243. fedora | amzn)
  1244. dnf remove fail2ban -y
  1245. dnf autoremove -y
  1246. ;;
  1247. arch | manjaro | parch)
  1248. pacman -Rns --noconfirm fail2ban
  1249. ;;
  1250. *)
  1251. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  1252. exit 1
  1253. ;;
  1254. esac
  1255. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  1256. before_show_menu
  1257. ;;
  1258. 0)
  1259. echo -e "${yellow}Cancelled.${plain}\n"
  1260. iplimit_main
  1261. ;;
  1262. *)
  1263. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1264. remove_iplimit
  1265. ;;
  1266. esac
  1267. }
  1268. show_usage() {
  1269. echo "x-ui control menu usages: "
  1270. echo "------------------------------------------"
  1271. echo -e "SUBCOMMANDS:"
  1272. echo -e "x-ui - Admin Management Script"
  1273. echo -e "x-ui start - Start"
  1274. echo -e "x-ui stop - Stop"
  1275. echo -e "x-ui restart - Restart"
  1276. echo -e "x-ui status - Current Status"
  1277. echo -e "x-ui settings - Current Settings"
  1278. echo -e "x-ui enable - Enable Autostart on OS Startup"
  1279. echo -e "x-ui disable - Disable Autostart on OS Startup"
  1280. echo -e "x-ui log - Check logs"
  1281. echo -e "x-ui banlog - Check Fail2ban ban logs"
  1282. echo -e "x-ui update - Update"
  1283. echo -e "x-ui custom - custom version"
  1284. echo -e "x-ui install - Install"
  1285. echo -e "x-ui uninstall - Uninstall"
  1286. echo "------------------------------------------"
  1287. }
  1288. show_menu() {
  1289. echo -e "
  1290. ${green}3X-UI Panel Management Script${plain}
  1291. ${green}0.${plain} Exit Script
  1292. ————————————————
  1293. ${green}1.${plain} Install
  1294. ${green}2.${plain} Update
  1295. ${green}3.${plain} Update Menu
  1296. ${green}4.${plain} Custom Version
  1297. ${green}5.${plain} Uninstall
  1298. ————————————————
  1299. ${green}6.${plain} Reset Username & Password & Secret Token
  1300. ${green}7.${plain} Reset Web Base Path
  1301. ${green}8.${plain} Reset Settings
  1302. ${green}9.${plain} Change Port
  1303. ${green}10.${plain} View Current Settings
  1304. ————————————————
  1305. ${green}11.${plain} Start
  1306. ${green}12.${plain} Stop
  1307. ${green}13.${plain} Restart
  1308. ${green}14.${plain} Check Status
  1309. ${green}15.${plain} Check Logs
  1310. ————————————————
  1311. ${green}16.${plain} Enable Autostart
  1312. ${green}17.${plain} Disable Autostart
  1313. ————————————————
  1314. ${green}18.${plain} SSL Certificate Management
  1315. ${green}19.${plain} Cloudflare SSL Certificate
  1316. ${green}20.${plain} IP Limit Management
  1317. ${green}21.${plain} Firewall Management
  1318. ————————————————
  1319. ${green}22.${plain} Enable BBR
  1320. ${green}23.${plain} Update Geo Files
  1321. ${green}24.${plain} Speedtest by Ookla
  1322. "
  1323. show_status
  1324. echo && read -p "Please enter your selection [0-24]: " num
  1325. case "${num}" in
  1326. 0)
  1327. exit 0
  1328. ;;
  1329. 1)
  1330. check_uninstall && install
  1331. ;;
  1332. 2)
  1333. check_install && update
  1334. ;;
  1335. 3)
  1336. check_install && update_menu
  1337. ;;
  1338. 4)
  1339. check_install && custom_version
  1340. ;;
  1341. 5)
  1342. check_install && uninstall
  1343. ;;
  1344. 6)
  1345. check_install && reset_user
  1346. ;;
  1347. 7)
  1348. check_install && reset_webbasepath
  1349. ;;
  1350. 8)
  1351. check_install && reset_config
  1352. ;;
  1353. 9)
  1354. check_install && set_port
  1355. ;;
  1356. 10)
  1357. check_install && check_config
  1358. ;;
  1359. 11)
  1360. check_install && start
  1361. ;;
  1362. 12)
  1363. check_install && stop
  1364. ;;
  1365. 13)
  1366. check_install && restart
  1367. ;;
  1368. 14)
  1369. check_install && status
  1370. ;;
  1371. 15)
  1372. check_install && show_log
  1373. ;;
  1374. 16)
  1375. check_install && enable
  1376. ;;
  1377. 17)
  1378. check_install && disable
  1379. ;;
  1380. 18)
  1381. ssl_cert_issue_main
  1382. ;;
  1383. 19)
  1384. ssl_cert_issue_CF
  1385. ;;
  1386. 20)
  1387. iplimit_main
  1388. ;;
  1389. 21)
  1390. firewall_menu
  1391. ;;
  1392. 22)
  1393. bbr_menu
  1394. ;;
  1395. 23)
  1396. update_geo
  1397. ;;
  1398. 24)
  1399. run_speedtest
  1400. ;;
  1401. *)
  1402. LOGE "Please enter the correct number [0-24]"
  1403. ;;
  1404. esac
  1405. }
  1406. if [[ $# > 0 ]]; then
  1407. case $1 in
  1408. "start")
  1409. check_install 0 && start 0
  1410. ;;
  1411. "stop")
  1412. check_install 0 && stop 0
  1413. ;;
  1414. "restart")
  1415. check_install 0 && restart 0
  1416. ;;
  1417. "status")
  1418. check_install 0 && status 0
  1419. ;;
  1420. "settings")
  1421. check_install 0 && check_config 0
  1422. ;;
  1423. "enable")
  1424. check_install 0 && enable 0
  1425. ;;
  1426. "disable")
  1427. check_install 0 && disable 0
  1428. ;;
  1429. "log")
  1430. check_install 0 && show_log 0
  1431. ;;
  1432. "banlog")
  1433. check_install 0 && show_banlog 0
  1434. ;;
  1435. "update")
  1436. check_install 0 && update 0
  1437. ;;
  1438. "custom")
  1439. check_install 0 && custom_version 0
  1440. ;;
  1441. "install")
  1442. check_uninstall 0 && install 0
  1443. ;;
  1444. "uninstall")
  1445. check_install 0 && uninstall 0
  1446. ;;
  1447. *) show_usage ;;
  1448. esac
  1449. else
  1450. show_menu
  1451. fi