x-ui.sh 76 KB

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