x-ui.sh 35 KB

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