#!/usr/bin/env bash # SSH 隧道管理器 一键安装(Linux / macOS) # 用法: curl -fsSL https://gitea.xbay.cc/leefer/ssh-e2e/raw/branch/main/install.sh | bash set -euo pipefail BASE_URL="https://gitea.xbay.cc/leefer/ssh-e2e" APP_DIR="$HOME/ssh-e2e" BIN_DIR="$HOME/.local/bin" say() { printf '\033[1;32m==>\033[0m %s\n' "$*"; } die() { printf '\033[1;31m错误:\033[0m %s\n' "$*" >&2; exit 1; } [ "$(uname)" = "Linux" ] || [ "$(uname)" = "Darwin" ] || die "此脚本仅支持 Linux / macOS" # ---------- 依赖 ---------- need_py() { command -v python3 >/dev/null 2>&1 || die "未找到 python3,请先安装 Python 3.10+" pyver=$(python3 -c 'import sys;print("%d.%d"%sys.version_info[:2])') case "$pyver" in 3.1[0-9]|3.[2-9][0-9]|[4-9].*) ;; *) die "需要 Python 3.10+,当前 $pyver" ;; esac } # ---------- 下载源码 ---------- say "下载最新代码到 $APP_DIR ..." mkdir -p "$APP_DIR" if command -v git >/dev/null 2>&1; then if [ -d "$APP_DIR/.git" ]; then git -C "$APP_DIR" fetch --all --quiet && git -C "$APP_DIR" reset --hard origin/main --quiet else git clone --depth 1 "$BASE_URL.git" "$APP_DIR" 2>/dev/null || true fi fi if [ ! -f "$APP_DIR/cli.py" ]; then # git 不可用或克隆失败:直接拉归档 archive_url="$BASE_URL/archive/main.tar.gz" if command -v curl >/dev/null 2>&1; then curl -fsSL "$archive_url" | tar -xz -C "$APP_DIR" --strip-components 1 else wget -qO- "$archive_url" | tar -xz -C "$APP_DIR" --strip-components 1 fi fi [ -f "$APP_DIR/cli.py" ] || die "代码下载失败,请检查网络后重试" # ---------- 虚拟环境 ---------- if [ ! -x "$APP_DIR/.venv/bin/python" ]; then say "创建虚拟环境并安装依赖(首次约 1-2 分钟)..." need_py python3 -m venv "$APP_DIR/.venv" fi if [ ! -e "$APP_DIR/.venv/deps-done" ]; then "$APP_DIR/.venv/bin/pip" install --quiet --disable-pip-version-check \ paramiko cryptography keyring > /dev/null touch "$APP_DIR/.venv/deps-done" fi # ---------- ssh-tunnel 命令 ---------- mkdir -p "$BIN_DIR" cat > "$BIN_DIR/ssh-tunnel" << EOF #!/usr/bin/env bash exec "$APP_DIR/.venv/bin/python" "$APP_DIR/cli.py" "\$@" EOF chmod +x "$BIN_DIR/ssh-tunnel" # PATH 提示 case ":$PATH:" in *":$BIN_DIR:"*) ;; *) shell_rc="$HOME/.bashrc" [ "$(uname)" = "Darwin" ] && [ -f "$HOME/.zshrc" ] && shell_rc="$HOME/.zshrc" grep -q 'ssh-e2e' "$shell_rc" 2>/dev/null || { echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$shell_rc" export PATH="$BIN_DIR:$PATH" } ;; esac say "安装完成!" echo echo " 配置隧道(示例):" echo " ssh-tunnel host add 我的服务器 root@1.2.3.4 22" echo " ssh-tunnel tunnel add web --host 我的服务器 --lp 3000 --rp 3000" echo " ssh-tunnel start web" echo echo " 常用命令: ssh-tunnel list | test <主机> | remove host|tunnel <名称>" echo " 图形界面(Windows): powershell -c \"irm $BASE_URL/raw/branch/main/install.ps1 | iex\"" echo [ -n "${ZSH_VERSION:-}${BASH_VERSION:-}" ] || echo "(注:新开终端或 source ~/.bashrc 后 ssh-tunnel 生效)"