添加手动安装v2脚本

可以选择本地文件安装
This commit is contained in:
lingling 2025-04-02 19:58:29 +08:00
parent 96ed46b2dc
commit 2be8b8de9f
1 changed files with 150 additions and 0 deletions

150
v2ray_manage.sh Normal file
View File

@ -0,0 +1,150 @@
#!/bin/bash
V2RAY_VERSION="v4.31.0"
V2RAY_ZIP="v2ray-linux-64.zip"
V2RAY_DIR="/usr/local/v2ray"
CONFIG_DIR="/etc/v2ray"
SERVICE_FILE="/usr/lib/systemd/system/v2ray.service"
USE_LOCAL=false
# 确保脚本是以 root 用户运行
if [ "$(id -u)" -ne 0 ]; then
echo "请以 root 用户运行该脚本!"
exit 1
fi
# 获取系统发行版名称
OS_TYPE=$(awk -F= '/^ID=/ { print $2 }' /etc/os-release)
# 安装依赖工具
function install_dependencies() {
echo "安装必要的依赖..."
if [ "$OS_TYPE" == "centos" ] || [ "$OS_TYPE" == "rhel" ]; then
yum install -y wget unzip
elif [ "$OS_TYPE" == "debian" ] || [ "$OS_TYPE" == "ubuntu" ]; then
apt update
apt install -y wget unzip
else
echo "不支持的操作系统类型:$OS_TYPE"
exit 1
fi
}
# 显示帮助信息
function show_help() {
echo "使用方法: $0 [install|uninstall] [--local]"
echo " install 安装 V2Ray"
echo " uninstall 卸载 V2Ray"
echo " --local 使用本地文件安装 (默认从 GitHub 下载)"
exit 0
}
# 解析命令参数
if [ $# -eq 0 ]; then
show_help
fi
ACTION=$1
if [ "$2" == "--local" ]; then
USE_LOCAL=true
fi
# 安装 V2Ray
function install_v2ray() {
echo "开始安装 V2Ray..."
# 安装依赖
install_dependencies
# 下载或使用本地文件
if [ "$USE_LOCAL" == "true" ]; then
echo "使用本地文件安装 V2Ray..."
if [ ! -f "$V2RAY_ZIP" ]; then
echo "错误:未找到本地文件 $V2RAY_ZIP,请放置到当前目录后重试!"
exit 1
fi
else
echo "从 GitHub 下载 V2Ray..."
wget -O "$V2RAY_ZIP" "https://github.com/v2fly/v2ray-core/releases/download/$V2RAY_VERSION/$V2RAY_ZIP"
fi
# 解压 V2Ray
echo "解压 V2Ray..."
mkdir -p v2ray
unzip -d v2ray "$V2RAY_ZIP"
# 复制 V2Ray 文件
echo "复制 V2Ray 文件..."
mkdir -p "$V2RAY_DIR"
cp v2ray/* "$V2RAY_DIR/"
# 创建配置目录
echo "创建配置目录..."
mkdir -p "$CONFIG_DIR"
cp vpoint_vmess_freedom.json "$CONFIG_DIR/config.json"
# 创建 systemd 服务文件
echo "创建 systemd 服务..."
cat <<EOF > "$SERVICE_FILE"
[Unit]
Description=V2Ray Service
Documentation=https://www.v2fly.org/
After=network.target nss-lookup.target
[Service]
User=nobody
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=$V2RAY_DIR/v2ray -config $CONFIG_DIR/config.json
Restart=on-failure
RestartPreventExitStatus=23
[Install]
WantedBy=multi-user.target
EOF
# 重新加载 systemd 并启动 V2Ray
echo "启动 V2Ray..."
systemctl daemon-reload
systemctl enable v2ray
systemctl start v2ray
echo "✅ V2Ray 安装完成!"
echo "你可以使用以下命令查看 V2Ray 运行状态:"
echo " systemctl status v2ray"
}
# 卸载 V2Ray
function uninstall_v2ray() {
echo "开始卸载 V2Ray..."
# 停止并禁用服务
echo "停止 V2Ray..."
systemctl stop v2ray
systemctl disable v2ray
# 删除文件
echo "删除 V2Ray 文件..."
rm -rf "$V2RAY_DIR" "$CONFIG_DIR" "$SERVICE_FILE"
# 重新加载 systemd
echo "重新加载 systemd 配置..."
systemctl daemon-reload
echo "✅ V2Ray 已成功卸载。"
}
# 根据参数执行相应操作
case "$ACTION" in
install)
install_v2ray
;;
uninstall)
uninstall_v2ray
;;
*)
show_help
;;
esac