x-ui.sh 36 KB

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