x-ui.sh 49 KB

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