#!/bin/sh
# 隧道客户端一键安装 — 零依赖，全架构兼容
# 用法: sh install.sh
set -e

BASE="https://tunnel.ltzy.top"
HOST="tunnel.ltzy.top"
PORT="9999"
TOKEN="2f4354f1642c634a0f75db5d8037e840"

# 检测架构
ARCH=$(uname -m)
case "$ARCH" in
  x86_64|amd64)   BIN="tuncli-amd64" ;;
  aarch64|arm64)  BIN="tuncli-arm64" ;;
  armv7l|armv6l)  BIN="tuncli-armv7" ;;
  i386|i686)      BIN="tuncli-386" ;;
  *)
    echo "未知架构: $ARCH，尝试使用 Python 版本"
    PY=$(which python3 python python3.7 2>/dev/null | head -1)
    if [ -n "$PY" ]; then
      curl -sLo /tmp/tunnel.py "$BASE/tunnel.py"
      nohup $PY /tmp/tunnel.py client --host "$HOST" --port "$PORT" --token "$TOKEN" > /tmp/tunnel.log 2>&1 &
      echo "已启动 (Python fallback)"
      exit 0
    fi
    echo "不支持的架构: $ARCH"
    exit 1
    ;;
esac

echo "架构: $ARCH → $BIN"
curl -sLo /tmp/tuncli "$BASE/$BIN"
chmod +x /tmp/tuncli
nohup /tmp/tuncli "$HOST" "$PORT" "$TOKEN" > /tmp/tunnel.log 2>&1 &
echo "已启动 (PID: $!)"
echo "日志: tail -f /tmp/tunnel.log"