x-ui.sh 76 KB

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