1
0

x-ui.sh 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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. ;;
  521. 1)
  522. ssl_cert_issue
  523. ;;
  524. 2)
  525. local domain=""
  526. read -p "Please enter your domain name to revoke the certificate: " domain
  527. ~/.acme.sh/acme.sh --revoke -d ${domain}
  528. LOGI "Certificate revoked"
  529. ;;
  530. 3)
  531. local domain=""
  532. read -p "Please enter your domain name to forcefully renew an SSL certificate: " domain
  533. ~/.acme.sh/acme.sh --renew -d ${domain} --force
  534. ;;
  535. *) echo "Invalid choice" ;;
  536. esac
  537. }
  538. ssl_cert_issue() {
  539. # check for acme.sh first
  540. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  541. echo "acme.sh could not be found. we will install it"
  542. install_acme
  543. if [ $? -ne 0 ]; then
  544. LOGE "install acme failed, please check logs"
  545. exit 1
  546. fi
  547. fi
  548. # install socat second
  549. case "${release}" in
  550. ubuntu | debian | armbian)
  551. apt update && apt install socat -y
  552. ;;
  553. centos | almalinux | rocky)
  554. yum -y update && yum -y install socat
  555. ;;
  556. fedora)
  557. dnf -y update && dnf -y install socat
  558. ;;
  559. *)
  560. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  561. exit 1
  562. ;;
  563. esac
  564. if [ $? -ne 0 ]; then
  565. LOGE "install socat failed, please check logs"
  566. exit 1
  567. else
  568. LOGI "install socat succeed..."
  569. fi
  570. # get the domain here,and we need verify it
  571. local domain=""
  572. read -p "Please enter your domain name:" domain
  573. LOGD "your domain is:${domain},check it..."
  574. # here we need to judge whether there exists cert already
  575. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  576. if [ ${currentCert} == ${domain} ]; then
  577. local certInfo=$(~/.acme.sh/acme.sh --list)
  578. LOGE "system already has certs here,can not issue again,current certs details:"
  579. LOGI "$certInfo"
  580. exit 1
  581. else
  582. LOGI "your domain is ready for issuing cert now..."
  583. fi
  584. # create a directory for install cert
  585. certPath="/root/cert/${domain}"
  586. if [ ! -d "$certPath" ]; then
  587. mkdir -p "$certPath"
  588. else
  589. rm -rf "$certPath"
  590. mkdir -p "$certPath"
  591. fi
  592. # get needed port here
  593. local WebPort=80
  594. read -p "please choose which port do you use,default will be 80 port:" WebPort
  595. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  596. LOGE "your input ${WebPort} is invalid,will use default port"
  597. fi
  598. LOGI "will use port:${WebPort} to issue certs,please make sure this port is open..."
  599. # NOTE:This should be handled by user
  600. # open the port and kill the occupied progress
  601. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  602. ~/.acme.sh/acme.sh --issue -d ${domain} --standalone --httpport ${WebPort}
  603. if [ $? -ne 0 ]; then
  604. LOGE "issue certs failed,please check logs"
  605. rm -rf ~/.acme.sh/${domain}
  606. exit 1
  607. else
  608. LOGE "issue certs succeed,installing certs..."
  609. fi
  610. # install cert
  611. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  612. --key-file /root/cert/${domain}/privkey.pem \
  613. --fullchain-file /root/cert/${domain}/fullchain.pem
  614. if [ $? -ne 0 ]; then
  615. LOGE "install certs failed,exit"
  616. rm -rf ~/.acme.sh/${domain}
  617. exit 1
  618. else
  619. LOGI "install certs succeed,enable auto renew..."
  620. fi
  621. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  622. if [ $? -ne 0 ]; then
  623. LOGE "auto renew failed, certs details:"
  624. ls -lah cert/*
  625. chmod 755 $certPath/*
  626. exit 1
  627. else
  628. LOGI "auto renew succeed, certs details:"
  629. ls -lah cert/*
  630. chmod 755 $certPath/*
  631. fi
  632. }
  633. ssl_cert_issue_CF() {
  634. echo -E ""
  635. LOGD "******Instructions for use******"
  636. LOGI "This Acme script requires the following data:"
  637. LOGI "1.Cloudflare Registered e-mail"
  638. LOGI "2.Cloudflare Global API Key"
  639. LOGI "3.The domain name that has been resolved dns to the current server by Cloudflare"
  640. LOGI "4.The script applies for a certificate. The default installation path is /root/cert "
  641. confirm "Confirmed?[y/n]" "y"
  642. if [ $? -eq 0 ]; then
  643. # check for acme.sh first
  644. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  645. echo "acme.sh could not be found. we will install it"
  646. install_acme
  647. if [ $? -ne 0 ]; then
  648. LOGE "install acme failed, please check logs"
  649. exit 1
  650. fi
  651. fi
  652. CF_Domain=""
  653. CF_GlobalKey=""
  654. CF_AccountEmail=""
  655. certPath=/root/cert
  656. if [ ! -d "$certPath" ]; then
  657. mkdir $certPath
  658. else
  659. rm -rf $certPath
  660. mkdir $certPath
  661. fi
  662. LOGD "Please set a domain name:"
  663. read -p "Input your domain here:" CF_Domain
  664. LOGD "Your domain name is set to:${CF_Domain}"
  665. LOGD "Please set the API key:"
  666. read -p "Input your key here:" CF_GlobalKey
  667. LOGD "Your API key is:${CF_GlobalKey}"
  668. LOGD "Please set up registered email:"
  669. read -p "Input your email here:" CF_AccountEmail
  670. LOGD "Your registered email address is:${CF_AccountEmail}"
  671. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  672. if [ $? -ne 0 ]; then
  673. LOGE "Default CA, Lets'Encrypt fail, script exiting..."
  674. exit 1
  675. fi
  676. export CF_Key="${CF_GlobalKey}"
  677. export CF_Email=${CF_AccountEmail}
  678. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  679. if [ $? -ne 0 ]; then
  680. LOGE "Certificate issuance failed, script exiting..."
  681. exit 1
  682. else
  683. LOGI "Certificate issued Successfully, Installing..."
  684. fi
  685. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
  686. --cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
  687. --fullchain-file /root/cert/fullchain.cer
  688. if [ $? -ne 0 ]; then
  689. LOGE "Certificate installation failed, script exiting..."
  690. exit 1
  691. else
  692. LOGI "Certificate installed Successfully,Turning on automatic updates..."
  693. fi
  694. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  695. if [ $? -ne 0 ]; then
  696. LOGE "Auto update setup Failed, script exiting..."
  697. ls -lah cert
  698. chmod 755 $certPath
  699. exit 1
  700. else
  701. LOGI "The certificate is installed and auto-renewal is turned on, Specific information is as follows"
  702. ls -lah cert
  703. chmod 755 $certPath
  704. fi
  705. else
  706. show_menu
  707. fi
  708. }
  709. warp_cloudflare() {
  710. echo -e "${green}\t1.${plain} Install WARP socks5 proxy"
  711. echo -e "${green}\t2.${plain} Account Type (free, plus, team)"
  712. echo -e "${green}\t3.${plain} Turn on/off WireProxy"
  713. echo -e "${green}\t4.${plain} Uninstall WARP"
  714. echo -e "${green}\t0.${plain} Back to Main Menu"
  715. read -p "Choose an option: " choice
  716. case "$choice" in
  717. 0)
  718. show_menu
  719. ;;
  720. 1)
  721. bash <(curl -sSL https://raw.githubusercontent.com/hamid-gh98/x-ui-scripts/main/install_warp_proxy.sh)
  722. ;;
  723. 2)
  724. warp a
  725. ;;
  726. 3)
  727. warp y
  728. ;;
  729. 4)
  730. warp u
  731. ;;
  732. *) echo "Invalid choice" ;;
  733. esac
  734. }
  735. run_speedtest() {
  736. # Check if Speedtest is already installed
  737. if ! command -v speedtest &>/dev/null; then
  738. # If not installed, install it
  739. local pkg_manager=""
  740. local speedtest_install_script=""
  741. if command -v dnf &>/dev/null; then
  742. pkg_manager="dnf"
  743. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  744. elif command -v yum &>/dev/null; then
  745. pkg_manager="yum"
  746. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  747. elif command -v apt-get &>/dev/null; then
  748. pkg_manager="apt-get"
  749. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  750. elif command -v apt &>/dev/null; then
  751. pkg_manager="apt"
  752. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  753. fi
  754. if [[ -z $pkg_manager ]]; then
  755. echo "Error: Package manager not found. You may need to install Speedtest manually."
  756. return 1
  757. else
  758. curl -s $speedtest_install_script | bash
  759. $pkg_manager install -y speedtest
  760. fi
  761. fi
  762. # Run Speedtest
  763. speedtest
  764. }
  765. create_iplimit_jails() {
  766. # Use default bantime if not passed => 30 minutes
  767. local bantime="${1:-30}"
  768. # Uncomment 'allowipv6 = auto' in fail2ban.conf
  769. sed -i 's/#allowipv6 = auto/allowipv6 = auto/g' /etc/fail2ban/fail2ban.conf
  770. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  771. [3x-ipl]
  772. enabled=true
  773. filter=3x-ipl
  774. action=3x-ipl
  775. logpath=${iplimit_log_path}
  776. maxretry=4
  777. findtime=60
  778. bantime=${bantime}m
  779. EOF
  780. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  781. [Definition]
  782. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  783. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  784. ignoreregex =
  785. EOF
  786. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  787. [INCLUDES]
  788. before = iptables-common.conf
  789. [Definition]
  790. actionstart = <iptables> -N f2b-<name>
  791. <iptables> -A f2b-<name> -j <returntype>
  792. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  793. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  794. <actionflush>
  795. <iptables> -X f2b-<name>
  796. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  797. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  798. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  799. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  800. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  801. [Init]
  802. EOF
  803. echo -e "${green}Ip Limit jail files created with a bantime of ${bantime} minutes.${plain}"
  804. }
  805. iplimit_remove_conflicts() {
  806. local jail_files=(
  807. /etc/fail2ban/jail.conf
  808. /etc/fail2ban/jail.local
  809. )
  810. for file in "${jail_files[@]}"; do
  811. # Check for [3x-ipl] config in jail file then remove it
  812. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  813. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  814. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  815. fi
  816. done
  817. }
  818. iplimit_main() {
  819. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  820. echo -e "${green}\t2.${plain} Change Ban Duration"
  821. echo -e "${green}\t3.${plain} Unban Everyone"
  822. echo -e "${green}\t4.${plain} Check Logs"
  823. echo -e "${green}\t5.${plain} fail2ban status"
  824. echo -e "${green}\t6.${plain} Uninstall IP Limit"
  825. echo -e "${green}\t0.${plain} Back to Main Menu"
  826. read -p "Choose an option: " choice
  827. case "$choice" in
  828. 0)
  829. show_menu
  830. ;;
  831. 1)
  832. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  833. if [[ $? == 0 ]]; then
  834. install_iplimit
  835. else
  836. iplimit_main
  837. fi
  838. ;;
  839. 2)
  840. read -rp "Please enter new Ban Duration in Minutes [default 30]: " NUM
  841. if [[ $NUM =~ ^[0-9]+$ ]]; then
  842. create_iplimit_jails ${NUM}
  843. systemctl restart fail2ban
  844. else
  845. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  846. fi
  847. iplimit_main
  848. ;;
  849. 3)
  850. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  851. if [[ $? == 0 ]]; then
  852. fail2ban-client reload --restart --unban 3x-ipl
  853. truncate -s 0 "${iplimit_banned_log_path}"
  854. echo -e "${green}All users Unbanned successfully.${plain}"
  855. iplimit_main
  856. else
  857. echo -e "${yellow}Cancelled.${plain}"
  858. fi
  859. iplimit_main
  860. ;;
  861. 4)
  862. show_banlog
  863. ;;
  864. 5)
  865. service fail2ban status
  866. ;;
  867. 6)
  868. remove_iplimit
  869. ;;
  870. *) echo "Invalid choice" ;;
  871. esac
  872. }
  873. install_iplimit() {
  874. if ! command -v fail2ban-client &>/dev/null; then
  875. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  876. # Check the OS and install necessary packages
  877. case "${release}" in
  878. ubuntu | debian)
  879. apt update && apt install fail2ban -y
  880. ;;
  881. centos | almalinux | rocky)
  882. yum update -y && yum install epel-release -y
  883. yum -y install fail2ban
  884. ;;
  885. fedora)
  886. dnf -y update && dnf -y install fail2ban
  887. ;;
  888. *)
  889. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  890. exit 1
  891. ;;
  892. esac
  893. if ! command -v fail2ban-client &>/dev/null; then
  894. echo -e "${red}Fail2ban installation failed.${plain}\n"
  895. exit 1
  896. fi
  897. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  898. else
  899. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  900. fi
  901. echo -e "${green}Configuring IP Limit...${plain}\n"
  902. # make sure there's no conflict for jail files
  903. iplimit_remove_conflicts
  904. # Check if log file exists
  905. if ! test -f "${iplimit_banned_log_path}"; then
  906. touch ${iplimit_banned_log_path}
  907. fi
  908. # Check if service log file exists so fail2ban won't return error
  909. if ! test -f "${iplimit_log_path}"; then
  910. touch ${iplimit_log_path}
  911. fi
  912. # Create the iplimit jail files
  913. # we didn't pass the bantime here to use the default value
  914. create_iplimit_jails
  915. # Launching fail2ban
  916. if ! systemctl is-active --quiet fail2ban; then
  917. systemctl start fail2ban
  918. systemctl enable 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. ;;
  940. 2)
  941. rm -rf /etc/fail2ban
  942. systemctl stop fail2ban
  943. case "${release}" in
  944. ubuntu | debian)
  945. apt-get remove -y fail2ban
  946. apt-get purge -y fail2ban -y
  947. apt-get autoremove -y
  948. ;;
  949. centos | almalinux | rocky)
  950. yum remove fail2ban -y
  951. yum autoremove -y
  952. ;;
  953. fedora)
  954. dnf remove fail2ban -y
  955. dnf autoremove -y
  956. ;;
  957. *)
  958. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  959. exit 1
  960. ;;
  961. esac
  962. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  963. before_show_menu
  964. ;;
  965. 0)
  966. echo -e "${yellow}Cancelled.${plain}\n"
  967. iplimit_main
  968. ;;
  969. *)
  970. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  971. remove_iplimit
  972. ;;
  973. esac
  974. }
  975. show_usage() {
  976. echo "x-ui control menu usages: "
  977. echo "------------------------------------------"
  978. echo -e "x-ui - Enter control menu"
  979. echo -e "x-ui start - Start x-ui "
  980. echo -e "x-ui stop - Stop x-ui "
  981. echo -e "x-ui restart - Restart x-ui "
  982. echo -e "x-ui status - Show x-ui status"
  983. echo -e "x-ui enable - Enable x-ui on system startup"
  984. echo -e "x-ui disable - Disable x-ui on system startup"
  985. echo -e "x-ui log - Check x-ui logs"
  986. echo -e "x-ui banlog - Check Fail2ban ban logs"
  987. echo -e "x-ui update - Update x-ui "
  988. echo -e "x-ui install - Install x-ui "
  989. echo -e "x-ui uninstall - Uninstall x-ui "
  990. echo "------------------------------------------"
  991. }
  992. show_menu() {
  993. echo -e "
  994. ${green}3X-ui Panel Management Script${plain}
  995. ${green}0.${plain} Exit Script
  996. ————————————————
  997. ${green}1.${plain} Install
  998. ${green}2.${plain} Update
  999. ${green}3.${plain} Custom Version
  1000. ${green}4.${plain} Uninstall
  1001. ————————————————
  1002. ${green}5.${plain} Reset Username & Password & Secret Token
  1003. ${green}6.${plain} Reset Settings
  1004. ${green}7.${plain} Change Port
  1005. ${green}8.${plain} View Current Settings
  1006. ————————————————
  1007. ${green}9.${plain} Start
  1008. ${green}10.${plain} Stop
  1009. ${green}11.${plain} Restart
  1010. ${green}12.${plain} Check Status
  1011. ${green}13.${plain} Check Logs
  1012. ————————————————
  1013. ${green}14.${plain} Enable Autostart
  1014. ${green}15.${plain} Disable Autostart
  1015. ————————————————
  1016. ${green}16.${plain} SSL Certificate Management
  1017. ${green}17.${plain} Cloudflare SSL Certificate
  1018. ${green}18.${plain} IP Limit Management
  1019. ${green}19.${plain} WARP Management
  1020. ————————————————
  1021. ${green}20.${plain} Enable BBR
  1022. ${green}21.${plain} Update Geo Files
  1023. ${green}22.${plain} Active Firewall and open ports
  1024. ${green}23.${plain} Speedtest by Ookla
  1025. "
  1026. show_status
  1027. echo && read -p "Please enter your selection [0-23]: " num
  1028. case "${num}" in
  1029. 0)
  1030. exit 0
  1031. ;;
  1032. 1)
  1033. check_uninstall && install
  1034. ;;
  1035. 2)
  1036. check_install && update
  1037. ;;
  1038. 3)
  1039. check_install && custom_version
  1040. ;;
  1041. 4)
  1042. check_install && uninstall
  1043. ;;
  1044. 5)
  1045. check_install && reset_user
  1046. ;;
  1047. 6)
  1048. check_install && reset_config
  1049. ;;
  1050. 7)
  1051. check_install && set_port
  1052. ;;
  1053. 8)
  1054. check_install && check_config
  1055. ;;
  1056. 9)
  1057. check_install && start
  1058. ;;
  1059. 10)
  1060. check_install && stop
  1061. ;;
  1062. 11)
  1063. check_install && restart
  1064. ;;
  1065. 12)
  1066. check_install && status
  1067. ;;
  1068. 13)
  1069. check_install && show_log
  1070. ;;
  1071. 14)
  1072. check_install && enable
  1073. ;;
  1074. 15)
  1075. check_install && disable
  1076. ;;
  1077. 16)
  1078. ssl_cert_issue_main
  1079. ;;
  1080. 17)
  1081. ssl_cert_issue_CF
  1082. ;;
  1083. 18)
  1084. iplimit_main
  1085. ;;
  1086. 19)
  1087. warp_cloudflare
  1088. ;;
  1089. 20)
  1090. enable_bbr
  1091. ;;
  1092. 21)
  1093. update_geo
  1094. ;;
  1095. 22)
  1096. open_ports
  1097. ;;
  1098. 23)
  1099. run_speedtest
  1100. ;;
  1101. *)
  1102. LOGE "Please enter the correct number [0-23]"
  1103. ;;
  1104. esac
  1105. }
  1106. if [[ $# > 0 ]]; then
  1107. case $1 in
  1108. "start")
  1109. check_install 0 && start 0
  1110. ;;
  1111. "stop")
  1112. check_install 0 && stop 0
  1113. ;;
  1114. "restart")
  1115. check_install 0 && restart 0
  1116. ;;
  1117. "status")
  1118. check_install 0 && status 0
  1119. ;;
  1120. "enable")
  1121. check_install 0 && enable 0
  1122. ;;
  1123. "disable")
  1124. check_install 0 && disable 0
  1125. ;;
  1126. "log")
  1127. check_install 0 && show_log 0
  1128. ;;
  1129. "banlog")
  1130. check_install 0 && show_banlog 0
  1131. ;;
  1132. "update")
  1133. check_install 0 && update 0
  1134. ;;
  1135. "install")
  1136. check_uninstall 0 && install 0
  1137. ;;
  1138. "uninstall")
  1139. check_install 0 && uninstall 0
  1140. ;;
  1141. *) show_usage ;;
  1142. esac
  1143. else
  1144. show_menu
  1145. fi