gasschina commited on
Commit
de0e55d
·
verified ·
1 Parent(s): 0d74de0

Upload scripts/tunnel_api.sh with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/tunnel_api.sh +91 -0
scripts/tunnel_api.sh ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # ============================================================================
3
+ # tunnel_api.sh — 为 Spark API (默认8080) 开 aitun.cc 公网隧道
4
+ # 位置: /content/drive/MyDrive/spark-t4/tunnel_api.sh
5
+ #
6
+ # 原理: 直接运行 aitun-client 二进制,并用独立的 HOME (= /root/.aitun-api)
7
+ # 注册,完全不触碰现有 Flask 桥接隧道的 /root/.aitun 配置,
8
+ # 两隧道互不影响。免费模式自动分配隧道码和公网 URL。
9
+ #
10
+ # 用法:
11
+ # bash tunnel_api.sh start # 开隧道并打印公网 URL(约10~20秒)
12
+ # bash tunnel_api.sh status
13
+ # bash tunnel_api.sh stop
14
+ # 自定义: PORT=8080 bash tunnel_api.sh start
15
+ # ============================================================================
16
+ set -euo pipefail
17
+
18
+ PORT="${PORT:-8080}"
19
+ CLIENT_BIN="/usr/local/lib/python3.13/dist-packages/aitun/bin/aitun-client-linux-amd64"
20
+ API_HOME="/root/.aitun-api" # 独立配置目录,保护桥接隧道
21
+ LOG_FILE="/content/aitun-api-tunnel.log"
22
+ PID_FILE="/content/aitun-api-tunnel.pid"
23
+
24
+ GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; CYAN='\033[0;36m'; NC='\033[0m'
25
+ info() { echo -e "${GREEN}[隧道]${NC} $*"; }
26
+ fail() { echo -e "${RED}[隧道]${NC} $*"; exit 1; }
27
+
28
+ [[ -x "$CLIENT_BIN" ]] || fail "未找到 aitun 客户端: $CLIENT_BIN (本机需已安装 aitun)"
29
+
30
+ extract_url() {
31
+ grep -oE "https://aitun\.cc/[A-Za-z0-9]+" "$LOG_FILE" 2>/dev/null | tail -1
32
+ }
33
+
34
+ do_start() {
35
+ do_stop 2>/dev/null || true
36
+ mkdir -p "$API_HOME"
37
+ info "启动隧道 (本地 :$PORT → 公网)..."
38
+ HOME="$API_HOME" nohup "$CLIENT_BIN" -s aitun.cc:6639 -p "$PORT" \
39
+ > "$LOG_FILE" 2>&1 &
40
+ echo $! > "$PID_FILE"
41
+
42
+ for i in $(seq 1 12); do
43
+ sleep 5
44
+ URL="$(extract_url)"
45
+ if [[ -n "$URL" ]]; then
46
+ kill -0 "$(cat "$PID_FILE")" 2>/dev/null || fail "隧道进程退出,日志: $(tail -10 "$LOG_FILE")"
47
+ info "隧道已建立!"
48
+ echo -e "${CYAN}──────────────────────────────────────────────────${NC}"
49
+ echo -e " 公网 API: $URL/v1/chat/completions"
50
+ echo -e " 健康检查: $URL/health"
51
+ echo -e " 模型列表: $URL/v1/models"
52
+ if [[ -s "/content/drive/MyDrive/spark-t4/.api_key" ]]; then
53
+ echo -e " API Key: $(cat /content/drive/MyDrive/spark-t4/.api_key)"
54
+ echo -e " 调用时带: Authorization: Bearer <API Key>"
55
+ fi
56
+ echo -e "${CYAN}──────────────────────────────────────────────────${NC}"
57
+ echo -e " curl 示例:"
58
+ echo -e " curl -N $URL/v1/chat/completions \\"
59
+ echo -e " -H 'Content-Type: application/json' \\"
60
+ if [[ -s "/content/drive/MyDrive/spark-t4/.api_key" ]]; then
61
+ echo -e " -H \"Authorization: Bearer \$(cat /content/drive/MyDrive/spark-t4/.api_key)\" \\"
62
+ fi
63
+ echo -e " -d '{\"messages\":[{\"role\":\"user\",\"content\":\"你好\"}],\"stream\":true}'"
64
+ return 0
65
+ fi
66
+ done
67
+ fail "60 秒未取得公网 URL,日志: $(tail -15 "$LOG_FILE")"
68
+ }
69
+
70
+ do_stop() {
71
+ if [[ -f "$PID_FILE" ]] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
72
+ kill "$(cat "$PID_FILE")" && info "隧道已停止 (PID $(cat "$PID_FILE"))"
73
+ fi
74
+ rm -f "$PID_FILE"
75
+ }
76
+
77
+ do_status() {
78
+ URL="$(extract_url)"
79
+ if [[ -f "$PID_FILE" ]] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
80
+ info "隧道运行中 (PID $(cat "$PID_FILE")) URL: ${URL:-未知}"
81
+ else
82
+ echo "隧道未运行"
83
+ fi
84
+ }
85
+
86
+ case "${1:-help}" in
87
+ start) do_start ;;
88
+ stop) do_stop ;;
89
+ status) do_status ;;
90
+ *) grep '^# ' "$0" | sed 's/^# //' ;;
91
+ esac