x-ui.sh 77 KB

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