1
0

x-ui.sh 77 KB

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