x-ui.sh 79 KB

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