tchbcb commited on
Commit
9bb3028
·
verified ·
1 Parent(s): 0318eea

backup s27b_tunnel.sh

Browse files
Files changed (1) hide show
  1. sft-adapter/s27b_tunnel.sh +77 -0
sft-adapter/s27b_tunnel.sh ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # ============================================================================
3
+ # s27b_tunnel.sh — 为 samai-27b-mixbit API 开 aitun.cc 公网隧道
4
+ # 移植自 s4b_tunnel.sh; 独立 HOME, 不影响 kagglemcp 网关隧道 (port 5000)
5
+ #
6
+ # 用法:
7
+ # bash s27b_tunnel.sh start | stop | status
8
+ # PORT=8100 bash s27b_tunnel.sh start
9
+ # ============================================================================
10
+ set -euo pipefail
11
+
12
+ PORT="${PORT:-8100}"
13
+ LOG_FILE="/tmp/k27/s27b-aitun-tunnel.log"
14
+ PID_FILE="/tmp/k27/s27b-aitun-tunnel.pid"
15
+
16
+ CLIENT_BIN="$(ls /usr/local/lib/python3.*/dist-packages/aitun/bin/aitun-client-linux-amd64 2>/dev/null | head -1 || true)"
17
+ [[ -z "$CLIENT_BIN" ]] && CLIENT_BIN="$(ls /usr/local/lib/python3.*/site-packages/aitun/bin/aitun-client-linux-amd64 2>/dev/null | head -1 || true)"
18
+ API_HOME="/root/.aitun-samai27b"
19
+
20
+ GREEN='\033[0;32m'; RED='\033[0;31m'; CYAN='\033[0;36m'; NC='\033[0m'
21
+ info() { echo -e "${GREEN}[隧道]${NC} $*"; }
22
+ fail() { echo -e "${RED}[隧道]${NC} $*"; exit 1; }
23
+
24
+ [[ -n "$CLIENT_BIN" && -x "$CLIENT_BIN" ]] || fail "未找到 aitun 客户端 (pip install aitun)"
25
+
26
+ extract_url() { grep -oE "https://aitun\.cc/[A-Za-z0-9]+" "$LOG_FILE" 2>/dev/null | tail -1; }
27
+
28
+ do_stop() {
29
+ if [[ -f "$PID_FILE" ]] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
30
+ kill "$(cat "$PID_FILE")" && info "隧道已停止 (PID $(cat "$PID_FILE"))"
31
+ fi
32
+ rm -f "$PID_FILE"
33
+ }
34
+
35
+ do_start() {
36
+ do_stop 2>/dev/null || true
37
+ mkdir -p "$API_HOME"
38
+ info "启动隧道 (本地 :$PORT → 公网)..."
39
+ HOME="$API_HOME" nohup "$CLIENT_BIN" -s aitun.cc:6639 -p "$PORT" \
40
+ > "$LOG_FILE" 2>&1 &
41
+ echo $! > "$PID_FILE"
42
+ for i in $(seq 1 12); do
43
+ sleep 5
44
+ URL="$(extract_url)"
45
+ if [[ -n "$URL" ]] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
46
+ info "隧道已建立!"
47
+ echo -e "${CYAN}──────────────────────────────────────────────────${NC}"
48
+ echo -e " 公网 API: $URL/v1/chat/completions"
49
+ echo -e " 健康检查: $URL/health"
50
+ echo -e " 模型列表: $URL/v1/models"
51
+ echo -e " API Key: 1234 (Authorization: Bearer 1234)"
52
+ echo -e "${CYAN}──────────────────────────────────────────────────${NC}"
53
+ echo -e " curl 示例:"
54
+ echo -e " curl -N $URL/v1/chat/completions \\"
55
+ echo -e " -H 'Content-Type: application/json' -H 'Authorization: Bearer 1234' \\"
56
+ echo -e " -d '{\"messages\":[{\"role\":\"user\",\"content\":\"你好\"}],\"stream\":true}'"
57
+ return 0
58
+ fi
59
+ kill -0 "$(cat "$PID_FILE")" 2>/dev/null || fail "隧道进程退出, 日志: $(tail -10 "$LOG_FILE")"
60
+ done
61
+ fail "60s 未取得公网 URL, 日志: $(tail -15 "$LOG_FILE")"
62
+ }
63
+
64
+ do_status() {
65
+ if [[ -f "$PID_FILE" ]] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
66
+ info "隧道运行中 (PID $(cat "$PID_FILE")) URL: $(extract_url)"
67
+ else
68
+ echo "隧道未运行"
69
+ fi
70
+ }
71
+
72
+ case "${1:-help}" in
73
+ start) do_start ;;
74
+ stop) do_stop ;;
75
+ status) do_status ;;
76
+ *) grep '^# ' "$0" | sed 's/^# //' ;;
77
+ esac