x-ui.sh 68 KB

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