x-ui.sh 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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 -i version_id /etc/os-release | cut -d \" -f2 | cut -d . -f1)
  32. if [[ "${release}" == "centos" ]]; then
  33. if [[ ${os_version} -lt 8 ]]; then
  34. echo -e "${red} Please use CentOS 8 or higher ${plain}\n" && exit 1
  35. fi
  36. elif [[ "${release}" == "ubuntu" ]]; then
  37. if [[ ${os_version} -lt 20 ]]; then
  38. echo -e "${red}please use Ubuntu 20 or higher version! ${plain}\n" && exit 1
  39. fi
  40. elif [[ "${release}" == "fedora" ]]; then
  41. if [[ ${os_version} -lt 36 ]]; then
  42. echo -e "${red}please use Fedora 36 or higher version! ${plain}\n" && exit 1
  43. fi
  44. elif [[ "${release}" == "debian" ]]; then
  45. if [[ ${os_version} -lt 11 ]]; then
  46. echo -e "${red} Please use Debian 11 or higher ${plain}\n" && exit 1
  47. fi
  48. elif [[ "${release}" == "almalinux" ]]; then
  49. if [[ ${os_version} -lt 9 ]]; then
  50. echo -e "${red} Please use Almalinux 9 or higher ${plain}\n" && exit 1
  51. fi
  52. elif [[ "${release}" == "rocky" ]]; then
  53. if [[ ${os_version} -lt 9 ]]; then
  54. echo -e "${red} Please use Rockylinux 9 or higher ${plain}\n" && exit 1
  55. fi
  56. elif [[ "${release}" == "arch" ]]; then
  57. echo "Your OS is ArchLinux"
  58. elif [[ "${release}" == "manjaro" ]]; then
  59. echo "Your OS is Manjaro"
  60. elif [[ "${release}" == "armbian" ]]; then
  61. echo "Your OS is Armbian"
  62. fi
  63. # Declare Variables
  64. log_folder="${XUI_LOG_FOLDER:=/var/log}"
  65. iplimit_log_path="${log_folder}/3xipl.log"
  66. iplimit_banned_log_path="${log_folder}/3xipl-banned.log"
  67. confirm() {
  68. if [[ $# > 1 ]]; then
  69. echo && read -p "$1 [Default $2]: " temp
  70. if [[ "${temp}" == "" ]]; then
  71. temp=$2
  72. fi
  73. else
  74. read -p "$1 [y/n]: " temp
  75. fi
  76. if [[ "${temp}" == "y" || "${temp}" == "Y" ]]; then
  77. return 0
  78. else
  79. return 1
  80. fi
  81. }
  82. confirm_restart() {
  83. confirm "Restart the panel, Attention: Restarting the panel will also restart xray" "y"
  84. if [[ $? == 0 ]]; then
  85. restart
  86. else
  87. show_menu
  88. fi
  89. }
  90. before_show_menu() {
  91. echo && echo -n -e "${yellow}Press enter to return to the main menu: ${plain}" && read temp
  92. show_menu
  93. }
  94. install() {
  95. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  96. if [[ $? == 0 ]]; then
  97. if [[ $# == 0 ]]; then
  98. start
  99. else
  100. start 0
  101. fi
  102. fi
  103. }
  104. update() {
  105. confirm "This function will forcefully reinstall the latest version, and the data will not be lost. Do you want to continue?" "y"
  106. if [[ $? != 0 ]]; then
  107. LOGE "Cancelled"
  108. if [[ $# == 0 ]]; then
  109. before_show_menu
  110. fi
  111. return 0
  112. fi
  113. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  114. if [[ $? == 0 ]]; then
  115. LOGI "Update is complete, Panel has automatically restarted "
  116. exit 0
  117. fi
  118. }
  119. custom_version() {
  120. echo "Enter the panel version (like 2.0.0):"
  121. read panel_version
  122. if [ -z "$panel_version" ]; then
  123. echo "Panel version cannot be empty. Exiting."
  124. exit 1
  125. fi
  126. download_link="https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh"
  127. # Use the entered panel version in the download link
  128. install_command="bash <(curl -Ls $download_link) v$panel_version"
  129. echo "Downloading and installing panel version $panel_version..."
  130. eval $install_command
  131. }
  132. uninstall() {
  133. confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
  134. if [[ $? != 0 ]]; then
  135. if [[ $# == 0 ]]; then
  136. show_menu
  137. fi
  138. return 0
  139. fi
  140. systemctl stop x-ui
  141. systemctl disable x-ui
  142. rm /etc/systemd/system/x-ui.service -f
  143. systemctl daemon-reload
  144. systemctl reset-failed
  145. rm /etc/x-ui/ -rf
  146. rm /usr/local/x-ui/ -rf
  147. echo ""
  148. echo -e "Uninstalled Successfully, If you want to remove this script, then after exiting the script run ${green}rm /usr/bin/x-ui -f${plain} to delete it."
  149. echo ""
  150. if [[ $# == 0 ]]; then
  151. before_show_menu
  152. fi
  153. }
  154. reset_user() {
  155. confirm "Are you sure to reset the username and password of the panel?" "n"
  156. if [[ $? != 0 ]]; then
  157. if [[ $# == 0 ]]; then
  158. show_menu
  159. fi
  160. return 0
  161. fi
  162. read -rp "Please set the login username [default is a random username]: " config_account
  163. [[ -z $config_account ]] && config_account=$(date +%s%N | md5sum | cut -c 1-8)
  164. read -rp "Please set the login password [default is a random password]: " config_password
  165. [[ -z $config_password ]] && config_password=$(date +%s%N | md5sum | cut -c 1-8)
  166. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password} >/dev/null 2>&1
  167. /usr/local/x-ui/x-ui setting -remove_secret >/dev/null 2>&1
  168. echo -e "Panel login username has been reset to: ${green} ${config_account} ${plain}"
  169. echo -e "Panel login password has been reset to: ${green} ${config_password} ${plain}"
  170. echo -e "${yellow} Panel login secret token disabled ${plain}"
  171. echo -e "${green} Please use the new login username and password to access the X-UI panel. Also remember them! ${plain}"
  172. confirm_restart
  173. }
  174. reset_config() {
  175. confirm "Are you sure you want to reset all panel settings, Account data will not be lost, Username and password will not change" "n"
  176. if [[ $? != 0 ]]; then
  177. if [[ $# == 0 ]]; then
  178. show_menu
  179. fi
  180. return 0
  181. fi
  182. /usr/local/x-ui/x-ui setting -reset
  183. 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"
  184. confirm_restart
  185. }
  186. check_config() {
  187. info=$(/usr/local/x-ui/x-ui setting -show true)
  188. if [[ $? != 0 ]]; then
  189. LOGE "get current settings error, please check logs"
  190. show_menu
  191. fi
  192. LOGI "${info}"
  193. }
  194. set_port() {
  195. echo && echo -n -e "Enter port number[1-65535]: " && read port
  196. if [[ -z "${port}" ]]; then
  197. LOGD "Cancelled"
  198. before_show_menu
  199. else
  200. /usr/local/x-ui/x-ui setting -port ${port}
  201. echo -e "The port is set, Please restart the panel now, and use the new port ${green}${port}${plain} to access web panel"
  202. confirm_restart
  203. fi
  204. }
  205. start() {
  206. check_status
  207. if [[ $? == 0 ]]; then
  208. echo ""
  209. LOGI "Panel is running, No need to start again, If you need to restart, please select restart"
  210. else
  211. systemctl start x-ui
  212. sleep 2
  213. check_status
  214. if [[ $? == 0 ]]; then
  215. LOGI "x-ui Started Successfully"
  216. else
  217. LOGE "panel Failed to start, Probably because it takes longer than two seconds to start, Please check the log information later"
  218. fi
  219. fi
  220. if [[ $# == 0 ]]; then
  221. before_show_menu
  222. fi
  223. }
  224. stop() {
  225. check_status
  226. if [[ $? == 1 ]]; then
  227. echo ""
  228. LOGI "Panel stopped, No need to stop again!"
  229. else
  230. systemctl stop x-ui
  231. sleep 2
  232. check_status
  233. if [[ $? == 1 ]]; then
  234. LOGI "x-ui and xray stopped successfully"
  235. else
  236. LOGE "Panel stop failed, Probably because the stop time exceeds two seconds, Please check the log information later"
  237. fi
  238. fi
  239. if [[ $# == 0 ]]; then
  240. before_show_menu
  241. fi
  242. }
  243. restart() {
  244. systemctl restart x-ui
  245. sleep 2
  246. check_status
  247. if [[ $? == 0 ]]; then
  248. LOGI "x-ui and xray Restarted successfully"
  249. else
  250. LOGE "Panel restart failed, Probably because it takes longer than two seconds to start, Please check the log information later"
  251. fi
  252. if [[ $# == 0 ]]; then
  253. before_show_menu
  254. fi
  255. }
  256. status() {
  257. systemctl status x-ui -l
  258. if [[ $# == 0 ]]; then
  259. before_show_menu
  260. fi
  261. }
  262. enable() {
  263. systemctl enable x-ui
  264. if [[ $? == 0 ]]; then
  265. LOGI "x-ui Set to boot automatically on startup successfully"
  266. else
  267. LOGE "x-ui Failed to set Autostart"
  268. fi
  269. if [[ $# == 0 ]]; then
  270. before_show_menu
  271. fi
  272. }
  273. disable() {
  274. systemctl disable x-ui
  275. if [[ $? == 0 ]]; then
  276. LOGI "x-ui Autostart Cancelled successfully"
  277. else
  278. LOGE "x-ui Failed to cancel autostart"
  279. fi
  280. if [[ $# == 0 ]]; then
  281. before_show_menu
  282. fi
  283. }
  284. show_log() {
  285. journalctl -u x-ui.service -e --no-pager -f
  286. if [[ $# == 0 ]]; then
  287. before_show_menu
  288. fi
  289. }
  290. show_banlog() {
  291. if test -f "${iplimit_banned_log_path}"; then
  292. if [[ -s "${iplimit_banned_log_path}" ]]; then
  293. cat ${iplimit_banned_log_path}
  294. else
  295. echo -e "${red}Log file is empty.${plain}\n"
  296. fi
  297. else
  298. echo -e "${red}Log file not found. Please Install Fail2ban and IP Limit first.${plain}\n"
  299. fi
  300. }
  301. enable_bbr() {
  302. if grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf && grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  303. echo -e "${green}BBR is already enabled!${plain}"
  304. exit 0
  305. fi
  306. # Check the OS and install necessary packages
  307. case "${release}" in
  308. ubuntu|debian)
  309. apt-get update && apt-get install -yqq --no-install-recommends ca-certificates
  310. ;;
  311. centos|almalinux|rocky)
  312. yum -y update && yum -y install ca-certificates
  313. ;;
  314. fedora)
  315. dnf -y update && dnf -y install ca-certificates
  316. ;;
  317. *)
  318. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  319. exit 1
  320. ;;
  321. esac
  322. # Enable BBR
  323. echo "net.core.default_qdisc=fq" | tee -a /etc/sysctl.conf
  324. echo "net.ipv4.tcp_congestion_control=bbr" | tee -a /etc/sysctl.conf
  325. # Apply changes
  326. sysctl -p
  327. # Verify that BBR is enabled
  328. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "bbr" ]]; then
  329. echo -e "${green}BBR has been enabled successfully.${plain}"
  330. else
  331. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  332. fi
  333. }
  334. update_shell() {
  335. wget -O /usr/bin/x-ui -N --no-check-certificate https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  336. if [[ $? != 0 ]]; then
  337. echo ""
  338. LOGE "Failed to download script, Please check whether the machine can connect Github"
  339. before_show_menu
  340. else
  341. chmod +x /usr/bin/x-ui
  342. LOGI "Upgrade script succeeded, Please rerun the script" && exit 0
  343. fi
  344. }
  345. # 0: running, 1: not running, 2: not installed
  346. check_status() {
  347. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  348. return 2
  349. fi
  350. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  351. if [[ "${temp}" == "running" ]]; then
  352. return 0
  353. else
  354. return 1
  355. fi
  356. }
  357. check_enabled() {
  358. temp=$(systemctl is-enabled x-ui)
  359. if [[ "${temp}" == "enabled" ]]; then
  360. return 0
  361. else
  362. return 1
  363. fi
  364. }
  365. check_uninstall() {
  366. check_status
  367. if [[ $? != 2 ]]; then
  368. echo ""
  369. LOGE "Panel installed, Please do not reinstall"
  370. if [[ $# == 0 ]]; then
  371. before_show_menu
  372. fi
  373. return 1
  374. else
  375. return 0
  376. fi
  377. }
  378. check_install() {
  379. check_status
  380. if [[ $? == 2 ]]; then
  381. echo ""
  382. LOGE "Please install the panel first"
  383. if [[ $# == 0 ]]; then
  384. before_show_menu
  385. fi
  386. return 1
  387. else
  388. return 0
  389. fi
  390. }
  391. show_status() {
  392. check_status
  393. case $? in
  394. 0)
  395. echo -e "Panel state: ${green}Running${plain}"
  396. show_enable_status
  397. ;;
  398. 1)
  399. echo -e "Panel state: ${yellow}Not Running${plain}"
  400. show_enable_status
  401. ;;
  402. 2)
  403. echo -e "Panel state: ${red}Not Installed${plain}"
  404. ;;
  405. esac
  406. show_xray_status
  407. }
  408. show_enable_status() {
  409. check_enabled
  410. if [[ $? == 0 ]]; then
  411. echo -e "Start automatically: ${green}Yes${plain}"
  412. else
  413. echo -e "Start automatically: ${red}No${plain}"
  414. fi
  415. }
  416. check_xray_status() {
  417. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  418. if [[ count -ne 0 ]]; then
  419. return 0
  420. else
  421. return 1
  422. fi
  423. }
  424. show_xray_status() {
  425. check_xray_status
  426. if [[ $? == 0 ]]; then
  427. echo -e "xray state: ${green}Running${plain}"
  428. else
  429. echo -e "xray state: ${red}Not Running${plain}"
  430. fi
  431. }
  432. open_ports() {
  433. if ! command -v ufw &>/dev/null; then
  434. echo "ufw firewall is not installed. Installing now..."
  435. apt-get update
  436. apt-get install -y ufw
  437. else
  438. echo "ufw firewall is already installed"
  439. fi
  440. # Check if the firewall is inactive
  441. if ufw status | grep -q "Status: active"; then
  442. echo "firewall is already active"
  443. else
  444. # Open the necessary ports
  445. ufw allow ssh
  446. ufw allow http
  447. ufw allow https
  448. ufw allow 2053/tcp
  449. # Enable the firewall
  450. ufw --force enable
  451. fi
  452. # Prompt the user to enter a list of ports
  453. read -p "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  454. # Check if the input is valid
  455. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  456. 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
  457. exit 1
  458. fi
  459. # Open the specified ports using ufw
  460. IFS=',' read -ra PORT_LIST <<<"$ports"
  461. for port in "${PORT_LIST[@]}"; do
  462. if [[ $port == *-* ]]; then
  463. # Split the range into start and end ports
  464. start_port=$(echo $port | cut -d'-' -f1)
  465. end_port=$(echo $port | cut -d'-' -f2)
  466. # Loop through the range and open each port
  467. for ((i = start_port; i <= end_port; i++)); do
  468. ufw allow $i
  469. done
  470. else
  471. ufw allow "$port"
  472. fi
  473. done
  474. # Confirm that the ports are open
  475. ufw status | grep $ports
  476. }
  477. update_geo() {
  478. local defaultBinFolder="/usr/local/x-ui/bin"
  479. read -p "Please enter x-ui bin folder path. Leave blank for default. (Default: '${defaultBinFolder}')" binFolder
  480. binFolder=${binFolder:-${defaultBinFolder}}
  481. if [[ ! -d ${binFolder} ]]; then
  482. LOGE "Folder ${binFolder} not exists!"
  483. LOGI "making bin folder: ${binFolder}..."
  484. mkdir -p ${binFolder}
  485. fi
  486. systemctl stop x-ui
  487. cd ${binFolder}
  488. rm -f geoip.dat geosite.dat geoip_IR.dat geosite_IR.dat geoip_VN.dat geosite_VN.dat
  489. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  490. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  491. wget -O geoip_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat
  492. wget -O geosite_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat
  493. wget -O geoip_VN.dat https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geoip.dat
  494. wget -O geosite_VN.dat https://github.com/vuong2023/vn-v2ray-rules/releases/latest/download/geosite.dat
  495. systemctl start x-ui
  496. echo -e "${green}Geosite.dat + Geoip.dat + geoip_IR.dat + geosite_IR.dat have been updated successfully in bin folder '${binfolder}'!${plain}"
  497. before_show_menu
  498. }
  499. install_acme() {
  500. cd ~
  501. LOGI "install acme..."
  502. curl https://get.acme.sh | sh
  503. if [ $? -ne 0 ]; then
  504. LOGE "install acme failed"
  505. return 1
  506. else
  507. LOGI "install acme succeed"
  508. fi
  509. return 0
  510. }
  511. ssl_cert_issue_main() {
  512. echo -e "${green}\t1.${plain} Get SSL"
  513. echo -e "${green}\t2.${plain} Revoke"
  514. echo -e "${green}\t3.${plain} Force Renew"
  515. echo -e "${green}\t0.${plain} Back to Main Menu"
  516. read -p "Choose an option: " choice
  517. case "$choice" in
  518. 0)
  519. show_menu ;;
  520. 1)
  521. ssl_cert_issue ;;
  522. 2)
  523. local domain=""
  524. read -p "Please enter your domain name to revoke the certificate: " domain
  525. ~/.acme.sh/acme.sh --revoke -d ${domain}
  526. LOGI "Certificate revoked"
  527. ;;
  528. 3)
  529. local domain=""
  530. read -p "Please enter your domain name to forcefully renew an SSL certificate: " domain
  531. ~/.acme.sh/acme.sh --renew -d ${domain} --force ;;
  532. *) echo "Invalid choice" ;;
  533. esac
  534. }
  535. ssl_cert_issue() {
  536. # check for acme.sh first
  537. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  538. echo "acme.sh could not be found. we will install it"
  539. install_acme
  540. if [ $? -ne 0 ]; then
  541. LOGE "install acme failed, please check logs"
  542. exit 1
  543. fi
  544. fi
  545. # install socat second
  546. case "${release}" in
  547. ubuntu|debian|armbian)
  548. apt update && apt install socat -y ;;
  549. centos|almalinux|rocky)
  550. yum -y update && yum -y install socat ;;
  551. fedora)
  552. dnf -y update && dnf -y install socat ;;
  553. *)
  554. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  555. exit 1 ;;
  556. esac
  557. if [ $? -ne 0 ]; then
  558. LOGE "install socat failed, please check logs"
  559. exit 1
  560. else
  561. LOGI "install socat succeed..."
  562. fi
  563. # get the domain here,and we need verify it
  564. local domain=""
  565. read -p "Please enter your domain name:" domain
  566. LOGD "your domain is:${domain},check it..."
  567. # here we need to judge whether there exists cert already
  568. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  569. if [ ${currentCert} == ${domain} ]; then
  570. local certInfo=$(~/.acme.sh/acme.sh --list)
  571. LOGE "system already has certs here,can not issue again,current certs details:"
  572. LOGI "$certInfo"
  573. exit 1
  574. else
  575. LOGI "your domain is ready for issuing cert now..."
  576. fi
  577. # create a directory for install cert
  578. certPath="/root/cert/${domain}"
  579. if [ ! -d "$certPath" ]; then
  580. mkdir -p "$certPath"
  581. else
  582. rm -rf "$certPath"
  583. mkdir -p "$certPath"
  584. fi
  585. # get needed port here
  586. local WebPort=80
  587. read -p "please choose which port do you use,default will be 80 port:" WebPort
  588. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  589. LOGE "your input ${WebPort} is invalid,will use default port"
  590. fi
  591. LOGI "will use port:${WebPort} to issue certs,please make sure this port is open..."
  592. # NOTE:This should be handled by user
  593. # open the port and kill the occupied progress
  594. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  595. ~/.acme.sh/acme.sh --issue -d ${domain} --standalone --httpport ${WebPort}
  596. if [ $? -ne 0 ]; then
  597. LOGE "issue certs failed,please check logs"
  598. rm -rf ~/.acme.sh/${domain}
  599. exit 1
  600. else
  601. LOGE "issue certs succeed,installing certs..."
  602. fi
  603. # install cert
  604. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  605. --key-file /root/cert/${domain}/privkey.pem \
  606. --fullchain-file /root/cert/${domain}/fullchain.pem
  607. if [ $? -ne 0 ]; then
  608. LOGE "install certs failed,exit"
  609. rm -rf ~/.acme.sh/${domain}
  610. exit 1
  611. else
  612. LOGI "install certs succeed,enable auto renew..."
  613. fi
  614. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  615. if [ $? -ne 0 ]; then
  616. LOGE "auto renew failed, certs details:"
  617. ls -lah cert/*
  618. chmod 755 $certPath/*
  619. exit 1
  620. else
  621. LOGI "auto renew succeed, certs details:"
  622. ls -lah cert/*
  623. chmod 755 $certPath/*
  624. fi
  625. }
  626. ssl_cert_issue_CF() {
  627. echo -E ""
  628. LOGD "******Instructions for use******"
  629. LOGI "This Acme script requires the following data:"
  630. LOGI "1.Cloudflare Registered e-mail"
  631. LOGI "2.Cloudflare Global API Key"
  632. LOGI "3.The domain name that has been resolved dns to the current server by Cloudflare"
  633. LOGI "4.The script applies for a certificate. The default installation path is /root/cert "
  634. confirm "Confirmed?[y/n]" "y"
  635. if [ $? -eq 0 ]; then
  636. # check for acme.sh first
  637. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  638. echo "acme.sh could not be found. we will install it"
  639. install_acme
  640. if [ $? -ne 0 ]; then
  641. LOGE "install acme failed, please check logs"
  642. exit 1
  643. fi
  644. fi
  645. CF_Domain=""
  646. CF_GlobalKey=""
  647. CF_AccountEmail=""
  648. certPath=/root/cert
  649. if [ ! -d "$certPath" ]; then
  650. mkdir $certPath
  651. else
  652. rm -rf $certPath
  653. mkdir $certPath
  654. fi
  655. LOGD "Please set a domain name:"
  656. read -p "Input your domain here:" CF_Domain
  657. LOGD "Your domain name is set to:${CF_Domain}"
  658. LOGD "Please set the API key:"
  659. read -p "Input your key here:" CF_GlobalKey
  660. LOGD "Your API key is:${CF_GlobalKey}"
  661. LOGD "Please set up registered email:"
  662. read -p "Input your email here:" CF_AccountEmail
  663. LOGD "Your registered email address is:${CF_AccountEmail}"
  664. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  665. if [ $? -ne 0 ]; then
  666. LOGE "Default CA, Lets'Encrypt fail, script exiting..."
  667. exit 1
  668. fi
  669. export CF_Key="${CF_GlobalKey}"
  670. export CF_Email=${CF_AccountEmail}
  671. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  672. if [ $? -ne 0 ]; then
  673. LOGE "Certificate issuance failed, script exiting..."
  674. exit 1
  675. else
  676. LOGI "Certificate issued Successfully, Installing..."
  677. fi
  678. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
  679. --cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
  680. --fullchain-file /root/cert/fullchain.cer
  681. if [ $? -ne 0 ]; then
  682. LOGE "Certificate installation failed, script exiting..."
  683. exit 1
  684. else
  685. LOGI "Certificate installed Successfully,Turning on automatic updates..."
  686. fi
  687. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  688. if [ $? -ne 0 ]; then
  689. LOGE "Auto update setup Failed, script exiting..."
  690. ls -lah cert
  691. chmod 755 $certPath
  692. exit 1
  693. else
  694. LOGI "The certificate is installed and auto-renewal is turned on, Specific information is as follows"
  695. ls -lah cert
  696. chmod 755 $certPath
  697. fi
  698. else
  699. show_menu
  700. fi
  701. }
  702. warp_cloudflare() {
  703. echo -e "${green}\t1.${plain} Install WARP socks5 proxy"
  704. echo -e "${green}\t2.${plain} Account Type (free, plus, team)"
  705. echo -e "${green}\t3.${plain} Turn on/off WireProxy"
  706. echo -e "${green}\t4.${plain} Uninstall WARP"
  707. echo -e "${green}\t0.${plain} Back to Main Menu"
  708. read -p "Choose an option: " choice
  709. case "$choice" in
  710. 0)
  711. show_menu ;;
  712. 1)
  713. bash <(curl -sSL https://raw.githubusercontent.com/hamid-gh98/x-ui-scripts/main/install_warp_proxy.sh)
  714. ;;
  715. 2)
  716. warp a
  717. ;;
  718. 3)
  719. warp y
  720. ;;
  721. 4)
  722. warp u
  723. ;;
  724. *) echo "Invalid choice" ;;
  725. esac
  726. }
  727. multi_protocol() {
  728. echo "This script only supports Vless and Vmess. if you use another protocols, DON'T INSTALL or get backup first! "
  729. echo -e "${green}\t1.${plain} Install Multi Protocol Script"
  730. echo -e "${green}\t2.${plain} Uninstall"
  731. echo -e "${green}\t3.${plain} Start Service"
  732. echo -e "${green}\t4.${plain} Stop Service"
  733. echo -e "${green}\t0.${plain} Back to Main Menu"
  734. read -p "Choose an option: " choice
  735. case "$choice" in
  736. 0)
  737. show_menu ;;
  738. 1)
  739. bash <(curl -Ls https://raw.githubusercontent.com/M4mmad/3xui-multi-protocol/master/install.sh --ipv4)
  740. ;;
  741. 2)
  742. bash <(curl -Ls https://raw.githubusercontent.com/M4mmad/3xui-multi-protocol/master/unistall.sh --ipv4)
  743. ;;
  744. 3)
  745. systemctl start 3xui-multi-protocol
  746. ;;
  747. 4)
  748. systemctl stop 3xui-multi-protocol
  749. ;;
  750. *) echo "Invalid choice" ;;
  751. esac
  752. }
  753. run_speedtest() {
  754. # Check if Speedtest is already installed
  755. if ! command -v speedtest &> /dev/null; then
  756. # If not installed, install it
  757. local pkg_manager=""
  758. local speedtest_install_script=""
  759. if command -v dnf &> /dev/null; then
  760. pkg_manager="dnf"
  761. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  762. elif command -v yum &> /dev/null; then
  763. pkg_manager="yum"
  764. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  765. elif command -v apt-get &> /dev/null; then
  766. pkg_manager="apt-get"
  767. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  768. elif command -v apt &> /dev/null; then
  769. pkg_manager="apt"
  770. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  771. fi
  772. if [[ -z $pkg_manager ]]; then
  773. echo "Error: Package manager not found. You may need to install Speedtest manually."
  774. return 1
  775. else
  776. curl -s $speedtest_install_script | bash
  777. $pkg_manager install -y speedtest
  778. fi
  779. fi
  780. # Run Speedtest
  781. speedtest
  782. }
  783. create_iplimit_jails() {
  784. # Use default bantime if not passed => 5 minutes
  785. local bantime="${1:-5}"
  786. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  787. [3x-ipl]
  788. enabled=true
  789. filter=3x-ipl
  790. action=3x-ipl
  791. logpath=${iplimit_log_path}
  792. maxretry=4
  793. findtime=60
  794. bantime=${bantime}m
  795. EOF
  796. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  797. [Definition]
  798. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  799. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  800. ignoreregex =
  801. EOF
  802. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  803. [INCLUDES]
  804. before = iptables-common.conf
  805. [Definition]
  806. actionstart = <iptables> -N f2b-<name>
  807. <iptables> -A f2b-<name> -j <returntype>
  808. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  809. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  810. <actionflush>
  811. <iptables> -X f2b-<name>
  812. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  813. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  814. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  815. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  816. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  817. [Init]
  818. EOF
  819. echo -e "${green}Created Ip Limit jail files with a bantime of ${bantime} minutes.${plain}"
  820. }
  821. iplimit_remove_conflicts() {
  822. local jail_files=(
  823. /etc/fail2ban/jail.conf
  824. /etc/fail2ban/jail.local
  825. )
  826. for file in "${jail_files[@]}"; do
  827. # Check for [3x-ipl] config in jail file then remove it
  828. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  829. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  830. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  831. fi
  832. done
  833. }
  834. iplimit_main() {
  835. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  836. echo -e "${green}\t2.${plain} Change Ban Duration"
  837. echo -e "${green}\t3.${plain} Unban Everyone"
  838. echo -e "${green}\t4.${plain} Check Logs"
  839. echo -e "${green}\t5.${plain} fail2ban status"
  840. echo -e "${green}\t6.${plain} Uninstall IP Limit"
  841. echo -e "${green}\t0.${plain} Back to Main Menu"
  842. read -p "Choose an option: " choice
  843. case "$choice" in
  844. 0)
  845. show_menu ;;
  846. 1)
  847. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  848. if [[ $? == 0 ]]; then
  849. install_iplimit
  850. else
  851. iplimit_main
  852. fi ;;
  853. 2)
  854. read -rp "Please enter new Ban Duration in Minutes [default 5]: " NUM
  855. if [[ $NUM =~ ^[0-9]+$ ]]; then
  856. create_iplimit_jails ${NUM}
  857. systemctl restart fail2ban
  858. else
  859. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  860. fi
  861. iplimit_main ;;
  862. 3)
  863. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  864. if [[ $? == 0 ]]; then
  865. fail2ban-client reload --restart --unban 3x-ipl
  866. echo -e "${green}All users Unbanned successfully.${plain}"
  867. iplimit_main
  868. else
  869. echo -e "${yellow}Cancelled.${plain}"
  870. fi
  871. iplimit_main ;;
  872. 4)
  873. show_banlog
  874. ;;
  875. 5)
  876. service fail2ban status
  877. ;;
  878. 6)
  879. remove_iplimit ;;
  880. *) echo "Invalid choice" ;;
  881. esac
  882. }
  883. install_iplimit() {
  884. if ! command -v fail2ban-client &>/dev/null; then
  885. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  886. # Check the OS and install necessary packages
  887. case "${release}" in
  888. ubuntu|debian)
  889. apt update && apt install fail2ban -y ;;
  890. centos|almalinux|rocky)
  891. yum -y update && yum -y install fail2ban ;;
  892. fedora)
  893. dnf -y update && dnf -y install fail2ban ;;
  894. *)
  895. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  896. exit 1 ;;
  897. esac
  898. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  899. else
  900. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  901. fi
  902. echo -e "${green}Configuring IP Limit...${plain}\n"
  903. # make sure there's no conflict for jail files
  904. iplimit_remove_conflicts
  905. # Check if log file exists
  906. if ! test -f "${iplimit_banned_log_path}"; then
  907. touch ${iplimit_banned_log_path}
  908. fi
  909. # Check if service log file exists so fail2ban won't return error
  910. if ! test -f "${iplimit_log_path}"; then
  911. touch ${iplimit_log_path}
  912. fi
  913. # Create the iplimit jail files
  914. # we didn't pass the bantime here to use the default value
  915. create_iplimit_jails
  916. # Launching fail2ban
  917. if ! systemctl is-active --quiet fail2ban; then
  918. systemctl start fail2ban
  919. else
  920. systemctl restart fail2ban
  921. fi
  922. systemctl enable fail2ban
  923. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  924. before_show_menu
  925. }
  926. remove_iplimit(){
  927. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  928. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  929. echo -e "${green}\t0.${plain} Abort"
  930. read -p "Choose an option: " num
  931. case "$num" in
  932. 1)
  933. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  934. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  935. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  936. systemctl restart fail2ban
  937. echo -e "${green}IP Limit removed successfully!${plain}\n"
  938. before_show_menu ;;
  939. 2)
  940. rm -rf /etc/fail2ban
  941. systemctl stop fail2ban
  942. case "${release}" in
  943. ubuntu|debian)
  944. apt-get purge fail2ban -y;;
  945. centos|almalinux|rocky)
  946. yum remove fail2ban -y;;
  947. fedora)
  948. dnf remove fail2ban -y;;
  949. *)
  950. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  951. exit 1 ;;
  952. esac
  953. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  954. before_show_menu ;;
  955. 0)
  956. echo -e "${yellow}Cancelled.${plain}\n"
  957. iplimit_main ;;
  958. *)
  959. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  960. remove_iplimit ;;
  961. esac
  962. }
  963. show_usage() {
  964. echo "x-ui control menu usages: "
  965. echo "------------------------------------------"
  966. echo -e "x-ui - Enter control menu"
  967. echo -e "x-ui start - Start x-ui "
  968. echo -e "x-ui stop - Stop x-ui "
  969. echo -e "x-ui restart - Restart x-ui "
  970. echo -e "x-ui status - Show x-ui status"
  971. echo -e "x-ui enable - Enable x-ui on system startup"
  972. echo -e "x-ui disable - Disable x-ui on system startup"
  973. echo -e "x-ui log - Check x-ui logs"
  974. echo -e "x-ui banlog - Check Fail2ban ban logs"
  975. echo -e "x-ui update - Update x-ui "
  976. echo -e "x-ui install - Install x-ui "
  977. echo -e "x-ui uninstall - Uninstall x-ui "
  978. echo "------------------------------------------"
  979. }
  980. show_menu() {
  981. echo -e "
  982. ${green}3X-ui Panel Management Script${plain}
  983. ${green}0.${plain} Exit Script
  984. ————————————————
  985. ${green}1.${plain} Install
  986. ${green}2.${plain} Update
  987. ${green}3.${plain} Custom Version
  988. ${green}4.${plain} Uninstall
  989. ————————————————
  990. ${green}5.${plain} Reset Username & Password & Secret Token
  991. ${green}6.${plain} Reset Settings
  992. ${green}7.${plain} Change Port
  993. ${green}8.${plain} View Current Settings
  994. ————————————————
  995. ${green}9.${plain} Start
  996. ${green}10.${plain} Stop
  997. ${green}11.${plain} Restart
  998. ${green}12.${plain} Check Status
  999. ${green}13.${plain} Check Logs
  1000. ————————————————
  1001. ${green}14.${plain} Enable x-ui On System Startup
  1002. ${green}15.${plain} Disable x-ui On System Startup
  1003. ————————————————
  1004. ${green}16.${plain} SSL Certificate Management
  1005. ${green}17.${plain} Cloudflare SSL Certificate
  1006. ${green}18.${plain} IP Limit Management
  1007. ${green}19.${plain} WARP Management
  1008. ${green}20.${plain} Multi Protocol Management
  1009. ————————————————
  1010. ${green}21.${plain} Enable BBR
  1011. ${green}22.${plain} Update Geo Files
  1012. ${green}23.${plain} Active Firewall and open ports
  1013. ${green}24.${plain} Speedtest by Ookla
  1014. "
  1015. show_status
  1016. echo && read -p "Please enter your selection [0-24]: " num
  1017. case "${num}" in
  1018. 0)
  1019. exit 0
  1020. ;;
  1021. 1)
  1022. check_uninstall && install
  1023. ;;
  1024. 2)
  1025. check_install && update
  1026. ;;
  1027. 3)
  1028. check_install && custom_version
  1029. ;;
  1030. 4)
  1031. check_install && uninstall
  1032. ;;
  1033. 5)
  1034. check_install && reset_user
  1035. ;;
  1036. 6)
  1037. check_install && reset_config
  1038. ;;
  1039. 7)
  1040. check_install && set_port
  1041. ;;
  1042. 8)
  1043. check_install && check_config
  1044. ;;
  1045. 9)
  1046. check_install && start
  1047. ;;
  1048. 10)
  1049. check_install && stop
  1050. ;;
  1051. 11)
  1052. check_install && restart
  1053. ;;
  1054. 12)
  1055. check_install && status
  1056. ;;
  1057. 13)
  1058. check_install && show_log
  1059. ;;
  1060. 14)
  1061. check_install && enable
  1062. ;;
  1063. 15)
  1064. check_install && disable
  1065. ;;
  1066. 16)
  1067. ssl_cert_issue_main
  1068. ;;
  1069. 17)
  1070. ssl_cert_issue_CF
  1071. ;;
  1072. 18)
  1073. iplimit_main
  1074. ;;
  1075. 19)
  1076. warp_cloudflare
  1077. ;;
  1078. 20)
  1079. multi_protocol
  1080. ;;
  1081. 21)
  1082. enable_bbr
  1083. ;;
  1084. 22)
  1085. update_geo
  1086. ;;
  1087. 23)
  1088. open_ports
  1089. ;;
  1090. 24)
  1091. run_speedtest
  1092. ;;
  1093. *)
  1094. LOGE "Please enter the correct number [0-24]"
  1095. ;;
  1096. esac
  1097. }
  1098. if [[ $# > 0 ]]; then
  1099. case $1 in
  1100. "start")
  1101. check_install 0 && start 0
  1102. ;;
  1103. "stop")
  1104. check_install 0 && stop 0
  1105. ;;
  1106. "restart")
  1107. check_install 0 && restart 0
  1108. ;;
  1109. "status")
  1110. check_install 0 && status 0
  1111. ;;
  1112. "enable")
  1113. check_install 0 && enable 0
  1114. ;;
  1115. "disable")
  1116. check_install 0 && disable 0
  1117. ;;
  1118. "log")
  1119. check_install 0 && show_log 0
  1120. ;;
  1121. "banlog")
  1122. check_install 0 && show_banlog 0
  1123. ;;
  1124. "update")
  1125. check_install 0 && update 0
  1126. ;;
  1127. "install")
  1128. check_uninstall 0 && install 0
  1129. ;;
  1130. "uninstall")
  1131. check_install 0 && uninstall 0
  1132. ;;
  1133. *) show_usage ;;
  1134. esac
  1135. else
  1136. show_menu
  1137. fi