x-ui.sh 34 KB

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