x-ui.sh 77 KB

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