x-ui.sh 79 KB

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