x-ui.sh 35 KB

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