Linux挂载FTP、WEBDAV、SMB为本地目录交互式脚本(可挂载多个相同协议)

此版本可挂载多个相同协议

1.保存文件为multi_mount_manager.sh
2.切换root用户执行脚本

#!/bin/bash
# multi_mount_manager.sh - 多协议挂载管理工具(支持多个相同协议远端)

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
ORANGE='\033[0;33m'
WHITE='\033[1;37m'
NC='\033[0m' # No Color

# 配置文件目录
CONFIG_DIR="$HOME/.mount_manager"
CONFIGS_DIR="$CONFIG_DIR/configs"
MOUNT_HISTORY="$CONFIG_DIR/mount_history"
ACTIVE_MOUNTS="$CONFIG_DIR/active_mounts"
CURRENT_PROFILE="$CONFIG_DIR/current_profile"

# 默认配置
DEFAULT_MOUNT_BASE="$HOME/mnt"

# 当前选中的配置
CURRENT_FTP_CONFIG=""
CURRENT_WEBDAV_CONFIG=""
CURRENT_SMB_CONFIG=""

# 加载所有配置
load_all_configs() {
    mkdir -p "$CONFIGS_DIR"
    
    # 如果配置文件目录为空,创建默认配置
    if [ ! -f "$CONFIGS_DIR/default_ftp.cfg" ]; then
        create_default_configs
    fi
    
    # 加载当前配置文件
    if [ -f "$CURRENT_PROFILE" ]; then
        source "$CURRENT_PROFILE"
    else
        echo "default" > "$CURRENT_PROFILE"
        CURRENT_PROFILE_NAME="default"
    fi
}

# 创建默认配置文件
create_default_configs() {
    # 创建默认FTP配置
    cat > "$CONFIGS_DIR/default_ftp.cfg" << EOF CONFIG_NAME="default_ftp" PROTOCOL="ftp" FTP_HOST="" FTP_USER="" FTP_PASS="" FTP_PORT="21" FTP_REMOTE_PATH="/" FTP_MOUNT_POINT="$DEFAULT_MOUNT_BASE/ftp" ENABLED="true" DESCRIPTION="默认FTP配置" EOF # 创建默认WebDAV配置 cat > "$CONFIGS_DIR/default_webdav.cfg" << EOF CONFIG_NAME="default_webdav" PROTOCOL="webdav" WEBDAV_URL="" WEBDAV_USER="" WEBDAV_PASS="" WEBDAV_REMOTE_PATH="/" WEBDAV_MOUNT_POINT="$DEFAULT_MOUNT_BASE/webdav" ENABLED="true" DESCRIPTION="默认WebDAV配置" EOF # 创建默认SMB配置 cat > "$CONFIGS_DIR/default_smb.cfg" << EOF CONFIG_NAME="default_smb" PROTOCOL="smb" SMB_SERVER="" SMB_SHARE="" SMB_USER="" SMB_PASS="" SMB_DOMAIN="WORKGROUP" SMB_REMOTE_PATH="/" SMB_MOUNT_POINT="$DEFAULT_MOUNT_BASE/smb" ENABLED="true" DESCRIPTION="默认SMB配置" EOF } # 加载配置 load_config() { local config_name="$1" local config_file="$CONFIGS_DIR/$config_name.cfg" if [ -f "$config_file" ]; then source "$config_file" return 0 else echo -e "${RED}配置不存在: $config_name${NC}" return 1 fi } # 保存配置 save_config() { local config_name="$1" local config_file="$CONFIGS_DIR/$config_name.cfg" case $PROTOCOL in ftp) cat > "$config_file" << EOF CONFIG_NAME="$CONFIG_NAME" PROTOCOL="ftp" FTP_HOST="$FTP_HOST" FTP_USER="$FTP_USER" FTP_PASS="$FTP_PASS" FTP_PORT="$FTP_PORT" FTP_REMOTE_PATH="$FTP_REMOTE_PATH" FTP_MOUNT_POINT="$FTP_MOUNT_POINT" ENABLED="$ENABLED" DESCRIPTION="$DESCRIPTION" EOF ;; webdav) cat > "$config_file" << EOF CONFIG_NAME="$CONFIG_NAME" PROTOCOL="webdav" WEBDAV_URL="$WEBDAV_URL" WEBDAV_USER="$WEBDAV_USER" WEBDAV_PASS="$WEBDAV_PASS" WEBDAV_REMOTE_PATH="$WEBDAV_REMOTE_PATH" WEBDAV_MOUNT_POINT="$WEBDAV_MOUNT_POINT" ENABLED="$ENABLED" DESCRIPTION="$DESCRIPTION" EOF ;; smb) cat > "$config_file" << EOF CONFIG_NAME="$CONFIG_NAME" PROTOCOL="smb" SMB_SERVER="$SMB_SERVER" SMB_SHARE="$SMB_SHARE" SMB_USER="$SMB_USER" SMB_PASS="$SMB_PASS" SMB_DOMAIN="$SMB_DOMAIN" SMB_REMOTE_PATH="$SMB_REMOTE_PATH" SMB_MOUNT_POINT="$SMB_MOUNT_POINT" ENABLED="$ENABLED" DESCRIPTION="$DESCRIPTION" EOF ;; esac chmod 600 "$config_file" echo -e "${GREEN}配置已保存: $config_name${NC}" } # 列出所有配置 list_configs() { local protocol="$1" echo -e "${CYAN}可用的${protocol^^}配置:${NC}" echo local count=0 for config_file in "$CONFIGS_DIR"/*.cfg; do if [ -f "$config_file" ]; then source "$config_file" 2>/dev/null
            if [ "$PROTOCOL" = "$protocol" ]; then
                count=$((count + 1))
                local status=""
                if [ "$ENABLED" = "true" ]; then
                    status="${GREEN}✓${NC}"
                else
                    status="${RED}✗${NC}"
                fi
                
                # 检查是否已挂载
                local mounted=""
                if mount | grep -q "$FTP_MOUNT_POINT" 2>/dev/null || \
                   mount | grep -q "$WEBDAV_MOUNT_POINT" 2>/dev/null || \
                   mount | grep -q "$SMB_MOUNT_POINT" 2>/dev/null; then
                    mounted="${GREEN}[已挂载]${NC}"
                fi
                
                echo -e "  $count. $status ${WHITE}$CONFIG_NAME${NC} $mounted"
                echo -e "     描述: $DESCRIPTION"
                case $protocol in
                    ftp)
                        echo -e "     服务器: $FTP_HOST:$FTP_PORT$FTP_REMOTE_PATH"
                        echo -e "     挂载点: $FTP_MOUNT_POINT"
                        ;;
                    webdav)
                        echo -e "     URL: $WEBDAV_URL$WEBDAV_REMOTE_PATH"
                        echo -e "     挂载点: $WEBDAV_MOUNT_POINT"
                        ;;
                    smb)
                        echo -e "     共享: //$SMB_SERVER/$SMB_SHARE$SMB_REMOTE_PATH"
                        echo -e "     挂载点: $SMB_MOUNT_POINT"
                        ;;
                esac
                echo
            fi
        fi
    done
    
    if [ $count -eq 0 ]; then
        echo -e "${YELLOW}没有找到$protocol配置${NC}"
        return 1
    fi
    
    return 0
}

# 选择配置
select_config() {
    local protocol="$1"
    
    if ! list_configs "$protocol"; then
        return 1
    fi
    
    echo -e "${CYAN}[操作选项]${NC}"
    echo "1. 选择配置"
    echo "2. 创建新配置"
    echo "3. 编辑配置"
    echo "4. 删除配置"
    echo "5. 复制配置"
    echo "0. 返回"
    echo
    read -p "请选择: " choice
    
    case $choice in
        1)
            read -p "输入配置编号或名称: " selection
            select_config_by_input "$protocol" "$selection"
            ;;
        2)
            create_new_config "$protocol"
            ;;
        3)
            edit_config "$protocol"
            ;;
        4)
            delete_config "$protocol"
            ;;
        5)
            clone_config "$protocol"
            ;;
        0)
            return 1
            ;;
        *)
            echo -e "${RED}无效选择${NC}"
            return 1
            ;;
    esac
    
    return 0
}

# 通过输入选择配置
select_config_by_input() {
    local protocol="$1"
    local input="$2"
    
    local configs=()
    local index=1
    
    # 收集所有配置
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            if [ "$PROTOCOL" = "$protocol" ]; then
                if [ "$index" = "$input" ] || [ "$CONFIG_NAME" = "$input" ]; then
                    echo -e "${GREEN}已选择配置: $CONFIG_NAME${NC}"
                    
                    # 设置当前配置
                    case $protocol in
                        ftp)
                            CURRENT_FTP_CONFIG="$CONFIG_NAME"
                            ;;
                        webdav)
                            CURRENT_WEBDAV_CONFIG="$CONFIG_NAME"
                            ;;
                        smb)
                            CURRENT_SMB_CONFIG="$CONFIG_NAME"
                            ;;
                    esac
                    return 0
                fi
                index=$((index + 1))
            fi
        fi
    done
    
    echo -e "${RED}未找到配置: $input${NC}"
    return 1
}

# 创建新配置
create_new_config() {
    local protocol="$1"
    
    echo -e "${GREEN}=== 创建新的${protocol^^}配置 ===${NC}"
    
    read -p "配置名称 (英文,不带空格): " config_name
    if [ -z "$config_name" ]; then
        echo -e "${RED}配置名称不能为空${NC}"
        return 1
    fi
    
    # 检查是否已存在
    if [ -f "$CONFIGS_DIR/$config_name.cfg" ]; then
        echo -e "${RED}配置已存在: $config_name${NC}"
        read -p "是否覆盖?(y/N): " overwrite
        if [[ ! "$overwrite" =~ ^[Yy]$ ]]; then
            return 1
        fi
    fi
    
    read -p "配置描述: " description
    
    # 设置默认值
    CONFIG_NAME="$config_name"
    PROTOCOL="$protocol"
    DESCRIPTION="$description"
    ENABLED="true"
    
    case $protocol in
        ftp)
            configure_ftp_new
            ;;
        webdav)
            configure_webdav_new
            ;;
        smb)
            configure_smb_new
            ;;
    esac
    
    save_config "$config_name"
    
    # 设置为当前配置
    case $protocol in
        ftp)
            CURRENT_FTP_CONFIG="$config_name"
            ;;
        webdav)
            CURRENT_WEBDAV_CONFIG="$config_name"
            ;;
        smb)
            CURRENT_SMB_CONFIG="$config_name"
            ;;
    esac
}

# 编辑配置
edit_config() {
    local protocol="$1"
    
    if ! list_configs "$protocol"; then
        return
    fi
    
    read -p "输入要编辑的配置编号或名称: " selection
    
    local config_name=""
    local index=1
    
    # 查找配置
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            if [ "$PROTOCOL" = "$protocol" ]; then
                if [ "$index" = "$selection" ] || [ "$CONFIG_NAME" = "$selection" ]; then
                    config_name="$CONFIG_NAME"
                    break
                fi
                index=$((index + 1))
            fi
        fi
    done
    
    if [ -z "$config_name" ]; then
        echo -e "${RED}未找到配置${NC}"
        return
    fi
    
    load_config "$config_name"
    
    echo -e "${GREEN}=== 编辑配置: $config_name ===${NC}"
    
    read -p "新描述 [当前: $DESCRIPTION]: " new_desc
    [ -n "$new_desc" ] && DESCRIPTION="$new_desc"
    
    read -p "是否启用 (true/false) [当前: $ENABLED]: " new_enabled
    [ -n "$new_enabled" ] && ENABLED="$new_enabled"
    
    case $protocol in
        ftp)
            configure_ftp_edit
            ;;
        webdav)
            configure_webdav_edit
            ;;
        smb)
            configure_smb_edit
            ;;
    esac
    
    save_config "$config_name"
    echo -e "${GREEN}配置已更新: $config_name${NC}"
}

# 删除配置
delete_config() {
    local protocol="$1"
    
    if ! list_configs "$protocol"; then
        return
    fi
    
    read -p "输入要删除的配置编号或名称: " selection
    
    local config_name=""
    local config_file=""
    local index=1
    
    # 查找配置
    for cfg_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$cfg_file" ]; then
            source "$cfg_file" 2>/dev/null
            if [ "$PROTOCOL" = "$protocol" ]; then
                if [ "$index" = "$selection" ] || [ "$CONFIG_NAME" = "$selection" ]; then
                    config_name="$CONFIG_NAME"
                    config_file="$cfg_file"
                    break
                fi
                index=$((index + 1))
            fi
        fi
    done
    
    if [ -z "$config_name" ]; then
        echo -e "${RED}未找到配置${NC}"
        return
    fi
    
    echo -e "${RED}警告:这将删除配置 '$config_name'${NC}"
    read -p "确认删除?(y/N): " confirm
    if [[ "$confirm" =~ ^[Yy]$ ]]; then
        rm -f "$config_file"
        
        # 如果删除的是当前配置,清空当前配置
        case $protocol in
            ftp)
                if [ "$CURRENT_FTP_CONFIG" = "$config_name" ]; then
                    CURRENT_FTP_CONFIG=""
                fi
                ;;
            webdav)
                if [ "$CURRENT_WEBDAV_CONFIG" = "$config_name" ]; then
                    CURRENT_WEBDAV_CONFIG=""
                fi
                ;;
            smb)
                if [ "$CURRENT_SMB_CONFIG" = "$config_name" ]; then
                    CURRENT_SMB_CONFIG=""
                fi
                ;;
        esac
        
        echo -e "${GREEN}配置已删除: $config_name${NC}"
    else
        echo -e "${YELLOW}删除已取消${NC}"
    fi
}

# 复制配置
clone_config() {
    local protocol="$1"
    
    if ! list_configs "$protocol"; then
        return
    fi
    
    read -p "输入要复制的配置编号或名称: " selection
    
    local source_config=""
    local index=1
    
    # 查找配置
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            if [ "$PROTOCOL" = "$protocol" ]; then
                if [ "$index" = "$selection" ] || [ "$CONFIG_NAME" = "$selection" ]; then
                    source_config="$CONFIG_NAME"
                    break
                fi
                index=$((index + 1))
            fi
        fi
    done
    
    if [ -z "$source_config" ]; then
        echo -e "${RED}未找到配置${NC}"
        return
    fi
    
    load_config "$source_config"
    
    read -p "新配置名称 (英文,不带空格): " new_name
    if [ -z "$new_name" ]; then
        echo -e "${RED}配置名称不能为空${NC}"
        return
    fi
    
    if [ -f "$CONFIGS_DIR/$new_name.cfg" ]; then
        echo -e "${RED}配置已存在: $new_name${NC}"
        return
    fi
    
    CONFIG_NAME="$new_name"
    DESCRIPTION="$DESCRIPTION (副本)"
    
    save_config "$new_name"
    echo -e "${GREEN}配置已复制为: $new_name${NC}"
}

# FTP配置(新建)
configure_ftp_new() {
    read -p "FTP服务器地址: " FTP_HOST
    read -p "端口 [默认: 21]: " FTP_PORT
    FTP_PORT=${FTP_PORT:-21}
    read -p "用户名: " FTP_USER
    read -s -p "密码: " FTP_PASS
    echo
    read -p "远端路径 [默认: /]: " FTP_REMOTE_PATH
    FTP_REMOTE_PATH=${FTP_REMOTE_PATH:-/}
    read -p "本地挂载点目录 [默认: $DEFAULT_MOUNT_BASE/ftp_${CONFIG_NAME}]: " mount_point
    FTP_MOUNT_POINT="${mount_point:-$DEFAULT_MOUNT_BASE/ftp_${CONFIG_NAME}}"
    
    # 创建挂载点目录
    mkdir -p "$FTP_MOUNT_POINT"
}

# FTP配置(编辑)
configure_ftp_edit() {
    read -p "FTP服务器地址 [当前: $FTP_HOST]: " input
    [ -n "$input" ] && FTP_HOST="$input"
    
    read -p "端口 [当前: $FTP_PORT]: " input
    [ -n "$input" ] && FTP_PORT="$input"
    
    read -p "用户名 [当前: $FTP_USER]: " input
    [ -n "$input" ] && FTP_USER="$input"
    
    read -s -p "密码 [输入新密码或回车保持]: " input
    echo
    [ -n "$input" ] && FTP_PASS="$input"
    
    read -p "远端路径 [当前: $FTP_REMOTE_PATH]: " input
    [ -n "$input" ] && FTP_REMOTE_PATH="$input"
    
    read -p "本地挂载点目录 [当前: $FTP_MOUNT_POINT]: " input
    [ -n "$input" ] && FTP_MOUNT_POINT="$input"
    
    # 创建挂载点目录
    mkdir -p "$FTP_MOUNT_POINT"
}

# WebDAV配置(新建)
configure_webdav_new() {
    read -p "WebDAV基础URL (如: http://example.com): " WEBDAV_URL
    read -p "用户名: " WEBDAV_USER
    read -s -p "密码: " WEBDAV_PASS
    echo
    read -p "远端路径 [默认: /]: " WEBDAV_REMOTE_PATH
    WEBDAV_REMOTE_PATH=${WEBDAV_REMOTE_PATH:-/}
    read -p "本地挂载点目录 [默认: $DEFAULT_MOUNT_BASE/webdav_${CONFIG_NAME}]: " mount_point
    WEBDAV_MOUNT_POINT="${mount_point:-$DEFAULT_MOUNT_BASE/webdav_${CONFIG_NAME}}"
    
    # 创建挂载点目录
    mkdir -p "$WEBDAV_MOUNT_POINT"
}

# WebDAV配置(编辑)
configure_webdav_edit() {
    read -p "WebDAV基础URL [当前: $WEBDAV_URL]: " input
    [ -n "$input" ] && WEBDAV_URL="$input"
    
    read -p "用户名 [当前: $WEBDAV_USER]: " input
    [ -n "$input" ] && WEBDAV_USER="$input"
    
    read -s -p "密码 [输入新密码或回车保持]: " input
    echo
    [ -n "$input" ] && WEBDAV_PASS="$input"
    
    read -p "远端路径 [当前: $WEBDAV_REMOTE_PATH]: " input
    [ -n "$input" ] && WEBDAV_REMOTE_PATH="$input"
    
    read -p "本地挂载点目录 [当前: $WEBDAV_MOUNT_POINT]: " input
    [ -n "$input" ] && WEBDAV_MOUNT_POINT="$input"
    
    # 创建挂载点目录
    mkdir -p "$WEBDAV_MOUNT_POINT"
}

# SMB配置(新建)
configure_smb_new() {
    read -p "SMB服务器地址: " SMB_SERVER
    read -p "共享名称: " SMB_SHARE
    read -p "域名 [默认: WORKGROUP]: " SMB_DOMAIN
    SMB_DOMAIN=${SMB_DOMAIN:-WORKGROUP}
    read -p "用户名: " SMB_USER
    read -s -p "密码: " SMB_PASS
    echo
    read -p "远端路径 [默认: /]: " SMB_REMOTE_PATH
    SMB_REMOTE_PATH=${SMB_REMOTE_PATH:-/}
    read -p "本地挂载点目录 [默认: $DEFAULT_MOUNT_BASE/smb_${CONFIG_NAME}]: " mount_point
    SMB_MOUNT_POINT="${mount_point:-$DEFAULT_MOUNT_BASE/smb_${CONFIG_NAME}}"
    
    # 创建挂载点目录
    mkdir -p "$SMB_MOUNT_POINT"
}

# SMB配置(编辑)
configure_smb_edit() {
    read -p "SMB服务器地址 [当前: $SMB_SERVER]: " input
    [ -n "$input" ] && SMB_SERVER="$input"
    
    read -p "共享名称 [当前: $SMB_SHARE]: " input
    [ -n "$input" ] && SMB_SHARE="$input"
    
    read -p "域名 [当前: $SMB_DOMAIN]: " input
    [ -n "$input" ] && SMB_DOMAIN="$input"
    
    read -p "用户名 [当前: $SMB_USER]: " input
    [ -n "$input" ] && SMB_USER="$input"
    
    read -s -p "密码 [输入新密码或回车保持]: " input
    echo
    [ -n "$input" ] && SMB_PASS="$input"
    
    read -p "远端路径 [当前: $SMB_REMOTE_PATH]: " input
    [ -n "$input" ] && SMB_REMOTE_PATH="$input"
    
    read -p "本地挂载点目录 [当前: $SMB_MOUNT_POINT]: " input
    [ -n "$input" ] && SMB_MOUNT_POINT="$input"
    
    # 创建挂载点目录
    mkdir -p "$SMB_MOUNT_POINT"
}

# 记录挂载历史
record_mount() {
    local protocol="$1"
    local config_name="$2"
    local server="$3"
    local remote_path="$4"
    local mount_point="$5"
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    
    echo "$timestamp|$protocol|$config_name|$server|$remote_path|$mount_point" >> "$MOUNT_HISTORY"
}

# 记录活动挂载
record_active_mount() {
    local config_name="$1"
    local mount_point="$2"
    
    # 删除旧的记录
    grep -v "^$config_name|" "$ACTIVE_MOUNTS" > "$ACTIVE_MOUNTS.tmp" 2>/dev/null || true
    mv "$ACTIVE_MOUNTS.tmp" "$ACTIVE_MOUNTS" 2>/dev/null || true
    
    # 添加新记录
    echo "$config_name|$mount_point|$(date '+%Y-%m-%d %H:%M:%S')" >> "$ACTIVE_MOUNTS"
}

# 移除活动挂载记录
remove_active_mount() {
    local config_name="$1"
    
    grep -v "^$config_name|" "$ACTIVE_MOUNTS" > "$ACTIVE_MOUNTS.tmp" 2>/dev/null || true
    mv "$ACTIVE_MOUNTS.tmp" "$ACTIVE_MOUNTS" 2>/dev/null || true
}

# 检查依赖
check_dependencies() {
    local missing_pkgs=""
    
    # 检查FTP依赖
    if ! command -v curlftpfs &> /dev/null; then
        missing_pkgs+=" curlftpfs"
    fi
    
    # 检查WebDAV依赖
    if ! command -v mount.davfs &> /dev/null; then
        missing_pkgs+=" davfs2"
    fi
    
    # 检查SMB依赖
    if ! command -v mount.cifs &> /dev/null; then
        missing_pkgs+=" cifs-utils"
    fi
    
    # 检查curl(用于目录浏览)
    if ! command -v curl &> /dev/null; then
        missing_pkgs+=" curl"
    fi
    
    if [ -n "$missing_pkgs" ]; then
        echo -e "${YELLOW}缺少必要软件包:$missing_pkgs${NC}"
        echo -e "${BLUE}是否安装?(y/N): ${NC}"
        read -n 1 install_choice
        echo
        
        if [[ "$install_choice" =~ ^[Yy]$ ]]; then
            install_dependencies "$missing_pkgs"
        else
            echo -e "${RED}部分功能可能无法使用${NC}"
        fi
    fi
}

# 安装依赖
install_dependencies() {
    local pkgs="$1"
    
    echo -e "${BLUE}正在安装依赖包...${NC}"
    
    if [ -f /etc/debian_version ]; then
        sudo apt update
        sudo apt install -y $pkgs
    elif [ -f /etc/redhat-release ]; then
        sudo yum install -y $pkgs
    elif [ -f /etc/arch-release ]; then
        sudo pacman -S --noconfirm $pkgs
    elif [ -f /etc/fedora-release ]; then
        sudo dnf install -y $pkgs
    else
        echo -e "${RED}无法识别发行版,请手动安装: $pkgs${NC}"
        return 1
    fi
    
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}依赖安装完成${NC}"
    else
        echo -e "${RED}依赖安装失败${NC}"
    fi
}

# 显示主菜单
show_main_menu() {
    clear
    echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
    echo -e "${BLUE}      多协议挂载管理工具 v3.0 (支持多个相同协议远端)${NC}"
    echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
    echo
    
    # 显示当前活动挂载
    echo -e "${YELLOW}[当前活动挂载]${NC}"
    show_active_mounts
    
    echo
    echo -e "${GREEN}[主菜单选项]${NC}"
    echo -e "  1. 挂载管理 (选择协议和配置)"
    echo -e "  2. 配置管理 (创建/编辑/删除配置)"
    echo -e "  3. 批量操作"
    echo -e "  4. 测试连接"
    echo -e "  5. 查看挂载历史"
    echo -e "  6. 设置开机自动挂载"
    echo -e "  7. 创建快捷命令"
    echo -e "  8. 配置文件管理"
    echo -e "  0. 退出"
    echo
    echo -e "${BLUE}═══════════════════════════════════════════════════════════════${NC}"
}

# 显示活动挂载
show_active_mounts() {
    if [ -f "$ACTIVE_MOUNTS" ] && [ -s "$ACTIVE_MOUNTS" ]; then
        echo -e "${CYAN}配置名称                挂载点                          挂载时间${NC}"
        echo "────────────────────────────────────────────────────────────────────"
        while IFS='|' read -r config_name mount_point mount_time; do
            # 检查是否真的已挂载
            if mount | grep -q "$mount_point"; then
                status="${GREEN}✓${NC}"
            else
                status="${RED}✗${NC}"
            fi
            
            # 获取协议和描述
            if [ -f "$CONFIGS_DIR/$config_name.cfg" ]; then
                source "$CONFIGS_DIR/$config_name.cfg" 2>/dev/null
                printf "%-25s %-30s %s %s\n" "$config_name" "$mount_point" "$mount_time" "$status"
            fi
        done < "$ACTIVE_MOUNTS" else echo -e " ${YELLOW}没有活动挂载${NC}" fi } # 挂载管理菜单 show_mount_menu() { clear echo -e "${BLUE}══════════════════════════════════════════════════${NC}" echo -e "${BLUE} 挂载管理${NC}" echo -e "${BLUE}══════════════════════════════════════════════════${NC}" echo echo -e "${CYAN}[选择协议]${NC}" echo -e " 1. ${GREEN}FTP${NC} - 文件传输协议" echo -e " 2. ${YELLOW}WebDAV${NC} - Web分布式创作和版本控制" echo -e " 3. ${PURPLE}SMB/CIFS${NC} - Windows文件共享" echo -e " 4. ${BLUE}查看所有活动挂载${NC}" echo -e " 5. ${RED}批量卸载所有${NC}" echo -e " 0. 返回主菜单" echo echo -e "${BLUE}══════════════════════════════════════════════════${NC}" } # 配置管理菜单 show_config_menu() { clear echo -e "${BLUE}══════════════════════════════════════════════════${NC}" echo -e "${BLUE} 配置管理${NC}" echo -e "${BLUE}══════════════════════════════════════════════════${NC}" echo echo -e "${CYAN}[选择协议]${NC}" echo -e " 1. ${GREEN}FTP配置管理${NC}" echo -e " 2. ${YELLOW}WebDAV配置管理${NC}" echo -e " 3. ${PURPLE}SMB配置管理${NC}" echo -e " 4. ${BLUE}查看所有配置${NC}" echo -e " 5. ${ORANGE}配置导入/导出${NC}" echo -e " 0. 返回主菜单" echo echo -e "${BLUE}══════════════════════════════════════════════════${NC}" } # FTP挂载功能 mount_ftp_config() { local config_name="$1" if ! load_config "$config_name"; then return 1 fi if [ "$ENABLED" != "true" ]; then echo -e "${RED}配置已被禁用: $config_name${NC}" return 1 fi echo -e "${GREEN}[挂载FTP: $config_name]${NC}" # 检查是否已挂载 if mount | grep -q "$FTP_MOUNT_POINT"; then echo -e "${YELLOW}FTP已经挂载到 $FTP_MOUNT_POINT${NC}" read -p "是否重新挂载?(y/N): " choice if [[ ! "$choice" =~ ^[Yy]$ ]]; then return fi umount_ftp_config "$config_name" fi # 创建挂载点 mkdir -p "$FTP_MOUNT_POINT" echo -e "${BLUE}正在挂载FTP...${NC}" echo -e "服务器: ${FTP_HOST}:${FTP_PORT}" echo -e "远端路径: $FTP_REMOTE_PATH" echo -e "挂载点: $FTP_MOUNT_POINT" local mount_cmd local full_url if [ -z "$FTP_PASS" ]; then # 匿名访问 full_url="ftp://$FTP_HOST:$FTP_PORT$FTP_REMOTE_PATH" mount_cmd="curlftpfs -o allow_other,uid=$(id -u),gid=$(id -g) \"$full_url\" \"$FTP_MOUNT_POINT\"" else # 认证访问 full_url="ftp://$FTP_USER:$FTP_PASS@$FTP_HOST:$FTP_PORT$FTP_REMOTE_PATH" mount_cmd="curlftpfs -o allow_other,uid=$(id -u),gid=$(id -g) \"$full_url\" \"$FTP_MOUNT_POINT\"" fi echo -e "${YELLOW}执行命令: $mount_cmd${NC}" if eval "$mount_cmd"; then echo -e "${GREEN}✓ FTP挂载成功${NC}" record_mount "FTP" "$config_name" "$FTP_HOST:$FTP_PORT" "$FTP_REMOTE_PATH" "$FTP_MOUNT_POINT" record_active_mount "$config_name" "$FTP_MOUNT_POINT" else echo -e "${RED}✗ FTP挂载失败${NC}" fi } # FTP卸载功能 umount_ftp_config() { local config_name="$1" if ! load_config "$config_name"; then return 1 fi if mount | grep -q "$FTP_MOUNT_POINT"; then echo -e "${BLUE}正在卸载FTP: $config_name...${NC}" fusermount -u "$FTP_MOUNT_POINT" 2>/dev/null || sudo fusermount -u "$FTP_MOUNT_POINT"
        echo -e "${GREEN}✓ FTP卸载完成: $config_name${NC}"
        remove_active_mount "$config_name"
    else
        echo -e "${YELLOW}FTP未挂载: $config_name${NC}"
    fi
}

# WebDAV挂载功能
mount_webdav_config() {
    local config_name="$1"
    
    if ! load_config "$config_name"; then
        return 1
    fi
    
    if [ "$ENABLED" != "true" ]; then
        echo -e "${RED}配置已被禁用: $config_name${NC}"
        return 1
    fi
    
    echo -e "${YELLOW}[挂载WebDAV: $config_name]${NC}"
    
    # 检查是否已挂载
    if mount | grep -q "$WEBDAV_MOUNT_POINT"; then
        echo -e "${YELLOW}WebDAV已经挂载到 $WEBDAV_MOUNT_POINT${NC}"
        read -p "是否重新挂载?(y/N): " choice
        if [[ ! "$choice" =~ ^[Yy]$ ]]; then
            return
        fi
        umount_webdav_config "$config_name"
    fi
    
    # 创建挂载点
    mkdir -p "$WEBDAV_MOUNT_POINT"
    
    local full_url="${WEBDAV_URL%/}${WEBDAV_REMOTE_PATH}"
    
    echo -e "${BLUE}正在挂载WebDAV...${NC}"
    echo -e "URL: $full_url"
    echo -e "挂载点: $WEBDAV_MOUNT_POINT"
    
    local mount_cmd
    if [ -z "$WEBDAV_PASS" ]; then
        # 匿名访问
        mount_cmd="sudo mount -t davfs \"$full_url\" \"$WEBDAV_MOUNT_POINT\" -o uid=$(id -u),gid=$(id -g),noexec"
    else
        # 认证访问
        mount_cmd="sudo mount -t davfs \"$full_url\" \"$WEBDAV_MOUNT_POINT\" -o uid=$(id -u),gid=$(id -g),noexec,username=$WEBDAV_USER,password=$WEBDAV_PASS"
    fi
    
    echo -e "${YELLOW}执行命令: $mount_cmd${NC}"
    
    if eval "$mount_cmd"; then
        echo -e "${GREEN}✓ WebDAV挂载成功${NC}"
        record_mount "WebDAV" "$config_name" "$WEBDAV_URL" "$WEBDAV_REMOTE_PATH" "$WEBDAV_MOUNT_POINT"
        record_active_mount "$config_name" "$WEBDAV_MOUNT_POINT"
    else
        echo -e "${RED}✗ WebDAV挂载失败${NC}"
    fi
}

# WebDAV卸载功能
umount_webdav_config() {
    local config_name="$1"
    
    if ! load_config "$config_name"; then
        return 1
    fi
    
    if mount | grep -q "$WEBDAV_MOUNT_POINT"; then
        echo -e "${BLUE}正在卸载WebDAV: $config_name...${NC}"
        sudo umount "$WEBDAV_MOUNT_POINT"
        echo -e "${GREEN}✓ WebDAV卸载完成: $config_name${NC}"
        remove_active_mount "$config_name"
    else
        echo -e "${YELLOW}WebDAV未挂载: $config_name${NC}"
    fi
}

# SMB挂载功能
mount_smb_config() {
    local config_name="$1"
    
    if ! load_config "$config_name"; then
        return 1
    fi
    
    if [ "$ENABLED" != "true" ]; then
        echo -e "${RED}配置已被禁用: $config_name${NC}"
        return 1
    fi
    
    echo -e "${PURPLE}[挂载SMB/CIFS: $config_name]${NC}"
    
    # 检查是否已挂载
    if mount | grep -q "$SMB_MOUNT_POINT"; then
        echo -e "${YELLOW}SMB已经挂载到 $SMB_MOUNT_POINT${NC}"
        read -p "是否重新挂载?(y/N): " choice
        if [[ ! "$choice" =~ ^[Yy]$ ]]; then
            return
        fi
        umount_smb_config "$config_name"
    fi
    
    # 创建挂载点
    mkdir -p "$SMB_MOUNT_POINT"
    
    local full_path="//$SMB_SERVER/$SMB_SHARE$SMB_REMOTE_PATH"
    
    echo -e "${BLUE}正在挂载SMB...${NC}"
    echo -e "共享: $full_path"
    echo -e "挂载点: $SMB_MOUNT_POINT"
    
    local mount_cmd
    if [ -z "$SMB_PASS" ]; then
        # 访客访问
        mount_cmd="sudo mount -t cifs \"$full_path\" \"$SMB_MOUNT_POINT\" -o guest,uid=$(id -u),gid=$(id -g),iocharset=utf8,file_mode=0777,dir_mode=0777"
    else
        # 认证访问
        mount_cmd="sudo mount -t cifs \"$full_path\" \"$SMB_MOUNT_POINT\" -o username=$SMB_USER,password=$SMB_PASS,domain=$SMB_DOMAIN,uid=$(id -u),gid=$(id -g),iocharset=utf8,file_mode=0777,dir_mode=0777"
    fi
    
    echo -e "${YELLOW}执行命令: $mount_cmd${NC}"
    
    if eval "$mount_cmd"; then
        echo -e "${GREEN}✓ SMB挂载成功${NC}"
        record_mount "SMB" "$config_name" "//$SMB_SERVER/$SMB_SHARE" "$SMB_REMOTE_PATH" "$SMB_MOUNT_POINT"
        record_active_mount "$config_name" "$SMB_MOUNT_POINT"
    else
        echo -e "${RED}✗ SMB挂载失败${NC}"
    fi
}

# SMB卸载功能
umount_smb_config() {
    local config_name="$1"
    
    if ! load_config "$config_name"; then
        return 1
    fi
    
    if mount | grep -q "$SMB_MOUNT_POINT"; then
        echo -e "${BLUE}正在卸载SMB: $config_name...${NC}"
        sudo umount "$SMB_MOUNT_POINT"
        echo -e "${GREEN}✓ SMB卸载完成: $config_name${NC}"
        remove_active_mount "$config_name"
    else
        echo -e "${YELLOW}SMB未挂载: $config_name${NC}"
    fi
}

# 批量操作菜单
show_batch_menu() {
    clear
    echo -e "${BLUE}══════════════════════════════════════════════════${NC}"
    echo -e "${BLUE}                  批量操作${NC}"
    echo -e "${BLUE}══════════════════════════════════════════════════${NC}"
    echo
    echo -e "${CYAN}[选择批量操作]${NC}"
    echo -e "  1. ${GREEN}挂载所有FTP配置${NC}"
    echo -e "  2. ${GREEN}卸载所有FTP配置${NC}"
    echo -e "  3. ${YELLOW}挂载所有WebDAV配置${NC}"
    echo -e "  4. ${YELLOW}卸载所有WebDAV配置${NC}"
    echo -e "  5. ${PURPLE}挂载所有SMB配置${NC}"
    echo -e "  6. ${PURPLE}卸载所有SMB配置${NC}"
    echo -e "  7. ${BLUE}挂载所有启用的配置${NC}"
    echo -e "  8. ${RED}卸载所有配置${NC}"
    echo -e "  0. 返回主菜单"
    echo
    echo -e "${BLUE}══════════════════════════════════════════════════${NC}"
}

# 挂载所有FTP配置
mount_all_ftp() {
    echo -e "${GREEN}[挂载所有FTP配置]${NC}"
    
    local count=0
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            if [ "$PROTOCOL" = "ftp" ] && [ "$ENABLED" = "true" ]; then
                mount_ftp_config "$CONFIG_NAME"
                count=$((count + 1))
            fi
        fi
    done
    
    if [ $count -eq 0 ]; then
        echo -e "${YELLOW}没有启用的FTP配置${NC}"
    else
        echo -e "${GREEN}已尝试挂载 $count 个FTP配置${NC}"
    fi
}

# 卸载所有FTP配置
umount_all_ftp() {
    echo -e "${RED}[卸载所有FTP配置]${NC}"
    
    local count=0
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            if [ "$PROTOCOL" = "ftp" ]; then
                umount_ftp_config "$CONFIG_NAME"
                count=$((count + 1))
            fi
        fi
    done
    
    echo -e "${GREEN}已尝试卸载 $count 个FTP配置${NC}"
}

# 挂载所有WebDAV配置
mount_all_webdav() {
    echo -e "${YELLOW}[挂载所有WebDAV配置]${NC}"
    
    local count=0
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            if [ "$PROTOCOL" = "webdav" ] && [ "$ENABLED" = "true" ]; then
                mount_webdav_config "$CONFIG_NAME"
                count=$((count + 1))
            fi
        fi
    done
    
    if [ $count -eq 0 ]; then
        echo -e "${YELLOW}没有启用的WebDAV配置${NC}"
    else
        echo -e "${GREEN}已尝试挂载 $count 个WebDAV配置${NC}"
    fi
}

# 卸载所有WebDAV配置
umount_all_webdav() {
    echo -e "${RED}[卸载所有WebDAV配置]${NC}"
    
    local count=0
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            if [ "$PROTOCOL" = "webdav" ]; then
                umount_webdav_config "$CONFIG_NAME"
                count=$((count + 1))
            fi
        fi
    done
    
    echo -e "${GREEN}已尝试卸载 $count 个WebDAV配置${NC}"
}

# 挂载所有SMB配置
mount_all_smb() {
    echo -e "${PURPLE}[挂载所有SMB配置]${NC}"
    
    local count=0
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            if [ "$PROTOCOL" = "smb" ] && [ "$ENABLED" = "true" ]; then
                mount_smb_config "$CONFIG_NAME"
                count=$((count + 1))
            fi
        fi
    done
    
    if [ $count -eq 0 ]; then
        echo -e "${YELLOW}没有启用的SMB配置${NC}"
    else
        echo -e "${GREEN}已尝试挂载 $count 个SMB配置${NC}"
    fi
}

# 卸载所有SMB配置
umount_all_smb() {
    echo -e "${RED}[卸载所有SMB配置]${NC}"
    
    local count=0
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            if [ "$PROTOCOL" = "smb" ]; then
                umount_smb_config "$CONFIG_NAME"
                count=$((count + 1))
            fi
        fi
    done
    
    echo -e "${GREEN}已尝试卸载 $count 个SMB配置${NC}"
}

# 挂载所有启用的配置
mount_all_enabled() {
    echo -e "${BLUE}[挂载所有启用的配置]${NC}"
    
    local ftp_count=0
    local webdav_count=0
    local smb_count=0
    
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            if [ "$ENABLED" = "true" ]; then
                case $PROTOCOL in
                    ftp)
                        mount_ftp_config "$CONFIG_NAME"
                        ftp_count=$((ftp_count + 1))
                        ;;
                    webdav)
                        mount_webdav_config "$CONFIG_NAME"
                        webdav_count=$((webdav_count + 1))
                        ;;
                    smb)
                        mount_smb_config "$CONFIG_NAME"
                        smb_count=$((smb_count + 1))
                        ;;
                esac
            fi
        fi
    done
    
    echo -e "${GREEN}挂载完成: FTP($ftp_count) WebDAV($webdav_count) SMB($smb_count)${NC}"
}

# 卸载所有配置
umount_all_configs() {
    echo -e "${RED}[卸载所有配置]${NC}"
    
    local total_count=0
    
    # 从活动挂载记录卸载
    if [ -f "$ACTIVE_MOUNTS" ]; then
        while IFS='|' read -r config_name mount_point mount_time; do
            if [ -f "$CONFIGS_DIR/$config_name.cfg" ]; then
                source "$CONFIGS_DIR/$config_name.cfg" 2>/dev/null
                case $PROTOCOL in
                    ftp)
                        umount_ftp_config "$config_name"
                        ;;
                    webdav)
                        umount_webdav_config "$config_name"
                        ;;
                    smb)
                        umount_smb_config "$config_name"
                        ;;
                esac
                total_count=$((total_count + 1))
            fi
        done < "$ACTIVE_MOUNTS" fi echo -e "${GREEN}已卸载 $total_count 个配置${NC}" } # 配置文件管理菜单 show_profile_menu() { clear echo -e "${BLUE}══════════════════════════════════════════════════${NC}" echo -e "${BLUE} 配置文件管理${NC}" echo -e "${BLUE}══════════════════════════════════════════════════${NC}" echo echo -e "${CYAN}[选择操作]${NC}" echo -e " 1. ${GREEN}导出所有配置${NC}" echo -e " 2. ${GREEN}导入配置${NC}" echo -e " 3. ${YELLOW}备份配置${NC}" echo -e " 4. ${YELLOW}恢复配置${NC}" echo -e " 5. ${RED}重置所有配置${NC}" echo -e " 6. ${BLUE}查看配置统计${NC}" echo -e " 0. 返回主菜单" echo echo -e "${BLUE}══════════════════════════════════════════════════${NC}" } # 导出所有配置 export_configs() { local export_dir="$CONFIG_DIR/exports" mkdir -p "$export_dir" local timestamp=$(date '+%Y%m%d_%H%M%S') local export_file="$export_dir/configs_backup_$timestamp.tar.gz" tar -czf "$export_file" -C "$CONFIG_DIR" configs if [ $? -eq 0 ]; then echo -e "${GREEN}配置已导出到: $export_file${NC}" echo -e "包含 $(ls "$CONFIGS_DIR"/*.cfg 2>/dev/null | wc -l) 个配置文件"
    else
        echo -e "${RED}导出失败${NC}"
    fi
}

# 导入配置
import_configs() {
    echo -e "${YELLOW}将配置文件 (.cfg) 放入目录: $CONFIGS_DIR/${NC}"
    echo -e "或者输入备份文件路径:"
    read -p "备份文件路径 (或回车跳过): " backup_file
    
    if [ -n "$backup_file" ] && [ -f "$backup_file" ]; then
        echo -e "${BLUE}正在从备份恢复...${NC}"
        tar -xzf "$backup_file" -C "$CONFIG_DIR"
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}配置已从备份恢复${NC}"
        else
            echo -e "${RED}恢复失败${NC}"
        fi
    else
        echo -e "${YELLOW}请手动将.cfg文件复制到 $CONFIGS_DIR/${NC}"
        echo -e "按回车键继续..."
        read
    fi
}

# 备份配置
backup_configs() {
    local backup_dir="$HOME/backups/mount_manager"
    mkdir -p "$backup_dir"
    
    local timestamp=$(date '+%Y%m%d_%H%M%S')
    local backup_file="$backup_dir/full_backup_$timestamp.tar.gz"
    
    tar -czf "$backup_file" -C "$CONFIG_DIR" .
    
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}完整备份已创建: $backup_file${NC}"
        echo -e "备份大小: $(du -h "$backup_file" | cut -f1)"
    else
        echo -e "${RED}备份失败${NC}"
    fi
}

# 恢复配置
restore_configs() {
    echo -e "${RED}警告:这将覆盖现有配置${NC}"
    read -p "确认恢复配置?(y/N): " confirm
    if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
        return
    fi
    
    echo -e "可用的备份文件:"
    local backup_dir="$HOME/backups/mount_manager"
    if [ -d "$backup_dir" ]; then
        ls -lh "$backup_dir"/*.tar.gz 2>/dev/null | nl
    else
        echo -e "${YELLOW}没有找到备份目录${NC}"
        return
    fi
    
    read -p "输入备份文件编号: " backup_num
    local backup_file=$(ls "$backup_dir"/*.tar.gz 2>/dev/null | sed -n "${backup_num}p")
    
    if [ -f "$backup_file" ]; then
        echo -e "${BLUE}正在从 $backup_file 恢复...${NC}"
        
        # 备份当前配置
        local temp_backup="$CONFIG_DIR/backup_before_restore.tar.gz"
        tar -czf "$temp_backup" -C "$CONFIG_DIR" .
        
        # 清空配置目录
        rm -rf "$CONFIG_DIR"/*
        
        # 恢复备份
        tar -xzf "$backup_file" -C "$CONFIG_DIR"
        
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}配置已成功恢复${NC}"
        else
            echo -e "${RED}恢复失败,尝试回滚${NC}"
            tar -xzf "$temp_backup" -C "$CONFIG_DIR"
        fi
        
        rm -f "$temp_backup"
    else
        echo -e "${RED}无效的备份文件${NC}"
    fi
}

# 重置所有配置
reset_configs() {
    echo -e "${RED}警告:这将删除所有配置!${NC}"
    read -p "确认重置所有配置?(输入'RESET'确认): " confirm
    
    if [ "$confirm" = "RESET" ]; then
        rm -rf "$CONFIG_DIR"
        echo -e "${GREEN}所有配置已重置${NC}"
        echo -e "请重新启动脚本"
        exit 0
    else
        echo -e "${YELLOW}重置已取消${NC}"
    fi
}

# 查看配置统计
show_config_stats() {
    echo -e "${CYAN}[配置统计]${NC}"
    echo
    
    local ftp_count=0
    local webdav_count=0
    local smb_count=0
    local enabled_count=0
    
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            case $PROTOCOL in
                ftp) ftp_count=$((ftp_count + 1)) ;;
                webdav) webdav_count=$((webdav_count + 1)) ;;
                smb) smb_count=$((smb_count + 1)) ;;
            esac
            [ "$ENABLED" = "true" ] && enabled_count=$((enabled_count + 1))
        fi
    done
    
    local total=$((ftp_count + webdav_count + smb_count))
    
    echo -e "总配置数: $total"
    echo -e "  FTP配置: $ftp_count"
    echo -e "  WebDAV配置: $webdav_count"
    echo -e "  SMB配置: $smb_count"
    echo -e "启用配置: $enabled_count"
    echo -e "禁用配置: $((total - enabled_count))"
    echo
    
    # 显示挂载点使用情况
    echo -e "${YELLOW}[挂载点使用情况]${NC}"
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            local mount_var="${PROTOCOL^^}_MOUNT_POINT"
            local mount_point="${!mount_var}"
            if mount | grep -q "$mount_point"; then
                echo -e "  ${GREEN}✓${NC} $CONFIG_NAME: $mount_point"
            fi
        fi
    done
}

# 测试连接
test_connections() {
    echo -e "${CYAN}=== 测试连接 ===${NC}"
    echo
    echo "1. 测试所有FTP连接"
    echo "2. 测试所有WebDAV连接"
    echo "3. 测试所有SMB连接"
    echo "4. 测试特定配置"
    echo "0. 返回"
    echo
    read -p "请选择: " choice
    
    case $choice in
        1)
            test_all_ftp
            ;;
        2)
            test_all_webdav
            ;;
        3)
            test_all_smb
            ;;
        4)
            test_specific_config
            ;;
        0)
            return
            ;;
        *)
            echo -e "${RED}无效选择${NC}"
            ;;
    esac
}

# 测试所有FTP连接
test_all_ftp() {
    echo -e "${GREEN}[测试所有FTP连接]${NC}"
    
    local success=0
    local total=0
    
    for config_file in "$CONFIGS_DIR"/*.cfg; do
        if [ -f "$config_file" ]; then
            source "$config_file" 2>/dev/null
            if [ "$PROTOCOL" = "ftp" ] && [ "$ENABLED" = "true" ]; then
                total=$((total + 1))
                echo -n "测试 $CONFIG_NAME ($FTP_HOST:$FTP_PORT)... "
                
                if [ -z "$FTP_PASS" ]; then
                    timeout 10 curl -s --list-only "ftp://$FTP_HOST:$FTP_PORT$FTP_REMOTE_PATH" > /dev/null 2>&1
                else
                    timeout 10 curl -s --list-only --user "$FTP_USER:$FTP_PASS" "ftp://$FTP_HOST:$FTP_PORT$FTP_REMOTE_PATH" > /dev/null 2>&1
                fi
                
                if [ $? -eq 0 ]; then
                    echo -e "${GREEN}✓${NC}"
                    success=$((success + 1))
                else
                    echo -e "${RED}✗${NC}"
                fi
            fi
        fi
    done
    
    echo -e "\n${BLUE}测试完成: $success/$total 成功${NC}"
}

# 主程序
main() {
    # 初始化
    mkdir -p "$CONFIGS_DIR"
    load_all_configs
    check_dependencies
    
    while true; do
        show_main_menu
        
        read -p "请选择操作 [0-8]: " choice
        
        case $choice in
            1)
                # 挂载管理
                while true; do
                    show_mount_menu
                    read -p "请选择 [0-5]: " mount_choice
                    
                    case $mount_choice in
                        1)
                            # FTP挂载
                            if select_config "ftp"; then
                                mount_ftp_config "$CURRENT_FTP_CONFIG"
                            fi
                            ;;
                        2)
                            # WebDAV挂载
                            if select_config "webdav"; then
                                mount_webdav_config "$CURRENT_WEBDAV_CONFIG"
                            fi
                            ;;
                        3)
                            # SMB挂载
                            if select_config "smb"; then
                                mount_smb_config "$CURRENT_SMB_CONFIG"
                            fi
                            ;;
                        4)
                            # 查看所有活动挂载
                            show_active_mounts
                            echo
                            echo -e "${YELLOW}[操作]${NC}"
                            echo "1. 卸载特定配置"
                            echo "2. 刷新状态"
                            echo "0. 返回"
                            read -p "请选择: " action
                            
                            case $action in
                                1)
                                    read -p "输入要卸载的配置名称: " config_name
                                    if [ -f "$CONFIGS_DIR/$config_name.cfg" ]; then
                                        source "$CONFIGS_DIR/$config_name.cfg"
                                        case $PROTOCOL in
                                            ftp) umount_ftp_config "$config_name" ;;
                                            webdav) umount_webdav_config "$config_name" ;;
                                            smb) umount_smb_config "$config_name" ;;
                                        esac
                                    else
                                        echo -e "${RED}配置不存在${NC}"
                                    fi
                                    ;;
                                2)
                                    continue
                                    ;;
                            esac
                            ;;
                        5)
                            # 批量卸载所有
                            umount_all_configs
                            ;;
                        0)
                            break
                            ;;
                        *)
                            echo -e "${RED}无效选择${NC}"
                            ;;
                    esac
                    
                    echo
                    read -p "按回车键继续..."
                done
                ;;
            2)
                # 配置管理
                while true; do
                    show_config_menu
                    read -p "请选择 [0-5]: " config_choice
                    
                    case $config_choice in
                        1)
                            # FTP配置管理
                            select_config "ftp"
                            ;;
                        2)
                            # WebDAV配置管理
                            select_config "webdav"
                            ;;
                        3)
                            # SMB配置管理
                            select_config "smb"
                            ;;
                        4)
                            # 查看所有配置
                            echo -e "${CYAN}[所有配置]${NC}"
                            echo
                            list_configs "ftp"
                            list_configs "webdav"
                            list_configs "smb"
                            ;;
                        5)
                            # 配置导入/导出
                            show_profile_menu
                            read -p "请选择 [0-6]: " profile_choice
                            
                            case $profile_choice in
                                1) export_configs ;;
                                2) import_configs ;;
                                3) backup_configs ;;
                                4) restore_configs ;;
                                5) reset_configs ;;
                                6) show_config_stats ;;
                                0) continue ;;
                                *) echo -e "${RED}无效选择${NC}" ;;
                            esac
                            ;;
                        0)
                            break
                            ;;
                        *)
                            echo -e "${RED}无效选择${NC}"
                            ;;
                    esac
                    
                    echo
                    read -p "按回车键继续..."
                done
                ;;
            3)
                # 批量操作
                while true; do
                    show_batch_menu
                    read -p "请选择 [0-8]: " batch_choice
                    
                    case $batch_choice in
                        1) mount_all_ftp ;;
                        2) umount_all_ftp ;;
                        3) mount_all_webdav ;;
                        4) umount_all_webdav ;;
                        5) mount_all_smb ;;
                        6) umount_all_smb ;;
                        7) mount_all_enabled ;;
                        8) umount_all_configs ;;
                        0) break ;;
                        *) echo -e "${RED}无效选择${NC}" ;;
                    esac
                    
                    echo
                    read -p "按回车键继续..."
                done
                ;;
            4)
                test_connections
                ;;
            5)
                show_mount_history
                ;;
            6)
                setup_autostart
                ;;
            7)
                create_shortcuts
                ;;
            8)
                show_profile_menu
                read -p "请选择 [0-6]: " profile_choice
                
                case $profile_choice in
                    1) export_configs ;;
                    2) import_configs ;;
                    3) backup_configs ;;
                    4) restore_configs ;;
                    5) reset_configs ;;
                    6) show_config_stats ;;
                    0) continue ;;
                    *) echo -e "${RED}无效选择${NC}" ;;
                esac
                ;;
            0)
                echo -e "${GREEN}感谢使用,再见!${NC}"
                exit 0
                ;;
            *)
                echo -e "${RED}无效选择,请重新输入${NC}"
                ;;
        esac
        
        echo
        read -p "按回车键继续..."
    done
}

# 运行主程序
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi

文档仓库 » Linux挂载FTP、WEBDAV、SMB为本地目录交互式脚本(可挂载多个相同协议)