x-ui.sh 34 KB

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