1
0

x-ui.sh 48 KB

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