x-ui.sh 49 KB

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