1
0

x-ui.sh 48 KB

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