x-ui.sh 48 KB

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