x-ui.sh 77 KB

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