From 320e454502c8d300716764fce360c75010568484 Mon Sep 17 00:00:00 2001 From: baoweise Date: Fri, 29 May 2026 13:29:09 +0800 Subject: [PATCH] feat: add multi-distribution support for package management and system service managers in installation script --- README.md | 161 ++++++++++++++++++++--------------------------------- install.sh | 138 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 167 insertions(+), 132 deletions(-) diff --git a/README.md b/README.md index e694fdd..9dbc2da 100644 --- a/README.md +++ b/README.md @@ -11,67 +11,57 @@ Bilingual: [中文](#中文) | [English](#english) [![YouTube](https://img.shields.io/badge/视频教程-YouTube-red?style=flat-square&logo=youtube&logoColor=white)](https://www.youtube.com/watch?v=s-ATfXR8BpI) [![Email](https://img.shields.io/badge/Bug反馈-yaohunse7@gmail.com-red?style=flat-square&logo=gmail&logoColor=white)](mailto:yaohunse7@gmail.com) +--- + +**AimiliVPN** 是一个专为 Linux VPS 设计的智能 VPN 代理网关管理器。它能够自动采集 VPNGate 开放节点,进行多线程可用性测试与延迟过滤,利用 OpenVPN 隧道与策略路由(Policy Routing)实现出站网络,并在本地提供高性能的 HTTP/SOCKS5 代理网关服务,非常适合用作 3x-ui / Xray 的落地出站代理,解锁流媒体与锁区网站。 --- -**AimiliVPN** 是一个专为 Linux VPS(如 Ubuntu)设计的智能 VPN 代理网关管理器。它能够自动采集 VPNGate 开放节点,进行多线程可用性测试与延迟过滤,利用 OpenVPN 隧道与策略路由(Policy Routing)实现出站网络,并在本地提供高性能的 HTTP/SOCKS5 代理网关服务,适合用作 Xray 的落地出站代理。 +### 🚀 快速开始 (一键部署) ---- - -### 🚀 快速开始 - -在您的 **Ubuntu** VPS 机器上,复制并运行以下一行指令即可完成自动安装部署: +只需一行命令,支持多种主流 Linux 发行版,自动识别并配置系统服务: ```bash bash <(curl -Ls https://raw.githubusercontent.com/baoweise-bot/aimili-vpngate/main/install.sh) ``` +> **支持的操作系统**: +> - **Ubuntu / Debian** (Systemd) +> - **Alpine Linux** (OpenRC) +> - **CentOS / RHEL / Rocky Linux / AlmaLinux / Fedora** (Systemd) + --- -### 🛠️ 快捷命令行 (CLI) +### 🛠️ 快捷命令行管理工具 `ml` -安装成功后,系统会在全局注册 `ml` 快捷管理指令,直接运行 `ml` 可打开图形化交互终端,也可通过以下指令执行: -* **`ml status`** 或 **`ml`**:查看当前运行状态(代理端口、活动 VPN 节点、直连延迟、网页后台登录地址等)。 -* **`ml start`**:启动 AimiliVPN 服务。 -* **`ml stop`**:停止 AimiliVPN 服务(并自动清理策略路由与 OpenVPN 进程)。 -* **`ml restart`**:重启服务。 -* **`ml logs`**:查看实时的 Systemd 服务运行日志。 -* **`ml web`**:切换网页绑定地址(127.0.0.1 仅本地,或 0.0.0.0 允许公网访问)与重置安全后缀。 -* **`ml port`**:修改网页管理控制台监听端口。 -* **`ml password`**:生成新的 12 位安全管理密码。 -* **`ml uninstall`**:完全卸载服务并清理相关环境。 +安装完成后,直接在终端输入 `ml`(或 `ml status`)即可打开交互式菜单,或直接使用以下子命令: -#### 💡 首次安装与常见报错解决(小白必看) +- **`ml`** 或 **`ml status`**:查看运行状态(代理端口、活动节点、延迟、网页登录后台等)。 +- **`ml start`** / **`ml stop`** / **`ml restart`**:启动/停止/重启服务。 +- **`ml logs`**:查看实时运行日志。 +- **`ml web`**:修改网页面板的绑定地址(如仅允许 127.0.0.1 本地或 0.0.0.0 公网访问)并随机重置安全后缀。 +- **`ml port`** / **`ml password`**:修改网页管理端口及重置管理密码。 +- **`ml update`**:一键检测并更新至远程最新源码。 +- **`ml uninstall`**:完全卸载 AimiliVPN 并清理环境。 -##### 1. 极简系统缺少依赖(Ubuntu 18-26 / Debian 首次安装) -如果系统是全新纯净版,可能会因为缺少 `curl` 或 `ca-certificates` 导致一键安装脚本下载失败。请在安装前执行以下命令补充依赖: +--- + +### 💡 小白避坑指南(常见问题解决) + +#### 1. 极简系统缺少基础命令导致一键脚本报错 +如果您的 VPS 是纯净版系统,可能会缺少 `curl`。请根据系统类型先运行以下命令安装: +- **Ubuntu/Debian**: `apt-get update && apt-get install -y curl ca-certificates` +- **Alpine**: `apk add curl ca-certificates` +- **CentOS/Rocky/Alma/Fedora**: `yum install -y curl ca-certificates` + +#### 2. Ubuntu/Debian 系统提示包管理器被占用 (Apt Lock) +若安装时提示 `Could not get lock /var/lib/dpkg/lock-frontend` 等错误,可运行以下指令一键解锁后重新安装: ```bash -sudo apt-get update && sudo apt-get install -y curl ca-certificates -``` - -##### 2. Debian 系统兼容运行方法 -本脚本一键包默认限制在 Ubuntu 系统运行。Debian 用户如需运行,可先下载并用 `sed` 临时将系统类型限制替换为 `"ubuntu"` 后再执行安装: -```bash -curl -Ls https://raw.githubusercontent.com/baoweise-bot/aimili-vpngate/main/install.sh -o install.sh -sed -i 's/"${ID:-}"/"ubuntu"/g' install.sh -sudo bash install.sh -``` - -##### 3. 包管理器被占用(Apt 锁冲突报错解决) -若一键安装提示 `Could not get lock /var/lib/dpkg/lock-frontend` 等“无法获得锁”的报错,可运行以下命令解除占用并重新安装: -```bash -# 1. 停止自动更新服务并终止相关进程 sudo systemctl stop unattended-upgrades 2>/dev/null sudo killall apt apt-get dpkg 2>/dev/null - -# 2. 清理残留锁文件 sudo rm -f /var/lib/dpkg/lock* /var/lib/apt/lists/lock /var/cache/apt/archives/lock - -# 3. 修复受损包并重新更新源 -sudo dpkg --configure -a -sudo apt-get update +sudo dpkg --configure -a && sudo apt-get update ``` -执行完毕后,重新运行一键安装脚本即可。 --- @@ -101,81 +91,52 @@ sudo apt-get update --- -**AimiliVPN** is an intelligent VPN proxy gateway manager designed specifically for Linux VPS (e.g. Ubuntu). It automatically collects open VPNGate nodes, conducts multi-threaded availability testing and latency filtering, establishes secure out-of-band routing via OpenVPN and policy routing to **prevent VPS lockouts**, and hosts a high-performance local SOCKS5/HTTP proxy gateway. It is highly optimized to serve as a residential/unlocked egress node for upstream proxies like 3x-ui / Xray. - -### ✨ Key Features - -1. ⚡ **Auto-Collection & Multi-Threaded Probing**: - * Periodically fetches candidate nodes from VPNGate. - * Performs concurrent ping latency and handshake tests to maintain a pool of high-quality nodes. -2. 🔒 **Anti-Lockout Routing (Policy Routing)**: - * Directs traffic from the virtual adapter `tun0` to a customized routing table (Table 100) without altering the system's default gateway. - * Keeps SSH sessions and server administration panels unaffected by the active VPN. -3. 🚫 **Fail-Safe Leak Protection**: - * Outbound socket connections inside the local proxy server are strictly bound to `tun0` via `SO_BINDTODEVICE`. - * If the VPN disconnects, proxy requests are instantly blocked with a `502 Bad Gateway` instead of falling back to the VPS physical IP address. -4. 🖥️ **Modern Web UI Panel**: - * Sleek dark/light responsive console (default port `8787`). - * Provides real-time geolocation, ISP, ASN, latency, and IP-type (residential/datacenter) detection. - * Enables manual node selection, blacklist resets, proxy speed-testing, and logs query. - * Secured by a random secret path suffix (e.g., `/EJsW2EeBo9lY/`) and password authentication. -5. 🛠️ **CLI Utility (ml)**: - * Command-line helper tool `ml` with a menu-driven interface. - * Provides quick statuses, starts/stops the daemon, resets passwords, and changes bind hosts. +**AimiliVPN** is an intelligent VPN proxy gateway manager designed specifically for Linux VPS. It automatically collects open VPNGate nodes, conducts multi-threaded availability testing and latency filtering, establishes secure tunnels via OpenVPN and policy routing (to **prevent VPS lockout**), and hosts a high-performance local SOCKS5/HTTP proxy gateway. It is highly optimized to serve as a residential/unlocked egress node for upstream proxies like 3x-ui / Xray. --- -### 🚀 Quick Start +### 🚀 Quick Start (One-Click Deployment) -To install and deploy AimiliVPN on your **Ubuntu** server, copy and paste the following command: +Copy and paste the command below to deploy AimiliVPN. The script will automatically detect your OS, install dependencies, and configure the background service: ```bash bash <(curl -Ls https://raw.githubusercontent.com/baoweise-bot/aimili-vpngate/main/install.sh) ``` +> **Supported Operating Systems**: +> - **Ubuntu / Debian** (Systemd) +> - **Alpine Linux** (OpenRC) +> - **CentOS / RHEL / Rocky Linux / AlmaLinux / Fedora** (Systemd) + --- -### 🛠️ CLI Helper Commands +### 🛠️ CLI Helper Commands (`ml`) Once installed, use the global command `ml` to launch the interactive helper menu, or use the shortcuts below: -* **`ml status`** or **`ml`**: Check running system status (active nodes, proxy ports, latency, URLs). -* **`ml start`**: Start the gateway service. -* **`ml stop`**: Stop the gateway service (and clean routing tables). -* **`ml restart`**: Restart the service. -* **`ml logs`**: View real-time Systemd output logs. -* **`ml web`**: Toggle Web UI accessibility (127.0.0.1 or 0.0.0.0) and reset suffix paths. -* **`ml port`**: Update the Web Console port. -* **`ml password`**: Regenerate a secure 12-character administration password. -* **`ml uninstall`**: Completely remove the service and repository files from your VPS. -#### 💡 Troubleshooting & First-Time Installation Tips +- **`ml`** or **`ml status`**: Check system running status (active nodes, proxy ports, latency, URLs). +- **`ml start`** / **`ml stop`** / **`ml restart`**: Start/Stop/Restart the gateway service. +- **`ml logs`**: View real-time service logs. +- **`ml web`**: Toggle Web UI accessibility (127.0.0.1 or 0.0.0.0) and random-reset suffix paths. +- **`ml port`** / **`ml password`**: Update Web Console port and admin credentials. +- **`ml update`**: One-click check and update to the latest codebase. +- **`ml uninstall`**: Completely remove the service and repository files from your VPS. -##### 1. Missing Dependencies on Minimal OS (Ubuntu / Debian) -If you are using a brand new minimal OS, the installation might fail due to missing `curl` or `ca-certificates`. Run the following command to pre-install dependencies: +--- + +### 💡 Troubleshooting & Pre-installation Guide + +#### 1. Missing `curl` on Brand New Minimal OS +If the install command fails due to a missing `curl` utility, install it manually according to your OS: +- **Ubuntu/Debian**: `apt-get update && apt-get install -y curl ca-certificates` +- **Alpine**: `apk add curl ca-certificates` +- **CentOS/Rocky/Alma/Fedora**: `yum install -y curl ca-certificates` + +#### 2. Package Manager Locked (`apt`/`dpkg` lock errors on Ubuntu/Debian) +If you see `Could not get lock /var/lib/dpkg/lock-frontend` error, run the following commands to unlock and retry: ```bash -sudo apt-get update && sudo apt-get install -y curl ca-certificates -``` - -##### 2. Bypass OS Restrictions for Debian -The script is restricted to Ubuntu by default. For Debian systems, run the following commands to download, patch, and install: -```bash -curl -Ls https://raw.githubusercontent.com/baoweise-bot/aimili-vpngate/main/install.sh -o install.sh -sed -i 's/"${ID:-}"/"ubuntu"/g' install.sh -sudo bash install.sh -``` - -##### 3. Package Manager Locked (`apt`/`dpkg` Lock Errors) -If you see `Could not get lock /var/lib/dpkg/lock-frontend` or similar busy errors, run these commands to unlock and retry: -```bash -# 1. Stop automatic upgrades & kill active processes sudo systemctl stop unattended-upgrades 2>/dev/null sudo killall apt apt-get dpkg 2>/dev/null - -# 2. Remove lock files sudo rm -f /var/lib/dpkg/lock* /var/lib/apt/lists/lock /var/cache/apt/archives/lock - -# 3. Repair package states & update -sudo dpkg --configure -a -sudo apt-get update +sudo dpkg --configure -a && sudo apt-get update ``` -Once done, re-run the installation script. diff --git a/install.sh b/install.sh index 91fa562..dd55c8c 100644 --- a/install.sh +++ b/install.sh @@ -1,6 +1,5 @@ #!/usr/bin/env bash set -e -export DEBIAN_FRONTEND=noninteractive RED='\033[0;31m' GREEN='\033[0;32m' @@ -9,23 +8,40 @@ BLUE='\033[0;36m' PLAIN='\033[0m' # 1. Check root permissions -if [[ "$(id -u)" != "0" ]]; then +if [ "$(id -u)" != "0" ]; then echo -e "${RED}错误: 必须以 root 权限运行此脚本。请使用: sudo bash $0${PLAIN}" exit 1 fi -# 2. Check OS distribution (Ubuntu only) +# 2. Check OS distribution and set package manager +OS_TYPE="" +PKG_MGR="" if [ -f /etc/os-release ]; then . /etc/os-release - if [[ "${ID:-}" != "ubuntu" ]]; then - echo -e "${RED}错误: 本系统不是 Ubuntu!目前 AimiliVPN 仅支持 Ubuntu 系统。${PLAIN}" - exit 1 - fi -else - echo -e "${RED}错误: 无法确定操作系统版本,缺少 /etc/os-release 文件。${PLAIN}" - exit 1 + OS_TYPE=$ID fi +case "$OS_TYPE" in + ubuntu|debian) + PKG_MGR="apt-get" + export DEBIAN_FRONTEND=noninteractive + ;; + alpine) + PKG_MGR="apk" + ;; + centos|rhel|rocky|almalinux|fedora) + if command -v dnf >/dev/null 2>&1; then + PKG_MGR="dnf" + else + PKG_MGR="yum" + fi + ;; + *) + echo -e "${RED}错误: 不支持的操作系统 ($OS_TYPE)!目前仅支持 Ubuntu/Debian/Alpine/CentOS/RHEL/Rocky/AlmaLinux/Fedora。${PLAIN}" + exit 1 + ;; +esac + echo -e "${BLUE}==========================================================${PLAIN}" echo -e "${BLUE} 欢迎使用 AimiliVPN 一键源码部署与管理脚本${PLAIN}" echo -e "${BLUE}==========================================================${PLAIN}" @@ -42,10 +58,27 @@ GITHUB_REPO="${2:-${DEFAULT_REPO}}" GITHUB_URL="https://github.com/${GITHUB_USER}/${GITHUB_REPO}.git" echo -e "\n${YELLOW}[1/4] 正在安装系统基础依赖...${PLAIN}" -echo -e " -> 正在运行 apt-get update 更新软件源清单..." -apt-get update -q || true -echo -e " -> 正在运行 apt-get install 安装基础依赖包 (openvpn, curl, git, iptables, iproute2, psmisc, python3)..." -apt-get install -y openvpn curl git ca-certificates iptables iproute2 psmisc python3 +if [ "$PKG_MGR" = "apt-get" ]; then + echo -e " -> 正在运行 apt-get update 更新软件源清单..." + apt-get update -q || true + echo -e " -> 正在运行 apt-get install 安装基础依赖包..." + apt-get install -y openvpn curl git ca-certificates iptables iproute2 psmisc python3 +elif [ "$PKG_MGR" = "apk" ]; then + echo -e " -> 正在运行 apk update 更新软件源清单..." + apk update || true + echo -e " -> 正在运行 apk add 安装基础依赖包..." + # bash is required for this script itself and some internal logic + apk add openvpn curl git ca-certificates iptables iproute2 psmisc python3 bash +elif [ "$PKG_MGR" = "dnf" ] || [ "$PKG_MGR" = "yum" ]; then + echo -e " -> 正在运行 $PKG_MGR 安装基础依赖包..." + if [ "$OS_TYPE" != "fedora" ]; then + echo -e " -> 正在安装 EPEL 软件源 (以支持 openvpn)..." + $PKG_MGR install -y epel-release || true + fi + # Try installing packages. Note: iproute or iproute2 + $PKG_MGR install -y openvpn curl git ca-certificates iptables iproute psmisc python3 || \ + $PKG_MGR install -y openvpn curl git ca-certificates iptables iproute2 psmisc python3 +fi # 4. Clone or pull the repository INSTALL_DIR="/opt/aimilivpn" @@ -84,10 +117,11 @@ else fi fi -# 5. Configure Systemd Service (direct python3 run) -echo -e "\n${YELLOW}[3/4] 正在配置 systemd 系统服务...${PLAIN}" -echo -e " -> 正在创建服务配置 /lib/systemd/system/aimilivpn.service ..." -cat > /lib/systemd/system/aimilivpn.service </dev/null 2>&1; then + echo -e " -> 检测到 systemd,正在创建服务配置 /lib/systemd/system/aimilivpn.service ..." + cat > /lib/systemd/system/aimilivpn.service </dev/null 2>&1; then + echo -e " -> 检测到 OpenRC,正在创建服务配置 /etc/init.d/aimilivpn ..." + cat > /etc/init.d/aimilivpn < 正在重新加载 systemd 系统服务列表并启用开机自启..." -systemctl daemon-reload -systemctl enable aimilivpn.service +description="AimiliVPN OpenVPN Manager with HTTP/SOCKS5 Proxy" +command="/usr/bin/python3" +command_args="${INSTALL_DIR}/vpngate_manager.py" +command_background="yes" +directory="${INSTALL_DIR}" +pidfile="/run/aimilivpn.pid" + +depend() { + need net + after firewall +} +EOF + chmod +x /etc/init.d/aimilivpn + rc-update add aimilivpn default +else + echo -e "${YELLOW}警告: 未能检测到 systemd 或 OpenRC,请手动管理服务。${PLAIN}" +fi # 6. Configure global command shortcut "ml" echo -e "\n${YELLOW}[4/4] 正在创建全局命令快捷接口 'ml'...${PLAIN}" @@ -120,6 +174,7 @@ import subprocess import time import tty import termios +import shutil INSTALL_DIR = "/opt/aimilivpn" LOG_FILE = "/opt/aimilivpn/vpngate_data/vpngate.log" @@ -379,21 +434,29 @@ def print_status(): print_line(f" export https_proxy=socks5://127.0.0.1:7928") print_line("=======================================================") +def run_service_cmd(cmd): + if shutil.which("systemctl"): + subprocess.run(["systemctl", cmd, "aimilivpn.service"]) + elif shutil.which("rc-service"): + subprocess.run(["rc-service", "aimilivpn", cmd]) + else: + print("未检测到支持的服务管理器 (systemd/OpenRC)") + def start_service(): print("正在启动 AimiliVPN 服务...", flush=True) - subprocess.run(["systemctl", "start", "aimilivpn.service"]) + run_service_cmd("start") print("已发送启动指令。") time.sleep(1) def stop_service(): print("正在停止 AimiliVPN 服务...", flush=True) - subprocess.run(["systemctl", "stop", "aimilivpn.service"]) + run_service_cmd("stop") print("已发送停止指令。") time.sleep(1) def restart_service(): print("正在重启 AimiliVPN 服务...", flush=True) - subprocess.run(["systemctl", "restart", "aimilivpn.service"]) + run_service_cmd("restart") print("已发送重启指令。") time.sleep(1) @@ -469,12 +532,19 @@ def uninstall_service(): confirm = input("确定要完全卸载 AimiliVPN 吗?(y/N): ") if confirm.lower() == 'y': print("正在完全卸载 AimiliVPN...", flush=True) - subprocess.run(["systemctl", "stop", "aimilivpn.service"]) - subprocess.run(["systemctl", "disable", "aimilivpn.service"]) - try: - os.unlink("/lib/systemd/system/aimilivpn.service") - except Exception: - pass + stop_service() + if shutil.which("systemctl"): + subprocess.run(["systemctl", "disable", "aimilivpn.service"]) + try: + os.unlink("/lib/systemd/system/aimilivpn.service") + except Exception: + pass + elif shutil.which("rc-service"): + subprocess.run(["rc-update", "del", "aimilivpn"]) + try: + os.unlink("/etc/init.d/aimilivpn") + except Exception: + pass try: os.unlink("/usr/bin/ml") except Exception: @@ -490,7 +560,7 @@ def ask_restart(): ans = input("配置已保存。是否立即重启服务生效?(Y/n): ").strip().lower() if ans in ('', 'y', 'yes'): print("正在重启 AimiliVPN 服务...", flush=True) - subprocess.run(["systemctl", "restart", "aimilivpn.service"]) + restart_service() print("服务已重启。") time.sleep(1.5) @@ -905,7 +975,11 @@ fi # 8. Start service echo -e "\n正在启动 AimiliVPN 服务并初始化网络..." -systemctl restart aimilivpn.service || true +if command -v systemctl >/dev/null 2>&1; then + systemctl restart aimilivpn.service || true +elif command -v rc-service >/dev/null 2>&1; then + rc-service aimilivpn restart || true +fi # Wait and poll for node loading and active connection echo -e "\n正在等待 AimiliVPN 首次获取节点并建立加密通道 (此过程可能需要 5-30 秒)..."