1
0

x-ui.sh 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. ssl_cert_issue_CF() {
  584. echo -E ""
  585. LOGD "******Instructions for use******"
  586. LOGI "This Acme script requires the following data:"
  587. LOGI "1.Cloudflare Registered e-mail"
  588. LOGI "2.Cloudflare Global API Key"
  589. LOGI "3.The domain name that has been resolved dns to the current server by Cloudflare"
  590. LOGI "4.The script applies for a certificate. The default installation path is /root/cert "
  591. confirm "Confirmed?[y/n]" "y"
  592. if [ $? -eq 0 ]; then
  593. # check for acme.sh first
  594. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  595. echo "acme.sh could not be found. we will install it"
  596. install_acme
  597. if [ $? -ne 0 ]; then
  598. LOGE "install acme failed, please check logs"
  599. exit 1
  600. fi
  601. fi
  602. CF_Domain=""
  603. CF_GlobalKey=""
  604. CF_AccountEmail=""
  605. certPath=/root/cert
  606. if [ ! -d "$certPath" ]; then
  607. mkdir $certPath
  608. else
  609. rm -rf $certPath
  610. mkdir $certPath
  611. fi
  612. LOGD "Please set a domain name:"
  613. read -p "Input your domain here:" CF_Domain
  614. LOGD "Your domain name is set to:${CF_Domain}"
  615. LOGD "Please set the API key:"
  616. read -p "Input your key here:" CF_GlobalKey
  617. LOGD "Your API key is:${CF_GlobalKey}"
  618. LOGD "Please set up registered email:"
  619. read -p "Input your email here:" CF_AccountEmail
  620. LOGD "Your registered email address is:${CF_AccountEmail}"
  621. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  622. if [ $? -ne 0 ]; then
  623. LOGE "Default CA, Lets'Encrypt fail, script exiting..."
  624. exit 1
  625. fi
  626. export CF_Key="${CF_GlobalKey}"
  627. export CF_Email=${CF_AccountEmail}
  628. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log
  629. if [ $? -ne 0 ]; then
  630. LOGE "Certificate issuance failed, script exiting..."
  631. exit 1
  632. else
  633. LOGI "Certificate issued Successfully, Installing..."
  634. fi
  635. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} --ca-file /root/cert/ca.cer \
  636. --cert-file /root/cert/${CF_Domain}.cer --key-file /root/cert/${CF_Domain}.key \
  637. --fullchain-file /root/cert/fullchain.cer
  638. if [ $? -ne 0 ]; then
  639. LOGE "Certificate installation failed, script exiting..."
  640. exit 1
  641. else
  642. LOGI "Certificate installed Successfully,Turning on automatic updates..."
  643. fi
  644. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  645. if [ $? -ne 0 ]; then
  646. LOGE "Auto update setup Failed, script exiting..."
  647. ls -lah cert
  648. chmod 755 $certPath
  649. exit 1
  650. else
  651. LOGI "The certificate is installed and auto-renewal is turned on, Specific information is as follows"
  652. ls -lah cert
  653. chmod 755 $certPath
  654. fi
  655. else
  656. show_menu
  657. fi
  658. }
  659. warp_cloudflare() {
  660. echo -e "${green}\t1.${plain} Install WARP socks5 proxy"
  661. echo -e "${green}\t2.${plain} Account Type (free, plus, team)"
  662. echo -e "${green}\t3.${plain} Turn on/off WireProxy"
  663. echo -e "${green}\t4.${plain} Uninstall WARP"
  664. read -p "Choose an option: " choice
  665. case "$choice" in
  666. 1)
  667. bash <(curl -sSL https://raw.githubusercontent.com/hamid-gh98/x-ui-scripts/main/install_warp_proxy.sh)
  668. ;;
  669. 2)
  670. warp a
  671. ;;
  672. 3)
  673. warp y
  674. ;;
  675. 4)
  676. warp u
  677. ;;
  678. *) echo "Invalid choice" ;;
  679. esac
  680. }
  681. run_speedtest() {
  682. # Check if Speedtest is already installed
  683. if ! command -v speedtest &> /dev/null; then
  684. # If not installed, install it
  685. local pkg_manager=""
  686. local speedtest_install_script=""
  687. if command -v dnf &> /dev/null; then
  688. pkg_manager="dnf"
  689. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  690. elif command -v yum &> /dev/null; then
  691. pkg_manager="yum"
  692. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  693. elif command -v apt-get &> /dev/null; then
  694. pkg_manager="apt-get"
  695. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  696. elif command -v apt &> /dev/null; then
  697. pkg_manager="apt"
  698. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  699. fi
  700. if [[ -z $pkg_manager ]]; then
  701. echo "Error: Package manager not found. You may need to install Speedtest manually."
  702. return 1
  703. else
  704. curl -s $speedtest_install_script | bash
  705. $pkg_manager install -y speedtest
  706. fi
  707. fi
  708. # Run Speedtest
  709. speedtest
  710. }
  711. create_iplimit_jails() {
  712. # Use default bantime if not passed => 5 minutes
  713. local bantime="${1:-5}"
  714. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  715. [3x-ipl]
  716. enabled=true
  717. filter=3x-ipl
  718. action=3x-ipl
  719. logpath=${iplimit_log_path}
  720. maxretry=4
  721. findtime=60
  722. bantime=${bantime}m
  723. EOF
  724. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  725. [Definition]
  726. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  727. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  728. ignoreregex =
  729. EOF
  730. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  731. [INCLUDES]
  732. before = iptables-common.conf
  733. [Definition]
  734. actionstart = <iptables> -N f2b-<name>
  735. <iptables> -A f2b-<name> -j <returntype>
  736. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  737. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  738. <actionflush>
  739. <iptables> -X f2b-<name>
  740. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  741. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  742. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  743. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  744. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  745. [Init]
  746. EOF
  747. echo -e "${green}Created Ip Limit jail files with a bantime of ${bantime} minutes.${plain}"
  748. }
  749. iplimit_remove_conflicts() {
  750. local jail_files=(
  751. /etc/fail2ban/jail.conf
  752. /etc/fail2ban/jail.local
  753. )
  754. for file in "${jail_files[@]}"; do
  755. # Check for [3x-ipl] config in jail file then remove it
  756. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  757. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  758. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  759. fi
  760. done
  761. }
  762. iplimit_main() {
  763. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  764. echo -e "${green}\t2.${plain} Change Ban Duration"
  765. echo -e "${green}\t3.${plain} Unban Everyone"
  766. echo -e "${green}\t4.${plain} Check Logs"
  767. echo -e "${green}\t5.${plain} fail2ban status"
  768. echo -e "${green}\t6.${plain} Uninstall IP Limit"
  769. echo -e "${green}\t0.${plain} Back to Main Menu"
  770. read -p "Choose an option: " choice
  771. case "$choice" in
  772. 0)
  773. show_menu ;;
  774. 1)
  775. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  776. if [[ $? == 0 ]]; then
  777. install_iplimit
  778. else
  779. iplimit_main
  780. fi ;;
  781. 2)
  782. read -rp "Please enter new Ban Duration in Minutes [default 5]: " NUM
  783. if [[ $NUM =~ ^[0-9]+$ ]]; then
  784. create_iplimit_jails ${NUM}
  785. systemctl restart fail2ban
  786. else
  787. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  788. fi
  789. iplimit_main ;;
  790. 3)
  791. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  792. if [[ $? == 0 ]]; then
  793. fail2ban-client reload --restart --unban 3x-ipl
  794. echo -e "${green}All users Unbanned successfully.${plain}"
  795. iplimit_main
  796. else
  797. echo -e "${yellow}Cancelled.${plain}"
  798. fi
  799. iplimit_main ;;
  800. 4)
  801. if test -f "${iplimit_banned_log_path}"; then
  802. if [[ -s "${iplimit_banned_log_path}" ]]; then
  803. cat ${iplimit_banned_log_path}
  804. else
  805. echo -e "${red}Log file is empty.${plain}\n"
  806. fi
  807. else
  808. echo -e "${red}Log file not found. Please Install Fail2ban and IP Limit first.${plain}\n"
  809. iplimit_main
  810. fi ;;
  811. 5)
  812. service fail2ban status
  813. ;;
  814. 6)
  815. remove_iplimit ;;
  816. *) echo "Invalid choice" ;;
  817. esac
  818. }
  819. install_iplimit() {
  820. if ! command -v fail2ban-client &>/dev/null; then
  821. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  822. # Check the OS and install necessary packages
  823. case "${release}" in
  824. ubuntu|debian)
  825. apt update && apt install fail2ban -y ;;
  826. centos)
  827. yum -y update && yum -y install fail2ban ;;
  828. fedora)
  829. dnf -y update && dnf -y install fail2ban ;;
  830. *)
  831. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  832. exit 1 ;;
  833. esac
  834. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  835. else
  836. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  837. fi
  838. echo -e "${green}Configuring IP Limit...${plain}\n"
  839. # make sure there's no conflict for jail files
  840. iplimit_remove_conflicts
  841. # Check if log file exists
  842. if ! test -f "${iplimit_banned_log_path}"; then
  843. touch ${iplimit_banned_log_path}
  844. fi
  845. # Check if service log file exists so fail2ban won't return error
  846. if ! test -f "${iplimit_log_path}"; then
  847. touch ${iplimit_log_path}
  848. fi
  849. # Create the iplimit jail files
  850. # we didn't pass the bantime here to use the default value
  851. create_iplimit_jails
  852. # Launching fail2ban
  853. if ! systemctl is-active --quiet fail2ban; then
  854. systemctl start fail2ban
  855. else
  856. systemctl restart fail2ban
  857. fi
  858. systemctl enable fail2ban
  859. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  860. before_show_menu
  861. }
  862. remove_iplimit(){
  863. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  864. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  865. echo -e "${green}\t0.${plain} Abort"
  866. read -p "Choose an option: " num
  867. case "$num" in
  868. 1)
  869. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  870. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  871. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  872. systemctl restart fail2ban
  873. echo -e "${green}IP Limit removed successfully!${plain}\n"
  874. before_show_menu ;;
  875. 2)
  876. rm -rf /etc/fail2ban
  877. systemctl stop fail2ban
  878. case "${release}" in
  879. ubuntu|debian)
  880. apt-get purge fail2ban -y;;
  881. centos)
  882. yum remove fail2ban -y;;
  883. fedora)
  884. dnf remove fail2ban -y;;
  885. *)
  886. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  887. exit 1 ;;
  888. esac
  889. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  890. before_show_menu ;;
  891. 0)
  892. echo -e "${yellow}Cancelled.${plain}\n"
  893. iplimit_main ;;
  894. *)
  895. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  896. remove_iplimit ;;
  897. esac
  898. }
  899. show_usage() {
  900. echo "x-ui control menu usages: "
  901. echo "------------------------------------------"
  902. echo -e "x-ui - Enter control menu"
  903. echo -e "x-ui start - Start x-ui "
  904. echo -e "x-ui stop - Stop x-ui "
  905. echo -e "x-ui restart - Restart x-ui "
  906. echo -e "x-ui status - Show x-ui status"
  907. echo -e "x-ui enable - Enable x-ui on system startup"
  908. echo -e "x-ui disable - Disable x-ui on system startup"
  909. echo -e "x-ui log - Check x-ui logs"
  910. echo -e "x-ui update - Update x-ui "
  911. echo -e "x-ui install - Install x-ui "
  912. echo -e "x-ui uninstall - Uninstall x-ui "
  913. echo "------------------------------------------"
  914. }
  915. show_menu() {
  916. echo -e "
  917. ${green}3X-ui Panel Management Script${plain}
  918. ${green}0.${plain} Exit Script
  919. ————————————————
  920. ${green}1.${plain} Install x-ui
  921. ${green}2.${plain} Update x-ui
  922. ${green}3.${plain} Uninstall x-ui
  923. ————————————————
  924. ${green}4.${plain} Reset Username & Password & Secret Token
  925. ${green}5.${plain} Reset Panel Settings
  926. ${green}6.${plain} Change Panel Port
  927. ${green}7.${plain} View Current Panel Settings
  928. ————————————————
  929. ${green}8.${plain} Start x-ui
  930. ${green}9.${plain} Stop x-ui
  931. ${green}10.${plain} Restart x-ui
  932. ${green}11.${plain} Check x-ui Status
  933. ${green}12.${plain} Check x-ui Logs
  934. ————————————————
  935. ${green}13.${plain} Enable x-ui On System Startup
  936. ${green}14.${plain} Disable x-ui On System Startup
  937. ————————————————
  938. ${green}15.${plain} SSL Certificate Management
  939. ${green}16.${plain} Cloudflare SSL Certificate
  940. ${green}17.${plain} IP Limit Management
  941. ${green}18.${plain} WARP Management
  942. ————————————————
  943. ${green}19.${plain} Enable BBR
  944. ${green}20.${plain} Update Geo Files
  945. ${green}21.${plain} Active Firewall and open ports
  946. ${green}22.${plain} Speedtest by Ookla
  947. "
  948. show_status
  949. echo && read -p "Please enter your selection [0-22]: " num
  950. case "${num}" in
  951. 0)
  952. exit 0
  953. ;;
  954. 1)
  955. check_uninstall && install
  956. ;;
  957. 2)
  958. check_install && update
  959. ;;
  960. 3)
  961. check_install && uninstall
  962. ;;
  963. 4)
  964. check_install && reset_user
  965. ;;
  966. 5)
  967. check_install && reset_config
  968. ;;
  969. 6)
  970. check_install && set_port
  971. ;;
  972. 7)
  973. check_install && check_config
  974. ;;
  975. 8)
  976. check_install && start
  977. ;;
  978. 9)
  979. check_install && stop
  980. ;;
  981. 10)
  982. check_install && restart
  983. ;;
  984. 11)
  985. check_install && status
  986. ;;
  987. 12)
  988. check_install && show_log
  989. ;;
  990. 13)
  991. check_install && enable
  992. ;;
  993. 14)
  994. check_install && disable
  995. ;;
  996. 15)
  997. ssl_cert_issue_main
  998. ;;
  999. 16)
  1000. ssl_cert_issue_CF
  1001. ;;
  1002. 17)
  1003. iplimit_main
  1004. ;;
  1005. 18)
  1006. warp_cloudflare
  1007. ;;
  1008. 19)
  1009. enable_bbr
  1010. ;;
  1011. 20)
  1012. update_geo
  1013. ;;
  1014. 21)
  1015. open_ports
  1016. ;;
  1017. 22)
  1018. run_speedtest
  1019. ;;
  1020. *)
  1021. LOGE "Please enter the correct number [0-22]"
  1022. ;;
  1023. esac
  1024. }
  1025. if [[ $# > 0 ]]; then
  1026. case $1 in
  1027. "start")
  1028. check_install 0 && start 0
  1029. ;;
  1030. "stop")
  1031. check_install 0 && stop 0
  1032. ;;
  1033. "restart")
  1034. check_install 0 && restart 0
  1035. ;;
  1036. "status")
  1037. check_install 0 && status 0
  1038. ;;
  1039. "enable")
  1040. check_install 0 && enable 0
  1041. ;;
  1042. "disable")
  1043. check_install 0 && disable 0
  1044. ;;
  1045. "log")
  1046. check_install 0 && show_log 0
  1047. ;;
  1048. "update")
  1049. check_install 0 && update 0
  1050. ;;
  1051. "install")
  1052. check_uninstall 0 && install 0
  1053. ;;
  1054. "uninstall")
  1055. check_install 0 && uninstall 0
  1056. ;;
  1057. *) show_usage ;;
  1058. esac
  1059. else
  1060. show_menu
  1061. fi