x-ui.sh 35 KB

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