1
0

x-ui.sh 63 KB

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