x-ui.sh 31 KB

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