1
0

x-ui.sh 86 KB

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