x-ui.sh 47 KB

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