x-ui.sh 83 KB

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