#!/bin/bash # V2rayA 自动安装/卸载脚本 # 使用方法: chmod +x v2raya.sh && ./v2raya.sh # curl -sS -O https://gitea.tohub.top/Share/script/raw/branch/main/v2raya.sh && chmod +x v2raya.sh && ./v2raya.sh # 彩色输出 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # No Color # 配置变量 INSTALL_DIR="/root/data/docker_data/v2raya" BASE_URL="https://gitea.tohub.top/Share/script/raw/branch/main" XRAY_VERSION="25.8.3" V2RAYA_VERSION="2.2.7.4" # 默认账号信息 DEFAULT_USER="admin@gmail.com" DEFAULT_PASS="gmail.com" # 打印信息函数 print_info() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # 检查是否以 root 权限运行 check_root() { if [[ $EUID -ne 0 ]]; then print_error "此脚本需要 root 权限运行" print_info "请使用: sudo $0" exit 1 fi } # 检测系统架构 detect_arch() { local arch=$(dpkg --print-architecture) if [[ "$arch" == "amd64" ]] || [[ "$arch" == "arm64" ]]; then echo "$arch" else print_error "不支持的系统架构: $arch" print_info "仅支持 amd64 和 arm64" exit 1 fi } # 转换架构名称(用于 V2rayA 文件命名) get_v2raya_arch() { local arch=$1 if [[ "$arch" == "amd64" ]]; then echo "x64" else echo "$arch" fi } # 创建安装目录 create_install_dir() { if [[ ! -d "$INSTALL_DIR" ]]; then print_info "创建安装目录: $INSTALL_DIR" mkdir -p "$INSTALL_DIR" if [[ $? -ne 0 ]]; then print_error "创建目录失败" exit 1 fi fi } # 下载文件 download_file() { local url=$1 local output=$2 print_info "正在下载: $url" # 如果存在旧的不完整文件,先删除 if [[ -f "$output" ]]; then rm -f "$output" fi if command -v wget &> /dev/null; then wget -q --show-progress "$url" -O "$output" elif command -v curl &> /dev/null; then curl -# -L "$url" -o "$output" else print_error "未找到 wget 或 curl,请先安装" exit 1 fi # 检查下载是否成功 if [[ $? -eq 0 ]] && [[ -f "$output" ]] && [[ -s "$output" ]]; then print_success "下载完成: $output" return 0 else print_error "下载失败: $url" rm -f "$output" return 1 fi } # 下载安装包 download_packages() { local arch=$(detect_arch) local v2raya_arch=$(get_v2raya_arch "$arch") print_info "检测到系统架构: $arch" create_install_dir local xray_file="xray_${XRAY_VERSION}_${arch}.deb" local v2raya_file="v2raya_${v2raya_arch}_${V2RAYA_VERSION}.deb" local download_failed=0 # 下载 Xray if [[ ! -f "$INSTALL_DIR/$xray_file" ]]; then if ! download_file "$BASE_URL/$xray_file" "$INSTALL_DIR/$xray_file"; then download_failed=1 fi else print_warning "文件已存在,跳过下载: $xray_file" fi # 下载 V2rayA if [[ ! -f "$INSTALL_DIR/$v2raya_file" ]]; then if ! download_file "$BASE_URL/$v2raya_file" "$INSTALL_DIR/$v2raya_file"; then download_failed=1 fi else print_warning "文件已存在,跳过下载: $v2raya_file" fi return $download_failed } # 验证文件是否有效 verify_deb_file() { local file=$1 if [[ ! -f "$file" ]]; then return 1 fi if [[ ! -s "$file" ]]; then print_warning "文件为空: $file" return 1 fi # 检查 file 命令是否存在 if command -v file &> /dev/null; then if ! file "$file" | grep -q "Debian"; then print_warning "文件不是有效的 deb 包: $file" return 1 fi else # 如果没有 file 命令,简单检查文件扩展名和大小 if [[ ! "$file" =~ \.deb$ ]]; then print_warning "文件扩展名不正确: $file" return 1 fi # 检查文件大小是否合理(至少 1KB) local size=$(stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null) if [[ $size -lt 1024 ]]; then print_warning "文件大小异常: $file (${size} bytes)" return 1 fi fi return 0 } # 安装 V2rayA install_v2raya() { local arch=$(detect_arch) local v2raya_arch=$(get_v2raya_arch "$arch") local xray_file="$INSTALL_DIR/xray_${XRAY_VERSION}_${arch}.deb" local v2raya_file="$INSTALL_DIR/v2raya_${v2raya_arch}_${V2RAYA_VERSION}.deb" # 检查文件是否存在 if [[ ! -f "$xray_file" ]] || [[ ! -f "$v2raya_file" ]]; then print_warning "安装包不存在,开始下载..." if ! download_packages; then print_error "下载失败,无法继续安装" print_info "请检查网络连接或下载地址是否正确" return 1 fi fi # 验证文件完整性 print_info "验证安装包完整性..." if ! verify_deb_file "$xray_file"; then print_error "Xray 安装包验证失败,请重新下载" rm -f "$xray_file" return 1 fi if ! verify_deb_file "$v2raya_file"; then print_error "V2rayA 安装包验证失败,请重新下载" rm -f "$v2raya_file" return 1 fi print_success "安装包验证通过" print_info "开始安装 Xray..." chmod 644 "$xray_file" if dpkg -i "$xray_file" 2>&1 | tee /tmp/xray_install.log; then apt --fix-broken install -y print_success "Xray 安装成功" else print_error "Xray 安装失败" print_info "错误日志已保存到: /tmp/xray_install.log" return 1 fi print_info "开始安装 V2rayA..." chmod 644 "$v2raya_file" if dpkg -i "$v2raya_file" 2>&1 | tee /tmp/v2raya_install.log; then apt --fix-broken install -y print_success "V2rayA 安装成功" else print_error "V2rayA 安装失败" print_info "错误日志已保存到: /tmp/v2raya_install.log" return 1 fi # 启动服务 print_info "启动 V2rayA 服务..." systemctl start v2raya.service if [[ $? -eq 0 ]]; then print_success "V2rayA 服务启动成功" else print_error "V2rayA 服务启动失败" print_info "请运行 'systemctl status v2raya.service' 查看详情" return 1 fi # 设置开机自启 print_info "设置开机自启..." systemctl enable v2raya.service print_success "已设置开机自启" # 配置防火墙 echo "" print_info "配置防火墙规则..." if command -v ufw &> /dev/null; then ufw allow 2017 >/dev/null 2>&1 print_success "已开放端口 2017" print_info "防火墙状态:" ufw status | grep -E "Status:|2017" | sed 's/^/ /' else print_warning "未检测到 ufw 防火墙,请手动配置防火墙规则" fi # 显示登录信息 echo "" print_success "============================================" print_success "V2rayA 安装完成!" print_success "============================================" print_info "访问地址: http://localhost:2017" print_info "默认账号: ${CYAN}$DEFAULT_USER${NC}" print_info "默认密码: ${CYAN}$DEFAULT_PASS${NC}" print_success "============================================" echo "" } # 卸载 V2rayA uninstall_v2raya() { local arch=$(detect_arch) local v2raya_arch=$(get_v2raya_arch "$arch") local xray_file="$INSTALL_DIR/xray_${XRAY_VERSION}_${arch}.deb" local v2raya_file="$INSTALL_DIR/v2raya_${v2raya_arch}_${V2RAYA_VERSION}.deb" print_warning "即将卸载 V2rayA 和 Xray" read -p "是否继续?[y/N] " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then print_info "已取消卸载" return fi # 停止服务 print_info "停止 V2rayA 服务..." systemctl stop v2raya.service 2>/dev/null # 禁用开机自启 print_info "禁用开机自启..." systemctl disable v2raya.service 2>/dev/null # 卸载软件包 print_info "卸载 V2rayA..." apt remove v2raya -y 2>/dev/null print_info "卸载 Xray..." apt remove xray -y 2>/dev/null # 清理依赖 print_info "清理不需要的依赖..." apt autoremove -y # 询问是否删除配置文件 read -p "是否删除配置文件?[y/N] " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then print_info "删除配置文件..." rm -rf /etc/v2raya 2>/dev/null apt purge xray v2raya -y 2>/dev/null fi # 自动删除安装包目录 echo "" print_info "清理安装包目录: $INSTALL_DIR" if [[ -d "$INSTALL_DIR" ]]; then rm -rf "$INSTALL_DIR" print_success "已删除: $INSTALL_DIR" else print_warning "目录不存在: $INSTALL_DIR" fi # 验证卸载 echo "" if dpkg -l | grep -E 'v2raya|xray' &> /dev/null; then print_warning "部分组件可能未完全卸载" dpkg -l | grep -E 'v2raya|xray' else print_success "V2rayA 和 Xray 已完全卸载" fi } # 检查安装状态 check_status() { local arch=$(detect_arch) local v2raya_arch=$(get_v2raya_arch "$arch") local xray_file="$INSTALL_DIR/xray_${XRAY_VERSION}_${arch}.deb" local v2raya_file="$INSTALL_DIR/v2raya_${v2raya_arch}_${V2RAYA_VERSION}.deb" echo "" print_info "============================================" print_info "检查 V2rayA 状态" print_info "============================================" # 检查软件安装状态 if dpkg -l | grep -q v2raya; then print_success "V2rayA 已安装" if systemctl is-active --quiet v2raya.service; then print_success "V2rayA 服务运行中" else print_warning "V2rayA 服务未运行" fi if systemctl is-enabled --quiet v2raya.service; then print_success "开机自启已启用" else print_warning "开机自启未启用" fi else print_warning "V2rayA 未安装" fi if dpkg -l | grep -q xray; then print_success "Xray 已安装" else print_warning "Xray 未安装" fi # 检查安装包状态 echo "" print_info "安装包状态:" if [[ -f "$xray_file" ]]; then local size=$(du -h "$xray_file" | cut -f1) print_info " Xray: ${CYAN}已下载${NC} (${size})" else print_warning " Xray: 未下载" fi if [[ -f "$v2raya_file" ]]; then local size=$(du -h "$v2raya_file" | cut -f1) print_info " V2rayA: ${CYAN}已下载${NC} (${size})" else print_warning " V2rayA: 未下载" fi print_info "============================================" echo "" } # 主菜单 show_menu() { clear echo -e "${CYAN}" echo "============================================" echo " V2rayA 自动安装/卸载脚本" echo "============================================" echo -e "${NC}" echo -e "${GREEN}1.${NC} 安装 V2rayA" echo -e "${GREEN}2.${NC} 卸载 V2rayA" echo -e "${GREEN}3.${NC} 检查状态" echo -e "${GREEN}0.${NC} 退出" echo "" echo -e "${CYAN}============================================${NC}" } # 主程序 main() { check_root while true; do show_menu read -p "请选择操作 [0-3]: " choice case $choice in 1) install_v2raya read -p "按任意键继续..." -n 1 ;; 2) uninstall_v2raya read -p "按任意键继续..." -n 1 ;; 3) check_status read -p "按任意键继续..." -n 1 ;; 0) print_info "退出脚本" exit 0 ;; *) print_error "无效的选择,请重新输入" sleep 2 ;; esac done } # 运行主程序 main