x-ui.sh 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. enable_bbr() {
  266. if grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf && grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  267. echo -e "${green}BBR is already enabled!${plain}"
  268. exit 0
  269. fi
  270. # Check the OS and install necessary packages
  271. case "${release}" in
  272. ubuntu|debian)
  273. apt-get update && apt-get install -yqq --no-install-recommends ca-certificates
  274. ;;
  275. centos)
  276. yum -y update && yum -y install ca-certificates
  277. ;;
  278. fedora)
  279. dnf -y update && dnf -y install ca-certificates
  280. ;;
  281. *)
  282. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  283. exit 1
  284. ;;
  285. esac
  286. # Enable BBR
  287. echo "net.core.default_qdisc=fq" | tee -a /etc/sysctl.conf
  288. echo "net.ipv4.tcp_congestion_control=bbr" | tee -a /etc/sysctl.conf
  289. # Apply changes
  290. sysctl -p
  291. # Verify that BBR is enabled
  292. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "bbr" ]]; then
  293. echo -e "${green}BBR has been enabled successfully.${plain}"
  294. else
  295. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  296. fi
  297. }
  298. update_shell() {
  299. wget -O /usr/bin/x-ui -N --no-check-certificate https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  300. if [[ $? != 0 ]]; then
  301. echo ""
  302. LOGE "Failed to download script, Please check whether the machine can connect Github"
  303. before_show_menu
  304. else
  305. chmod +x /usr/bin/x-ui
  306. LOGI "Upgrade script succeeded, Please rerun the script" && exit 0
  307. fi
  308. }
  309. # 0: running, 1: not running, 2: not installed
  310. check_status() {
  311. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  312. return 2
  313. fi
  314. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  315. if [[ "${temp}" == "running" ]]; then
  316. return 0
  317. else
  318. return 1
  319. fi
  320. }
  321. check_enabled() {
  322. temp=$(systemctl is-enabled x-ui)
  323. if [[ "${temp}" == "enabled" ]]; then
  324. return 0
  325. else
  326. return 1
  327. fi
  328. }
  329. check_uninstall() {
  330. check_status
  331. if [[ $? != 2 ]]; then
  332. echo ""
  333. LOGE "Panel installed, Please do not reinstall"
  334. if [[ $# == 0 ]]; then
  335. before_show_menu
  336. fi
  337. return 1
  338. else
  339. return 0
  340. fi
  341. }
  342. check_install() {
  343. check_status
  344. if [[ $? == 2 ]]; then
  345. echo ""
  346. LOGE "Please install the panel first"
  347. if [[ $# == 0 ]]; then
  348. before_show_menu
  349. fi
  350. return 1
  351. else
  352. return 0
  353. fi
  354. }
  355. show_status() {
  356. check_status
  357. case $? in
  358. 0)
  359. echo -e "Panel state: ${green}Running${plain}"
  360. show_enable_status
  361. ;;
  362. 1)
  363. echo -e "Panel state: ${yellow}Not Running${plain}"
  364. show_enable_status
  365. ;;
  366. 2)
  367. echo -e "Panel state: ${red}Not Installed${plain}"
  368. ;;
  369. esac
  370. show_xray_status
  371. }
  372. show_enable_status() {
  373. check_enabled
  374. if [[ $? == 0 ]]; then
  375. echo -e "Start automatically: ${green}Yes${plain}"
  376. else
  377. echo -e "Start automatically: ${red}No${plain}"
  378. fi
  379. }
  380. check_xray_status() {
  381. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  382. if [[ count -ne 0 ]]; then
  383. return 0
  384. else
  385. return 1
  386. fi
  387. }
  388. show_xray_status() {
  389. check_xray_status
  390. if [[ $? == 0 ]]; then
  391. echo -e "xray state: ${green}Running${plain}"
  392. else
  393. echo -e "xray state: ${red}Not Running${plain}"
  394. fi
  395. }
  396. open_ports() {
  397. if ! command -v ufw &>/dev/null; then
  398. echo "ufw firewall is not installed. Installing now..."
  399. apt-get update
  400. apt-get install -y ufw
  401. else
  402. echo "ufw firewall is already installed"
  403. fi
  404. # Check if the firewall is inactive
  405. if ufw status | grep -q "Status: active"; then
  406. echo "firewall is already active"
  407. else
  408. # Open the necessary ports
  409. ufw allow ssh
  410. ufw allow http
  411. ufw allow https
  412. ufw allow 2053/tcp
  413. # Enable the firewall
  414. ufw --force enable
  415. fi
  416. # Prompt the user to enter a list of ports
  417. read -p "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  418. # Check if the input is valid
  419. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  420. 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
  421. exit 1
  422. fi
  423. # Open the specified ports using ufw
  424. IFS=',' read -ra PORT_LIST <<<"$ports"
  425. for port in "${PORT_LIST[@]}"; do
  426. if [[ $port == *-* ]]; then
  427. # Split the range into start and end ports
  428. start_port=$(echo $port | cut -d'-' -f1)
  429. end_port=$(echo $port | cut -d'-' -f2)
  430. # Loop through the range and open each port
  431. for ((i = start_port; i <= end_port; i++)); do
  432. ufw allow $i
  433. done
  434. else
  435. ufw allow "$port"
  436. fi
  437. done
  438. # Confirm that the ports are open
  439. ufw status | grep $ports
  440. }
  441. update_geo() {
  442. local defaultBinFolder="/usr/local/x-ui/bin"
  443. read -p "Please enter x-ui bin folder path. Leave blank for default. (Default: '${defaultBinFolder}')" binFolder
  444. binFolder=${binFolder:-${defaultBinFolder}}
  445. if [[ ! -d ${binFolder} ]]; then
  446. LOGE "Folder ${binFolder} not exists!"
  447. LOGI "making bin folder: ${binFolder}..."
  448. mkdir -p ${binFolder}
  449. fi
  450. systemctl stop x-ui
  451. cd ${binFolder}
  452. rm -f geoip.dat geosite.dat iran.dat
  453. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  454. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  455. wget -N https://github.com/MasterKia/iran-hosted-domains/releases/latest/download/iran.dat
  456. systemctl start x-ui
  457. echo -e "${green}Geosite.dat + Geoip.dat + Iran.dat have been updated successfully in bin folder '${binfolder}'!${plain}"
  458. before_show_menu
  459. }
  460. install_acme() {
  461. cd ~
  462. LOGI "install acme..."
  463. curl https://get.acme.sh | sh
  464. if [ $? -ne 0 ]; then
  465. LOGE "install acme failed"
  466. return 1
  467. else
  468. LOGI "install acme succeed"
  469. fi
  470. return 0
  471. }
  472. ssl_cert_issue_main() {
  473. echo -e "${green}\t1.${plain} Get SSL"
  474. echo -e "${green}\t2.${plain} Revoke"
  475. echo -e "${green}\t3.${plain} Force Renew"
  476. read -p "Choose an option: " choice
  477. case "$choice" in
  478. 1) ssl_cert_issue ;;
  479. 2)
  480. local domain=""
  481. read -p "Please enter your domain name to revoke the certificate: " domain
  482. ~/.acme.sh/acme.sh --revoke -d ${domain}
  483. LOGI "Certificate revoked"
  484. ;;
  485. 3)
  486. local domain=""
  487. read -p "Please enter your domain name to forcefully renew an SSL certificate: " domain
  488. ~/.acme.sh/acme.sh --renew -d ${domain} --force ;;
  489. *) echo "Invalid choice" ;;
  490. esac
  491. }
  492. ssl_cert_issue() {
  493. # check for acme.sh first
  494. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  495. echo "acme.sh could not be found. we will install it"
  496. install_acme
  497. if [ $? -ne 0 ]; then
  498. LOGE "install acme failed, please check logs"
  499. exit 1
  500. fi
  501. fi
  502. # install socat second
  503. case "${release}" in
  504. ubuntu|debian)
  505. apt update && apt install socat -y ;;
  506. centos)
  507. yum -y update && yum -y install socat ;;
  508. fedora)
  509. dnf -y update && dnf -y install socat ;;
  510. *)
  511. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  512. exit 1 ;;
  513. esac
  514. if [ $? -ne 0 ]; then
  515. LOGE "install socat failed, please check logs"
  516. exit 1
  517. else
  518. LOGI "install socat succeed..."
  519. fi
  520. # get the domain here,and we need verify it
  521. local domain=""
  522. read -p "Please enter your domain name:" domain
  523. LOGD "your domain is:${domain},check it..."
  524. # here we need to judge whether there exists cert already
  525. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  526. if [ ${currentCert} == ${domain} ]; then
  527. local certInfo=$(~/.acme.sh/acme.sh --list)
  528. LOGE "system already has certs here,can not issue again,current certs details:"
  529. LOGI "$certInfo"
  530. exit 1
  531. else
  532. LOGI "your domain is ready for issuing cert now..."
  533. fi
  534. # create a directory for install cert
  535. certPath="/root/cert/${domain}"
  536. if [ ! -d "$certPath" ]; then
  537. mkdir -p "$certPath"
  538. else
  539. rm -rf "$certPath"
  540. mkdir -p "$certPath"
  541. fi
  542. # get needed port here
  543. local WebPort=80
  544. read -p "please choose which port do you use,default will be 80 port:" WebPort
  545. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  546. LOGE "your input ${WebPort} is invalid,will use default port"
  547. fi
  548. LOGI "will use port:${WebPort} to issue certs,please make sure this port is open..."
  549. # NOTE:This should be handled by user
  550. # open the port and kill the occupied progress
  551. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  552. ~/.acme.sh/acme.sh --issue -d ${domain} --standalone --httpport ${WebPort}
  553. if [ $? -ne 0 ]; then
  554. LOGE "issue certs failed,please check logs"
  555. rm -rf ~/.acme.sh/${domain}
  556. exit 1
  557. else
  558. LOGE "issue certs succeed,installing certs..."
  559. fi
  560. # install cert
  561. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  562. --key-file /root/cert/${domain}/privkey.pem \
  563. --fullchain-file /root/cert/${domain}/fullchain.pem
  564. if [ $? -ne 0 ]; then
  565. LOGE "install certs failed,exit"
  566. rm -rf ~/.acme.sh/${domain}
  567. exit 1
  568. else
  569. LOGI "install certs succeed,enable auto renew..."
  570. fi
  571. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  572. if [ $? -ne 0 ]; then
  573. LOGE "auto renew failed, certs details:"
  574. ls -lah cert/*
  575. chmod 755 $certPath/*
  576. exit 1
  577. else
  578. LOGI "auto renew succeed, certs details:"
  579. ls -lah cert/*
  580. chmod 755 $certPath/*
  581. fi
  582. }
  583. warp_cloudflare() {
  584. echo -e "${green}\t1.${plain} Install WARP socks5 proxy"
  585. echo -e "${green}\t2.${plain} Account Type (free, plus, team)"
  586. echo -e "${green}\t3.${plain} Turn on/off WireProxy"
  587. echo -e "${green}\t4.${plain} Uninstall WARP"
  588. read -p "Choose an option: " choice
  589. case "$choice" in
  590. 1)
  591. bash <(curl -sSL https://raw.githubusercontent.com/hamid-gh98/x-ui-scripts/main/install_warp_proxy.sh)
  592. ;;
  593. 2)
  594. warp a
  595. ;;
  596. 3)
  597. warp y
  598. ;;
  599. 4)
  600. warp u
  601. ;;
  602. *) echo "Invalid choice" ;;
  603. esac
  604. }
  605. run_speedtest() {
  606. # Check if Speedtest is already installed
  607. if ! command -v speedtest &> /dev/null; then
  608. # If not installed, install it
  609. local pkg_manager=""
  610. local speedtest_install_script=""
  611. if command -v dnf &> /dev/null; then
  612. pkg_manager="dnf"
  613. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  614. elif command -v yum &> /dev/null; then
  615. pkg_manager="yum"
  616. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  617. elif command -v apt-get &> /dev/null; then
  618. pkg_manager="apt-get"
  619. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  620. elif command -v apt &> /dev/null; then
  621. pkg_manager="apt"
  622. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  623. fi
  624. if [[ -z $pkg_manager ]]; then
  625. echo "Error: Package manager not found. You may need to install Speedtest manually."
  626. return 1
  627. else
  628. curl -s $speedtest_install_script | bash
  629. $pkg_manager install -y speedtest
  630. fi
  631. fi
  632. # Run Speedtest
  633. speedtest
  634. }
  635. create_iplimit_jails() {
  636. # Use default bantime if not passed => 5 minutes
  637. local bantime="${1:-5}"
  638. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  639. [3x-ipl]
  640. enabled=true
  641. filter=3x-ipl
  642. action=3x-ipl
  643. logpath=${iplimit_log_path}
  644. maxretry=4
  645. findtime=60
  646. bantime=${bantime}m
  647. EOF
  648. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  649. [Definition]
  650. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  651. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  652. ignoreregex =
  653. EOF
  654. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  655. [INCLUDES]
  656. before = iptables-common.conf
  657. [Definition]
  658. actionstart = <iptables> -N f2b-<name>
  659. <iptables> -A f2b-<name> -j <returntype>
  660. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  661. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  662. <actionflush>
  663. <iptables> -X f2b-<name>
  664. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  665. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  666. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  667. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  668. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  669. [Init]
  670. EOF
  671. echo -e "${green}Created Ip Limit jail files with a bantime of ${bantime} minutes.${plain}"
  672. }
  673. iplimit_remove_conflicts() {
  674. local jail_files=(
  675. /etc/fail2ban/jail.conf
  676. /etc/fail2ban/jail.local
  677. )
  678. for file in "${jail_files[@]}"; do
  679. # Check for [3x-ipl] config in jail file then remove it
  680. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  681. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  682. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  683. fi
  684. done
  685. }
  686. iplimit_main() {
  687. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  688. echo -e "${green}\t2.${plain} Change Ban Duration"
  689. echo -e "${green}\t3.${plain} Unban Everyone"
  690. echo -e "${green}\t4.${plain} Check Logs"
  691. echo -e "${green}\t5.${plain} fail2ban status"
  692. echo -e "${green}\t6.${plain} Uninstall IP Limit"
  693. echo -e "${green}\t0.${plain} Back to Main Menu"
  694. read -p "Choose an option: " choice
  695. case "$choice" in
  696. 0)
  697. show_menu ;;
  698. 1)
  699. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  700. if [[ $? == 0 ]]; then
  701. install_iplimit
  702. else
  703. iplimit_main
  704. fi ;;
  705. 2)
  706. read -rp "Please enter new Ban Duration in Minutes [default 5]: " NUM
  707. if [[ $NUM =~ ^[0-9]+$ ]]; then
  708. create_iplimit_jails ${NUM}
  709. systemctl restart fail2ban
  710. else
  711. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  712. fi
  713. iplimit_main ;;
  714. 3)
  715. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  716. if [[ $? == 0 ]]; then
  717. fail2ban-client reload --restart --unban 3x-ipl
  718. echo -e "${green}All users Unbanned successfully.${plain}"
  719. iplimit_main
  720. else
  721. echo -e "${yellow}Cancelled.${plain}"
  722. fi
  723. iplimit_main ;;
  724. 4)
  725. if test -f "${iplimit_banned_log_path}"; then
  726. if [[ -s "${iplimit_banned_log_path}" ]]; then
  727. cat ${iplimit_banned_log_path}
  728. else
  729. echo -e "${red}Log file is empty.${plain}\n"
  730. fi
  731. else
  732. echo -e "${red}Log file not found. Please Install Fail2ban and IP Limit first.${plain}\n"
  733. iplimit_main
  734. fi ;;
  735. 5)
  736. service fail2ban status
  737. ;;
  738. 6)
  739. remove_iplimit ;;
  740. *) echo "Invalid choice" ;;
  741. esac
  742. }
  743. install_iplimit() {
  744. if ! command -v fail2ban-client &>/dev/null; then
  745. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  746. # Check the OS and install necessary packages
  747. case "${release}" in
  748. ubuntu|debian)
  749. apt update && apt install fail2ban -y ;;
  750. centos)
  751. yum -y update && yum -y install fail2ban ;;
  752. fedora)
  753. dnf -y update && dnf -y install fail2ban ;;
  754. *)
  755. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  756. exit 1 ;;
  757. esac
  758. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  759. else
  760. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  761. fi
  762. echo -e "${green}Configuring IP Limit...${plain}\n"
  763. # make sure there's no conflict for jail files
  764. iplimit_remove_conflicts
  765. # Check if log file exists
  766. if ! test -f "${iplimit_banned_log_path}"; then
  767. touch ${iplimit_banned_log_path}
  768. fi
  769. # Check if service log file exists so fail2ban won't return error
  770. if ! test -f "${iplimit_log_path}"; then
  771. touch ${iplimit_log_path}
  772. fi
  773. # Create the iplimit jail files
  774. # we didn't pass the bantime here to use the default value
  775. create_iplimit_jails
  776. # Launching fail2ban
  777. if ! systemctl is-active --quiet fail2ban; then
  778. systemctl start fail2ban
  779. else
  780. systemctl restart fail2ban
  781. fi
  782. systemctl enable fail2ban
  783. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  784. before_show_menu
  785. }
  786. remove_iplimit(){
  787. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  788. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  789. echo -e "${green}\t0.${plain} Abort"
  790. read -p "Choose an option: " num
  791. case "$num" in
  792. 1)
  793. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  794. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  795. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  796. systemctl restart fail2ban
  797. echo -e "${green}IP Limit removed successfully!${plain}\n"
  798. before_show_menu ;;
  799. 2)
  800. rm -rf /etc/fail2ban
  801. systemctl stop fail2ban
  802. case "${release}" in
  803. ubuntu|debian)
  804. apt-get purge fail2ban -y;;
  805. centos)
  806. yum remove fail2ban -y;;
  807. fedora)
  808. dnf remove fail2ban -y;;
  809. *)
  810. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  811. exit 1 ;;
  812. esac
  813. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  814. before_show_menu ;;
  815. 0)
  816. echo -e "${yellow}Cancelled.${plain}\n"
  817. iplimit_main ;;
  818. *)
  819. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  820. remove_iplimit ;;
  821. esac
  822. }
  823. show_usage() {
  824. echo "x-ui control menu usages: "
  825. echo "------------------------------------------"
  826. echo -e "x-ui - Enter control menu"
  827. echo -e "x-ui start - Start x-ui "
  828. echo -e "x-ui stop - Stop x-ui "
  829. echo -e "x-ui restart - Restart x-ui "
  830. echo -e "x-ui status - Show x-ui status"
  831. echo -e "x-ui enable - Enable x-ui on system startup"
  832. echo -e "x-ui disable - Disable x-ui on system startup"
  833. echo -e "x-ui log - Check x-ui logs"
  834. echo -e "x-ui update - Update x-ui "
  835. echo -e "x-ui install - Install x-ui "
  836. echo -e "x-ui uninstall - Uninstall x-ui "
  837. echo "------------------------------------------"
  838. }
  839. show_menu() {
  840. echo -e "
  841. ${green}3X-ui Panel Management Script${plain}
  842. ${green}0.${plain} Exit Script
  843. ————————————————
  844. ${green}1.${plain} Install x-ui
  845. ${green}2.${plain} Update x-ui
  846. ${green}3.${plain} Uninstall x-ui
  847. ————————————————
  848. ${green}4.${plain} Reset Username & Password & Secret Token
  849. ${green}5.${plain} Reset Panel Settings
  850. ${green}6.${plain} Change Panel Port
  851. ${green}7.${plain} View Current Panel Settings
  852. ————————————————
  853. ${green}8.${plain} Start x-ui
  854. ${green}9.${plain} Stop x-ui
  855. ${green}10.${plain} Restart x-ui
  856. ${green}11.${plain} Check x-ui Status
  857. ${green}12.${plain} Check x-ui Logs
  858. ————————————————
  859. ${green}13.${plain} Enable x-ui On System Startup
  860. ${green}14.${plain} Disable x-ui On System Startup
  861. ————————————————
  862. ${green}15.${plain} SSL Certificate Management
  863. ${green}16.${plain} IP Limit Management
  864. ${green}17.${plain} WARP Management
  865. ————————————————
  866. ${green}18.${plain} Enable BBR
  867. ${green}19.${plain} Update Geo Files
  868. ${green}20.${plain} Active Firewall and open ports
  869. ${green}21.${plain} Speedtest by Ookla
  870. "
  871. show_status
  872. echo && read -p "Please enter your selection [0-21]: " num
  873. case "${num}" in
  874. 0)
  875. exit 0
  876. ;;
  877. 1)
  878. check_uninstall && install
  879. ;;
  880. 2)
  881. check_install && update
  882. ;;
  883. 3)
  884. check_install && uninstall
  885. ;;
  886. 4)
  887. check_install && reset_user
  888. ;;
  889. 5)
  890. check_install && reset_config
  891. ;;
  892. 6)
  893. check_install && set_port
  894. ;;
  895. 7)
  896. check_install && check_config
  897. ;;
  898. 8)
  899. check_install && start
  900. ;;
  901. 9)
  902. check_install && stop
  903. ;;
  904. 10)
  905. check_install && restart
  906. ;;
  907. 11)
  908. check_install && status
  909. ;;
  910. 12)
  911. check_install && show_log
  912. ;;
  913. 13)
  914. check_install && enable
  915. ;;
  916. 14)
  917. check_install && disable
  918. ;;
  919. 15)
  920. ssl_cert_issue_main
  921. ;;
  922. 16)
  923. iplimit_main
  924. ;;
  925. 17)
  926. warp_cloudflare
  927. ;;
  928. 18)
  929. enable_bbr
  930. ;;
  931. 19)
  932. update_geo
  933. ;;
  934. 20)
  935. open_ports
  936. ;;
  937. 21)
  938. run_speedtest
  939. ;;
  940. *)
  941. LOGE "Please enter the correct number [0-21]"
  942. ;;
  943. esac
  944. }
  945. if [[ $# > 0 ]]; then
  946. case $1 in
  947. "start")
  948. check_install 0 && start 0
  949. ;;
  950. "stop")
  951. check_install 0 && stop 0
  952. ;;
  953. "restart")
  954. check_install 0 && restart 0
  955. ;;
  956. "status")
  957. check_install 0 && status 0
  958. ;;
  959. "enable")
  960. check_install 0 && enable 0
  961. ;;
  962. "disable")
  963. check_install 0 && disable 0
  964. ;;
  965. "log")
  966. check_install 0 && show_log 0
  967. ;;
  968. "update")
  969. check_install 0 && update 0
  970. ;;
  971. "install")
  972. check_uninstall 0 && install 0
  973. ;;
  974. "uninstall")
  975. check_install 0 && uninstall 0
  976. ;;
  977. *) show_usage ;;
  978. esac
  979. else
  980. show_menu
  981. fi