1
0

x-ui.sh 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  1. #!/bin/bash
  2. red='\033[0;31m'
  3. green='\033[0;32m'
  4. blue='\033[0;34m'
  5. yellow='\033[0;33m'
  6. plain='\033[0m'
  7. #Add some basic function here
  8. function LOGD() {
  9. echo -e "${yellow}[DEG] $* ${plain}"
  10. }
  11. function LOGE() {
  12. echo -e "${red}[ERR] $* ${plain}"
  13. }
  14. function LOGI() {
  15. echo -e "${green}[INF] $* ${plain}"
  16. }
  17. # check root
  18. [[ $EUID -ne 0 ]] && LOGE "ERROR: You must be root to run this script! \n" && exit 1
  19. # Check OS and set release variable
  20. if [[ -f /etc/os-release ]]; then
  21. source /etc/os-release
  22. release=$ID
  23. elif [[ -f /usr/lib/os-release ]]; then
  24. source /usr/lib/os-release
  25. release=$ID
  26. else
  27. echo "Failed to check the system OS, please contact the author!" >&2
  28. exit 1
  29. fi
  30. echo "The OS release is: $release"
  31. os_version=""
  32. os_version=$(grep "^VERSION_ID" /etc/os-release | cut -d '=' -f2 | tr -d '"' | tr -d '.')
  33. # Declare Variables
  34. log_folder="${XUI_LOG_FOLDER:=/var/log}"
  35. iplimit_log_path="${log_folder}/3xipl.log"
  36. iplimit_banned_log_path="${log_folder}/3xipl-banned.log"
  37. confirm() {
  38. if [[ $# > 1 ]]; then
  39. echo && read -rp "$1 [Default $2]: " temp
  40. if [[ "${temp}" == "" ]]; then
  41. temp=$2
  42. fi
  43. else
  44. read -rp "$1 [y/n]: " temp
  45. fi
  46. if [[ "${temp}" == "y" || "${temp}" == "Y" ]]; then
  47. return 0
  48. else
  49. return 1
  50. fi
  51. }
  52. confirm_restart() {
  53. confirm "Restart the panel, Attention: Restarting the panel will also restart xray" "y"
  54. if [[ $? == 0 ]]; then
  55. restart
  56. else
  57. show_menu
  58. fi
  59. }
  60. before_show_menu() {
  61. echo && echo -n -e "${yellow}Press enter to return to the main menu: ${plain}" && read -r temp
  62. show_menu
  63. }
  64. install() {
  65. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  66. if [[ $? == 0 ]]; then
  67. if [[ $# == 0 ]]; then
  68. start
  69. else
  70. start 0
  71. fi
  72. fi
  73. }
  74. update() {
  75. confirm "This function will forcefully reinstall the latest version, and the data will not be lost. Do you want to continue?" "y"
  76. if [[ $? != 0 ]]; then
  77. LOGE "Cancelled"
  78. if [[ $# == 0 ]]; then
  79. before_show_menu
  80. fi
  81. return 0
  82. fi
  83. bash <(curl -Ls https://raw.githubusercontent.com/MHSanaei/3x-ui/main/install.sh)
  84. if [[ $? == 0 ]]; then
  85. LOGI "Update is complete, Panel has automatically restarted "
  86. before_show_menu
  87. fi
  88. }
  89. update_menu() {
  90. echo -e "${yellow}Updating Menu${plain}"
  91. confirm "This function will update the menu to the latest changes." "y"
  92. if [[ $? != 0 ]]; then
  93. LOGE "Cancelled"
  94. if [[ $# == 0 ]]; then
  95. before_show_menu
  96. fi
  97. return 0
  98. fi
  99. wget -O /usr/bin/x-ui https://raw.githubusercontent.com/MHSanaei/3x-ui/main/x-ui.sh
  100. chmod +x /usr/local/x-ui/x-ui.sh
  101. chmod +x /usr/bin/x-ui
  102. if [[ $? == 0 ]]; then
  103. echo -e "${green}Update successful. The panel has automatically restarted.${plain}"
  104. exit 0
  105. else
  106. echo -e "${red}Failed to update the menu.${plain}"
  107. return 1
  108. fi
  109. }
  110. legacy_version() {
  111. echo -n "Enter the panel version (like 2.4.0):"
  112. read -r tag_version
  113. if [ -z "$tag_version" ]; then
  114. echo "Panel version cannot be empty. Exiting."
  115. exit 1
  116. fi
  117. # Use the entered panel version in the download link
  118. install_command="bash <(curl -Ls "https://raw.githubusercontent.com/mhsanaei/3x-ui/v$tag_version/install.sh") v$tag_version"
  119. echo "Downloading and installing panel version $tag_version..."
  120. eval $install_command
  121. }
  122. # Function to handle the deletion of the script file
  123. delete_script() {
  124. rm "$0" # Remove the script file itself
  125. exit 1
  126. }
  127. uninstall() {
  128. confirm "Are you sure you want to uninstall the panel? xray will also uninstalled!" "n"
  129. if [[ $? != 0 ]]; then
  130. if [[ $# == 0 ]]; then
  131. show_menu
  132. fi
  133. return 0
  134. fi
  135. systemctl stop x-ui
  136. systemctl disable x-ui
  137. rm /etc/systemd/system/x-ui.service -f
  138. systemctl daemon-reload
  139. systemctl reset-failed
  140. rm /etc/x-ui/ -rf
  141. rm /usr/local/x-ui/ -rf
  142. echo ""
  143. echo -e "Uninstalled Successfully.\n"
  144. echo "If you need to install this panel again, you can use below command:"
  145. echo -e "${green}bash <(curl -Ls https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh)${plain}"
  146. echo ""
  147. # Trap the SIGTERM signal
  148. trap delete_script SIGTERM
  149. delete_script
  150. }
  151. reset_user() {
  152. confirm "Are you sure to reset the username and password of the panel?" "n"
  153. if [[ $? != 0 ]]; then
  154. if [[ $# == 0 ]]; then
  155. show_menu
  156. fi
  157. return 0
  158. fi
  159. read -rp "Please set the login username [default is a random username]: " config_account
  160. [[ -z $config_account ]] && config_account=$(date +%s%N | md5sum | cut -c 1-8)
  161. read -rp "Please set the login password [default is a random password]: " config_password
  162. [[ -z $config_password ]] && config_password=$(date +%s%N | md5sum | cut -c 1-8)
  163. /usr/local/x-ui/x-ui setting -username ${config_account} -password ${config_password} >/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 "${green} Please use the new login username and password to access the X-UI panel. Also remember them! ${plain}"
  167. confirm_restart
  168. }
  169. gen_random_string() {
  170. local length="$1"
  171. local random_string=$(LC_ALL=C tr -dc 'a-zA-Z0-9' </dev/urandom | fold -w "$length" | head -n 1)
  172. echo "$random_string"
  173. }
  174. reset_webbasepath() {
  175. echo -e "${yellow}Resetting Web Base Path${plain}"
  176. read -rp "Are you sure you want to reset the web base path? (y/n): " confirm
  177. if [[ $confirm != "y" && $confirm != "Y" ]]; then
  178. echo -e "${yellow}Operation canceled.${plain}"
  179. return
  180. fi
  181. config_webBasePath=$(gen_random_string 10)
  182. # Apply the new web base path setting
  183. /usr/local/x-ui/x-ui setting -webBasePath "${config_webBasePath}" >/dev/null 2>&1
  184. echo -e "Web base path has been reset to: ${green}${config_webBasePath}${plain}"
  185. echo -e "${green}Please use the new web base path to access the panel.${plain}"
  186. restart
  187. }
  188. reset_config() {
  189. confirm "Are you sure you want to reset all panel settings, Account data will not be lost, Username and password will not change" "n"
  190. if [[ $? != 0 ]]; then
  191. if [[ $# == 0 ]]; then
  192. show_menu
  193. fi
  194. return 0
  195. fi
  196. /usr/local/x-ui/x-ui setting -reset
  197. echo -e "All panel settings have been reset to default."
  198. restart
  199. }
  200. check_config() {
  201. local info=$(/usr/local/x-ui/x-ui setting -show true)
  202. if [[ $? != 0 ]]; then
  203. LOGE "get current settings error, please check logs"
  204. show_menu
  205. return
  206. fi
  207. LOGI "${info}"
  208. local existing_webBasePath=$(echo "$info" | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  209. local existing_port=$(echo "$info" | grep -Eo 'port: .+' | awk '{print $2}')
  210. local existing_cert=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}')
  211. local server_ip=$(curl -s https://api.ipify.org)
  212. if [[ -n "$existing_cert" ]]; then
  213. local domain=$(basename "$(dirname "$existing_cert")")
  214. if [[ "$domain" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
  215. echo -e "${green}Access URL: https://${domain}:${existing_port}${existing_webBasePath}${plain}"
  216. else
  217. echo -e "${green}Access URL: https://${server_ip}:${existing_port}${existing_webBasePath}${plain}"
  218. fi
  219. else
  220. echo -e "${green}Access URL: http://${server_ip}:${existing_port}${existing_webBasePath}${plain}"
  221. fi
  222. }
  223. set_port() {
  224. echo -n "Enter port number[1-65535]: "
  225. read -r port
  226. if [[ -z "${port}" ]]; then
  227. LOGD "Cancelled"
  228. before_show_menu
  229. else
  230. /usr/local/x-ui/x-ui setting -port ${port}
  231. echo -e "The port is set, Please restart the panel now, and use the new port ${green}${port}${plain} to access web panel"
  232. confirm_restart
  233. fi
  234. }
  235. start() {
  236. check_status
  237. if [[ $? == 0 ]]; then
  238. echo ""
  239. LOGI "Panel is running, No need to start again, If you need to restart, please select restart"
  240. else
  241. systemctl start x-ui
  242. sleep 2
  243. check_status
  244. if [[ $? == 0 ]]; then
  245. LOGI "x-ui Started Successfully"
  246. else
  247. LOGE "panel Failed to start, Probably because it takes longer than two seconds to start, Please check the log information later"
  248. fi
  249. fi
  250. if [[ $# == 0 ]]; then
  251. before_show_menu
  252. fi
  253. }
  254. stop() {
  255. check_status
  256. if [[ $? == 1 ]]; then
  257. echo ""
  258. LOGI "Panel stopped, No need to stop again!"
  259. else
  260. systemctl stop x-ui
  261. sleep 2
  262. check_status
  263. if [[ $? == 1 ]]; then
  264. LOGI "x-ui and xray stopped successfully"
  265. else
  266. LOGE "Panel stop failed, Probably because the stop time exceeds two seconds, Please check the log information later"
  267. fi
  268. fi
  269. if [[ $# == 0 ]]; then
  270. before_show_menu
  271. fi
  272. }
  273. restart() {
  274. systemctl restart x-ui
  275. sleep 2
  276. check_status
  277. if [[ $? == 0 ]]; then
  278. LOGI "x-ui and xray Restarted successfully"
  279. else
  280. LOGE "Panel restart failed, Probably because it takes longer than two seconds to start, Please check the log information later"
  281. fi
  282. if [[ $# == 0 ]]; then
  283. before_show_menu
  284. fi
  285. }
  286. status() {
  287. systemctl status x-ui -l
  288. if [[ $# == 0 ]]; then
  289. before_show_menu
  290. fi
  291. }
  292. enable() {
  293. systemctl enable x-ui
  294. if [[ $? == 0 ]]; then
  295. LOGI "x-ui Set to boot automatically on startup successfully"
  296. else
  297. LOGE "x-ui Failed to set Autostart"
  298. fi
  299. if [[ $# == 0 ]]; then
  300. before_show_menu
  301. fi
  302. }
  303. disable() {
  304. systemctl disable x-ui
  305. if [[ $? == 0 ]]; then
  306. LOGI "x-ui Autostart Cancelled successfully"
  307. else
  308. LOGE "x-ui Failed to cancel autostart"
  309. fi
  310. if [[ $# == 0 ]]; then
  311. before_show_menu
  312. fi
  313. }
  314. show_log() {
  315. echo -e "${green}\t1.${plain} Debug Log"
  316. echo -e "${green}\t2.${plain} Clear All logs"
  317. echo -e "${green}\t0.${plain} Back to Main Menu"
  318. read -rp "Choose an option: " choice
  319. case "$choice" in
  320. 0)
  321. show_menu
  322. ;;
  323. 1)
  324. journalctl -u x-ui -e --no-pager -f -p debug
  325. if [[ $# == 0 ]]; then
  326. before_show_menu
  327. fi
  328. ;;
  329. 2)
  330. sudo journalctl --rotate
  331. sudo journalctl --vacuum-time=1s
  332. echo "All Logs cleared."
  333. restart
  334. ;;
  335. *)
  336. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  337. show_log
  338. ;;
  339. esac
  340. }
  341. show_banlog() {
  342. local system_log="/var/log/fail2ban.log"
  343. echo -e "${green}Checking ban logs...${plain}\n"
  344. if ! systemctl is-active --quiet fail2ban; then
  345. echo -e "${red}Fail2ban service is not running!${plain}\n"
  346. return 1
  347. fi
  348. if [[ -f "$system_log" ]]; then
  349. echo -e "${green}Recent system ban activities from fail2ban.log:${plain}"
  350. grep "3x-ipl" "$system_log" | grep -E "Ban|Unban" | tail -n 10 || echo -e "${yellow}No recent system ban activities found${plain}"
  351. echo ""
  352. fi
  353. if [[ -f "${iplimit_banned_log_path}" ]]; then
  354. echo -e "${green}3X-IPL ban log entries:${plain}"
  355. if [[ -s "${iplimit_banned_log_path}" ]]; then
  356. grep -v "INIT" "${iplimit_banned_log_path}" | tail -n 10 || echo -e "${yellow}No ban entries found${plain}"
  357. else
  358. echo -e "${yellow}Ban log file is empty${plain}"
  359. fi
  360. else
  361. echo -e "${red}Ban log file not found at: ${iplimit_banned_log_path}${plain}"
  362. fi
  363. echo -e "\n${green}Current jail status:${plain}"
  364. fail2ban-client status 3x-ipl || echo -e "${yellow}Unable to get jail status${plain}"
  365. }
  366. bbr_menu() {
  367. echo -e "${green}\t1.${plain} Enable BBR"
  368. echo -e "${green}\t2.${plain} Disable BBR"
  369. echo -e "${green}\t0.${plain} Back to Main Menu"
  370. read -rp "Choose an option: " choice
  371. case "$choice" in
  372. 0)
  373. show_menu
  374. ;;
  375. 1)
  376. enable_bbr
  377. bbr_menu
  378. ;;
  379. 2)
  380. disable_bbr
  381. bbr_menu
  382. ;;
  383. *)
  384. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  385. bbr_menu
  386. ;;
  387. esac
  388. }
  389. disable_bbr() {
  390. if ! grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf || ! grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  391. echo -e "${yellow}BBR is not currently enabled.${plain}"
  392. before_show_menu
  393. fi
  394. # Replace BBR with CUBIC configurations
  395. sed -i 's/net.core.default_qdisc=fq/net.core.default_qdisc=pfifo_fast/' /etc/sysctl.conf
  396. sed -i 's/net.ipv4.tcp_congestion_control=bbr/net.ipv4.tcp_congestion_control=cubic/' /etc/sysctl.conf
  397. # Apply changes
  398. sysctl -p
  399. # Verify that BBR is replaced with CUBIC
  400. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "cubic" ]]; then
  401. echo -e "${green}BBR has been replaced with CUBIC successfully.${plain}"
  402. else
  403. echo -e "${red}Failed to replace BBR with CUBIC. Please check your system configuration.${plain}"
  404. fi
  405. }
  406. enable_bbr() {
  407. if grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf && grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then
  408. echo -e "${green}BBR is already enabled!${plain}"
  409. before_show_menu
  410. fi
  411. # Check the OS and install necessary packages
  412. case "${release}" in
  413. ubuntu | debian | armbian)
  414. apt-get update && apt-get install -yqq --no-install-recommends ca-certificates
  415. ;;
  416. centos | almalinux | rocky | ol)
  417. yum -y update && yum -y install ca-certificates
  418. ;;
  419. fedora | amzn | virtuozzo)
  420. dnf -y update && dnf -y install ca-certificates
  421. ;;
  422. arch | manjaro | parch)
  423. pacman -Sy --noconfirm ca-certificates
  424. ;;
  425. *)
  426. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  427. exit 1
  428. ;;
  429. esac
  430. # Enable BBR
  431. echo "net.core.default_qdisc=fq" | tee -a /etc/sysctl.conf
  432. echo "net.ipv4.tcp_congestion_control=bbr" | tee -a /etc/sysctl.conf
  433. # Apply changes
  434. sysctl -p
  435. # Verify that BBR is enabled
  436. if [[ $(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}') == "bbr" ]]; then
  437. echo -e "${green}BBR has been enabled successfully.${plain}"
  438. else
  439. echo -e "${red}Failed to enable BBR. Please check your system configuration.${plain}"
  440. fi
  441. }
  442. update_shell() {
  443. wget -O /usr/bin/x-ui -N https://github.com/MHSanaei/3x-ui/raw/main/x-ui.sh
  444. if [[ $? != 0 ]]; then
  445. echo ""
  446. LOGE "Failed to download script, Please check whether the machine can connect Github"
  447. before_show_menu
  448. else
  449. chmod +x /usr/bin/x-ui
  450. LOGI "Upgrade script succeeded, Please rerun the script"
  451. before_show_menu
  452. fi
  453. }
  454. # 0: running, 1: not running, 2: not installed
  455. check_status() {
  456. if [[ ! -f /etc/systemd/system/x-ui.service ]]; then
  457. return 2
  458. fi
  459. temp=$(systemctl status x-ui | grep Active | awk '{print $3}' | cut -d "(" -f2 | cut -d ")" -f1)
  460. if [[ "${temp}" == "running" ]]; then
  461. return 0
  462. else
  463. return 1
  464. fi
  465. }
  466. check_enabled() {
  467. temp=$(systemctl is-enabled x-ui)
  468. if [[ "${temp}" == "enabled" ]]; then
  469. return 0
  470. else
  471. return 1
  472. fi
  473. }
  474. check_uninstall() {
  475. check_status
  476. if [[ $? != 2 ]]; then
  477. echo ""
  478. LOGE "Panel installed, Please do not reinstall"
  479. if [[ $# == 0 ]]; then
  480. before_show_menu
  481. fi
  482. return 1
  483. else
  484. return 0
  485. fi
  486. }
  487. check_install() {
  488. check_status
  489. if [[ $? == 2 ]]; then
  490. echo ""
  491. LOGE "Please install the panel first"
  492. if [[ $# == 0 ]]; then
  493. before_show_menu
  494. fi
  495. return 1
  496. else
  497. return 0
  498. fi
  499. }
  500. show_status() {
  501. check_status
  502. case $? in
  503. 0)
  504. echo -e "Panel state: ${green}Running${plain}"
  505. show_enable_status
  506. ;;
  507. 1)
  508. echo -e "Panel state: ${yellow}Not Running${plain}"
  509. show_enable_status
  510. ;;
  511. 2)
  512. echo -e "Panel state: ${red}Not Installed${plain}"
  513. ;;
  514. esac
  515. show_xray_status
  516. }
  517. show_enable_status() {
  518. check_enabled
  519. if [[ $? == 0 ]]; then
  520. echo -e "Start automatically: ${green}Yes${plain}"
  521. else
  522. echo -e "Start automatically: ${red}No${plain}"
  523. fi
  524. }
  525. check_xray_status() {
  526. count=$(ps -ef | grep "xray-linux" | grep -v "grep" | wc -l)
  527. if [[ count -ne 0 ]]; then
  528. return 0
  529. else
  530. return 1
  531. fi
  532. }
  533. show_xray_status() {
  534. check_xray_status
  535. if [[ $? == 0 ]]; then
  536. echo -e "xray state: ${green}Running${plain}"
  537. else
  538. echo -e "xray state: ${red}Not Running${plain}"
  539. fi
  540. }
  541. firewall_menu() {
  542. echo -e "${green}\t1.${plain} ${green}Install${plain} Firewall"
  543. echo -e "${green}\t2.${plain} Port List [numbered]"
  544. echo -e "${green}\t3.${plain} ${green}Open${plain} Ports"
  545. echo -e "${green}\t4.${plain} ${red}Delete${plain} Ports from List"
  546. echo -e "${green}\t5.${plain} ${green}Enable${plain} Firewall"
  547. echo -e "${green}\t6.${plain} ${red}Disable${plain} Firewall"
  548. echo -e "${green}\t7.${plain} Firewall Status"
  549. echo -e "${green}\t0.${plain} Back to Main Menu"
  550. read -rp "Choose an option: " choice
  551. case "$choice" in
  552. 0)
  553. show_menu
  554. ;;
  555. 1)
  556. install_firewall
  557. firewall_menu
  558. ;;
  559. 2)
  560. ufw status numbered
  561. firewall_menu
  562. ;;
  563. 3)
  564. open_ports
  565. firewall_menu
  566. ;;
  567. 4)
  568. delete_ports
  569. firewall_menu
  570. ;;
  571. 5)
  572. ufw enable
  573. firewall_menu
  574. ;;
  575. 6)
  576. ufw disable
  577. firewall_menu
  578. ;;
  579. 7)
  580. ufw status verbose
  581. firewall_menu
  582. ;;
  583. *)
  584. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  585. firewall_menu
  586. ;;
  587. esac
  588. }
  589. install_firewall() {
  590. if ! command -v ufw &>/dev/null; then
  591. echo "ufw firewall is not installed. Installing now..."
  592. apt-get update
  593. apt-get install -y ufw
  594. else
  595. echo "ufw firewall is already installed"
  596. fi
  597. # Check if the firewall is inactive
  598. if ufw status | grep -q "Status: active"; then
  599. echo "Firewall is already active"
  600. else
  601. echo "Activating firewall..."
  602. # Open the necessary ports
  603. ufw allow ssh
  604. ufw allow http
  605. ufw allow https
  606. ufw allow 2053/tcp #webPort
  607. ufw allow 2096/tcp #subport
  608. # Enable the firewall
  609. ufw --force enable
  610. fi
  611. }
  612. open_ports() {
  613. # Prompt the user to enter the ports they want to open
  614. read -rp "Enter the ports you want to open (e.g. 80,443,2053 or range 400-500): " ports
  615. # Check if the input is valid
  616. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  617. 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
  618. exit 1
  619. fi
  620. # Open the specified ports using ufw
  621. IFS=',' read -ra PORT_LIST <<<"$ports"
  622. for port in "${PORT_LIST[@]}"; do
  623. if [[ $port == *-* ]]; then
  624. # Split the range into start and end ports
  625. start_port=$(echo $port | cut -d'-' -f1)
  626. end_port=$(echo $port | cut -d'-' -f2)
  627. # Open the port range
  628. ufw allow $start_port:$end_port/tcp
  629. ufw allow $start_port:$end_port/udp
  630. else
  631. # Open the single port
  632. ufw allow "$port"
  633. fi
  634. done
  635. # Confirm that the ports are opened
  636. echo "Opened the specified ports:"
  637. for port in "${PORT_LIST[@]}"; do
  638. if [[ $port == *-* ]]; then
  639. start_port=$(echo $port | cut -d'-' -f1)
  640. end_port=$(echo $port | cut -d'-' -f2)
  641. # Check if the port range has been successfully opened
  642. (ufw status | grep -q "$start_port:$end_port") && echo "$start_port-$end_port"
  643. else
  644. # Check if the individual port has been successfully opened
  645. (ufw status | grep -q "$port") && echo "$port"
  646. fi
  647. done
  648. }
  649. delete_ports() {
  650. # Display current rules with numbers
  651. echo "Current UFW rules:"
  652. ufw status numbered
  653. # Ask the user how they want to delete rules
  654. echo "Do you want to delete rules by:"
  655. echo "1) Rule numbers"
  656. echo "2) Ports"
  657. read -rp "Enter your choice (1 or 2): " choice
  658. if [[ $choice -eq 1 ]]; then
  659. # Deleting by rule numbers
  660. read -rp "Enter the rule numbers you want to delete (1, 2, etc.): " rule_numbers
  661. # Validate the input
  662. if ! [[ $rule_numbers =~ ^([0-9]+)(,[0-9]+)*$ ]]; then
  663. echo "Error: Invalid input. Please enter a comma-separated list of rule numbers." >&2
  664. exit 1
  665. fi
  666. # Split numbers into an array
  667. IFS=',' read -ra RULE_NUMBERS <<<"$rule_numbers"
  668. for rule_number in "${RULE_NUMBERS[@]}"; do
  669. # Delete the rule by number
  670. ufw delete "$rule_number" || echo "Failed to delete rule number $rule_number"
  671. done
  672. echo "Selected rules have been deleted."
  673. elif [[ $choice -eq 2 ]]; then
  674. # Deleting by ports
  675. read -rp "Enter the ports you want to delete (e.g. 80,443,2053 or range 400-500): " ports
  676. # Validate the input
  677. if ! [[ $ports =~ ^([0-9]+|[0-9]+-[0-9]+)(,([0-9]+|[0-9]+-[0-9]+))*$ ]]; then
  678. 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
  679. exit 1
  680. fi
  681. # Split ports into an array
  682. IFS=',' read -ra PORT_LIST <<<"$ports"
  683. for port in "${PORT_LIST[@]}"; do
  684. if [[ $port == *-* ]]; then
  685. # Split the port range
  686. start_port=$(echo $port | cut -d'-' -f1)
  687. end_port=$(echo $port | cut -d'-' -f2)
  688. # Delete the port range
  689. ufw delete allow $start_port:$end_port/tcp
  690. ufw delete allow $start_port:$end_port/udp
  691. else
  692. # Delete a single port
  693. ufw delete allow "$port"
  694. fi
  695. done
  696. # Confirmation of deletion
  697. echo "Deleted the specified ports:"
  698. for port in "${PORT_LIST[@]}"; do
  699. if [[ $port == *-* ]]; then
  700. start_port=$(echo $port | cut -d'-' -f1)
  701. end_port=$(echo $port | cut -d'-' -f2)
  702. # Check if the port range has been deleted
  703. (ufw status | grep -q "$start_port:$end_port") || echo "$start_port-$end_port"
  704. else
  705. # Check if the individual port has been deleted
  706. (ufw status | grep -q "$port") || echo "$port"
  707. fi
  708. done
  709. else
  710. echo "${red}Error:${plain} Invalid choice. Please enter 1 or 2." >&2
  711. exit 1
  712. fi
  713. }
  714. update_geo() {
  715. echo -e "${green}\t1.${plain} Loyalsoldier (geoip.dat, geosite.dat)"
  716. echo -e "${green}\t2.${plain} chocolate4u (geoip_IR.dat, geosite_IR.dat)"
  717. echo -e "${green}\t3.${plain} runetfreedom (geoip_RU.dat, geosite_RU.dat)"
  718. echo -e "${green}\t0.${plain} Back to Main Menu"
  719. read -rp "Choose an option: " choice
  720. cd /usr/local/x-ui/bin
  721. case "$choice" in
  722. 0)
  723. show_menu
  724. ;;
  725. 1)
  726. systemctl stop x-ui
  727. rm -f geoip.dat geosite.dat
  728. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat
  729. wget -N https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat
  730. echo -e "${green}Loyalsoldier datasets have been updated successfully!${plain}"
  731. restart
  732. ;;
  733. 2)
  734. systemctl stop x-ui
  735. rm -f geoip_IR.dat geosite_IR.dat
  736. wget -O geoip_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geoip.dat
  737. wget -O geosite_IR.dat -N https://github.com/chocolate4u/Iran-v2ray-rules/releases/latest/download/geosite.dat
  738. echo -e "${green}chocolate4u datasets have been updated successfully!${plain}"
  739. restart
  740. ;;
  741. 3)
  742. systemctl stop x-ui
  743. rm -f geoip_RU.dat geosite_RU.dat
  744. wget -O geoip_RU.dat -N https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geoip.dat
  745. wget -O geosite_RU.dat -N https://github.com/runetfreedom/russia-v2ray-rules-dat/releases/latest/download/geosite.dat
  746. echo -e "${green}runetfreedom datasets have been updated successfully!${plain}"
  747. restart
  748. ;;
  749. *)
  750. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  751. update_geo
  752. ;;
  753. esac
  754. before_show_menu
  755. }
  756. install_acme() {
  757. # Check if acme.sh is already installed
  758. if command -v ~/.acme.sh/acme.sh &>/dev/null; then
  759. LOGI "acme.sh is already installed."
  760. return 0
  761. fi
  762. LOGI "Installing acme.sh..."
  763. cd ~ || return 1 # Ensure you can change to the home directory
  764. curl -s https://get.acme.sh | sh
  765. if [ $? -ne 0 ]; then
  766. LOGE "Installation of acme.sh failed."
  767. return 1
  768. else
  769. LOGI "Installation of acme.sh succeeded."
  770. fi
  771. return 0
  772. }
  773. ssl_cert_issue_main() {
  774. echo -e "${green}\t1.${plain} Get SSL"
  775. echo -e "${green}\t2.${plain} Revoke"
  776. echo -e "${green}\t3.${plain} Force Renew"
  777. echo -e "${green}\t4.${plain} Show Existing Domains"
  778. echo -e "${green}\t5.${plain} Set Cert paths for the panel"
  779. echo -e "${green}\t0.${plain} Back to Main Menu"
  780. read -rp "Choose an option: " choice
  781. case "$choice" in
  782. 0)
  783. show_menu
  784. ;;
  785. 1)
  786. ssl_cert_issue
  787. ssl_cert_issue_main
  788. ;;
  789. 2)
  790. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  791. if [ -z "$domains" ]; then
  792. echo "No certificates found to revoke."
  793. else
  794. echo "Existing domains:"
  795. echo "$domains"
  796. read -rp "Please enter a domain from the list to revoke the certificate: " domain
  797. if echo "$domains" | grep -qw "$domain"; then
  798. ~/.acme.sh/acme.sh --revoke -d ${domain}
  799. LOGI "Certificate revoked for domain: $domain"
  800. else
  801. echo "Invalid domain entered."
  802. fi
  803. fi
  804. ssl_cert_issue_main
  805. ;;
  806. 3)
  807. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  808. if [ -z "$domains" ]; then
  809. echo "No certificates found to renew."
  810. else
  811. echo "Existing domains:"
  812. echo "$domains"
  813. read -rp "Please enter a domain from the list to renew the SSL certificate: " domain
  814. if echo "$domains" | grep -qw "$domain"; then
  815. ~/.acme.sh/acme.sh --renew -d ${domain} --force
  816. LOGI "Certificate forcefully renewed for domain: $domain"
  817. else
  818. echo "Invalid domain entered."
  819. fi
  820. fi
  821. ssl_cert_issue_main
  822. ;;
  823. 4)
  824. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  825. if [ -z "$domains" ]; then
  826. echo "No certificates found."
  827. else
  828. echo "Existing domains and their paths:"
  829. for domain in $domains; do
  830. local cert_path="/root/cert/${domain}/fullchain.pem"
  831. local key_path="/root/cert/${domain}/privkey.pem"
  832. if [[ -f "${cert_path}" && -f "${key_path}" ]]; then
  833. echo -e "Domain: ${domain}"
  834. echo -e "\tCertificate Path: ${cert_path}"
  835. echo -e "\tPrivate Key Path: ${key_path}"
  836. else
  837. echo -e "Domain: ${domain} - Certificate or Key missing."
  838. fi
  839. done
  840. fi
  841. ssl_cert_issue_main
  842. ;;
  843. 5)
  844. local domains=$(find /root/cert/ -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
  845. if [ -z "$domains" ]; then
  846. echo "No certificates found."
  847. else
  848. echo "Available domains:"
  849. echo "$domains"
  850. read -rp "Please choose a domain to set the panel paths: " domain
  851. if echo "$domains" | grep -qw "$domain"; then
  852. local webCertFile="/root/cert/${domain}/fullchain.pem"
  853. local webKeyFile="/root/cert/${domain}/privkey.pem"
  854. if [[ -f "${webCertFile}" && -f "${webKeyFile}" ]]; then
  855. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  856. echo "Panel paths set for domain: $domain"
  857. echo " - Certificate File: $webCertFile"
  858. echo " - Private Key File: $webKeyFile"
  859. restart
  860. else
  861. echo "Certificate or private key not found for domain: $domain."
  862. fi
  863. else
  864. echo "Invalid domain entered."
  865. fi
  866. fi
  867. ssl_cert_issue_main
  868. ;;
  869. *)
  870. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  871. ssl_cert_issue_main
  872. ;;
  873. esac
  874. }
  875. ssl_cert_issue() {
  876. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  877. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  878. # check for acme.sh first
  879. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  880. echo "acme.sh could not be found. we will install it"
  881. install_acme
  882. if [ $? -ne 0 ]; then
  883. LOGE "install acme failed, please check logs"
  884. exit 1
  885. fi
  886. fi
  887. # install socat second
  888. case "${release}" in
  889. ubuntu | debian | armbian)
  890. apt update && apt install socat -y
  891. ;;
  892. centos | almalinux | rocky | ol)
  893. yum -y update && yum -y install socat
  894. ;;
  895. fedora | amzn | virtuozzo)
  896. dnf -y update && dnf -y install socat
  897. ;;
  898. arch | manjaro | parch)
  899. pacman -Sy --noconfirm socat
  900. ;;
  901. *)
  902. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  903. exit 1
  904. ;;
  905. esac
  906. if [ $? -ne 0 ]; then
  907. LOGE "install socat failed, please check logs"
  908. exit 1
  909. else
  910. LOGI "install socat succeed..."
  911. fi
  912. # get the domain here, and we need to verify it
  913. local domain=""
  914. read -rp "Please enter your domain name: " domain
  915. LOGD "Your domain is: ${domain}, checking it..."
  916. # check if there already exists a certificate
  917. local currentCert=$(~/.acme.sh/acme.sh --list | tail -1 | awk '{print $1}')
  918. if [ "${currentCert}" == "${domain}" ]; then
  919. local certInfo=$(~/.acme.sh/acme.sh --list)
  920. LOGE "System already has certificates for this domain. Cannot issue again. Current certificate details:"
  921. LOGI "$certInfo"
  922. exit 1
  923. else
  924. LOGI "Your domain is ready for issuing certificates now..."
  925. fi
  926. # create a directory for the certificate
  927. certPath="/root/cert/${domain}"
  928. if [ ! -d "$certPath" ]; then
  929. mkdir -p "$certPath"
  930. else
  931. rm -rf "$certPath"
  932. mkdir -p "$certPath"
  933. fi
  934. # get the port number for the standalone server
  935. local WebPort=80
  936. read -rp "Please choose which port to use (default is 80): " WebPort
  937. if [[ ${WebPort} -gt 65535 || ${WebPort} -lt 1 ]]; then
  938. LOGE "Your input ${WebPort} is invalid, will use default port 80."
  939. WebPort=80
  940. fi
  941. LOGI "Will use port: ${WebPort} to issue certificates. Please make sure this port is open."
  942. # issue the certificate
  943. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  944. ~/.acme.sh/acme.sh --issue -d ${domain} --listen-v6 --standalone --httpport ${WebPort} --force
  945. if [ $? -ne 0 ]; then
  946. LOGE "Issuing certificate failed, please check logs."
  947. rm -rf ~/.acme.sh/${domain}
  948. exit 1
  949. else
  950. LOGE "Issuing certificate succeeded, installing certificates..."
  951. fi
  952. reloadCmd="x-ui restart"
  953. LOGI "Default --reloadcmd for ACME is: ${yellow}x-ui restart"
  954. LOGI "This command will run on every certificate issue and renew."
  955. read -rp "Would you like to modify --reloadcmd for ACME? (y/n): " setReloadcmd
  956. if [[ "$setReloadcmd" == "y" || "$setReloadcmd" == "Y" ]]; then
  957. echo -e "\n${green}\t1.${plain} Preset: systemctl reload nginx ; x-ui restart"
  958. echo -e "${green}\t2.${plain} Input your own command"
  959. echo -e "${green}\t0.${plain} Keep default reloadcmd"
  960. read -rp "Choose an option: " choice
  961. case "$choice" in
  962. 1)
  963. LOGI "Reloadcmd is: systemctl reload nginx ; x-ui restart"
  964. reloadCmd="systemctl reload nginx ; x-ui restart"
  965. ;;
  966. 2)
  967. LOGD "It's recommended to put x-ui restart at the end, so it won't raise an error if other services fails"
  968. read -rp "Please enter your reloadcmd (example: systemctl reload nginx ; x-ui restart): " reloadCmd
  969. LOGI "Your reloadcmd is: ${reloadCmd}"
  970. ;;
  971. *)
  972. LOGI "Keep default reloadcmd"
  973. ;;
  974. esac
  975. fi
  976. # install the certificate
  977. ~/.acme.sh/acme.sh --installcert -d ${domain} \
  978. --key-file /root/cert/${domain}/privkey.pem \
  979. --fullchain-file /root/cert/${domain}/fullchain.pem --reloadcmd "${reloadCmd}"
  980. if [ $? -ne 0 ]; then
  981. LOGE "Installing certificate failed, exiting."
  982. rm -rf ~/.acme.sh/${domain}
  983. exit 1
  984. else
  985. LOGI "Installing certificate succeeded, enabling auto renew..."
  986. fi
  987. # enable auto-renew
  988. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  989. if [ $? -ne 0 ]; then
  990. LOGE "Auto renew failed, certificate details:"
  991. ls -lah cert/*
  992. chmod 755 $certPath/*
  993. exit 1
  994. else
  995. LOGI "Auto renew succeeded, certificate details:"
  996. ls -lah cert/*
  997. chmod 755 $certPath/*
  998. fi
  999. # Prompt user to set panel paths after successful certificate installation
  1000. read -rp "Would you like to set this certificate for the panel? (y/n): " setPanel
  1001. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  1002. local webCertFile="/root/cert/${domain}/fullchain.pem"
  1003. local webKeyFile="/root/cert/${domain}/privkey.pem"
  1004. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  1005. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  1006. LOGI "Panel paths set for domain: $domain"
  1007. LOGI " - Certificate File: $webCertFile"
  1008. LOGI " - Private Key File: $webKeyFile"
  1009. echo -e "${green}Access URL: https://${domain}:${existing_port}${existing_webBasePath}${plain}"
  1010. restart
  1011. else
  1012. LOGE "Error: Certificate or private key file not found for domain: $domain."
  1013. fi
  1014. else
  1015. LOGI "Skipping panel path setting."
  1016. fi
  1017. }
  1018. ssl_cert_issue_CF() {
  1019. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  1020. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  1021. LOGI "****** Instructions for Use ******"
  1022. LOGI "Follow the steps below to complete the process:"
  1023. LOGI "1. Cloudflare Registered E-mail."
  1024. LOGI "2. Cloudflare Global API Key."
  1025. LOGI "3. The Domain Name."
  1026. LOGI "4. Once the certificate is issued, you will be prompted to set the certificate for the panel (optional)."
  1027. LOGI "5. The script also supports automatic renewal of the SSL certificate after installation."
  1028. confirm "Do you confirm the information and wish to proceed? [y/n]" "y"
  1029. if [ $? -eq 0 ]; then
  1030. # Check for acme.sh first
  1031. if ! command -v ~/.acme.sh/acme.sh &>/dev/null; then
  1032. echo "acme.sh could not be found. We will install it."
  1033. install_acme
  1034. if [ $? -ne 0 ]; then
  1035. LOGE "Install acme failed, please check logs."
  1036. exit 1
  1037. fi
  1038. fi
  1039. CF_Domain=""
  1040. LOGD "Please set a domain name:"
  1041. read -rp "Input your domain here: " CF_Domain
  1042. LOGD "Your domain name is set to: ${CF_Domain}"
  1043. # Set up Cloudflare API details
  1044. CF_GlobalKey=""
  1045. CF_AccountEmail=""
  1046. LOGD "Please set the API key:"
  1047. read -rp "Input your key here: " CF_GlobalKey
  1048. LOGD "Your API key is: ${CF_GlobalKey}"
  1049. LOGD "Please set up registered email:"
  1050. read -rp "Input your email here: " CF_AccountEmail
  1051. LOGD "Your registered email address is: ${CF_AccountEmail}"
  1052. # Set the default CA to Let's Encrypt
  1053. ~/.acme.sh/acme.sh --set-default-ca --server letsencrypt
  1054. if [ $? -ne 0 ]; then
  1055. LOGE "Default CA, Let'sEncrypt fail, script exiting..."
  1056. exit 1
  1057. fi
  1058. export CF_Key="${CF_GlobalKey}"
  1059. export CF_Email="${CF_AccountEmail}"
  1060. # Issue the certificate using Cloudflare DNS
  1061. ~/.acme.sh/acme.sh --issue --dns dns_cf -d ${CF_Domain} -d *.${CF_Domain} --log --force
  1062. if [ $? -ne 0 ]; then
  1063. LOGE "Certificate issuance failed, script exiting..."
  1064. exit 1
  1065. else
  1066. LOGI "Certificate issued successfully, Installing..."
  1067. fi
  1068. # Install the certificate
  1069. certPath="/root/cert/${CF_Domain}"
  1070. if [ -d "$certPath" ]; then
  1071. rm -rf ${certPath}
  1072. fi
  1073. mkdir -p ${certPath}
  1074. if [ $? -ne 0 ]; then
  1075. LOGE "Failed to create directory: ${certPath}"
  1076. exit 1
  1077. fi
  1078. reloadCmd="x-ui restart"
  1079. LOGI "Default --reloadcmd for ACME is: ${yellow}x-ui restart"
  1080. LOGI "This command will run on every certificate issue and renew."
  1081. read -rp "Would you like to modify --reloadcmd for ACME? (y/n): " setReloadcmd
  1082. if [[ "$setReloadcmd" == "y" || "$setReloadcmd" == "Y" ]]; then
  1083. echo -e "\n${green}\t1.${plain} Preset: systemctl reload nginx ; x-ui restart"
  1084. echo -e "${green}\t2.${plain} Input your own command"
  1085. echo -e "${green}\t0.${plain} Keep default reloadcmd"
  1086. read -rp "Choose an option: " choice
  1087. case "$choice" in
  1088. 1)
  1089. LOGI "Reloadcmd is: systemctl reload nginx ; x-ui restart"
  1090. reloadCmd="systemctl reload nginx ; x-ui restart"
  1091. ;;
  1092. 2)
  1093. LOGD "It's recommended to put x-ui restart at the end, so it won't raise an error if other services fails"
  1094. read -rp "Please enter your reloadcmd (example: systemctl reload nginx ; x-ui restart): " reloadCmd
  1095. LOGI "Your reloadcmd is: ${reloadCmd}"
  1096. ;;
  1097. *)
  1098. LOGI "Keep default reloadcmd"
  1099. ;;
  1100. esac
  1101. fi
  1102. ~/.acme.sh/acme.sh --installcert -d ${CF_Domain} -d *.${CF_Domain} \
  1103. --key-file ${certPath}/privkey.pem \
  1104. --fullchain-file ${certPath}/fullchain.pem --reloadcmd "${reloadCmd}"
  1105. if [ $? -ne 0 ]; then
  1106. LOGE "Certificate installation failed, script exiting..."
  1107. exit 1
  1108. else
  1109. LOGI "Certificate installed successfully, Turning on automatic updates..."
  1110. fi
  1111. # Enable auto-update
  1112. ~/.acme.sh/acme.sh --upgrade --auto-upgrade
  1113. if [ $? -ne 0 ]; then
  1114. LOGE "Auto update setup failed, script exiting..."
  1115. exit 1
  1116. else
  1117. LOGI "The certificate is installed and auto-renewal is turned on. Specific information is as follows:"
  1118. ls -lah ${certPath}/*
  1119. chmod 755 ${certPath}/*
  1120. fi
  1121. # Prompt user to set panel paths after successful certificate installation
  1122. read -rp "Would you like to set this certificate for the panel? (y/n): " setPanel
  1123. if [[ "$setPanel" == "y" || "$setPanel" == "Y" ]]; then
  1124. local webCertFile="${certPath}/fullchain.pem"
  1125. local webKeyFile="${certPath}/privkey.pem"
  1126. if [[ -f "$webCertFile" && -f "$webKeyFile" ]]; then
  1127. /usr/local/x-ui/x-ui cert -webCert "$webCertFile" -webCertKey "$webKeyFile"
  1128. LOGI "Panel paths set for domain: $CF_Domain"
  1129. LOGI " - Certificate File: $webCertFile"
  1130. LOGI " - Private Key File: $webKeyFile"
  1131. echo -e "${green}Access URL: https://${CF_Domain}:${existing_port}${existing_webBasePath}${plain}"
  1132. restart
  1133. else
  1134. LOGE "Error: Certificate or private key file not found for domain: $CF_Domain."
  1135. fi
  1136. else
  1137. LOGI "Skipping panel path setting."
  1138. fi
  1139. else
  1140. show_menu
  1141. fi
  1142. }
  1143. run_speedtest() {
  1144. # Check if Speedtest is already installed
  1145. if ! command -v speedtest &>/dev/null; then
  1146. # If not installed, determine installation method
  1147. if command -v snap &>/dev/null; then
  1148. # Use snap to install Speedtest
  1149. echo "Installing Speedtest using snap..."
  1150. snap install speedtest
  1151. else
  1152. # Fallback to using package managers
  1153. local pkg_manager=""
  1154. local speedtest_install_script=""
  1155. if command -v dnf &>/dev/null; then
  1156. pkg_manager="dnf"
  1157. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1158. elif command -v yum &>/dev/null; then
  1159. pkg_manager="yum"
  1160. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.rpm.sh"
  1161. elif command -v apt-get &>/dev/null; then
  1162. pkg_manager="apt-get"
  1163. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1164. elif command -v apt &>/dev/null; then
  1165. pkg_manager="apt"
  1166. speedtest_install_script="https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh"
  1167. fi
  1168. if [[ -z $pkg_manager ]]; then
  1169. echo "Error: Package manager not found. You may need to install Speedtest manually."
  1170. return 1
  1171. else
  1172. echo "Installing Speedtest using $pkg_manager..."
  1173. curl -s $speedtest_install_script | bash
  1174. $pkg_manager install -y speedtest
  1175. fi
  1176. fi
  1177. fi
  1178. speedtest
  1179. }
  1180. create_iplimit_jails() {
  1181. # Use default bantime if not passed => 30 minutes
  1182. local bantime="${1:-30}"
  1183. # Uncomment 'allowipv6 = auto' in fail2ban.conf
  1184. sed -i 's/#allowipv6 = auto/allowipv6 = auto/g' /etc/fail2ban/fail2ban.conf
  1185. # On Debian 12+ fail2ban's default backend should be changed to systemd
  1186. if [[ "${release}" == "debian" && ${os_version} -ge 12 ]]; then
  1187. sed -i '0,/action =/s/backend = auto/backend = systemd/' /etc/fail2ban/jail.conf
  1188. fi
  1189. cat << EOF > /etc/fail2ban/jail.d/3x-ipl.conf
  1190. [3x-ipl]
  1191. enabled=true
  1192. backend=auto
  1193. filter=3x-ipl
  1194. action=3x-ipl
  1195. logpath=${iplimit_log_path}
  1196. maxretry=2
  1197. findtime=32
  1198. bantime=${bantime}m
  1199. EOF
  1200. cat << EOF > /etc/fail2ban/filter.d/3x-ipl.conf
  1201. [Definition]
  1202. datepattern = ^%%Y/%%m/%%d %%H:%%M:%%S
  1203. failregex = \[LIMIT_IP\]\s*Email\s*=\s*<F-USER>.+</F-USER>\s*\|\|\s*SRC\s*=\s*<ADDR>
  1204. ignoreregex =
  1205. EOF
  1206. cat << EOF > /etc/fail2ban/action.d/3x-ipl.conf
  1207. [INCLUDES]
  1208. before = iptables-allports.conf
  1209. [Definition]
  1210. actionstart = <iptables> -N f2b-<name>
  1211. <iptables> -A f2b-<name> -j <returntype>
  1212. <iptables> -I <chain> -p <protocol> -j f2b-<name>
  1213. actionstop = <iptables> -D <chain> -p <protocol> -j f2b-<name>
  1214. <actionflush>
  1215. <iptables> -X f2b-<name>
  1216. actioncheck = <iptables> -n -L <chain> | grep -q 'f2b-<name>[ \t]'
  1217. actionban = <iptables> -I f2b-<name> 1 -s <ip> -j <blocktype>
  1218. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") BAN [Email] = <F-USER> [IP] = <ip> banned for <bantime> seconds." >> ${iplimit_banned_log_path}
  1219. actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
  1220. echo "\$(date +"%%Y/%%m/%%d %%H:%%M:%%S") UNBAN [Email] = <F-USER> [IP] = <ip> unbanned." >> ${iplimit_banned_log_path}
  1221. [Init]
  1222. name = default
  1223. protocol = tcp
  1224. chain = INPUT
  1225. EOF
  1226. echo -e "${green}Ip Limit jail files created with a bantime of ${bantime} minutes.${plain}"
  1227. }
  1228. iplimit_remove_conflicts() {
  1229. local jail_files=(
  1230. /etc/fail2ban/jail.conf
  1231. /etc/fail2ban/jail.local
  1232. )
  1233. for file in "${jail_files[@]}"; do
  1234. # Check for [3x-ipl] config in jail file then remove it
  1235. if test -f "${file}" && grep -qw '3x-ipl' ${file}; then
  1236. sed -i "/\[3x-ipl\]/,/^$/d" ${file}
  1237. echo -e "${yellow}Removing conflicts of [3x-ipl] in jail (${file})!${plain}\n"
  1238. fi
  1239. done
  1240. }
  1241. ip_validation() {
  1242. ipv6_regex="^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$"
  1243. ipv4_regex="^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)$"
  1244. }
  1245. iplimit_main() {
  1246. echo -e "\n${green}\t1.${plain} Install Fail2ban and configure IP Limit"
  1247. echo -e "${green}\t2.${plain} Change Ban Duration"
  1248. echo -e "${green}\t3.${plain} Unban Everyone"
  1249. echo -e "${green}\t4.${plain} Ban Logs"
  1250. echo -e "${green}\t5.${plain} Ban an IP Address"
  1251. echo -e "${green}\t6.${plain} Unban an IP Address"
  1252. echo -e "${green}\t7.${plain} Real-Time Logs"
  1253. echo -e "${green}\t8.${plain} Service Status"
  1254. echo -e "${green}\t9.${plain} Service Restart"
  1255. echo -e "${green}\t10.${plain} Uninstall Fail2ban and IP Limit"
  1256. echo -e "${green}\t0.${plain} Back to Main Menu"
  1257. read -rp "Choose an option: " choice
  1258. case "$choice" in
  1259. 0)
  1260. show_menu
  1261. ;;
  1262. 1)
  1263. confirm "Proceed with installation of Fail2ban & IP Limit?" "y"
  1264. if [[ $? == 0 ]]; then
  1265. install_iplimit
  1266. else
  1267. iplimit_main
  1268. fi
  1269. ;;
  1270. 2)
  1271. read -rp "Please enter new Ban Duration in Minutes [default 30]: " NUM
  1272. if [[ $NUM =~ ^[0-9]+$ ]]; then
  1273. create_iplimit_jails ${NUM}
  1274. systemctl restart fail2ban
  1275. else
  1276. echo -e "${red}${NUM} is not a number! Please, try again.${plain}"
  1277. fi
  1278. iplimit_main
  1279. ;;
  1280. 3)
  1281. confirm "Proceed with Unbanning everyone from IP Limit jail?" "y"
  1282. if [[ $? == 0 ]]; then
  1283. fail2ban-client reload --restart --unban 3x-ipl
  1284. truncate -s 0 "${iplimit_banned_log_path}"
  1285. echo -e "${green}All users Unbanned successfully.${plain}"
  1286. iplimit_main
  1287. else
  1288. echo -e "${yellow}Cancelled.${plain}"
  1289. fi
  1290. iplimit_main
  1291. ;;
  1292. 4)
  1293. show_banlog
  1294. iplimit_main
  1295. ;;
  1296. 5)
  1297. read -rp "Enter the IP address you want to ban: " ban_ip
  1298. ip_validation
  1299. if [[ $ban_ip =~ $ipv4_regex || $ban_ip =~ $ipv6_regex ]]; then
  1300. fail2ban-client set 3x-ipl banip "$ban_ip"
  1301. echo -e "${green}IP Address ${ban_ip} has been banned successfully.${plain}"
  1302. else
  1303. echo -e "${red}Invalid IP address format! Please try again.${plain}"
  1304. fi
  1305. iplimit_main
  1306. ;;
  1307. 6)
  1308. read -rp "Enter the IP address you want to unban: " unban_ip
  1309. ip_validation
  1310. if [[ $unban_ip =~ $ipv4_regex || $unban_ip =~ $ipv6_regex ]]; then
  1311. fail2ban-client set 3x-ipl unbanip "$unban_ip"
  1312. echo -e "${green}IP Address ${unban_ip} has been unbanned successfully.${plain}"
  1313. else
  1314. echo -e "${red}Invalid IP address format! Please try again.${plain}"
  1315. fi
  1316. iplimit_main
  1317. ;;
  1318. 7)
  1319. tail -f /var/log/fail2ban.log
  1320. iplimit_main
  1321. ;;
  1322. 8)
  1323. service fail2ban status
  1324. iplimit_main
  1325. ;;
  1326. 9)
  1327. systemctl restart fail2ban
  1328. iplimit_main
  1329. ;;
  1330. 10)
  1331. remove_iplimit
  1332. iplimit_main
  1333. ;;
  1334. *)
  1335. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1336. iplimit_main
  1337. ;;
  1338. esac
  1339. }
  1340. install_iplimit() {
  1341. if ! command -v fail2ban-client &>/dev/null; then
  1342. echo -e "${green}Fail2ban is not installed. Installing now...!${plain}\n"
  1343. # Check the OS and install necessary packages
  1344. case "${release}" in
  1345. ubuntu)
  1346. if [[ "${os_version}" -ge 24 ]]; then
  1347. apt update && apt install python3-pip -y
  1348. python3 -m pip install pyasynchat --break-system-packages
  1349. fi
  1350. apt update && apt install fail2ban -y
  1351. ;;
  1352. debian | armbian)
  1353. apt update && apt install fail2ban -y
  1354. ;;
  1355. centos | almalinux | rocky | ol)
  1356. yum update -y && yum install epel-release -y
  1357. yum -y install fail2ban
  1358. ;;
  1359. fedora | amzn | virtuozzo)
  1360. dnf -y update && dnf -y install fail2ban
  1361. ;;
  1362. arch | manjaro | parch)
  1363. pacman -Syu --noconfirm fail2ban
  1364. ;;
  1365. *)
  1366. echo -e "${red}Unsupported operating system. Please check the script and install the necessary packages manually.${plain}\n"
  1367. exit 1
  1368. ;;
  1369. esac
  1370. if ! command -v fail2ban-client &>/dev/null; then
  1371. echo -e "${red}Fail2ban installation failed.${plain}\n"
  1372. exit 1
  1373. fi
  1374. echo -e "${green}Fail2ban installed successfully!${plain}\n"
  1375. else
  1376. echo -e "${yellow}Fail2ban is already installed.${plain}\n"
  1377. fi
  1378. echo -e "${green}Configuring IP Limit...${plain}\n"
  1379. # make sure there's no conflict for jail files
  1380. iplimit_remove_conflicts
  1381. # Check if log file exists
  1382. if ! test -f "${iplimit_banned_log_path}"; then
  1383. touch ${iplimit_banned_log_path}
  1384. fi
  1385. # Check if service log file exists so fail2ban won't return error
  1386. if ! test -f "${iplimit_log_path}"; then
  1387. touch ${iplimit_log_path}
  1388. fi
  1389. # Create the iplimit jail files
  1390. # we didn't pass the bantime here to use the default value
  1391. create_iplimit_jails
  1392. # Launching fail2ban
  1393. if ! systemctl is-active --quiet fail2ban; then
  1394. systemctl start fail2ban
  1395. else
  1396. systemctl restart fail2ban
  1397. fi
  1398. systemctl enable fail2ban
  1399. echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
  1400. before_show_menu
  1401. }
  1402. remove_iplimit() {
  1403. echo -e "${green}\t1.${plain} Only remove IP Limit configurations"
  1404. echo -e "${green}\t2.${plain} Uninstall Fail2ban and IP Limit"
  1405. echo -e "${green}\t0.${plain} Back to Main Menu"
  1406. read -rp "Choose an option: " num
  1407. case "$num" in
  1408. 1)
  1409. rm -f /etc/fail2ban/filter.d/3x-ipl.conf
  1410. rm -f /etc/fail2ban/action.d/3x-ipl.conf
  1411. rm -f /etc/fail2ban/jail.d/3x-ipl.conf
  1412. systemctl restart fail2ban
  1413. echo -e "${green}IP Limit removed successfully!${plain}\n"
  1414. before_show_menu
  1415. ;;
  1416. 2)
  1417. rm -rf /etc/fail2ban
  1418. systemctl stop fail2ban
  1419. case "${release}" in
  1420. ubuntu | debian | armbian)
  1421. apt-get remove -y fail2ban
  1422. apt-get purge -y fail2ban -y
  1423. apt-get autoremove -y
  1424. ;;
  1425. centos | almalinux | rocky | ol)
  1426. yum remove fail2ban -y
  1427. yum autoremove -y
  1428. ;;
  1429. fedora | amzn | virtuozzo)
  1430. dnf remove fail2ban -y
  1431. dnf autoremove -y
  1432. ;;
  1433. arch | manjaro | parch)
  1434. pacman -Rns --noconfirm fail2ban
  1435. ;;
  1436. *)
  1437. echo -e "${red}Unsupported operating system. Please uninstall Fail2ban manually.${plain}\n"
  1438. exit 1
  1439. ;;
  1440. esac
  1441. echo -e "${green}Fail2ban and IP Limit removed successfully!${plain}\n"
  1442. before_show_menu
  1443. ;;
  1444. 0)
  1445. show_menu
  1446. ;;
  1447. *)
  1448. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1449. remove_iplimit
  1450. ;;
  1451. esac
  1452. }
  1453. SSH_port_forwarding() {
  1454. local server_ip=$(curl -s https://api.ipify.org)
  1455. local existing_webBasePath=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'webBasePath: .+' | awk '{print $2}')
  1456. local existing_port=$(/usr/local/x-ui/x-ui setting -show true | grep -Eo 'port: .+' | awk '{print $2}')
  1457. local existing_listenIP=$(/usr/local/x-ui/x-ui setting -getListen true | grep -Eo 'listenIP: .+' | awk '{print $2}')
  1458. local existing_cert=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'cert: .+' | awk '{print $2}')
  1459. local existing_key=$(/usr/local/x-ui/x-ui setting -getCert true | grep -Eo 'key: .+' | awk '{print $2}')
  1460. local config_listenIP=""
  1461. local listen_choice=""
  1462. if [[ -n "$existing_cert" && -n "$existing_key" ]]; then
  1463. echo -e "${green}Panel is secure with SSL.${plain}"
  1464. before_show_menu
  1465. fi
  1466. if [[ -z "$existing_cert" && -z "$existing_key" && (-z "$existing_listenIP" || "$existing_listenIP" == "0.0.0.0") ]]; then
  1467. echo -e "\n${red}Warning: No Cert and Key found! The panel is not secure.${plain}"
  1468. echo "Please obtain a certificate or set up SSH port forwarding."
  1469. fi
  1470. if [[ -n "$existing_listenIP" && "$existing_listenIP" != "0.0.0.0" && (-z "$existing_cert" && -z "$existing_key") ]]; then
  1471. echo -e "\n${green}Current SSH Port Forwarding Configuration:${plain}"
  1472. echo -e "Standard SSH command:"
  1473. echo -e "${yellow}ssh -L 2222:${existing_listenIP}:${existing_port} root@${server_ip}${plain}"
  1474. echo -e "\nIf using SSH key:"
  1475. echo -e "${yellow}ssh -i <sshkeypath> -L 2222:${existing_listenIP}:${existing_port} root@${server_ip}${plain}"
  1476. echo -e "\nAfter connecting, access the panel at:"
  1477. echo -e "${yellow}http://localhost:2222${existing_webBasePath}${plain}"
  1478. fi
  1479. echo -e "\nChoose an option:"
  1480. echo -e "${green}1.${plain} Set listen IP"
  1481. echo -e "${green}2.${plain} Clear listen IP"
  1482. echo -e "${green}0.${plain} Back to Main Menu"
  1483. read -rp "Choose an option: " num
  1484. case "$num" in
  1485. 1)
  1486. if [[ -z "$existing_listenIP" || "$existing_listenIP" == "0.0.0.0" ]]; then
  1487. echo -e "\nNo listenIP configured. Choose an option:"
  1488. echo -e "1. Use default IP (127.0.0.1)"
  1489. echo -e "2. Set a custom IP"
  1490. read -rp "Select an option (1 or 2): " listen_choice
  1491. config_listenIP="127.0.0.1"
  1492. [[ "$listen_choice" == "2" ]] && read -rp "Enter custom IP to listen on: " config_listenIP
  1493. /usr/local/x-ui/x-ui setting -listenIP "${config_listenIP}" >/dev/null 2>&1
  1494. echo -e "${green}listen IP has been set to ${config_listenIP}.${plain}"
  1495. echo -e "\n${green}SSH Port Forwarding Configuration:${plain}"
  1496. echo -e "Standard SSH command:"
  1497. echo -e "${yellow}ssh -L 2222:${config_listenIP}:${existing_port} root@${server_ip}${plain}"
  1498. echo -e "\nIf using SSH key:"
  1499. echo -e "${yellow}ssh -i <sshkeypath> -L 2222:${config_listenIP}:${existing_port} root@${server_ip}${plain}"
  1500. echo -e "\nAfter connecting, access the panel at:"
  1501. echo -e "${yellow}http://localhost:2222${existing_webBasePath}${plain}"
  1502. restart
  1503. else
  1504. config_listenIP="${existing_listenIP}"
  1505. echo -e "${green}Current listen IP is already set to ${config_listenIP}.${plain}"
  1506. fi
  1507. ;;
  1508. 2)
  1509. /usr/local/x-ui/x-ui setting -listenIP 0.0.0.0 >/dev/null 2>&1
  1510. echo -e "${green}Listen IP has been cleared.${plain}"
  1511. restart
  1512. ;;
  1513. 0)
  1514. show_menu
  1515. ;;
  1516. *)
  1517. echo -e "${red}Invalid option. Please select a valid number.${plain}\n"
  1518. SSH_port_forwarding
  1519. ;;
  1520. esac
  1521. }
  1522. show_usage() {
  1523. echo -e "┌───────────────────────────────────────────────────────┐
  1524. │ ${blue}x-ui control menu usages (subcommands):${plain} │
  1525. │ │
  1526. │ ${blue}x-ui${plain} - Admin Management Script │
  1527. │ ${blue}x-ui start${plain} - Start │
  1528. │ ${blue}x-ui stop${plain} - Stop │
  1529. │ ${blue}x-ui restart${plain} - Restart │
  1530. │ ${blue}x-ui status${plain} - Current Status │
  1531. │ ${blue}x-ui settings${plain} - Current Settings │
  1532. │ ${blue}x-ui enable${plain} - Enable Autostart on OS Startup │
  1533. │ ${blue}x-ui disable${plain} - Disable Autostart on OS Startup │
  1534. │ ${blue}x-ui log${plain} - Check logs │
  1535. │ ${blue}x-ui banlog${plain} - Check Fail2ban ban logs │
  1536. │ ${blue}x-ui update${plain} - Update │
  1537. │ ${blue}x-ui legacy${plain} - legacy version │
  1538. │ ${blue}x-ui install${plain} - Install │
  1539. │ ${blue}x-ui uninstall${plain} - Uninstall │
  1540. └───────────────────────────────────────────────────────┘"
  1541. }
  1542. show_menu() {
  1543. echo -e "
  1544. ╔────────────────────────────────────────────────╗
  1545. │ ${green}3X-UI Panel Management Script${plain} │
  1546. │ ${green}0.${plain} Exit Script │
  1547. │────────────────────────────────────────────────│
  1548. │ ${green}1.${plain} Install │
  1549. │ ${green}2.${plain} Update │
  1550. │ ${green}3.${plain} Update Menu │
  1551. │ ${green}4.${plain} Legacy Version │
  1552. │ ${green}5.${plain} Uninstall │
  1553. │────────────────────────────────────────────────│
  1554. │ ${green}6.${plain} Reset Username & Password │
  1555. │ ${green}7.${plain} Reset Web Base Path │
  1556. │ ${green}8.${plain} Reset Settings │
  1557. │ ${green}9.${plain} Change Port │
  1558. │ ${green}10.${plain} View Current Settings │
  1559. │────────────────────────────────────────────────│
  1560. │ ${green}11.${plain} Start │
  1561. │ ${green}12.${plain} Stop │
  1562. │ ${green}13.${plain} Restart │
  1563. │ ${green}14.${plain} Check Status │
  1564. │ ${green}15.${plain} Logs Management │
  1565. │────────────────────────────────────────────────│
  1566. │ ${green}16.${plain} Enable Autostart │
  1567. │ ${green}17.${plain} Disable Autostart │
  1568. │────────────────────────────────────────────────│
  1569. │ ${green}18.${plain} SSL Certificate Management │
  1570. │ ${green}19.${plain} Cloudflare SSL Certificate │
  1571. │ ${green}20.${plain} IP Limit Management │
  1572. │ ${green}21.${plain} Firewall Management │
  1573. │ ${green}22.${plain} SSH Port Forwarding Management │
  1574. │────────────────────────────────────────────────│
  1575. │ ${green}23.${plain} Enable BBR │
  1576. │ ${green}24.${plain} Update Geo Files │
  1577. │ ${green}25.${plain} Speedtest by Ookla │
  1578. ╚────────────────────────────────────────────────╝
  1579. "
  1580. show_status
  1581. echo && read -rp "Please enter your selection [0-25]: " num
  1582. case "${num}" in
  1583. 0)
  1584. exit 0
  1585. ;;
  1586. 1)
  1587. check_uninstall && install
  1588. ;;
  1589. 2)
  1590. check_install && update
  1591. ;;
  1592. 3)
  1593. check_install && update_menu
  1594. ;;
  1595. 4)
  1596. check_install && legacy_version
  1597. ;;
  1598. 5)
  1599. check_install && uninstall
  1600. ;;
  1601. 6)
  1602. check_install && reset_user
  1603. ;;
  1604. 7)
  1605. check_install && reset_webbasepath
  1606. ;;
  1607. 8)
  1608. check_install && reset_config
  1609. ;;
  1610. 9)
  1611. check_install && set_port
  1612. ;;
  1613. 10)
  1614. check_install && check_config
  1615. ;;
  1616. 11)
  1617. check_install && start
  1618. ;;
  1619. 12)
  1620. check_install && stop
  1621. ;;
  1622. 13)
  1623. check_install && restart
  1624. ;;
  1625. 14)
  1626. check_install && status
  1627. ;;
  1628. 15)
  1629. check_install && show_log
  1630. ;;
  1631. 16)
  1632. check_install && enable
  1633. ;;
  1634. 17)
  1635. check_install && disable
  1636. ;;
  1637. 18)
  1638. ssl_cert_issue_main
  1639. ;;
  1640. 19)
  1641. ssl_cert_issue_CF
  1642. ;;
  1643. 20)
  1644. iplimit_main
  1645. ;;
  1646. 21)
  1647. firewall_menu
  1648. ;;
  1649. 22)
  1650. SSH_port_forwarding
  1651. ;;
  1652. 23)
  1653. bbr_menu
  1654. ;;
  1655. 24)
  1656. update_geo
  1657. ;;
  1658. 25)
  1659. run_speedtest
  1660. ;;
  1661. *)
  1662. LOGE "Please enter the correct number [0-25]"
  1663. ;;
  1664. esac
  1665. }
  1666. if [[ $# > 0 ]]; then
  1667. case $1 in
  1668. "start")
  1669. check_install 0 && start 0
  1670. ;;
  1671. "stop")
  1672. check_install 0 && stop 0
  1673. ;;
  1674. "restart")
  1675. check_install 0 && restart 0
  1676. ;;
  1677. "status")
  1678. check_install 0 && status 0
  1679. ;;
  1680. "settings")
  1681. check_install 0 && check_config 0
  1682. ;;
  1683. "enable")
  1684. check_install 0 && enable 0
  1685. ;;
  1686. "disable")
  1687. check_install 0 && disable 0
  1688. ;;
  1689. "log")
  1690. check_install 0 && show_log 0
  1691. ;;
  1692. "banlog")
  1693. check_install 0 && show_banlog 0
  1694. ;;
  1695. "update")
  1696. check_install 0 && update 0
  1697. ;;
  1698. "legacy")
  1699. check_install 0 && legacy_version 0
  1700. ;;
  1701. "install")
  1702. check_uninstall 0 && install 0
  1703. ;;
  1704. "uninstall")
  1705. check_install 0 && uninstall 0
  1706. ;;
  1707. *) show_usage ;;
  1708. esac
  1709. else
  1710. show_menu
  1711. fi