From 62a0a417df9d155e35c1122b639724abdbb157f4 Mon Sep 17 00:00:00 2001 From: yunshu <33997505+hello-yunshu@users.noreply.github.com> Date: Thu, 12 Dec 2024 22:20:36 +0800 Subject: [PATCH] Use read_optimize --- fail2ban_manager.sh | 24 ++++------- file_manager.sh | 34 ++++----------- install.sh | 100 ++++++++++++++++++++++---------------------- 3 files changed, 66 insertions(+), 92 deletions(-) diff --git a/fail2ban_manager.sh b/fail2ban_manager.sh index 2df5e56..1eb62ef 100644 --- a/fail2ban_manager.sh +++ b/fail2ban_manager.sh @@ -1,7 +1,7 @@ #!/bin/bash # 定义当前版本号 -mf_SCRIPT_VERSION="1.0.4" +mf_SCRIPT_VERSION="1.0.5" mf_main_menu() { check_system @@ -94,9 +94,7 @@ mf_manage_fail2ban() { echo "3. 停止 Fail2ban" echo "4. 添加自定义规则" echo "5. 返回" - read -rp "请输入: " mf_action - [[ -z "${mf_action}" ]] && mf_action=1 - + read_optimize "请输入: " mf_action 1 case $mf_action in 1) mf_start_enable_fail2ban @@ -126,19 +124,11 @@ mf_add_custom_rule() { local max_retry local ban_time - read -rp "请输入新的 Jail 名称: " jail_name - read -rp "请输入 Filter 名称: " filter_name - read -rp "请输入日志路径: " log_path - read -rp "请输入最大重试次数 (默认 5): " max_retry - read -rp "请输入封禁时间 (秒, 默认 604800 秒): " ban_time - - max_retry=${max_retry:-5} - ban_time=${ban_time:-604800} - - if [[ -z "$jail_name" || -z "$filter_name" || -z "$log_path" ]]; then - echo -e "\n${Error} ${RedBG} Jail 名称、Filter 名称和日志路径不能为空 ${Font}" - return - fi + read_optimize "请输入新的 Jail 名称: " "jail_name" NULL + read_optimize "请输入 Filter 名称: " "filter_name" NULL + read_optimize "请输入日志路径: " "log_path" NULL + read_optimize "请输入最大重试次数 (默认 5): " "max_retry" 5 1 99 "最大重试次数必须在 1 到 99 之间" + read_optimize "请输入封禁时间 (秒, 默认 604800 秒): " "ban_time" 604800 1 8640000 "封禁时间必须在 1 到 8640000 秒之间" if grep -q "\[$jail_name\]" /etc/fail2ban/jail.local; then echo -e "${Warning} ${YellowBG} Jail '$jail_name' 已存在 ${Font}" diff --git a/file_manager.sh b/file_manager.sh index 6940d6a..9589679 100644 --- a/file_manager.sh +++ b/file_manager.sh @@ -1,7 +1,7 @@ #!/bin/bash # 定义当前版本号 -fm_SCRIPT_VERSION="1.0.3" +fm_SCRIPT_VERSION="1.0.4" # 检查是否提供了扩展名参数 if [ -z "$1" ]; then @@ -88,9 +88,7 @@ fm_list_files() { fm_create_servername_file() { local url fm_list_files - echo -e "请输入网址 (例如 hey.run) ${Font}" - echo -e "不要包含 http:// 或 https:// 开头 ${Font}" - read -p "请输入: " url + read_optimize "请输入网址 (例如 hey.run) ${Font}\n不要包含 http:// 或 https:// 开头 ${Font}\n请输入: " url if [[ $url =~ ^(http|https):// ]]; then echo -e "\n${Error} ${RedBG} 网址不能包含 http:// 或 https:// 开头 ${Font}" return @@ -105,15 +103,9 @@ fm_create_servername_file() { fm_create_ws_or_grpc_server_file() { local host port weight content firewall_set_fq fm_list_files - read -p "请输入主机 (host): " host - read -p "请输入端口 (port): " port - read -p "请输入权重 (0~100 默认值 50): " weight - weight=${weight:-50} - - if ! [[ $weight =~ ^[0-9]+$ ]] || [ "$weight" -lt 0 ] || [ "$weight" -gt 100 ]; then - echo -e "\n${Error} ${RedBG} 权重必须是 0 到 100 之间的整数 ${Font}" - return - fi + read_optimize "请输入主机 (host): " host + read_optimize "请输入端口 (port): " port "" 1 65535 + read_optimize "请输入权重 (0~100 默认值 50): " weight "50" 0 100 content="server ${host}:${port} weight=${weight} max_fails=2 fail_timeout=10;" echo "$content" > "${host}.${fm_EXTENSION}" @@ -158,12 +150,7 @@ fm_edit_file() { fm_list_files local num_files=${#files[@]} local choice - read -p "请输入要编辑的文件编号 (1-$num_files): " choice - - if ! [[ $choice =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt "$num_files" ]; then - echo -e "\n${Error} ${RedBG} 无效的选择 请重试 ${Font}" - return - fi + read_optimize "请输入要编辑的文件编号 (1-$num_files): " choice "" 1 "$num_files" local filename="${files[$((choice - 1))]}" @@ -185,12 +172,7 @@ fm_delete_file() { local num_files=${#files[@]} local choice - read -p "请输入要删除的文件编号 (1-$num_files): " choice - - if ! [[ $choice =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt "$num_files" ]; then - echo -e "\n${Error} ${RedBG} 无效的选择 请重试 ${Font}" - return - fi + read_optimize "请输入要删除的文件编号 (1-$num_files): " choice "" 1 "$num_files" local filename="${files[$((choice - 1))]}" @@ -227,7 +209,7 @@ fm_main_menu() { echo -e "4 ${Green}删除一个已存在的 $fm_EXTENSION 文件${Font}" echo -e "5 ${Green}退出${Font}" local choice - read -p "请选择一个选项: " choice + read_optimize "请选择一个选项: " choice "" 1 5 case $choice in 1) fm_list_files ;; diff --git a/install.sh b/install.sh index 984063f..fe3cbca 100644 --- a/install.sh +++ b/install.sh @@ -205,18 +205,28 @@ dependency_install() { } read_optimize() { - read -rp "$1" $2 - if [[ -z $(eval echo \$$2) ]]; then - if [[ $3 != "NULL" ]]; then - eval $(echo "$2")="$3" + local prompt="$1" var_name="$2" default_value="${3:-NULL}" min_value="${4:-}" max_value="${5:-}" error_msg="${6:-值为空或超出范围, 请重新输入!}" + local user_input + + read -rp "$prompt" user_input + + if [[ -z $user_input ]]; then + if [[ $default_value != "NULL" ]]; then + user_input=$default_value else - echo -e "${Error} ${RedBG} 请输入正确的值! ${Font}" - read_optimize "$1" "$2" $3 $4 $5 "$6" + echo -e "${Error} ${RedBG} 值为空, 请重新输入! ${Font}" + read_optimize "$prompt" "$var_name" "$default_value" "$min_value" "$max_value" "$error_msg" + return fi - elif [[ ! -z $4 ]] && [[ ! -z $5 ]]; then - if [[ $(eval echo \$$2) -le $4 ]] || [[ $(eval echo \$$2) -gt $5 ]]; then - echo -e "${Error} ${RedBG} $6 ${Font}" - read_optimize "$1" "$2" $3 $4 $5 "$6" + fi + + printf -v "$var_name" "%s" "$user_input" + + if [[ -n $min_value ]] && [[ -n $max_value ]]; then + if (( user_input < min_value )) || (( user_input > max_value )); then + echo -e "${Error} ${RedBG} $error_msg ${Font}" + read_optimize "$prompt" "$var_name" "$default_value" "$min_value" "$max_value" "$error_msg" + return fi fi } @@ -258,7 +268,8 @@ ws_grpc_choose() { echo -e "${Red}1${Font}: ws (默认)" echo "2: gRPC" echo "3: ws+gRPC" - read -rp "请输入: " choose_network + local choose_network + read_optimize "请输入: " "choose_network" 1 1 3 "请输入有效的数字" if [[ $choose_network == 2 ]]; then [[ ${shell_mode} == "Nginx+ws+TLS" ]] && shell_mode="Nginx+gRPC+TLS" [[ ${shell_mode} == "Reality" ]] && shell_mode="Reality+gRPC" @@ -491,11 +502,7 @@ email_set() { read -r custom_email_fq case $custom_email_fq in [yY][eE][sS] | [yY]) - read -r -p "请输入合法的email (e.g. me@idleleo.com):" custom_email - if [[ -z "${custom_email}" ]]; then - echo -e "${Error} ${RedBG} 用户名不可为空! ${Font}" - email_set - fi + read_optimize "请输入合法的email (e.g. me@idleleo.com): " "custom_email" "NULL" ;; *) custom_email="$(head -n 10 /dev/urandom | md5sum | head -c ${random_num})@idleleo.com" @@ -549,11 +556,7 @@ target_set() { while true; do echo -e "\n${GreenBG} 请输入一个域名 (e.g. bing.com)${Font}" echo -e "${Green}域名要求支持 TLSv1.3、X25519 与 H2 以及域名非跳转用${Font}" - read -p "确认域名符合要求后请输入: " domain - if [ -z "$domain" ]; then - echo -e "${Warning} ${YellowBG} 没有输入域名, 请重新输入${RedBG}${Font}" - continue - fi + read_optimize "确认域名符合要求后请输入: " "domain" "NULL" echo -e "${Green}正在检测域名请等待…${Font}" output=$(nmap --script ssl-enum-ciphers -p 443 "${domain}") @@ -600,7 +603,7 @@ serverNames_set() { read -r custom_serverNames_fq case $custom_serverNames_fq in [yY][eE][sS] | [yY]) - read -p "请输入: " serverNames + read_optimize "请输入: " "serverNames" "NULL" ;; *) serverNames=$target @@ -640,7 +643,8 @@ nginx_upstream_server_set() { echo "1: ws" echo "2: gRPC" echo "3: 返回" - read -rp "请输入: " upstream_choose + local upstream_choose + read_optimize "请输入: " "upstream_choose" "NULL" 1 3 "请重新输入正确的数字" fm_remote_url="https://raw.githubusercontent.com/hello-yunshu/Xray_bash_onekey/main/file_manager.sh" fm_file_path=${nginx_conf_dir} @@ -1167,8 +1171,8 @@ domain_check() { echo -e "${Red}1${Font}: IPv4 (默认)" echo "2: IPv6 (不推荐)" echo "3: 域名" - read -rp "请输入: " ip_version_fq - [[ -z ${ip_version_fq} ]] && ip_version_fq=1 + local ip_version_fq + read_optimize "请输入: " "ip_version_fq" 1 1 3 "请输入有效的数字" echo -e "${OK} ${GreenBG} 正在获取 公网IP 信息, 请耐心等待 ${Font}" if [[ ${ip_version_fq} == 1 ]]; then local_ip=$(curl -4 ip.sb) @@ -1181,7 +1185,7 @@ domain_check() { elif [[ ${ip_version_fq} == 3 ]]; then echo -e "${Warning} ${GreenBG} 此选项用于服务器商仅提供域名访问服务器 ${Font}" echo -e "${Warning} ${GreenBG} 注意服务器商域名添加 CNAME 记录 ${Font}" - read -rp "请输入: " local_ip + read_optimize "请输入: " "local_ip" "NULL" ip_version=${local_ip} else local_ip=$(curl -4 ip.sb) @@ -1198,7 +1202,8 @@ domain_check() { echo "1: 继续安装" echo "2: 重新输入" echo -e "${Red}3${Font}: 终止安装 (默认)" - read -r install + local install + read_optimize "请输入: " "install" 3 1 3 "请输入有效的数字" case $install in 1) echo -e "${OK} ${GreenBG} 继续安装 ${Font}" @@ -1248,7 +1253,8 @@ ip_check() { echo -e "${Red}1${Font}: IPv4 (默认)" echo "2: IPv6 (不推荐)" echo "3: 手动输入" - read -rp "请输入: " ip_version_fq + local ip_version_fq + read_optimize "请输入: " "ip_version_fq" 1 1 3 "请输入有效的数字" [[ -z ${ip_version_fq} ]] && ip_version=1 echo -e "${OK} ${GreenBG} 正在获取 公网IP 信息, 请耐心等待 ${Font}" if [[ ${ip_version_fq} == 1 ]]; then @@ -1258,7 +1264,7 @@ ip_check() { local_ip=$(curl -6 ip.sb) ip_version="IPv6" elif [[ ${ip_version_fq} == 3 ]]; then - read -rp "请输入: " local_ip + read_optimize "请输入: " "local_ip" "NULL" ip_version=${local_ip} else local_ip=$(curl -4 ip.sb) @@ -2021,15 +2027,8 @@ vless_qr_link_image() { } vless_link_image_choice() { - echo -e "\n${GreenBG} 请选择生成的分享链接种类: ${Font}" - echo "1: V2RayN/V2RayNG/Qv2ray" - read -rp "请输入: " link_version - [[ -z ${link_version} ]] && link_version=1 - if [[ $link_version == 1 ]]; then - vless_qr_link_image - else - vless_qr_link_image - fi + echo -e "\n${GreenBG} 生成分享链接: ${Font}" + vless_qr_link_image } info_extraction() { @@ -2241,8 +2240,8 @@ tls_type() { echo -e "${GreenBG} 建议选择 TLS1.3 only (安全模式) ${Font}" echo -e "1: TLS1.2 and TLS1.3 (兼容模式)" echo -e "${Red}2${Font}: TLS1.3 only (安全模式)" - read -rp "请输入: " tls_version - [[ -z ${tls_version} ]] && tls_version=2 + local choose_network + read_optimize "请输入: " "choose_network" 2 1 2 "请输入有效的数字" if [[ $tls_version == 1 ]]; then # if [[ ${tls_mode} == "TLS" ]]; then # sed -i "s/^\( *\)ssl_protocols\( *\).*/\1ssl_protocols\2TLSv1.2 TLSv1.3;/" $nginx_conf @@ -2337,15 +2336,16 @@ show_user() { echo -e "${GreenBG} 请选择 显示用户使用的协议 ws/gRPC ${Font}" echo -e "${Red}1${Font}: ws (默认)" echo "2: gRPC" - read -rp "请输入: " choose_user_prot - [[ -z ${choose_user_prot} ]] && choose_user_prot=1 + local choose_user_prot + read_optimize "请输入: " "choose_user_prot" 1 1 2 "请输入有效的数字" choose_user_prot=$((choose_user_prot - 1)) elif [[ ${tls_mode} == "Reality" ]]; then choose_user_prot=0 fi echo -e "\n${GreenBG} 请选择 要显示的用户编号: ${Font}" jq -r -c .inbounds[${choose_user_prot}].settings.clients[].email ${xray_conf} | awk '{print NR""": "$0}' - read -rp "请输入: " show_user_index + local show_user_index + read_optimize "请输入: " "show_user_index" "NULL" if [[ $(jq -r '.inbounds['${choose_user_prot}'].settings.clients|length' ${xray_conf}) -lt ${show_user_index} ]] || [[ ${show_user_index} == 0 ]]; then echo -e "${Error} ${RedBG} 选择错误! ${Font}" show_user @@ -2404,8 +2404,8 @@ add_user() { echo -e "${GreenBG} 请选择 添加用户使用的协议 ws/gRPC ${Font}" echo -e "${Red}1${Font}: ws (默认)" echo "2: gRPC" - read -rp "请输入: " choose_user_prot - [[ -z ${choose_user_prot} ]] && choose_user_prot=1 + local choose_user_prot + read_optimize "请输入: " "choose_user_prot" 1 1 2 "请输入有效的数字" choose_user_prot=$((choose_user_prot - 1)) local reality_user_more="" elif [[ ${tls_mode} == "Reality" ]]; then @@ -2443,15 +2443,16 @@ remove_user() { echo -e "${GreenBG} 请选择 删除用户使用的协议 ws/gRPC ${Font}" echo -e "${Red}1${Font}: ws (默认)" echo "2: gRPC" - read -rp "请输入: " choose_user_prot - [[ -z ${choose_user_prot} ]] && choose_user_prot=1 + local choose_user_prot + read_optimize "请输入: " "choose_user_prot" 1 1 2 "请输入有效的数字" choose_user_prot=$((choose_user_prot - 1)) elif [[ ${tls_mode} == "Reality" ]]; then choose_user_prot=0 fi echo -e "\n${GreenBG} 请选择 要删除的用户编号 ${Font}" jq -r -c .inbounds[${choose_user_prot}].settings.clients[].email ${xray_conf} | awk '{print NR""": "$0}' - read -rp "请输入: " del_user_index + local del_user_index + read_optimize "请输入: " "del_user_index" "NULL" if [[ $(jq -r '.inbounds['${choose_user_prot}'].settings.clients|length' ${xray_conf}) -lt ${del_user_index} ]] || [[ ${show_user_index} == 0 ]]; then echo -e "${Error} ${RedBG} 选择错误! ${Font}" remove_user @@ -3192,7 +3193,8 @@ menu() { echo -e "${Green}31.${Font} 清空 证书文件" echo -e "${Green}32.${Font} 退出 \n" - read -rp "请输入数字: " menu_num + local menu_num + read_optimize "请输入选项: " "menu_num" "NULL" 0 32 "请输入 0 到 32 之间的有效数字" case $menu_num in 0) update_sh