x-ui.sh 46 KB

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