x-ui.sh 63 KB

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