1
0

x-ui.sh 76 KB

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