Files
vps/v2raya/v2raya.sh
2025-11-07 14:07:46 +08:00

324 lines
8.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# V2rayA 自动安装/卸载脚本
# 使用方法: chmod +x v2raya.sh && ./v2raya.sh
# curl -sS -O https://gitea.tohub.top/Share/vps/raw/branch/main/v2raya/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"
BASE_URL="https://gitea.tohub.top/Share/vps/raw/branch/main/v2raya"
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
}
# 创建安装目录
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 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 ]]; then
print_success "下载完成: $output"
return 0
else
print_error "下载失败: $url"
return 1
fi
}
# 下载安装包
download_packages() {
local arch=$(detect_arch)
print_info "检测到系统架构: $arch"
create_install_dir
local xray_file="xray_${XRAY_VERSION}_${arch}.deb"
local v2raya_file="v2raya_${V2RAYA_VERSION}_${arch}.deb"
# 下载 Xray
if [[ ! -f "$INSTALL_DIR/$xray_file" ]]; then
download_file "$BASE_URL/$xray_file" "$INSTALL_DIR/$xray_file"
else
print_warning "文件已存在,跳过下载: $xray_file"
fi
# 下载 V2rayA
if [[ ! -f "$INSTALL_DIR/$v2raya_file" ]]; then
download_file "$BASE_URL/$v2raya_file" "$INSTALL_DIR/$v2raya_file"
else
print_warning "文件已存在,跳过下载: $v2raya_file"
fi
}
# 安装 V2rayA
install_v2raya() {
local arch=$(detect_arch)
local xray_file="$INSTALL_DIR/xray_${XRAY_VERSION}_${arch}.deb"
local v2raya_file="$INSTALL_DIR/v2raya_${V2RAYA_VERSION}_${arch}.deb"
# 检查文件是否存在
if [[ ! -f "$xray_file" ]] || [[ ! -f "$v2raya_file" ]]; then
print_warning "安装包不存在,开始下载..."
download_packages
fi
print_info "开始安装 Xray..."
chmod 644 "$xray_file"
dpkg -i "$xray_file"
apt --fix-broken install -y
if [[ $? -ne 0 ]]; then
print_error "Xray 安装失败"
exit 1
fi
print_success "Xray 安装成功"
print_info "开始安装 V2rayA..."
chmod 644 "$v2raya_file"
dpkg -i "$v2raya_file"
apt --fix-broken install -y
if [[ $? -ne 0 ]]; then
print_error "V2rayA 安装失败"
exit 1
fi
print_success "V2rayA 安装成功"
# 启动服务
print_info "启动 V2rayA 服务..."
systemctl start v2raya.service
if [[ $? -eq 0 ]]; then
print_success "V2rayA 服务启动成功"
else
print_error "V2rayA 服务启动失败"
fi
# 设置开机自启
print_info "设置开机自启..."
systemctl enable v2raya.service
print_success "已设置开机自启"
# 显示登录信息
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() {
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
# 验证卸载
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() {
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
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}4.${NC} 检查状态"
echo -e "${GREEN}0.${NC} 退出"
echo ""
echo -e "${CYAN}============================================${NC}"
}
# 主程序
main() {
check_root
while true; do
show_menu
read -p "请选择操作 [0-4]: " choice
case $choice in
1)
install_v2raya
read -p "按任意键继续..." -n 1
;;
2)
uninstall_v2raya
read -p "按任意键继续..." -n 1
;;
3)
download_packages
read -p "按任意键继续..." -n 1
;;
4)
check_status
read -p "按任意键继续..." -n 1
;;
0)
print_info "退出脚本"
exit 0
;;
*)
print_error "无效的选择,请重新输入"
sleep 2
;;
esac
done
}
# 运行主程序
main