1
0

x-ui.sh 34 KB

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