1
0

x-ui.sh 53 KB

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